blob: d56a0249593f42a07abb96d98c374b508f15c530 [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
Wale Ogunwale843bfb92015-03-27 11:06:48 -070019import static android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
20import static android.content.Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
Craig Mautner15df08a2015-04-01 12:17:18 -070021import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_ALWAYS;
22import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_DEFAULT;
23import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
24import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_NEVER;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -070025import static android.content.pm.ApplicationInfo.PRIVATE_FLAG_PRIVILEGED;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070026import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner84984fa2014-06-19 11:19:20 -070027import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
28import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
29import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
Craig Mautner0eea92c2013-05-16 13:35:39 -070030import static com.android.server.am.ActivityStackSupervisor.DEBUG_ADD_REMOVE;
Craig Mautnerde4ef022013-04-07 19:01:33 -070031
Craig Mautnerb0f7dc72013-04-01 16:34:45 -070032import android.app.Activity;
Craig Mautner9db9a0b2013-04-29 17:05:56 -070033import android.app.ActivityManager;
Craig Mautnerc0ffce52014-07-01 12:38:52 -070034import android.app.ActivityManager.TaskThumbnail;
Craig Mautner648f69b2014-09-18 14:16:26 -070035import android.app.ActivityManager.TaskDescription;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -070036import android.app.ActivityOptions;
Dianne Hackborn885fbe52014-08-23 15:23:58 -070037import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.content.ComponentName;
39import android.content.Intent;
40import android.content.pm.ActivityInfo;
Dianne Hackborn885fbe52014-08-23 15:23:58 -070041import android.content.pm.ApplicationInfo;
42import android.content.pm.IPackageManager;
43import android.content.pm.PackageManager;
Craig Mautner9db9a0b2013-04-29 17:05:56 -070044import android.graphics.Bitmap;
Craig Mautnerc0ffce52014-07-01 12:38:52 -070045import android.os.ParcelFileDescriptor;
Dianne Hackborn885fbe52014-08-23 15:23:58 -070046import android.os.RemoteException;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070047import android.os.UserHandle;
Dianne Hackborn91097de2014-04-04 18:02:06 -070048import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn7f96b792012-05-29 18:46:45 -070049import android.util.Slog;
Dianne Hackborn91097de2014-04-04 18:02:06 -070050import com.android.internal.app.IVoiceInteractor;
Craig Mautner21d24a22014-04-23 11:45:37 -070051import com.android.internal.util.XmlUtils;
52import org.xmlpull.v1.XmlPullParser;
53import org.xmlpull.v1.XmlPullParserException;
54import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055
Craig Mautnerc0ffce52014-07-01 12:38:52 -070056import java.io.File;
Craig Mautner21d24a22014-04-23 11:45:37 -070057import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import java.io.PrintWriter;
Craig Mautner5d9c7be2013-02-15 14:02:56 -080059import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
Craig Mautnerc0ffce52014-07-01 12:38:52 -070061final class TaskRecord {
Wale Ogunwalee23149f2015-03-06 15:39:44 -080062 private static final String TAG = TAG_WITH_CLASS_NAME ? "TaskRecord" : TAG_AM;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070063 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Craig Mautnere0570202015-05-13 13:06:11 -070064 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070065 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080066
Wale Ogunwale18795a22014-12-03 11:38:33 -080067 static final String ATTR_TASKID = "task_id";
Craig Mautner21d24a22014-04-23 11:45:37 -070068 private static final String TAG_INTENT = "intent";
69 private static final String TAG_AFFINITYINTENT = "affinity_intent";
Wale Ogunwale18795a22014-12-03 11:38:33 -080070 static final String ATTR_REALACTIVITY = "real_activity";
Craig Mautner21d24a22014-04-23 11:45:37 -070071 private static final String ATTR_ORIGACTIVITY = "orig_activity";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -070072 private static final String TAG_ACTIVITY = "activity";
Craig Mautner21d24a22014-04-23 11:45:37 -070073 private static final String ATTR_AFFINITY = "affinity";
Dianne Hackborn79228822014-09-16 11:11:23 -070074 private static final String ATTR_ROOT_AFFINITY = "root_affinity";
Craig Mautner21d24a22014-04-23 11:45:37 -070075 private static final String ATTR_ROOTHASRESET = "root_has_reset";
Dianne Hackborn13420f22014-07-18 15:43:56 -070076 private static final String ATTR_AUTOREMOVERECENTS = "auto_remove_recents";
Craig Mautner21d24a22014-04-23 11:45:37 -070077 private static final String ATTR_ASKEDCOMPATMODE = "asked_compat_mode";
78 private static final String ATTR_USERID = "user_id";
Dianne Hackborn885fbe52014-08-23 15:23:58 -070079 private static final String ATTR_EFFECTIVE_UID = "effective_uid";
Craig Mautner21d24a22014-04-23 11:45:37 -070080 private static final String ATTR_TASKTYPE = "task_type";
Winson Chungffa2ec62014-07-03 15:54:42 -070081 private static final String ATTR_FIRSTACTIVETIME = "first_active_time";
Winson Chungf1fbd772014-06-24 18:06:58 -070082 private static final String ATTR_LASTACTIVETIME = "last_active_time";
Craig Mautner21d24a22014-04-23 11:45:37 -070083 private static final String ATTR_LASTDESCRIPTION = "last_description";
84 private static final String ATTR_LASTTIMEMOVED = "last_time_moved";
Craig Mautner9d4e9bc2014-06-18 18:34:56 -070085 private static final String ATTR_NEVERRELINQUISH = "never_relinquish_identity";
Wale Ogunwale18795a22014-12-03 11:38:33 -080086 static final String ATTR_TASK_AFFILIATION = "task_affiliation";
Craig Mautnera228ae92014-07-09 05:44:55 -070087 private static final String ATTR_PREV_AFFILIATION = "prev_affiliation";
88 private static final String ATTR_NEXT_AFFILIATION = "next_affiliation";
Winson Chungec396d62014-08-06 17:08:00 -070089 private static final String ATTR_TASK_AFFILIATION_COLOR = "task_affiliation_color";
Craig Mautnerdc00cbe2014-07-20 17:48:47 -070090 private static final String ATTR_CALLING_UID = "calling_uid";
91 private static final String ATTR_CALLING_PACKAGE = "calling_package";
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -080092 private static final String ATTR_RESIZEABLE = "resizeable";
Wale Ogunwalea7afaa22015-05-01 20:39:18 -070093 private static final String ATTR_PRIVILEGED = "privileged";
Craig Mautner21d24a22014-04-23 11:45:37 -070094
95 private static final String TASK_THUMBNAIL_SUFFIX = "_task_thumbnail";
96
Craig Mautner0b633fc2014-07-23 10:42:18 -070097 static final boolean IGNORE_RETURN_TO_RECENTS = true;
98
Wale Ogunwale18795a22014-12-03 11:38:33 -080099 static final int INVALID_TASK_ID = -1;
100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 final int taskId; // Unique identifier for this task.
Dianne Hackborn79228822014-09-16 11:11:23 -0700102 String affinity; // The affinity name for this task, or null; may change identity.
103 String rootAffinity; // Initial base affinity, or null; does not change from initial root.
Dianne Hackborn91097de2014-04-04 18:02:06 -0700104 final IVoiceInteractionSession voiceSession; // Voice interaction session driving task
105 final IVoiceInteractor voiceInteractor; // Associated interactor to provide to app
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 Intent intent; // The original intent that started the task.
107 Intent affinityIntent; // Intent of affinity-moved activity that started this task.
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700108 int effectiveUid; // The current effective uid of the identity of this task.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 ComponentName origActivity; // The non-alias activity component of the intent.
110 ComponentName realActivity; // The actual activity component that started the task.
Winson Chungffa2ec62014-07-03 15:54:42 -0700111 long firstActiveTime; // First time this task was active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 long lastActiveTime; // Last time this task was active, including sleep.
Dianne Hackborn852975d2014-08-22 17:42:43 -0700113 boolean inRecents; // Actually in the recents list?
114 boolean isAvailable; // Is the activity available to be launched?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 boolean rootWasReset; // True if the intent at the root of the task had
116 // the FLAG_ACTIVITY_RESET_TASK_IF_NEEDED flag.
Dianne Hackborn13420f22014-07-18 15:43:56 -0700117 boolean autoRemoveRecents; // If true, we should automatically remove the task from
118 // recents when activity finishes
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700119 boolean askedCompatMode;// Have asked the user about compat mode for this task.
Dianne Hackbornd38aed82014-06-10 21:36:35 -0700120 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 -0800121
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700122 String stringName; // caching of toString() result.
Dianne Hackborn9da2d402012-03-15 13:43:08 -0700123 int userId; // user for which this task was created
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800124
125 int numFullscreen; // Number of fullscreen activities.
126
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800127 boolean mResizeable; // Activities in the task resizeable. Based on the resizable setting of
128 // the root activity.
Craig Mautner15df08a2015-04-01 12:17:18 -0700129 int mLockTaskMode; // Which tasklock mode to launch this task in. One of
130 // ActivityManager.LOCK_TASK_LAUNCH_MODE_*
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700131 private boolean mPrivileged; // The root activity application of this task holds
132 // privileged permissions.
133
Craig Mautner15df08a2015-04-01 12:17:18 -0700134 /** Can't be put in lockTask mode. */
135 final static int LOCK_TASK_AUTH_DONT_LOCK = 0;
Craig Mautnere0570202015-05-13 13:06:11 -0700136 /** Can enter lockTask with user approval. Can never start over existing lockTask task. */
Craig Mautner15df08a2015-04-01 12:17:18 -0700137 final static int LOCK_TASK_AUTH_PINNABLE = 1;
138 /** Starts in LOCK_TASK_MODE_LOCKED automatically. Can start over existing lockTask task. */
139 final static int LOCK_TASK_AUTH_LAUNCHABLE = 2;
Craig Mautnere0570202015-05-13 13:06:11 -0700140 /** Can enter lockTask with user approval. Can start over existing lockTask task. */
Craig Mautner15df08a2015-04-01 12:17:18 -0700141 final static int LOCK_TASK_AUTH_WHITELISTED = 3;
142 int mLockTaskAuth = LOCK_TASK_AUTH_PINNABLE;
143
144 int mLockTaskUid = -1; // The uid of the application that called startLockTask().
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800145
Winson Chung03a9bae2014-05-02 09:56:12 -0700146 // This represents the last resolved activity values for this task
147 // NOTE: This value needs to be persisted with each task
Craig Mautner648f69b2014-09-18 14:16:26 -0700148 TaskDescription lastTaskDescription = new TaskDescription();
Winson Chung03a9bae2014-05-02 09:56:12 -0700149
Craig Mautnerd2328952013-03-05 12:46:26 -0800150 /** List of all activities in the task arranged in history order */
Craig Mautner21d24a22014-04-23 11:45:37 -0700151 final ArrayList<ActivityRecord> mActivities;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800152
Craig Mautnerd2328952013-03-05 12:46:26 -0800153 /** Current stack */
154 ActivityStack stack;
155
Craig Mautner2c1faed2013-07-23 12:56:02 -0700156 /** Takes on same set of values as ActivityRecord.mActivityType */
Craig Mautner21d24a22014-04-23 11:45:37 -0700157 int taskType;
Craig Mautner1602ec22013-05-12 10:24:27 -0700158
Craig Mautner21d24a22014-04-23 11:45:37 -0700159 /** Takes on same value as first root activity */
160 boolean isPersistable = false;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700161 int maxRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700162
163 /** Only used for persistable tasks, otherwise 0. The last time this task was moved. Used for
164 * determining the order when restoring. Sign indicates whether last task movement was to front
165 * (positive) or back (negative). Absolute value indicates time. */
166 long mLastTimeMoved = System.currentTimeMillis();
167
Craig Mautner84984fa2014-06-19 11:19:20 -0700168 /** Indication of what to run next when task exits. Use ActivityRecord types.
169 * ActivityRecord.APPLICATION_ACTIVITY_TYPE indicates to resume the task below this one in the
170 * task stack. */
171 private int mTaskToReturnTo = APPLICATION_ACTIVITY_TYPE;
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700172
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700173 /** If original intent did not allow relinquishing task identity, save that information */
174 boolean mNeverRelinquishIdentity = true;
175
Craig Mautner362449a2014-06-20 14:04:39 -0700176 // Used in the unique case where we are clearing the task in order to reuse it. In that case we
177 // do not want to delete the stack when the task goes empty.
178 boolean mReuseTask = false;
179
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700180 private Bitmap mLastThumbnail; // Last thumbnail captured for this item.
Wale Ogunwalebe23ff42014-10-21 16:29:51 -0700181 private final File mLastThumbnailFile; // File containing last thumbnail.
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700182 private final String mFilename;
183 CharSequence lastDescription; // Last description captured for this item.
184
Craig Mautnera228ae92014-07-09 05:44:55 -0700185 int mAffiliatedTaskId; // taskId of parent affiliation or self if no parent.
Winson Chungec396d62014-08-06 17:08:00 -0700186 int mAffiliatedTaskColor; // color of the parent task affiliation.
Craig Mautnera228ae92014-07-09 05:44:55 -0700187 TaskRecord mPrevAffiliate; // previous task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800188 int mPrevAffiliateTaskId = INVALID_TASK_ID; // previous id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700189 TaskRecord mNextAffiliate; // next task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800190 int mNextAffiliateTaskId = INVALID_TASK_ID; // next id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700191
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700192 // For relaunching the task from recents as though it was launched by the original launcher.
193 int mCallingUid;
194 String mCallingPackage;
195
Craig Mautner21d24a22014-04-23 11:45:37 -0700196 final ActivityManagerService mService;
197
198 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700199 IVoiceInteractionSession _voiceSession, IVoiceInteractor _voiceInteractor) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700200 mService = service;
Craig Mautnera228ae92014-07-09 05:44:55 -0700201 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
202 TaskPersister.IMAGE_EXTENSION;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700203 mLastThumbnailFile = new File(TaskPersister.sImagesDir, mFilename);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 taskId = _taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700205 mAffiliatedTaskId = _taskId;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700206 voiceSession = _voiceSession;
207 voiceInteractor = _voiceInteractor;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700208 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800209 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700210 mCallingUid = info.applicationInfo.uid;
211 mCallingPackage = info.packageName;
Martijn Coenend4a69702014-06-30 11:12:17 -0700212 setIntent(_intent, info);
Craig Mautner21d24a22014-04-23 11:45:37 -0700213 }
214
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700215 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Craig Mautner648f69b2014-09-18 14:16:26 -0700216 TaskDescription _taskDescription) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700217 mService = service;
218 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
219 TaskPersister.IMAGE_EXTENSION;
220 mLastThumbnailFile = new File(TaskPersister.sImagesDir, mFilename);
221 taskId = _taskId;
222 mAffiliatedTaskId = _taskId;
223 voiceSession = null;
224 voiceInteractor = null;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700225 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800226 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700227 mCallingUid = info.applicationInfo.uid;
228 mCallingPackage = info.packageName;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700229 setIntent(_intent, info);
230
231 taskType = ActivityRecord.APPLICATION_ACTIVITY_TYPE;
232 isPersistable = true;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700233 // Clamp to [1, max].
234 maxRecents = Math.min(Math.max(info.maxRecents, 1),
235 ActivityManager.getMaxAppRecentsLimitStatic());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700236
237 taskType = APPLICATION_ACTIVITY_TYPE;
238 mTaskToReturnTo = HOME_ACTIVITY_TYPE;
239 userId = UserHandle.getUserId(info.applicationInfo.uid);
240 lastTaskDescription = _taskDescription;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700241 }
242
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800243 private TaskRecord(ActivityManagerService service, int _taskId, Intent _intent,
244 Intent _affinityIntent, String _affinity, String _rootAffinity,
245 ComponentName _realActivity, ComponentName _origActivity, boolean _rootWasReset,
246 boolean _autoRemoveRecents, boolean _askedCompatMode, int _taskType, int _userId,
247 int _effectiveUid, String _lastDescription, ArrayList<ActivityRecord> activities,
248 long _firstActiveTime, long _lastActiveTime, long lastTimeMoved,
249 boolean neverRelinquishIdentity, TaskDescription _lastTaskDescription,
250 int taskAffiliation, int prevTaskId, int nextTaskId, int taskAffiliationColor,
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700251 int callingUid, String callingPackage, boolean resizeable, boolean privileged) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700252 mService = service;
Craig Mautnera228ae92014-07-09 05:44:55 -0700253 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
254 TaskPersister.IMAGE_EXTENSION;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700255 mLastThumbnailFile = new File(TaskPersister.sImagesDir, mFilename);
Craig Mautner21d24a22014-04-23 11:45:37 -0700256 taskId = _taskId;
257 intent = _intent;
258 affinityIntent = _affinityIntent;
259 affinity = _affinity;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800260 rootAffinity = _rootAffinity;
Craig Mautner21d24a22014-04-23 11:45:37 -0700261 voiceSession = null;
262 voiceInteractor = null;
263 realActivity = _realActivity;
264 origActivity = _origActivity;
265 rootWasReset = _rootWasReset;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700266 isAvailable = true;
Dianne Hackborn13420f22014-07-18 15:43:56 -0700267 autoRemoveRecents = _autoRemoveRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700268 askedCompatMode = _askedCompatMode;
269 taskType = _taskType;
Craig Mautner84984fa2014-06-19 11:19:20 -0700270 mTaskToReturnTo = HOME_ACTIVITY_TYPE;
Craig Mautner21d24a22014-04-23 11:45:37 -0700271 userId = _userId;
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700272 effectiveUid = _effectiveUid;
Winson Chungffa2ec62014-07-03 15:54:42 -0700273 firstActiveTime = _firstActiveTime;
Winson Chungf1fbd772014-06-24 18:06:58 -0700274 lastActiveTime = _lastActiveTime;
Craig Mautner21d24a22014-04-23 11:45:37 -0700275 lastDescription = _lastDescription;
276 mActivities = activities;
277 mLastTimeMoved = lastTimeMoved;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700278 mNeverRelinquishIdentity = neverRelinquishIdentity;
Winson Chung2cb86c72014-06-25 12:03:30 -0700279 lastTaskDescription = _lastTaskDescription;
Craig Mautnera228ae92014-07-09 05:44:55 -0700280 mAffiliatedTaskId = taskAffiliation;
Winson Chungec396d62014-08-06 17:08:00 -0700281 mAffiliatedTaskColor = taskAffiliationColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700282 mPrevAffiliateTaskId = prevTaskId;
283 mNextAffiliateTaskId = nextTaskId;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700284 mCallingUid = callingUid;
285 mCallingPackage = callingPackage;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800286 mResizeable = resizeable;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700287 mPrivileged = privileged;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 }
289
290 void touchActiveTime() {
Craig Mautner494f6bdd2014-07-21 11:17:46 -0700291 lastActiveTime = System.currentTimeMillis();
Winson Chungffa2ec62014-07-03 15:54:42 -0700292 if (firstActiveTime == 0) {
293 firstActiveTime = lastActiveTime;
294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 long getInactiveDuration() {
Craig Mautner494f6bdd2014-07-21 11:17:46 -0700298 return System.currentTimeMillis() - lastActiveTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700300
Winson Chungfee26772014-08-05 12:21:52 -0700301 /** Sets the original intent, and the calling uid and package. */
302 void setIntent(ActivityRecord r) {
Winson Chungfee26772014-08-05 12:21:52 -0700303 mCallingUid = r.launchedFromUid;
304 mCallingPackage = r.launchedFromPackage;
Craig Mautner15df08a2015-04-01 12:17:18 -0700305 setIntent(r.intent, r.info);
Winson Chungfee26772014-08-05 12:21:52 -0700306 }
307
308 /** Sets the original intent, _without_ updating the calling uid or package. */
309 private void setIntent(Intent _intent, ActivityInfo info) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700310 if (intent == null) {
311 mNeverRelinquishIdentity =
312 (info.flags & ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY) == 0;
313 } else if (mNeverRelinquishIdentity) {
314 return;
315 }
316
317 affinity = info.taskAffinity;
Dianne Hackborn79228822014-09-16 11:11:23 -0700318 if (intent == null) {
319 // If this task already has an intent associated with it, don't set the root
320 // affinity -- we don't want it changing after initially set, but the initially
321 // set value may be null.
322 rootAffinity = affinity;
323 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700324 effectiveUid = info.applicationInfo.uid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700325 stringName = null;
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 if (info.targetActivity == null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800328 if (_intent != null) {
329 // If this Intent has a selector, we want to clear it for the
330 // recent task since it is not relevant if the user later wants
331 // to re-launch the app.
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700332 if (_intent.getSelector() != null || _intent.getSourceBounds() != null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800333 _intent = new Intent(_intent);
334 _intent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700335 _intent.setSourceBounds(null);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800336 }
337 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700338 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Setting Intent of " + this + " to " + _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 intent = _intent;
340 realActivity = _intent != null ? _intent.getComponent() : null;
341 origActivity = null;
342 } else {
343 ComponentName targetComponent = new ComponentName(
344 info.packageName, info.targetActivity);
345 if (_intent != null) {
346 Intent targetIntent = new Intent(_intent);
347 targetIntent.setComponent(targetComponent);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800348 targetIntent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700349 targetIntent.setSourceBounds(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700350 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700351 "Setting Intent of " + this + " to target " + targetIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 intent = targetIntent;
353 realActivity = targetComponent;
354 origActivity = _intent.getComponent();
355 } else {
356 intent = null;
357 realActivity = targetComponent;
358 origActivity = new ComponentName(info.packageName, info.name);
359 }
360 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700361
Craig Mautner47b20ba2014-09-17 17:23:44 -0700362 final int intentFlags = intent == null ? 0 : intent.getFlags();
363 if ((intentFlags & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 // Once we are set to an Intent with this flag, we count this
365 // task as having a true root activity.
366 rootWasReset = true;
367 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700368
Dianne Hackborn09233282014-04-30 11:33:59 -0700369 userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner41db4a72014-05-07 17:20:56 -0700370 if ((info.flags & ActivityInfo.FLAG_AUTO_REMOVE_FROM_RECENTS) != 0) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700371 // If the activity itself has requested auto-remove, then just always do it.
372 autoRemoveRecents = true;
Wale Ogunwale843bfb92015-03-27 11:06:48 -0700373 } else if ((intentFlags & (FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_RETAIN_IN_RECENTS))
374 == FLAG_ACTIVITY_NEW_DOCUMENT) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700375 // If the caller has not asked for the document to be retained, then we may
376 // want to turn on auto-remove, depending on whether the target has set its
377 // own document launch mode.
378 if (info.documentLaunchMode != ActivityInfo.DOCUMENT_LAUNCH_NONE) {
379 autoRemoveRecents = false;
380 } else {
381 autoRemoveRecents = true;
382 }
383 } else {
384 autoRemoveRecents = false;
Craig Mautner41db4a72014-05-07 17:20:56 -0700385 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800386 mResizeable = info.resizeable;
Craig Mautner15df08a2015-04-01 12:17:18 -0700387 mLockTaskMode = info.lockTaskLaunchMode;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700388 mPrivileged = (info.applicationInfo.privateFlags & PRIVATE_FLAG_PRIVILEGED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -0700389 setLockTaskAuth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800391
Craig Mautner84984fa2014-06-19 11:19:20 -0700392 void setTaskToReturnTo(int taskToReturnTo) {
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700393 mTaskToReturnTo = (IGNORE_RETURN_TO_RECENTS && taskToReturnTo == RECENTS_ACTIVITY_TYPE)
394 ? HOME_ACTIVITY_TYPE : taskToReturnTo;
Craig Mautner84984fa2014-06-19 11:19:20 -0700395 }
396
397 int getTaskToReturnTo() {
398 return mTaskToReturnTo;
399 }
400
Craig Mautnera228ae92014-07-09 05:44:55 -0700401 void setPrevAffiliate(TaskRecord prevAffiliate) {
402 mPrevAffiliate = prevAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800403 mPrevAffiliateTaskId = prevAffiliate == null ? INVALID_TASK_ID : prevAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700404 }
405
406 void setNextAffiliate(TaskRecord nextAffiliate) {
407 mNextAffiliate = nextAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800408 mNextAffiliateTaskId = nextAffiliate == null ? INVALID_TASK_ID : nextAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700409 }
410
411 // Close up recents linked list.
412 void closeRecentsChain() {
413 if (mPrevAffiliate != null) {
414 mPrevAffiliate.setNextAffiliate(mNextAffiliate);
415 }
416 if (mNextAffiliate != null) {
417 mNextAffiliate.setPrevAffiliate(mPrevAffiliate);
418 }
419 setPrevAffiliate(null);
420 setNextAffiliate(null);
421 }
422
Winson Chung740c3ac2014-11-12 16:14:38 -0800423 void removedFromRecents() {
Dianne Hackborn852975d2014-08-22 17:42:43 -0700424 disposeThumbnail();
425 closeRecentsChain();
426 if (inRecents) {
427 inRecents = false;
Winson Chung740c3ac2014-11-12 16:14:38 -0800428 mService.notifyTaskPersisterLocked(this, false);
Dianne Hackborn852975d2014-08-22 17:42:43 -0700429 }
430 }
431
Craig Mautnera228ae92014-07-09 05:44:55 -0700432 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
433 closeRecentsChain();
434 mAffiliatedTaskId = taskToAffiliateWith.mAffiliatedTaskId;
Winson Chungec396d62014-08-06 17:08:00 -0700435 mAffiliatedTaskColor = taskToAffiliateWith.mAffiliatedTaskColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700436 // Find the end
437 while (taskToAffiliateWith.mNextAffiliate != null) {
438 final TaskRecord nextRecents = taskToAffiliateWith.mNextAffiliate;
439 if (nextRecents.mAffiliatedTaskId != mAffiliatedTaskId) {
440 Slog.e(TAG, "setTaskToAffiliateWith: nextRecents=" + nextRecents + " affilTaskId="
441 + nextRecents.mAffiliatedTaskId + " should be " + mAffiliatedTaskId);
442 if (nextRecents.mPrevAffiliate == taskToAffiliateWith) {
443 nextRecents.setPrevAffiliate(null);
444 }
445 taskToAffiliateWith.setNextAffiliate(null);
446 break;
447 }
448 taskToAffiliateWith = nextRecents;
449 }
450 taskToAffiliateWith.setNextAffiliate(this);
451 setPrevAffiliate(taskToAffiliateWith);
452 setNextAffiliate(null);
453 }
454
Winson Chung096f36b2014-08-20 15:39:01 -0700455 /**
456 * Sets the last thumbnail.
457 * @return whether the thumbnail was set
458 */
459 boolean setLastThumbnail(Bitmap thumbnail) {
460 if (mLastThumbnail != thumbnail) {
461 mLastThumbnail = thumbnail;
462 if (thumbnail == null) {
463 if (mLastThumbnailFile != null) {
464 mLastThumbnailFile.delete();
465 }
466 } else {
467 mService.mTaskPersister.saveImage(thumbnail, mFilename);
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700468 }
Winson Chung096f36b2014-08-20 15:39:01 -0700469 return true;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700470 }
Winson Chung096f36b2014-08-20 15:39:01 -0700471 return false;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700472 }
473
474 void getLastThumbnail(TaskThumbnail thumbs) {
475 thumbs.mainThumbnail = mLastThumbnail;
476 thumbs.thumbnailFileDescriptor = null;
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700477 if (mLastThumbnail == null) {
Craig Mautner648f69b2014-09-18 14:16:26 -0700478 thumbs.mainThumbnail = mService.mTaskPersister.getImageFromWriteQueue(mFilename);
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700479 }
Winson Chung096f36b2014-08-20 15:39:01 -0700480 // Only load the thumbnail file if we don't have a thumbnail
481 if (thumbs.mainThumbnail == null && mLastThumbnailFile.exists()) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700482 try {
483 thumbs.thumbnailFileDescriptor = ParcelFileDescriptor.open(mLastThumbnailFile,
484 ParcelFileDescriptor.MODE_READ_ONLY);
485 } catch (IOException e) {
Dianne Hackborn9844d292013-10-04 16:44:22 -0700486 }
487 }
488 }
489
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700490 void freeLastThumbnail() {
491 mLastThumbnail = null;
492 }
493
494 void disposeThumbnail() {
495 mLastThumbnail = null;
496 lastDescription = null;
497 }
498
Winson Chung1147c402014-05-14 11:05:00 -0700499 /** Returns the intent for the root activity for this task */
500 Intent getBaseIntent() {
501 return intent != null ? intent : affinityIntent;
502 }
503
Winson Chung3b3f4642014-04-22 10:08:18 -0700504 /** Returns the first non-finishing activity from the root. */
505 ActivityRecord getRootActivity() {
506 for (int i = 0; i < mActivities.size(); i++) {
507 final ActivityRecord r = mActivities.get(i);
508 if (r.finishing) {
509 continue;
510 }
511 return r;
512 }
513 return null;
514 }
515
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800516 ActivityRecord getTopActivity() {
517 for (int i = mActivities.size() - 1; i >= 0; --i) {
518 final ActivityRecord r = mActivities.get(i);
519 if (r.finishing) {
520 continue;
521 }
522 return r;
523 }
524 return null;
525 }
526
Craig Mautner6b74cb52013-09-27 17:02:21 -0700527 ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700528 if (stack != null) {
529 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
530 ActivityRecord r = mActivities.get(activityNdx);
531 if (!r.finishing && r != notTop && stack.okToShowLocked(r)) {
532 return r;
533 }
Craig Mautner6b74cb52013-09-27 17:02:21 -0700534 }
535 }
536 return null;
537 }
538
Craig Mautner3b475fe2013-12-16 15:58:31 -0800539 /** Call after activity movement or finish to make sure that frontOfTask is set correctly */
540 final void setFrontOfTask() {
541 boolean foundFront = false;
542 final int numActivities = mActivities.size();
Craig Mautner704e40b2013-12-18 16:43:51 -0800543 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
Craig Mautner3b475fe2013-12-16 15:58:31 -0800544 final ActivityRecord r = mActivities.get(activityNdx);
545 if (foundFront || r.finishing) {
546 r.frontOfTask = false;
547 } else {
548 r.frontOfTask = true;
549 // Set frontOfTask false for every following activity.
550 foundFront = true;
551 }
552 }
Craig Mautner9587ee02014-06-23 15:00:10 +0000553 if (!foundFront && numActivities > 0) {
554 // All activities of this task are finishing. As we ought to have a frontOfTask
555 // activity, make the bottom activity front.
556 mActivities.get(0).frontOfTask = true;
557 }
Craig Mautner3b475fe2013-12-16 15:58:31 -0800558 }
559
Craig Mautnerde4ef022013-04-07 19:01:33 -0700560 /**
Craig Mautner3b475fe2013-12-16 15:58:31 -0800561 * Reorder the history stack so that the passed activity is brought to the front.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700562 */
563 final void moveActivityToFrontLocked(ActivityRecord newTop) {
564 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + newTop
565 + " to stack at top", new RuntimeException("here").fillInStackTrace());
566
Craig Mautnerde4ef022013-04-07 19:01:33 -0700567 mActivities.remove(newTop);
568 mActivities.add(newTop);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700569 updateEffectiveIntent();
Craig Mautner3b475fe2013-12-16 15:58:31 -0800570
571 setFrontOfTask();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700572 }
573
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800574 void addActivityAtBottom(ActivityRecord r) {
Craig Mautner77878772013-03-04 19:46:24 -0800575 addActivityAtIndex(0, r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800576 }
577
578 void addActivityToTop(ActivityRecord r) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700579 addActivityAtIndex(mActivities.size(), r);
580 }
581
582 void addActivityAtIndex(int index, ActivityRecord r) {
Craig Mautner6170f732013-04-02 13:05:23 -0700583 // 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 -0800584 if (!mActivities.remove(r) && r.fullscreen) {
585 // Was not previously in list.
586 numFullscreen++;
587 }
Craig Mautner2c1faed2013-07-23 12:56:02 -0700588 // Only set this based on the first activity
589 if (mActivities.isEmpty()) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700590 taskType = r.mActivityType;
591 isPersistable = r.isPersistable();
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700592 mCallingUid = r.launchedFromUid;
593 mCallingPackage = r.launchedFromPackage;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700594 // Clamp to [1, max].
595 maxRecents = Math.min(Math.max(r.info.maxRecents, 1),
596 ActivityManager.getMaxAppRecentsLimitStatic());
Craig Mautner2c1faed2013-07-23 12:56:02 -0700597 } else {
598 // Otherwise make all added activities match this one.
Craig Mautner21d24a22014-04-23 11:45:37 -0700599 r.mActivityType = taskType;
Craig Mautner78733002013-06-10 13:54:49 -0700600 }
Craig Mautner77878772013-03-04 19:46:24 -0800601 mActivities.add(index, r);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700602 updateEffectiveIntent();
Craig Mautner21d24a22014-04-23 11:45:37 -0700603 if (r.isPersistable()) {
604 mService.notifyTaskPersisterLocked(this, false);
605 }
Craig Mautner77878772013-03-04 19:46:24 -0800606 }
607
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800608 /** @return true if this was the last activity in the task */
609 boolean removeActivity(ActivityRecord r) {
610 if (mActivities.remove(r) && r.fullscreen) {
611 // Was previously in list.
612 numFullscreen--;
613 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700614 if (r.isPersistable()) {
615 mService.notifyTaskPersisterLocked(this, false);
616 }
Craig Mautner41326202014-06-20 14:38:21 -0700617 if (mActivities.isEmpty()) {
Craig Mautner5afcd4d2014-06-21 18:11:33 -0700618 return !mReuseTask;
Craig Mautner41326202014-06-20 14:38:21 -0700619 }
620 updateEffectiveIntent();
621 return false;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800622 }
623
Craig Mautner41db4a72014-05-07 17:20:56 -0700624 boolean autoRemoveFromRecents() {
Dianne Hackbornd38aed82014-06-10 21:36:35 -0700625 // We will automatically remove the task either if it has explicitly asked for
626 // this, or it is empty and has never contained an activity that got shown to
627 // the user.
Dianne Hackborn13420f22014-07-18 15:43:56 -0700628 return autoRemoveRecents || (mActivities.isEmpty() && !hasBeenVisible);
Craig Mautner41db4a72014-05-07 17:20:56 -0700629 }
630
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700631 /**
632 * Completely remove all activities associated with an existing
633 * task starting at a specified index.
634 */
635 final void performClearTaskAtIndexLocked(int activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700636 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700637 for ( ; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700638 final ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700639 if (r.finishing) {
640 continue;
641 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700642 if (stack == null) {
643 // Task was restored from persistent storage.
644 r.takeFromHistory();
645 mActivities.remove(activityNdx);
646 --activityNdx;
647 --numActivities;
Todd Kennedy539db512014-12-15 09:57:55 -0800648 } else if (stack.finishActivityLocked(
649 r, Activity.RESULT_CANCELED, null, "clear-task-index", false)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700650 --activityNdx;
651 --numActivities;
652 }
653 }
654 }
655
656 /**
657 * Completely remove all activities associated with an existing task.
658 */
659 final void performClearTaskLocked() {
Craig Mautner362449a2014-06-20 14:04:39 -0700660 mReuseTask = true;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700661 performClearTaskAtIndexLocked(0);
Craig Mautner362449a2014-06-20 14:04:39 -0700662 mReuseTask = false;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700663 }
664
665 /**
666 * Perform clear operation as requested by
667 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the
668 * stack to the given task, then look for
669 * an instance of that activity in the stack and, if found, finish all
670 * activities on top of it and return the instance.
671 *
672 * @param newR Description of the new activity being started.
673 * @return Returns the old activity that should be continued to be used,
674 * or null if none was found.
675 */
676 final ActivityRecord performClearTaskLocked(ActivityRecord newR, int launchFlags) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700677 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700678 for (int activityNdx = numActivities - 1; activityNdx >= 0; --activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700679 ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700680 if (r.finishing) {
681 continue;
682 }
683 if (r.realActivity.equals(newR.realActivity)) {
684 // Here it is! Now finish everything in front...
Craig Mautner1602ec22013-05-12 10:24:27 -0700685 final ActivityRecord ret = r;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700686
687 for (++activityNdx; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700688 r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700689 if (r.finishing) {
690 continue;
691 }
692 ActivityOptions opts = r.takeOptionsLocked();
693 if (opts != null) {
694 ret.updateOptionsLocked(opts);
695 }
Wale Ogunwale7d701172015-03-11 15:36:30 -0700696 if (stack != null && stack.finishActivityLocked(
Todd Kennedy539db512014-12-15 09:57:55 -0800697 r, Activity.RESULT_CANCELED, null, "clear-task-stack", false)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700698 --activityNdx;
699 --numActivities;
700 }
701 }
702
703 // Finally, if this is a normal launch mode (that is, not
704 // expecting onNewIntent()), then we will finish the current
705 // instance of the activity so a new fresh one can be started.
706 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
707 && (launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0) {
708 if (!ret.finishing) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700709 if (stack != null) {
710 stack.finishActivityLocked(
711 ret, Activity.RESULT_CANCELED, null, "clear-task-top", false);
712 }
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700713 return null;
714 }
715 }
716
717 return ret;
718 }
719 }
720
721 return null;
722 }
723
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700724 public TaskThumbnail getTaskThumbnailLocked() {
Craig Mautner21d24a22014-04-23 11:45:37 -0700725 if (stack != null) {
726 final ActivityRecord resumedActivity = stack.mResumedActivity;
727 if (resumedActivity != null && resumedActivity.task == this) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700728 final Bitmap thumbnail = stack.screenshotActivities(resumedActivity);
729 setLastThumbnail(thumbnail);
Craig Mautner21d24a22014-04-23 11:45:37 -0700730 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700731 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700732 final TaskThumbnail taskThumbnail = new TaskThumbnail();
733 getLastThumbnail(taskThumbnail);
734 return taskThumbnail;
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700735 }
736
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700737 public void removeTaskActivitiesLocked() {
738 // Just remove the entire task.
739 performClearTaskAtIndexLocked(0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700740 }
741
Craig Mautner432f64e2015-05-20 14:59:57 -0700742 String lockTaskAuthToString() {
743 switch (mLockTaskAuth) {
744 case LOCK_TASK_AUTH_DONT_LOCK: return "LOCK_TASK_AUTH_DONT_LOCK";
745 case LOCK_TASK_AUTH_PINNABLE: return "LOCK_TASK_AUTH_PINNABLE";
746 case LOCK_TASK_AUTH_LAUNCHABLE: return "LOCK_TASK_AUTH_LAUNCHABLE";
747 case LOCK_TASK_AUTH_WHITELISTED: return "LOCK_TASK_AUTH_WHITELISTED";
748 default: return "unknown=" + mLockTaskAuth;
749 }
750 }
751
Craig Mautner15df08a2015-04-01 12:17:18 -0700752 void setLockTaskAuth() {
753 switch (mLockTaskMode) {
754 case LOCK_TASK_LAUNCH_MODE_DEFAULT:
755 mLockTaskAuth = isLockTaskWhitelistedLocked() ?
756 LOCK_TASK_AUTH_WHITELISTED : LOCK_TASK_AUTH_PINNABLE;
757 break;
758
759 case LOCK_TASK_LAUNCH_MODE_NEVER:
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700760 mLockTaskAuth = mPrivileged ?
Craig Mautner15df08a2015-04-01 12:17:18 -0700761 LOCK_TASK_AUTH_DONT_LOCK : LOCK_TASK_AUTH_PINNABLE;
762 break;
763
764 case LOCK_TASK_LAUNCH_MODE_ALWAYS:
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700765 mLockTaskAuth = mPrivileged ?
Craig Mautner15df08a2015-04-01 12:17:18 -0700766 LOCK_TASK_AUTH_LAUNCHABLE: LOCK_TASK_AUTH_PINNABLE;
767 break;
768
769 case LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED:
770 mLockTaskAuth = isLockTaskWhitelistedLocked() ?
771 LOCK_TASK_AUTH_LAUNCHABLE : LOCK_TASK_AUTH_PINNABLE;
772 break;
773 }
Craig Mautner432f64e2015-05-20 14:59:57 -0700774 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "setLockTaskAuth: task=" + this +
775 " mLockTaskAuth=" + lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -0700776 }
777
778 boolean isLockTaskWhitelistedLocked() {
779 if (mCallingPackage == null) {
780 return false;
781 }
782 String[] packages = mService.mLockTaskPackages.get(userId);
783 if (packages == null) {
784 return false;
785 }
786 for (int i = packages.length - 1; i >= 0; --i) {
787 if (mCallingPackage.equals(packages[i])) {
788 return true;
789 }
790 }
791 return false;
792 }
Craig Mautnera82aa092013-09-13 15:34:08 -0700793 boolean isHomeTask() {
Craig Mautner84984fa2014-06-19 11:19:20 -0700794 return taskType == HOME_ACTIVITY_TYPE;
Craig Mautnera82aa092013-09-13 15:34:08 -0700795 }
796
Craig Mautner86d67a42013-05-14 10:34:38 -0700797 boolean isApplicationTask() {
Craig Mautner84984fa2014-06-19 11:19:20 -0700798 return taskType == APPLICATION_ACTIVITY_TYPE;
799 }
800
801 boolean isOverHomeStack() {
802 return mTaskToReturnTo == HOME_ACTIVITY_TYPE || mTaskToReturnTo == RECENTS_ACTIVITY_TYPE;
Craig Mautner1602ec22013-05-12 10:24:27 -0700803 }
804
Craig Mautner525f3d92013-05-07 14:01:50 -0700805 /**
806 * Find the activity in the history stack within the given task. Returns
807 * the index within the history at which it's found, or < 0 if not found.
808 */
809 final ActivityRecord findActivityInHistoryLocked(ActivityRecord r) {
810 final ComponentName realActivity = r.realActivity;
811 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
812 ActivityRecord candidate = mActivities.get(activityNdx);
813 if (candidate.finishing) {
814 continue;
815 }
816 if (candidate.realActivity.equals(realActivity)) {
817 return candidate;
818 }
819 }
820 return null;
821 }
822
Winson Chunga449dc02014-05-16 11:15:04 -0700823 /** Updates the last task description values. */
824 void updateTaskDescription() {
825 // Traverse upwards looking for any break between main task activities and
826 // utility activities.
827 int activityNdx;
828 final int numActivities = mActivities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700829 final boolean relinquish = numActivities == 0 ? false :
830 (mActivities.get(0).info.flags & ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY) != 0;
Winson Chunga449dc02014-05-16 11:15:04 -0700831 for (activityNdx = Math.min(numActivities, 1); activityNdx < numActivities;
Craig Mautner21d24a22014-04-23 11:45:37 -0700832 ++activityNdx) {
Winson Chunga449dc02014-05-16 11:15:04 -0700833 final ActivityRecord r = mActivities.get(activityNdx);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700834 if (relinquish && (r.info.flags & ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
835 // This will be the top activity for determining taskDescription. Pre-inc to
836 // overcome initial decrement below.
837 ++activityNdx;
838 break;
839 }
Winson Chunga449dc02014-05-16 11:15:04 -0700840 if (r.intent != null &&
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700841 (r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Winson Chunga449dc02014-05-16 11:15:04 -0700842 break;
843 }
844 }
845 if (activityNdx > 0) {
846 // Traverse downwards starting below break looking for set label, icon.
847 // Note that if there are activities in the task but none of them set the
848 // recent activity values, then we do not fall back to the last set
849 // values in the TaskRecord.
850 String label = null;
Craig Mautner648f69b2014-09-18 14:16:26 -0700851 String iconFilename = null;
Winson Chunga449dc02014-05-16 11:15:04 -0700852 int colorPrimary = 0;
853 for (--activityNdx; activityNdx >= 0; --activityNdx) {
854 final ActivityRecord r = mActivities.get(activityNdx);
855 if (r.taskDescription != null) {
856 if (label == null) {
857 label = r.taskDescription.getLabel();
858 }
Craig Mautner648f69b2014-09-18 14:16:26 -0700859 if (iconFilename == null) {
860 iconFilename = r.taskDescription.getIconFilename();
Winson Chunga449dc02014-05-16 11:15:04 -0700861 }
862 if (colorPrimary == 0) {
863 colorPrimary = r.taskDescription.getPrimaryColor();
Winson Chunga449dc02014-05-16 11:15:04 -0700864 }
865 }
866 }
Craig Mautner648f69b2014-09-18 14:16:26 -0700867 lastTaskDescription = new TaskDescription(label, colorPrimary, iconFilename);
Winson Chungec396d62014-08-06 17:08:00 -0700868 // Update the task affiliation color if we are the parent of the group
869 if (taskId == mAffiliatedTaskId) {
870 mAffiliatedTaskColor = lastTaskDescription.getPrimaryColor();
871 }
Winson Chunga449dc02014-05-16 11:15:04 -0700872 }
873 }
874
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700875 int findEffectiveRootIndex() {
Craig Mautner4767f4b2014-09-18 15:38:33 -0700876 int effectiveNdx = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700877 final int topActivityNdx = mActivities.size() - 1;
Dianne Hackborn39569af2014-09-23 10:56:58 -0700878 for (int activityNdx = 0; activityNdx <= topActivityNdx; ++activityNdx) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700879 final ActivityRecord r = mActivities.get(activityNdx);
880 if (r.finishing) {
881 continue;
882 }
Craig Mautner4767f4b2014-09-18 15:38:33 -0700883 effectiveNdx = activityNdx;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700884 if ((r.info.flags & ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
885 break;
886 }
887 }
Craig Mautner4767f4b2014-09-18 15:38:33 -0700888 return effectiveNdx;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700889 }
890
891 void updateEffectiveIntent() {
892 final int effectiveRootIndex = findEffectiveRootIndex();
893 final ActivityRecord r = mActivities.get(effectiveRootIndex);
Winson Chungfee26772014-08-05 12:21:52 -0700894 setIntent(r);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700895 }
896
Craig Mautner21d24a22014-04-23 11:45:37 -0700897 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700898 if (DEBUG_RECENTS) Slog.i(TAG_RECENTS, "Saving task=" + this);
Craig Mautner21d24a22014-04-23 11:45:37 -0700899
900 out.attribute(null, ATTR_TASKID, String.valueOf(taskId));
901 if (realActivity != null) {
902 out.attribute(null, ATTR_REALACTIVITY, realActivity.flattenToShortString());
903 }
904 if (origActivity != null) {
905 out.attribute(null, ATTR_ORIGACTIVITY, origActivity.flattenToShortString());
906 }
Dianne Hackborn79228822014-09-16 11:11:23 -0700907 // Write affinity, and root affinity if it is different from affinity.
908 // We use the special string "@" for a null root affinity, so we can identify
909 // later whether we were given a root affinity or should just make it the
910 // same as the affinity.
Craig Mautner21d24a22014-04-23 11:45:37 -0700911 if (affinity != null) {
912 out.attribute(null, ATTR_AFFINITY, affinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700913 if (!affinity.equals(rootAffinity)) {
914 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
915 }
916 } else if (rootAffinity != null) {
917 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
Craig Mautner21d24a22014-04-23 11:45:37 -0700918 }
919 out.attribute(null, ATTR_ROOTHASRESET, String.valueOf(rootWasReset));
Dianne Hackborn13420f22014-07-18 15:43:56 -0700920 out.attribute(null, ATTR_AUTOREMOVERECENTS, String.valueOf(autoRemoveRecents));
Craig Mautner21d24a22014-04-23 11:45:37 -0700921 out.attribute(null, ATTR_ASKEDCOMPATMODE, String.valueOf(askedCompatMode));
922 out.attribute(null, ATTR_USERID, String.valueOf(userId));
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700923 out.attribute(null, ATTR_EFFECTIVE_UID, String.valueOf(effectiveUid));
Craig Mautner21d24a22014-04-23 11:45:37 -0700924 out.attribute(null, ATTR_TASKTYPE, String.valueOf(taskType));
Winson Chungffa2ec62014-07-03 15:54:42 -0700925 out.attribute(null, ATTR_FIRSTACTIVETIME, String.valueOf(firstActiveTime));
Winson Chungf1fbd772014-06-24 18:06:58 -0700926 out.attribute(null, ATTR_LASTACTIVETIME, String.valueOf(lastActiveTime));
Craig Mautner21d24a22014-04-23 11:45:37 -0700927 out.attribute(null, ATTR_LASTTIMEMOVED, String.valueOf(mLastTimeMoved));
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700928 out.attribute(null, ATTR_NEVERRELINQUISH, String.valueOf(mNeverRelinquishIdentity));
Craig Mautner21d24a22014-04-23 11:45:37 -0700929 if (lastDescription != null) {
930 out.attribute(null, ATTR_LASTDESCRIPTION, lastDescription.toString());
931 }
Winson Chung2cb86c72014-06-25 12:03:30 -0700932 if (lastTaskDescription != null) {
Craig Mautner648f69b2014-09-18 14:16:26 -0700933 lastTaskDescription.saveToXml(out);
Winson Chung2cb86c72014-06-25 12:03:30 -0700934 }
Winson Chungec396d62014-08-06 17:08:00 -0700935 out.attribute(null, ATTR_TASK_AFFILIATION_COLOR, String.valueOf(mAffiliatedTaskColor));
Craig Mautnera228ae92014-07-09 05:44:55 -0700936 out.attribute(null, ATTR_TASK_AFFILIATION, String.valueOf(mAffiliatedTaskId));
937 out.attribute(null, ATTR_PREV_AFFILIATION, String.valueOf(mPrevAffiliateTaskId));
938 out.attribute(null, ATTR_NEXT_AFFILIATION, String.valueOf(mNextAffiliateTaskId));
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700939 out.attribute(null, ATTR_CALLING_UID, String.valueOf(mCallingUid));
940 out.attribute(null, ATTR_CALLING_PACKAGE, mCallingPackage == null ? "" : mCallingPackage);
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800941 out.attribute(null, ATTR_RESIZEABLE, String.valueOf(mResizeable));
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700942 out.attribute(null, ATTR_PRIVILEGED, String.valueOf(mPrivileged));
Winson Chung2cb86c72014-06-25 12:03:30 -0700943
Craig Mautner21d24a22014-04-23 11:45:37 -0700944 if (affinityIntent != null) {
945 out.startTag(null, TAG_AFFINITYINTENT);
946 affinityIntent.saveToXml(out);
947 out.endTag(null, TAG_AFFINITYINTENT);
948 }
949
950 out.startTag(null, TAG_INTENT);
951 intent.saveToXml(out);
952 out.endTag(null, TAG_INTENT);
953
954 final ArrayList<ActivityRecord> activities = mActivities;
955 final int numActivities = activities.size();
956 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
957 final ActivityRecord r = activities.get(activityNdx);
Craig Mautner43e52ed2014-06-16 17:18:52 -0700958 if (r.info.persistableMode == ActivityInfo.PERSIST_ROOT_ONLY || !r.isPersistable() ||
Wale Ogunwale843bfb92015-03-27 11:06:48 -0700959 ((r.intent.getFlags() & FLAG_ACTIVITY_NEW_DOCUMENT
960 | FLAG_ACTIVITY_RETAIN_IN_RECENTS) == FLAG_ACTIVITY_NEW_DOCUMENT) &&
Craig Mautner43e52ed2014-06-16 17:18:52 -0700961 activityNdx > 0) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -0700962 // Stop at first non-persistable or first break in task (CLEAR_WHEN_TASK_RESET).
Craig Mautner21d24a22014-04-23 11:45:37 -0700963 break;
964 }
965 out.startTag(null, TAG_ACTIVITY);
966 r.saveToXml(out);
967 out.endTag(null, TAG_ACTIVITY);
968 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700969 }
970
971 static TaskRecord restoreFromXml(XmlPullParser in, ActivityStackSupervisor stackSupervisor)
972 throws IOException, XmlPullParserException {
973 Intent intent = null;
974 Intent affinityIntent = null;
975 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
976 ComponentName realActivity = null;
977 ComponentName origActivity = null;
978 String affinity = null;
Dianne Hackborn79228822014-09-16 11:11:23 -0700979 String rootAffinity = null;
980 boolean hasRootAffinity = false;
Craig Mautner21d24a22014-04-23 11:45:37 -0700981 boolean rootHasReset = false;
Dianne Hackborn13420f22014-07-18 15:43:56 -0700982 boolean autoRemoveRecents = false;
Craig Mautner21d24a22014-04-23 11:45:37 -0700983 boolean askedCompatMode = false;
984 int taskType = ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner21d24a22014-04-23 11:45:37 -0700985 int userId = 0;
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700986 int effectiveUid = -1;
Craig Mautner21d24a22014-04-23 11:45:37 -0700987 String lastDescription = null;
Winson Chungffa2ec62014-07-03 15:54:42 -0700988 long firstActiveTime = -1;
Winson Chung2cb86c72014-06-25 12:03:30 -0700989 long lastActiveTime = -1;
Craig Mautner21d24a22014-04-23 11:45:37 -0700990 long lastTimeOnTop = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700991 boolean neverRelinquishIdentity = true;
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700992 int taskId = INVALID_TASK_ID;
Craig Mautner21d24a22014-04-23 11:45:37 -0700993 final int outerDepth = in.getDepth();
Craig Mautner648f69b2014-09-18 14:16:26 -0700994 TaskDescription taskDescription = new TaskDescription();
Wale Ogunwale18795a22014-12-03 11:38:33 -0800995 int taskAffiliation = INVALID_TASK_ID;
Winson Chungec396d62014-08-06 17:08:00 -0700996 int taskAffiliationColor = 0;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800997 int prevTaskId = INVALID_TASK_ID;
998 int nextTaskId = INVALID_TASK_ID;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700999 int callingUid = -1;
1000 String callingPackage = "";
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001001 boolean resizeable = false;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001002 boolean privileged = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001003
1004 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
1005 final String attrName = in.getAttributeName(attrNdx);
1006 final String attrValue = in.getAttributeValue(attrNdx);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001007 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: attribute name=" +
1008 attrName + " value=" + attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001009 if (ATTR_TASKID.equals(attrName)) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001010 if (taskId == INVALID_TASK_ID) taskId = Integer.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001011 } else if (ATTR_REALACTIVITY.equals(attrName)) {
1012 realActivity = ComponentName.unflattenFromString(attrValue);
1013 } else if (ATTR_ORIGACTIVITY.equals(attrName)) {
1014 origActivity = ComponentName.unflattenFromString(attrValue);
1015 } else if (ATTR_AFFINITY.equals(attrName)) {
1016 affinity = attrValue;
Dianne Hackborn79228822014-09-16 11:11:23 -07001017 } else if (ATTR_ROOT_AFFINITY.equals(attrName)) {
1018 rootAffinity = attrValue;
1019 hasRootAffinity = true;
Craig Mautner21d24a22014-04-23 11:45:37 -07001020 } else if (ATTR_ROOTHASRESET.equals(attrName)) {
1021 rootHasReset = Boolean.valueOf(attrValue);
Dianne Hackborn13420f22014-07-18 15:43:56 -07001022 } else if (ATTR_AUTOREMOVERECENTS.equals(attrName)) {
1023 autoRemoveRecents = Boolean.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001024 } else if (ATTR_ASKEDCOMPATMODE.equals(attrName)) {
1025 askedCompatMode = Boolean.valueOf(attrValue);
1026 } else if (ATTR_USERID.equals(attrName)) {
1027 userId = Integer.valueOf(attrValue);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001028 } else if (ATTR_EFFECTIVE_UID.equals(attrName)) {
1029 effectiveUid = Integer.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001030 } else if (ATTR_TASKTYPE.equals(attrName)) {
1031 taskType = Integer.valueOf(attrValue);
Winson Chungffa2ec62014-07-03 15:54:42 -07001032 } else if (ATTR_FIRSTACTIVETIME.equals(attrName)) {
1033 firstActiveTime = Long.valueOf(attrValue);
Winson Chungf1fbd772014-06-24 18:06:58 -07001034 } else if (ATTR_LASTACTIVETIME.equals(attrName)) {
1035 lastActiveTime = Long.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001036 } else if (ATTR_LASTDESCRIPTION.equals(attrName)) {
1037 lastDescription = attrValue;
1038 } else if (ATTR_LASTTIMEMOVED.equals(attrName)) {
1039 lastTimeOnTop = Long.valueOf(attrValue);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001040 } else if (ATTR_NEVERRELINQUISH.equals(attrName)) {
1041 neverRelinquishIdentity = Boolean.valueOf(attrValue);
Craig Mautner648f69b2014-09-18 14:16:26 -07001042 } else if (attrName.startsWith(TaskDescription.ATTR_TASKDESCRIPTION_PREFIX)) {
1043 taskDescription.restoreFromXml(attrName, attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001044 } else if (ATTR_TASK_AFFILIATION.equals(attrName)) {
1045 taskAffiliation = Integer.valueOf(attrValue);
1046 } else if (ATTR_PREV_AFFILIATION.equals(attrName)) {
1047 prevTaskId = Integer.valueOf(attrValue);
1048 } else if (ATTR_NEXT_AFFILIATION.equals(attrName)) {
1049 nextTaskId = Integer.valueOf(attrValue);
Winson Chungec396d62014-08-06 17:08:00 -07001050 } else if (ATTR_TASK_AFFILIATION_COLOR.equals(attrName)) {
1051 taskAffiliationColor = Integer.valueOf(attrValue);
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001052 } else if (ATTR_CALLING_UID.equals(attrName)) {
1053 callingUid = Integer.valueOf(attrValue);
1054 } else if (ATTR_CALLING_PACKAGE.equals(attrName)) {
1055 callingPackage = attrValue;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001056 } else if (ATTR_RESIZEABLE.equals(attrName)) {
1057 resizeable = Boolean.valueOf(attrValue);
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001058 } else if (ATTR_PRIVILEGED.equals(attrName)) {
1059 privileged = Boolean.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001060 } else {
1061 Slog.w(TAG, "TaskRecord: Unknown attribute=" + attrName);
1062 }
1063 }
1064
1065 int event;
1066 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
1067 (event != XmlPullParser.END_TAG || in.getDepth() < outerDepth)) {
1068 if (event == XmlPullParser.START_TAG) {
1069 final String name = in.getName();
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001070 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: START_TAG name=" +
1071 name);
Craig Mautner21d24a22014-04-23 11:45:37 -07001072 if (TAG_AFFINITYINTENT.equals(name)) {
1073 affinityIntent = Intent.restoreFromXml(in);
1074 } else if (TAG_INTENT.equals(name)) {
1075 intent = Intent.restoreFromXml(in);
1076 } else if (TAG_ACTIVITY.equals(name)) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001077 ActivityRecord activity = ActivityRecord.restoreFromXml(in, stackSupervisor);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001078 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: activity=" +
1079 activity);
Craig Mautner21d24a22014-04-23 11:45:37 -07001080 if (activity != null) {
1081 activities.add(activity);
1082 }
1083 } else {
1084 Slog.e(TAG, "restoreTask: Unexpected name=" + name);
1085 XmlUtils.skipCurrentTag(in);
1086 }
1087 }
1088 }
Dianne Hackborn79228822014-09-16 11:11:23 -07001089 if (!hasRootAffinity) {
1090 rootAffinity = affinity;
1091 } else if ("@".equals(rootAffinity)) {
1092 rootAffinity = null;
1093 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001094 if (effectiveUid <= 0) {
1095 Intent checkIntent = intent != null ? intent : affinityIntent;
1096 effectiveUid = 0;
1097 if (checkIntent != null) {
1098 IPackageManager pm = AppGlobals.getPackageManager();
1099 try {
1100 ApplicationInfo ai = pm.getApplicationInfo(
1101 checkIntent.getComponent().getPackageName(),
1102 PackageManager.GET_UNINSTALLED_PACKAGES
1103 | PackageManager.GET_DISABLED_COMPONENTS, userId);
1104 if (ai != null) {
1105 effectiveUid = ai.uid;
1106 }
1107 } catch (RemoteException e) {
1108 }
1109 }
1110 Slog.w(TAG, "Updating task #" + taskId + " for " + checkIntent
1111 + ": effectiveUid=" + effectiveUid);
1112 }
1113
Craig Mautner21d24a22014-04-23 11:45:37 -07001114 final TaskRecord task = new TaskRecord(stackSupervisor.mService, taskId, intent,
Dianne Hackborn79228822014-09-16 11:11:23 -07001115 affinityIntent, affinity, rootAffinity, realActivity, origActivity, rootHasReset,
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001116 autoRemoveRecents, askedCompatMode, taskType, userId, effectiveUid, lastDescription,
1117 activities, firstActiveTime, lastActiveTime, lastTimeOnTop, neverRelinquishIdentity,
Winson Chungec396d62014-08-06 17:08:00 -07001118 taskDescription, taskAffiliation, prevTaskId, nextTaskId, taskAffiliationColor,
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001119 callingUid, callingPackage, resizeable, privileged);
Craig Mautner21d24a22014-04-23 11:45:37 -07001120
1121 for (int activityNdx = activities.size() - 1; activityNdx >=0; --activityNdx) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001122 activities.get(activityNdx).task = task;
Craig Mautner21d24a22014-04-23 11:45:37 -07001123 }
1124
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001125 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "Restored task=" + task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001126 return task;
1127 }
1128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001130 pw.print(prefix); pw.print("userId="); pw.print(userId);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001131 pw.print(" effectiveUid="); UserHandle.formatUid(pw, effectiveUid);
1132 pw.print(" mCallingUid="); UserHandle.formatUid(pw, mCallingUid);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001133 pw.print(" mCallingPackage="); pw.println(mCallingPackage);
Dianne Hackborn79228822014-09-16 11:11:23 -07001134 if (affinity != null || rootAffinity != null) {
1135 pw.print(prefix); pw.print("affinity="); pw.print(affinity);
1136 if (affinity == null || !affinity.equals(rootAffinity)) {
1137 pw.print(" root="); pw.println(rootAffinity);
1138 } else {
1139 pw.println();
1140 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001141 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07001142 if (voiceSession != null || voiceInteractor != null) {
1143 pw.print(prefix); pw.print("VOICE: session=0x");
1144 pw.print(Integer.toHexString(System.identityHashCode(voiceSession)));
1145 pw.print(" interactor=0x");
1146 pw.println(Integer.toHexString(System.identityHashCode(voiceInteractor)));
1147 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001148 if (intent != null) {
1149 StringBuilder sb = new StringBuilder(128);
1150 sb.append(prefix); sb.append("intent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08001151 intent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001152 sb.append('}');
1153 pw.println(sb.toString());
1154 }
1155 if (affinityIntent != null) {
1156 StringBuilder sb = new StringBuilder(128);
1157 sb.append(prefix); sb.append("affinityIntent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08001158 affinityIntent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001159 sb.append('}');
1160 pw.println(sb.toString());
1161 }
1162 if (origActivity != null) {
1163 pw.print(prefix); pw.print("origActivity=");
1164 pw.println(origActivity.flattenToShortString());
1165 }
1166 if (realActivity != null) {
1167 pw.print(prefix); pw.print("realActivity=");
1168 pw.println(realActivity.flattenToShortString());
1169 }
Dianne Hackborn852975d2014-08-22 17:42:43 -07001170 if (autoRemoveRecents || isPersistable || taskType != 0 || mTaskToReturnTo != 0
1171 || numFullscreen != 0) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001172 pw.print(prefix); pw.print("autoRemoveRecents="); pw.print(autoRemoveRecents);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001173 pw.print(" isPersistable="); pw.print(isPersistable);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001174 pw.print(" numFullscreen="); pw.print(numFullscreen);
1175 pw.print(" taskType="); pw.print(taskType);
1176 pw.print(" mTaskToReturnTo="); pw.println(mTaskToReturnTo);
1177 }
Craig Mautner432f64e2015-05-20 14:59:57 -07001178 if (rootWasReset || mNeverRelinquishIdentity || mReuseTask
1179 || mLockTaskAuth != LOCK_TASK_AUTH_PINNABLE) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001180 pw.print(prefix); pw.print("rootWasReset="); pw.print(rootWasReset);
1181 pw.print(" mNeverRelinquishIdentity="); pw.print(mNeverRelinquishIdentity);
Craig Mautner432f64e2015-05-20 14:59:57 -07001182 pw.print(" mReuseTask="); pw.print(mReuseTask);
1183 pw.print(" mLockTaskAuth="); pw.println(lockTaskAuthToString());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001184 }
Wale Ogunwale18795a22014-12-03 11:38:33 -08001185 if (mAffiliatedTaskId != taskId || mPrevAffiliateTaskId != INVALID_TASK_ID
1186 || mPrevAffiliate != null || mNextAffiliateTaskId != INVALID_TASK_ID
1187 || mNextAffiliate != null) {
Dianne Hackborn852975d2014-08-22 17:42:43 -07001188 pw.print(prefix); pw.print("affiliation="); pw.print(mAffiliatedTaskId);
1189 pw.print(" prevAffiliation="); pw.print(mPrevAffiliateTaskId);
1190 pw.print(" (");
1191 if (mPrevAffiliate == null) {
1192 pw.print("null");
1193 } else {
1194 pw.print(Integer.toHexString(System.identityHashCode(mPrevAffiliate)));
1195 }
1196 pw.print(") nextAffiliation="); pw.print(mNextAffiliateTaskId);
1197 pw.print(" (");
1198 if (mNextAffiliate == null) {
1199 pw.print("null");
1200 } else {
1201 pw.print(Integer.toHexString(System.identityHashCode(mNextAffiliate)));
1202 }
1203 pw.println(")");
1204 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001205 pw.print(prefix); pw.print("Activities="); pw.println(mActivities);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001206 if (!askedCompatMode || !inRecents || !isAvailable) {
1207 pw.print(prefix); pw.print("askedCompatMode="); pw.print(askedCompatMode);
1208 pw.print(" inRecents="); pw.print(inRecents);
1209 pw.print(" isAvailable="); pw.println(isAvailable);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07001210 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001211 pw.print(prefix); pw.print("lastThumbnail="); pw.print(mLastThumbnail);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001212 pw.print(" lastThumbnailFile="); pw.println(mLastThumbnailFile);
1213 if (lastDescription != null) {
1214 pw.print(prefix); pw.print("lastDescription="); pw.println(lastDescription);
1215 }
Dianne Hackbornd38aed82014-06-10 21:36:35 -07001216 pw.print(prefix); pw.print("hasBeenVisible="); pw.print(hasBeenVisible);
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001217 pw.print(" mResizeable="); pw.print(mResizeable);
Winson Chungffa2ec62014-07-03 15:54:42 -07001218 pw.print(" firstActiveTime="); pw.print(lastActiveTime);
Dianne Hackbornd38aed82014-06-10 21:36:35 -07001219 pw.print(" lastActiveTime="); pw.print(lastActiveTime);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001220 pw.print(" (inactive for ");
1221 pw.print((getInactiveDuration()/1000)); pw.println("s)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 }
1223
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001224 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001226 StringBuilder sb = new StringBuilder(128);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001227 if (stringName != null) {
1228 sb.append(stringName);
1229 sb.append(" U=");
1230 sb.append(userId);
1231 sb.append(" sz=");
1232 sb.append(mActivities.size());
1233 sb.append('}');
1234 return sb.toString();
1235 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001236 sb.append("TaskRecord{");
1237 sb.append(Integer.toHexString(System.identityHashCode(this)));
1238 sb.append(" #");
1239 sb.append(taskId);
1240 if (affinity != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001241 sb.append(" A=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001242 sb.append(affinity);
1243 } else if (intent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001244 sb.append(" I=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001245 sb.append(intent.getComponent().flattenToShortString());
1246 } else if (affinityIntent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001247 sb.append(" aI=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001248 sb.append(affinityIntent.getComponent().flattenToShortString());
1249 } else {
1250 sb.append(" ??");
1251 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001252 stringName = sb.toString();
1253 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 }
1255}