blob: fd787df251158643f3d4f98dff8d0a2530e7fc65 [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";
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +000098 static final String ATTR_REALACTIVITY_SUSPENDED = "real_activity_suspended";
Craig Mautner21d24a22014-04-23 11:45:37 -070099 private static final String ATTR_ORIGACTIVITY = "orig_activity";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700100 private static final String TAG_ACTIVITY = "activity";
Craig Mautner21d24a22014-04-23 11:45:37 -0700101 private static final String ATTR_AFFINITY = "affinity";
Dianne Hackborn79228822014-09-16 11:11:23 -0700102 private static final String ATTR_ROOT_AFFINITY = "root_affinity";
Craig Mautner21d24a22014-04-23 11:45:37 -0700103 private static final String ATTR_ROOTHASRESET = "root_has_reset";
Dianne Hackborn13420f22014-07-18 15:43:56 -0700104 private static final String ATTR_AUTOREMOVERECENTS = "auto_remove_recents";
Craig Mautner21d24a22014-04-23 11:45:37 -0700105 private static final String ATTR_ASKEDCOMPATMODE = "asked_compat_mode";
106 private static final String ATTR_USERID = "user_id";
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700107 private static final String ATTR_EFFECTIVE_UID = "effective_uid";
Craig Mautner21d24a22014-04-23 11:45:37 -0700108 private static final String ATTR_TASKTYPE = "task_type";
Winson Chungffa2ec62014-07-03 15:54:42 -0700109 private static final String ATTR_FIRSTACTIVETIME = "first_active_time";
Winson Chungf1fbd772014-06-24 18:06:58 -0700110 private static final String ATTR_LASTACTIVETIME = "last_active_time";
Craig Mautner21d24a22014-04-23 11:45:37 -0700111 private static final String ATTR_LASTDESCRIPTION = "last_description";
112 private static final String ATTR_LASTTIMEMOVED = "last_time_moved";
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700113 private static final String ATTR_NEVERRELINQUISH = "never_relinquish_identity";
Wale Ogunwale18795a22014-12-03 11:38:33 -0800114 static final String ATTR_TASK_AFFILIATION = "task_affiliation";
Craig Mautnera228ae92014-07-09 05:44:55 -0700115 private static final String ATTR_PREV_AFFILIATION = "prev_affiliation";
116 private static final String ATTR_NEXT_AFFILIATION = "next_affiliation";
Winson Chungec396d62014-08-06 17:08:00 -0700117 private static final String ATTR_TASK_AFFILIATION_COLOR = "task_affiliation_color";
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700118 private static final String ATTR_CALLING_UID = "calling_uid";
119 private static final String ATTR_CALLING_PACKAGE = "calling_package";
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800120 private static final String ATTR_RESIZEABLE = "resizeable";
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700121 private static final String ATTR_PRIVILEGED = "privileged";
Wale Ogunwale706ed792015-08-02 10:29:44 -0700122 private static final String ATTR_NON_FULLSCREEN_BOUNDS = "non_fullscreen_bounds";
Craig Mautner21d24a22014-04-23 11:45:37 -0700123
124 private static final String TASK_THUMBNAIL_SUFFIX = "_task_thumbnail";
125
Craig Mautner0b633fc2014-07-23 10:42:18 -0700126 static final boolean IGNORE_RETURN_TO_RECENTS = true;
127
Wale Ogunwale18795a22014-12-03 11:38:33 -0800128 static final int INVALID_TASK_ID = -1;
129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 final int taskId; // Unique identifier for this task.
Dianne Hackborn79228822014-09-16 11:11:23 -0700131 String affinity; // The affinity name for this task, or null; may change identity.
132 String rootAffinity; // Initial base affinity, or null; does not change from initial root.
Dianne Hackborn91097de2014-04-04 18:02:06 -0700133 final IVoiceInteractionSession voiceSession; // Voice interaction session driving task
134 final IVoiceInteractor voiceInteractor; // Associated interactor to provide to app
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 Intent intent; // The original intent that started the task.
136 Intent affinityIntent; // Intent of affinity-moved activity that started this task.
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700137 int effectiveUid; // The current effective uid of the identity of this task.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 ComponentName origActivity; // The non-alias activity component of the intent.
139 ComponentName realActivity; // The actual activity component that started the task.
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +0000140 boolean realActivitySuspended; // True if the actual activity component that started the
141 // task is suspended.
Winson Chungffa2ec62014-07-03 15:54:42 -0700142 long firstActiveTime; // First time this task was active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 long lastActiveTime; // Last time this task was active, including sleep.
Dianne Hackborn852975d2014-08-22 17:42:43 -0700144 boolean inRecents; // Actually in the recents list?
145 boolean isAvailable; // Is the activity available to be launched?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 boolean rootWasReset; // True if the intent at the root of the task had
147 // the FLAG_ACTIVITY_RESET_TASK_IF_NEEDED flag.
Dianne Hackborn13420f22014-07-18 15:43:56 -0700148 boolean autoRemoveRecents; // If true, we should automatically remove the task from
149 // recents when activity finishes
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700150 boolean askedCompatMode;// Have asked the user about compat mode for this task.
Dianne Hackbornd38aed82014-06-10 21:36:35 -0700151 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 -0800152
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700153 String stringName; // caching of toString() result.
Dianne Hackborn9da2d402012-03-15 13:43:08 -0700154 int userId; // user for which this task was created
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800155
156 int numFullscreen; // Number of fullscreen activities.
157
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800158 boolean mResizeable; // Activities in the task resizeable. Based on the resizable setting of
159 // the root activity.
Craig Mautner15df08a2015-04-01 12:17:18 -0700160 int mLockTaskMode; // Which tasklock mode to launch this task in. One of
161 // ActivityManager.LOCK_TASK_LAUNCH_MODE_*
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700162 private boolean mPrivileged; // The root activity application of this task holds
163 // privileged permissions.
164
Craig Mautner15df08a2015-04-01 12:17:18 -0700165 /** Can't be put in lockTask mode. */
166 final static int LOCK_TASK_AUTH_DONT_LOCK = 0;
Benjamin Franz469dd582015-06-09 14:24:36 +0100167 /** Can enter app pinning with user approval. Can never start over existing lockTask task. */
Craig Mautner15df08a2015-04-01 12:17:18 -0700168 final static int LOCK_TASK_AUTH_PINNABLE = 1;
169 /** Starts in LOCK_TASK_MODE_LOCKED automatically. Can start over existing lockTask task. */
170 final static int LOCK_TASK_AUTH_LAUNCHABLE = 2;
Benjamin Franz469dd582015-06-09 14:24:36 +0100171 /** Can enter lockTask without user approval. Can start over existing lockTask task. */
Craig Mautner15df08a2015-04-01 12:17:18 -0700172 final static int LOCK_TASK_AUTH_WHITELISTED = 3;
Benjamin Franz469dd582015-06-09 14:24:36 +0100173 /** Priv-app that starts in LOCK_TASK_MODE_LOCKED automatically. Can start over existing
174 * lockTask task. */
175 final static int LOCK_TASK_AUTH_LAUNCHABLE_PRIV = 4;
Craig Mautner15df08a2015-04-01 12:17:18 -0700176 int mLockTaskAuth = LOCK_TASK_AUTH_PINNABLE;
177
178 int mLockTaskUid = -1; // The uid of the application that called startLockTask().
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800179
Winson Chung03a9bae2014-05-02 09:56:12 -0700180 // This represents the last resolved activity values for this task
181 // NOTE: This value needs to be persisted with each task
Craig Mautner648f69b2014-09-18 14:16:26 -0700182 TaskDescription lastTaskDescription = new TaskDescription();
Winson Chung03a9bae2014-05-02 09:56:12 -0700183
Craig Mautnerd2328952013-03-05 12:46:26 -0800184 /** List of all activities in the task arranged in history order */
Craig Mautner21d24a22014-04-23 11:45:37 -0700185 final ArrayList<ActivityRecord> mActivities;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800186
Craig Mautnerd2328952013-03-05 12:46:26 -0800187 /** Current stack */
188 ActivityStack stack;
189
Craig Mautner2c1faed2013-07-23 12:56:02 -0700190 /** Takes on same set of values as ActivityRecord.mActivityType */
Craig Mautner21d24a22014-04-23 11:45:37 -0700191 int taskType;
Craig Mautner1602ec22013-05-12 10:24:27 -0700192
Craig Mautner21d24a22014-04-23 11:45:37 -0700193 /** Takes on same value as first root activity */
194 boolean isPersistable = false;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700195 int maxRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700196
197 /** Only used for persistable tasks, otherwise 0. The last time this task was moved. Used for
198 * determining the order when restoring. Sign indicates whether last task movement was to front
199 * (positive) or back (negative). Absolute value indicates time. */
200 long mLastTimeMoved = System.currentTimeMillis();
201
Craig Mautner84984fa2014-06-19 11:19:20 -0700202 /** Indication of what to run next when task exits. Use ActivityRecord types.
203 * ActivityRecord.APPLICATION_ACTIVITY_TYPE indicates to resume the task below this one in the
204 * task stack. */
205 private int mTaskToReturnTo = APPLICATION_ACTIVITY_TYPE;
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700206
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700207 /** If original intent did not allow relinquishing task identity, save that information */
208 boolean mNeverRelinquishIdentity = true;
209
Craig Mautner362449a2014-06-20 14:04:39 -0700210 // Used in the unique case where we are clearing the task in order to reuse it. In that case we
211 // do not want to delete the stack when the task goes empty.
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -0800212 private boolean mReuseTask = false;
Craig Mautner362449a2014-06-20 14:04:39 -0700213
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700214 private Bitmap mLastThumbnail; // Last thumbnail captured for this item.
Wale Ogunwalebe23ff42014-10-21 16:29:51 -0700215 private final File mLastThumbnailFile; // File containing last thumbnail.
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700216 private final String mFilename;
Winsonc809cbb2015-11-02 12:06:15 -0800217 private TaskThumbnailInfo mLastThumbnailInfo;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700218 CharSequence lastDescription; // Last description captured for this item.
219
Craig Mautnera228ae92014-07-09 05:44:55 -0700220 int mAffiliatedTaskId; // taskId of parent affiliation or self if no parent.
Winson Chungec396d62014-08-06 17:08:00 -0700221 int mAffiliatedTaskColor; // color of the parent task affiliation.
Craig Mautnera228ae92014-07-09 05:44:55 -0700222 TaskRecord mPrevAffiliate; // previous task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800223 int mPrevAffiliateTaskId = INVALID_TASK_ID; // previous id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700224 TaskRecord mNextAffiliate; // next task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800225 int mNextAffiliateTaskId = INVALID_TASK_ID; // next id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700226
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700227 // For relaunching the task from recents as though it was launched by the original launcher.
228 int mCallingUid;
229 String mCallingPackage;
230
Craig Mautner21d24a22014-04-23 11:45:37 -0700231 final ActivityManagerService mService;
232
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700233 // Whether or not this task covers the entire screen; by default tasks are fullscreen.
234 boolean mFullscreen = true;
235
236 // Bounds of the Task. null for fullscreen tasks.
237 Rect mBounds = null;
Wale Ogunwale706ed792015-08-02 10:29:44 -0700238 // Last non-fullscreen bounds the task was launched in or resized to.
239 // The information is persisted and used to determine the appropriate stack to launch the
240 // task into on restore.
241 Rect mLastNonFullscreenBounds = null;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700242 // Minimal size for width/height of this task when it's resizeable. -1 means it should use the
243 // default minimal size.
244 final int mMinimalSize;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700245
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700246 // Ranking (from top) of this task among all visible tasks. (-1 means it's not visible)
247 // This number will be assigned when we evaluate OOM scores for all visible tasks.
248 int mLayerRank = -1;
249
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700250 Configuration mOverrideConfig = Configuration.EMPTY;
251
Craig Mautner21d24a22014-04-23 11:45:37 -0700252 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700253 IVoiceInteractionSession _voiceSession, IVoiceInteractor _voiceInteractor) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700254 mService = service;
Craig Mautnera228ae92014-07-09 05:44:55 -0700255 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
256 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800257 userId = UserHandle.getUserId(info.applicationInfo.uid);
258 mLastThumbnailFile = new File(TaskPersister.getUserImagesDir(userId), mFilename);
Winsonc809cbb2015-11-02 12:06:15 -0800259 mLastThumbnailInfo = new TaskThumbnailInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 taskId = _taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700261 mAffiliatedTaskId = _taskId;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700262 voiceSession = _voiceSession;
263 voiceInteractor = _voiceInteractor;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700264 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800265 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700266 mCallingUid = info.applicationInfo.uid;
267 mCallingPackage = info.packageName;
Martijn Coenend4a69702014-06-30 11:12:17 -0700268 setIntent(_intent, info);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700269 mMinimalSize = info != null && info.layout != null ? info.layout.minimalSize : -1;
Craig Mautner21d24a22014-04-23 11:45:37 -0700270 }
271
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700272 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Winsonc809cbb2015-11-02 12:06:15 -0800273 TaskDescription _taskDescription, TaskThumbnailInfo thumbnailInfo) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700274 mService = service;
275 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
276 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800277 userId = UserHandle.getUserId(info.applicationInfo.uid);
278 mLastThumbnailFile = new File(TaskPersister.getUserImagesDir(userId), mFilename);
Winsonc809cbb2015-11-02 12:06:15 -0800279 mLastThumbnailInfo = thumbnailInfo;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700280 taskId = _taskId;
281 mAffiliatedTaskId = _taskId;
282 voiceSession = null;
283 voiceInteractor = null;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700284 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800285 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700286 mCallingUid = info.applicationInfo.uid;
287 mCallingPackage = info.packageName;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700288 setIntent(_intent, info);
289
290 taskType = ActivityRecord.APPLICATION_ACTIVITY_TYPE;
291 isPersistable = true;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700292 // Clamp to [1, max].
293 maxRecents = Math.min(Math.max(info.maxRecents, 1),
294 ActivityManager.getMaxAppRecentsLimitStatic());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700295
296 taskType = APPLICATION_ACTIVITY_TYPE;
297 mTaskToReturnTo = HOME_ACTIVITY_TYPE;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700298 lastTaskDescription = _taskDescription;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700299 mMinimalSize = info != null && info.layout != null ? info.layout.minimalSize : -1;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700300 }
301
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800302 private TaskRecord(ActivityManagerService service, int _taskId, Intent _intent,
303 Intent _affinityIntent, String _affinity, String _rootAffinity,
304 ComponentName _realActivity, ComponentName _origActivity, boolean _rootWasReset,
305 boolean _autoRemoveRecents, boolean _askedCompatMode, int _taskType, int _userId,
306 int _effectiveUid, String _lastDescription, ArrayList<ActivityRecord> activities,
307 long _firstActiveTime, long _lastActiveTime, long lastTimeMoved,
308 boolean neverRelinquishIdentity, TaskDescription _lastTaskDescription,
Winsonc809cbb2015-11-02 12:06:15 -0800309 TaskThumbnailInfo lastThumbnailInfo, int taskAffiliation, int prevTaskId,
310 int nextTaskId, int taskAffiliationColor, int callingUid, String callingPackage,
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +0000311 boolean resizeable, boolean privileged, boolean realActivitySuspended) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700312 mService = service;
Craig Mautnera228ae92014-07-09 05:44:55 -0700313 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
314 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800315 mLastThumbnailFile = new File(TaskPersister.getUserImagesDir(_userId), mFilename);
Winsonc809cbb2015-11-02 12:06:15 -0800316 mLastThumbnailInfo = lastThumbnailInfo;
Craig Mautner21d24a22014-04-23 11:45:37 -0700317 taskId = _taskId;
318 intent = _intent;
319 affinityIntent = _affinityIntent;
320 affinity = _affinity;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800321 rootAffinity = _rootAffinity;
Craig Mautner21d24a22014-04-23 11:45:37 -0700322 voiceSession = null;
323 voiceInteractor = null;
324 realActivity = _realActivity;
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +0000325 realActivitySuspended = realActivitySuspended;
Craig Mautner21d24a22014-04-23 11:45:37 -0700326 origActivity = _origActivity;
327 rootWasReset = _rootWasReset;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700328 isAvailable = true;
Dianne Hackborn13420f22014-07-18 15:43:56 -0700329 autoRemoveRecents = _autoRemoveRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700330 askedCompatMode = _askedCompatMode;
331 taskType = _taskType;
Craig Mautner84984fa2014-06-19 11:19:20 -0700332 mTaskToReturnTo = HOME_ACTIVITY_TYPE;
Craig Mautner21d24a22014-04-23 11:45:37 -0700333 userId = _userId;
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700334 effectiveUid = _effectiveUid;
Winson Chungffa2ec62014-07-03 15:54:42 -0700335 firstActiveTime = _firstActiveTime;
Winson Chungf1fbd772014-06-24 18:06:58 -0700336 lastActiveTime = _lastActiveTime;
Craig Mautner21d24a22014-04-23 11:45:37 -0700337 lastDescription = _lastDescription;
338 mActivities = activities;
339 mLastTimeMoved = lastTimeMoved;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700340 mNeverRelinquishIdentity = neverRelinquishIdentity;
Winson Chung2cb86c72014-06-25 12:03:30 -0700341 lastTaskDescription = _lastTaskDescription;
Craig Mautnera228ae92014-07-09 05:44:55 -0700342 mAffiliatedTaskId = taskAffiliation;
Winson Chungec396d62014-08-06 17:08:00 -0700343 mAffiliatedTaskColor = taskAffiliationColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700344 mPrevAffiliateTaskId = prevTaskId;
345 mNextAffiliateTaskId = nextTaskId;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700346 mCallingUid = callingUid;
347 mCallingPackage = callingPackage;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -0800348 mResizeable = resizeable || mService.mForceResizableActivities;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700349 mPrivileged = privileged;
Suprabh Shukla74f9f162015-11-06 15:39:42 -0800350 ActivityInfo info = (mActivities.size() > 0) ? mActivities.get(0).info : null;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700351 mMinimalSize = info != null && info.layout != null ? info.layout.minimalSize : -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 }
353
354 void touchActiveTime() {
Craig Mautner494f6bdd2014-07-21 11:17:46 -0700355 lastActiveTime = System.currentTimeMillis();
Winson Chungffa2ec62014-07-03 15:54:42 -0700356 if (firstActiveTime == 0) {
357 firstActiveTime = lastActiveTime;
358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 long getInactiveDuration() {
Craig Mautner494f6bdd2014-07-21 11:17:46 -0700362 return System.currentTimeMillis() - lastActiveTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700364
Winson Chungfee26772014-08-05 12:21:52 -0700365 /** Sets the original intent, and the calling uid and package. */
366 void setIntent(ActivityRecord r) {
Winson Chungfee26772014-08-05 12:21:52 -0700367 mCallingUid = r.launchedFromUid;
368 mCallingPackage = r.launchedFromPackage;
Craig Mautner15df08a2015-04-01 12:17:18 -0700369 setIntent(r.intent, r.info);
Winson Chungfee26772014-08-05 12:21:52 -0700370 }
371
372 /** Sets the original intent, _without_ updating the calling uid or package. */
373 private void setIntent(Intent _intent, ActivityInfo info) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700374 if (intent == null) {
375 mNeverRelinquishIdentity =
376 (info.flags & ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY) == 0;
377 } else if (mNeverRelinquishIdentity) {
378 return;
379 }
380
381 affinity = info.taskAffinity;
Dianne Hackborn79228822014-09-16 11:11:23 -0700382 if (intent == null) {
383 // If this task already has an intent associated with it, don't set the root
384 // affinity -- we don't want it changing after initially set, but the initially
385 // set value may be null.
386 rootAffinity = affinity;
387 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700388 effectiveUid = info.applicationInfo.uid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700389 stringName = null;
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 if (info.targetActivity == null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800392 if (_intent != null) {
393 // If this Intent has a selector, we want to clear it for the
394 // recent task since it is not relevant if the user later wants
395 // to re-launch the app.
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700396 if (_intent.getSelector() != null || _intent.getSourceBounds() != null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800397 _intent = new Intent(_intent);
398 _intent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700399 _intent.setSourceBounds(null);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800400 }
401 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700402 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Setting Intent of " + this + " to " + _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 intent = _intent;
404 realActivity = _intent != null ? _intent.getComponent() : null;
405 origActivity = null;
406 } else {
407 ComponentName targetComponent = new ComponentName(
408 info.packageName, info.targetActivity);
409 if (_intent != null) {
410 Intent targetIntent = new Intent(_intent);
411 targetIntent.setComponent(targetComponent);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800412 targetIntent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700413 targetIntent.setSourceBounds(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700414 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700415 "Setting Intent of " + this + " to target " + targetIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 intent = targetIntent;
417 realActivity = targetComponent;
418 origActivity = _intent.getComponent();
419 } else {
420 intent = null;
421 realActivity = targetComponent;
422 origActivity = new ComponentName(info.packageName, info.name);
423 }
424 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700425
Craig Mautner47b20ba2014-09-17 17:23:44 -0700426 final int intentFlags = intent == null ? 0 : intent.getFlags();
427 if ((intentFlags & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 // Once we are set to an Intent with this flag, we count this
429 // task as having a true root activity.
430 rootWasReset = true;
431 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700432
Dianne Hackborn09233282014-04-30 11:33:59 -0700433 userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner41db4a72014-05-07 17:20:56 -0700434 if ((info.flags & ActivityInfo.FLAG_AUTO_REMOVE_FROM_RECENTS) != 0) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700435 // If the activity itself has requested auto-remove, then just always do it.
436 autoRemoveRecents = true;
Wale Ogunwale843bfb92015-03-27 11:06:48 -0700437 } else if ((intentFlags & (FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_RETAIN_IN_RECENTS))
438 == FLAG_ACTIVITY_NEW_DOCUMENT) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700439 // If the caller has not asked for the document to be retained, then we may
440 // want to turn on auto-remove, depending on whether the target has set its
441 // own document launch mode.
442 if (info.documentLaunchMode != ActivityInfo.DOCUMENT_LAUNCH_NONE) {
443 autoRemoveRecents = false;
444 } else {
445 autoRemoveRecents = true;
446 }
447 } else {
448 autoRemoveRecents = false;
Craig Mautner41db4a72014-05-07 17:20:56 -0700449 }
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800450 mResizeable = (info.flags & FLAG_RESIZEABLE) != 0 || mService.mForceResizableActivities;
Craig Mautner15df08a2015-04-01 12:17:18 -0700451 mLockTaskMode = info.lockTaskLaunchMode;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700452 mPrivileged = (info.applicationInfo.privateFlags & PRIVATE_FLAG_PRIVILEGED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -0700453 setLockTaskAuth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800455
Craig Mautner84984fa2014-06-19 11:19:20 -0700456 void setTaskToReturnTo(int taskToReturnTo) {
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700457 mTaskToReturnTo = (IGNORE_RETURN_TO_RECENTS && taskToReturnTo == RECENTS_ACTIVITY_TYPE)
458 ? HOME_ACTIVITY_TYPE : taskToReturnTo;
Craig Mautner84984fa2014-06-19 11:19:20 -0700459 }
460
461 int getTaskToReturnTo() {
462 return mTaskToReturnTo;
463 }
464
Craig Mautnera228ae92014-07-09 05:44:55 -0700465 void setPrevAffiliate(TaskRecord prevAffiliate) {
466 mPrevAffiliate = prevAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800467 mPrevAffiliateTaskId = prevAffiliate == null ? INVALID_TASK_ID : prevAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700468 }
469
470 void setNextAffiliate(TaskRecord nextAffiliate) {
471 mNextAffiliate = nextAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800472 mNextAffiliateTaskId = nextAffiliate == null ? INVALID_TASK_ID : nextAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700473 }
474
475 // Close up recents linked list.
476 void closeRecentsChain() {
477 if (mPrevAffiliate != null) {
478 mPrevAffiliate.setNextAffiliate(mNextAffiliate);
479 }
480 if (mNextAffiliate != null) {
481 mNextAffiliate.setPrevAffiliate(mPrevAffiliate);
482 }
483 setPrevAffiliate(null);
484 setNextAffiliate(null);
485 }
486
Winson Chung740c3ac2014-11-12 16:14:38 -0800487 void removedFromRecents() {
Dianne Hackborn852975d2014-08-22 17:42:43 -0700488 disposeThumbnail();
489 closeRecentsChain();
490 if (inRecents) {
491 inRecents = false;
Winson Chung740c3ac2014-11-12 16:14:38 -0800492 mService.notifyTaskPersisterLocked(this, false);
Dianne Hackborn852975d2014-08-22 17:42:43 -0700493 }
494 }
495
Craig Mautnera228ae92014-07-09 05:44:55 -0700496 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
497 closeRecentsChain();
498 mAffiliatedTaskId = taskToAffiliateWith.mAffiliatedTaskId;
Winson Chungec396d62014-08-06 17:08:00 -0700499 mAffiliatedTaskColor = taskToAffiliateWith.mAffiliatedTaskColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700500 // Find the end
501 while (taskToAffiliateWith.mNextAffiliate != null) {
502 final TaskRecord nextRecents = taskToAffiliateWith.mNextAffiliate;
503 if (nextRecents.mAffiliatedTaskId != mAffiliatedTaskId) {
504 Slog.e(TAG, "setTaskToAffiliateWith: nextRecents=" + nextRecents + " affilTaskId="
505 + nextRecents.mAffiliatedTaskId + " should be " + mAffiliatedTaskId);
506 if (nextRecents.mPrevAffiliate == taskToAffiliateWith) {
507 nextRecents.setPrevAffiliate(null);
508 }
509 taskToAffiliateWith.setNextAffiliate(null);
510 break;
511 }
512 taskToAffiliateWith = nextRecents;
513 }
514 taskToAffiliateWith.setNextAffiliate(this);
515 setPrevAffiliate(taskToAffiliateWith);
516 setNextAffiliate(null);
517 }
518
Winson Chung096f36b2014-08-20 15:39:01 -0700519 /**
Winsonc809cbb2015-11-02 12:06:15 -0800520 * Sets the last thumbnail with the current task bounds and the system orientation.
Winson Chung096f36b2014-08-20 15:39:01 -0700521 * @return whether the thumbnail was set
522 */
Winsonc809cbb2015-11-02 12:06:15 -0800523 boolean setLastThumbnailLocked(Bitmap thumbnail) {
524 final Configuration serviceConfig = mService.mConfiguration;
525 int taskWidth = 0;
526 int taskHeight = 0;
527 if (mBounds != null) {
528 // Non-fullscreen tasks
529 taskWidth = mBounds.width();
530 taskHeight = mBounds.height();
531 } else if (stack != null) {
532 // Fullscreen tasks
533 final Point displaySize = new Point();
534 stack.getDisplaySize(displaySize);
535 taskWidth = displaySize.x;
536 taskHeight = displaySize.y;
537 } else {
538 Slog.e(TAG, "setLastThumbnailLocked() called on Task without stack");
539 }
540 return setLastThumbnailLocked(thumbnail, taskWidth, taskHeight, serviceConfig.orientation);
541 }
542
543 /**
544 * Sets the last thumbnail with the current task bounds.
545 * @return whether the thumbnail was set
546 */
547 private boolean setLastThumbnailLocked(Bitmap thumbnail, int taskWidth, int taskHeight,
548 int screenOrientation) {
Winson Chung096f36b2014-08-20 15:39:01 -0700549 if (mLastThumbnail != thumbnail) {
550 mLastThumbnail = thumbnail;
Winsonc809cbb2015-11-02 12:06:15 -0800551 mLastThumbnailInfo.taskWidth = taskWidth;
552 mLastThumbnailInfo.taskHeight = taskHeight;
553 mLastThumbnailInfo.screenOrientation = screenOrientation;
Winson Chung096f36b2014-08-20 15:39:01 -0700554 if (thumbnail == null) {
555 if (mLastThumbnailFile != null) {
556 mLastThumbnailFile.delete();
557 }
558 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800559 mService.mRecentTasks.saveImage(thumbnail, mLastThumbnailFile.getAbsolutePath());
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700560 }
Winson Chung096f36b2014-08-20 15:39:01 -0700561 return true;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700562 }
Winson Chung096f36b2014-08-20 15:39:01 -0700563 return false;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700564 }
565
566 void getLastThumbnail(TaskThumbnail thumbs) {
567 thumbs.mainThumbnail = mLastThumbnail;
Winsonc809cbb2015-11-02 12:06:15 -0800568 thumbs.thumbnailInfo = mLastThumbnailInfo;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700569 thumbs.thumbnailFileDescriptor = null;
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700570 if (mLastThumbnail == null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800571 thumbs.mainThumbnail = mService.mRecentTasks.getImageFromWriteQueue(
Suprabh Shukla23593142015-11-03 17:31:15 -0800572 mLastThumbnailFile.getAbsolutePath());
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700573 }
Winson Chung096f36b2014-08-20 15:39:01 -0700574 // Only load the thumbnail file if we don't have a thumbnail
575 if (thumbs.mainThumbnail == null && mLastThumbnailFile.exists()) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700576 try {
577 thumbs.thumbnailFileDescriptor = ParcelFileDescriptor.open(mLastThumbnailFile,
578 ParcelFileDescriptor.MODE_READ_ONLY);
579 } catch (IOException e) {
Dianne Hackborn9844d292013-10-04 16:44:22 -0700580 }
581 }
582 }
583
Winsonc809cbb2015-11-02 12:06:15 -0800584 /**
585 * Removes in-memory thumbnail data when the max number of in-memory task thumbnails is reached.
586 */
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700587 void freeLastThumbnail() {
588 mLastThumbnail = null;
589 }
590
Winsonc809cbb2015-11-02 12:06:15 -0800591 /**
592 * Removes all associated thumbnail data when a task is removed or pruned from recents.
593 */
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700594 void disposeThumbnail() {
Winsonc6a2da02015-11-11 18:11:59 -0800595 mLastThumbnailInfo.reset();
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700596 mLastThumbnail = null;
597 lastDescription = null;
598 }
599
Winson Chung1147c402014-05-14 11:05:00 -0700600 /** Returns the intent for the root activity for this task */
601 Intent getBaseIntent() {
602 return intent != null ? intent : affinityIntent;
603 }
604
Winson Chung3b3f4642014-04-22 10:08:18 -0700605 /** Returns the first non-finishing activity from the root. */
606 ActivityRecord getRootActivity() {
607 for (int i = 0; i < mActivities.size(); i++) {
608 final ActivityRecord r = mActivities.get(i);
609 if (r.finishing) {
610 continue;
611 }
612 return r;
613 }
614 return null;
615 }
616
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800617 ActivityRecord getTopActivity() {
618 for (int i = mActivities.size() - 1; i >= 0; --i) {
619 final ActivityRecord r = mActivities.get(i);
620 if (r.finishing) {
621 continue;
622 }
623 return r;
624 }
625 return null;
626 }
627
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700628 ActivityRecord topRunningActivityLocked() {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700629 if (stack != null) {
630 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
631 ActivityRecord r = mActivities.get(activityNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700632 if (!r.finishing && stack.okToShowLocked(r)) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700633 return r;
634 }
Craig Mautner6b74cb52013-09-27 17:02:21 -0700635 }
636 }
637 return null;
638 }
639
Wale Ogunwale6d42df62015-11-05 09:50:55 -0800640 void setFrontOfTask() {
641 setFrontOfTask(null);
642 }
643
Craig Mautner3b475fe2013-12-16 15:58:31 -0800644 /** Call after activity movement or finish to make sure that frontOfTask is set correctly */
Wale Ogunwale6d42df62015-11-05 09:50:55 -0800645 void setFrontOfTask(ActivityRecord newTop) {
646 // If a top candidate is suggested by the caller, go ahead and use it and mark all others
647 // as not front. This is needed in situations where the current front activity in the
648 // task isn't finished yet and we want to set the front to the activity moved to the front
649 // of the task.
650 boolean foundFront = newTop != null ? true : false;
651
Craig Mautner3b475fe2013-12-16 15:58:31 -0800652 final int numActivities = mActivities.size();
Craig Mautner704e40b2013-12-18 16:43:51 -0800653 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
Craig Mautner3b475fe2013-12-16 15:58:31 -0800654 final ActivityRecord r = mActivities.get(activityNdx);
655 if (foundFront || r.finishing) {
656 r.frontOfTask = false;
657 } else {
658 r.frontOfTask = true;
659 // Set frontOfTask false for every following activity.
660 foundFront = true;
661 }
662 }
Craig Mautner9587ee02014-06-23 15:00:10 +0000663 if (!foundFront && numActivities > 0) {
664 // All activities of this task are finishing. As we ought to have a frontOfTask
665 // activity, make the bottom activity front.
666 mActivities.get(0).frontOfTask = true;
667 }
Wale Ogunwale6d42df62015-11-05 09:50:55 -0800668 if (newTop != null) {
669 newTop.frontOfTask = true;
670 }
Craig Mautner3b475fe2013-12-16 15:58:31 -0800671 }
672
Craig Mautnerde4ef022013-04-07 19:01:33 -0700673 /**
Craig Mautner3b475fe2013-12-16 15:58:31 -0800674 * Reorder the history stack so that the passed activity is brought to the front.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700675 */
676 final void moveActivityToFrontLocked(ActivityRecord newTop) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700677 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
678 "Removing and adding activity " + newTop
679 + " to stack at top callers=" + Debug.getCallers(4));
Craig Mautnerde4ef022013-04-07 19:01:33 -0700680
Craig Mautnerde4ef022013-04-07 19:01:33 -0700681 mActivities.remove(newTop);
682 mActivities.add(newTop);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700683 updateEffectiveIntent();
Craig Mautner3b475fe2013-12-16 15:58:31 -0800684
Wale Ogunwale6d42df62015-11-05 09:50:55 -0800685 setFrontOfTask(newTop);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700686 }
687
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800688 void addActivityAtBottom(ActivityRecord r) {
Craig Mautner77878772013-03-04 19:46:24 -0800689 addActivityAtIndex(0, r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800690 }
691
692 void addActivityToTop(ActivityRecord r) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700693 addActivityAtIndex(mActivities.size(), r);
694 }
695
696 void addActivityAtIndex(int index, ActivityRecord r) {
Craig Mautner6170f732013-04-02 13:05:23 -0700697 // 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 -0800698 if (!mActivities.remove(r) && r.fullscreen) {
699 // Was not previously in list.
700 numFullscreen++;
701 }
Craig Mautner2c1faed2013-07-23 12:56:02 -0700702 // Only set this based on the first activity
703 if (mActivities.isEmpty()) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700704 taskType = r.mActivityType;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -0800705 if (taskType == HOME_ACTIVITY_TYPE && mService.mForceResizableActivities) {
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800706 mResizeable = r.isResizeable();
Jorim Jaggi8ccdf3a2015-10-22 12:50:31 +0200707 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700708 isPersistable = r.isPersistable();
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700709 mCallingUid = r.launchedFromUid;
710 mCallingPackage = r.launchedFromPackage;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700711 // Clamp to [1, max].
712 maxRecents = Math.min(Math.max(r.info.maxRecents, 1),
713 ActivityManager.getMaxAppRecentsLimitStatic());
Craig Mautner2c1faed2013-07-23 12:56:02 -0700714 } else {
715 // Otherwise make all added activities match this one.
Craig Mautner21d24a22014-04-23 11:45:37 -0700716 r.mActivityType = taskType;
Craig Mautner78733002013-06-10 13:54:49 -0700717 }
Craig Mautner77878772013-03-04 19:46:24 -0800718 mActivities.add(index, r);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700719 updateEffectiveIntent();
Craig Mautner21d24a22014-04-23 11:45:37 -0700720 if (r.isPersistable()) {
721 mService.notifyTaskPersisterLocked(this, false);
722 }
Craig Mautner77878772013-03-04 19:46:24 -0800723 }
724
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800725 /** @return true if this was the last activity in the task */
726 boolean removeActivity(ActivityRecord r) {
727 if (mActivities.remove(r) && r.fullscreen) {
728 // Was previously in list.
729 numFullscreen--;
730 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700731 if (r.isPersistable()) {
732 mService.notifyTaskPersisterLocked(this, false);
733 }
Craig Mautner41326202014-06-20 14:38:21 -0700734 if (mActivities.isEmpty()) {
Craig Mautner5afcd4d2014-06-21 18:11:33 -0700735 return !mReuseTask;
Craig Mautner41326202014-06-20 14:38:21 -0700736 }
737 updateEffectiveIntent();
738 return false;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800739 }
740
Craig Mautner41db4a72014-05-07 17:20:56 -0700741 boolean autoRemoveFromRecents() {
Dianne Hackbornd38aed82014-06-10 21:36:35 -0700742 // We will automatically remove the task either if it has explicitly asked for
743 // this, or it is empty and has never contained an activity that got shown to
744 // the user.
Dianne Hackborn13420f22014-07-18 15:43:56 -0700745 return autoRemoveRecents || (mActivities.isEmpty() && !hasBeenVisible);
Craig Mautner41db4a72014-05-07 17:20:56 -0700746 }
747
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700748 /**
749 * Completely remove all activities associated with an existing
750 * task starting at a specified index.
751 */
752 final void performClearTaskAtIndexLocked(int activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700753 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700754 for ( ; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700755 final ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700756 if (r.finishing) {
757 continue;
758 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700759 if (stack == null) {
760 // Task was restored from persistent storage.
761 r.takeFromHistory();
762 mActivities.remove(activityNdx);
763 --activityNdx;
764 --numActivities;
Todd Kennedy539db512014-12-15 09:57:55 -0800765 } else if (stack.finishActivityLocked(
766 r, Activity.RESULT_CANCELED, null, "clear-task-index", false)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700767 --activityNdx;
768 --numActivities;
769 }
770 }
771 }
772
773 /**
774 * Completely remove all activities associated with an existing task.
775 */
776 final void performClearTaskLocked() {
Craig Mautner362449a2014-06-20 14:04:39 -0700777 mReuseTask = true;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700778 performClearTaskAtIndexLocked(0);
Craig Mautner362449a2014-06-20 14:04:39 -0700779 mReuseTask = false;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700780 }
781
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -0800782 ActivityRecord performClearTaskForReuseLocked(ActivityRecord newR, int launchFlags) {
783 mReuseTask = true;
784 final ActivityRecord result = performClearTaskLocked(newR, launchFlags);
785 mReuseTask = false;
786 return result;
787 }
788
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700789 /**
790 * Perform clear operation as requested by
791 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the
792 * stack to the given task, then look for
793 * an instance of that activity in the stack and, if found, finish all
794 * activities on top of it and return the instance.
795 *
796 * @param newR Description of the new activity being started.
797 * @return Returns the old activity that should be continued to be used,
798 * or null if none was found.
799 */
800 final ActivityRecord performClearTaskLocked(ActivityRecord newR, int launchFlags) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700801 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700802 for (int activityNdx = numActivities - 1; activityNdx >= 0; --activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700803 ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700804 if (r.finishing) {
805 continue;
806 }
807 if (r.realActivity.equals(newR.realActivity)) {
808 // Here it is! Now finish everything in front...
Craig Mautner1602ec22013-05-12 10:24:27 -0700809 final ActivityRecord ret = r;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700810
811 for (++activityNdx; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700812 r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700813 if (r.finishing) {
814 continue;
815 }
816 ActivityOptions opts = r.takeOptionsLocked();
817 if (opts != null) {
818 ret.updateOptionsLocked(opts);
819 }
Wale Ogunwale7d701172015-03-11 15:36:30 -0700820 if (stack != null && stack.finishActivityLocked(
Todd Kennedy539db512014-12-15 09:57:55 -0800821 r, Activity.RESULT_CANCELED, null, "clear-task-stack", false)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700822 --activityNdx;
823 --numActivities;
824 }
825 }
826
827 // Finally, if this is a normal launch mode (that is, not
828 // expecting onNewIntent()), then we will finish the current
829 // instance of the activity so a new fresh one can be started.
830 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
831 && (launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0) {
832 if (!ret.finishing) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700833 if (stack != null) {
834 stack.finishActivityLocked(
835 ret, Activity.RESULT_CANCELED, null, "clear-task-top", false);
836 }
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700837 return null;
838 }
839 }
840
841 return ret;
842 }
843 }
844
845 return null;
846 }
847
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700848 public TaskThumbnail getTaskThumbnailLocked() {
Craig Mautner21d24a22014-04-23 11:45:37 -0700849 if (stack != null) {
850 final ActivityRecord resumedActivity = stack.mResumedActivity;
851 if (resumedActivity != null && resumedActivity.task == this) {
Winson8b1871d2015-11-20 09:56:20 -0800852 final Bitmap thumbnail = stack.screenshotActivitiesLocked(resumedActivity);
Winsonc809cbb2015-11-02 12:06:15 -0800853 setLastThumbnailLocked(thumbnail);
Craig Mautner21d24a22014-04-23 11:45:37 -0700854 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700855 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700856 final TaskThumbnail taskThumbnail = new TaskThumbnail();
857 getLastThumbnail(taskThumbnail);
858 return taskThumbnail;
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700859 }
860
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700861 public void removeTaskActivitiesLocked() {
862 // Just remove the entire task.
863 performClearTaskAtIndexLocked(0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700864 }
865
Craig Mautner432f64e2015-05-20 14:59:57 -0700866 String lockTaskAuthToString() {
867 switch (mLockTaskAuth) {
868 case LOCK_TASK_AUTH_DONT_LOCK: return "LOCK_TASK_AUTH_DONT_LOCK";
869 case LOCK_TASK_AUTH_PINNABLE: return "LOCK_TASK_AUTH_PINNABLE";
870 case LOCK_TASK_AUTH_LAUNCHABLE: return "LOCK_TASK_AUTH_LAUNCHABLE";
871 case LOCK_TASK_AUTH_WHITELISTED: return "LOCK_TASK_AUTH_WHITELISTED";
Benjamin Franz469dd582015-06-09 14:24:36 +0100872 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV: return "LOCK_TASK_AUTH_LAUNCHABLE_PRIV";
Craig Mautner432f64e2015-05-20 14:59:57 -0700873 default: return "unknown=" + mLockTaskAuth;
874 }
875 }
876
Craig Mautner15df08a2015-04-01 12:17:18 -0700877 void setLockTaskAuth() {
Benjamin Franz469dd582015-06-09 14:24:36 +0100878 if (!mPrivileged &&
879 (mLockTaskMode == LOCK_TASK_LAUNCH_MODE_ALWAYS ||
880 mLockTaskMode == LOCK_TASK_LAUNCH_MODE_NEVER)) {
881 // Non-priv apps are not allowed to use always or never, fall back to default
882 mLockTaskMode = LOCK_TASK_LAUNCH_MODE_DEFAULT;
883 }
Craig Mautner15df08a2015-04-01 12:17:18 -0700884 switch (mLockTaskMode) {
885 case LOCK_TASK_LAUNCH_MODE_DEFAULT:
886 mLockTaskAuth = isLockTaskWhitelistedLocked() ?
887 LOCK_TASK_AUTH_WHITELISTED : LOCK_TASK_AUTH_PINNABLE;
888 break;
889
890 case LOCK_TASK_LAUNCH_MODE_NEVER:
Benjamin Franz469dd582015-06-09 14:24:36 +0100891 mLockTaskAuth = LOCK_TASK_AUTH_DONT_LOCK;
Craig Mautner15df08a2015-04-01 12:17:18 -0700892 break;
893
894 case LOCK_TASK_LAUNCH_MODE_ALWAYS:
Benjamin Franz469dd582015-06-09 14:24:36 +0100895 mLockTaskAuth = LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -0700896 break;
897
898 case LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED:
899 mLockTaskAuth = isLockTaskWhitelistedLocked() ?
900 LOCK_TASK_AUTH_LAUNCHABLE : LOCK_TASK_AUTH_PINNABLE;
901 break;
902 }
Craig Mautner432f64e2015-05-20 14:59:57 -0700903 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "setLockTaskAuth: task=" + this +
904 " mLockTaskAuth=" + lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -0700905 }
906
907 boolean isLockTaskWhitelistedLocked() {
Benjamin Franz6fd84cc2015-08-06 18:09:03 +0100908 String pkg = (realActivity != null) ? realActivity.getPackageName() : null;
909 if (pkg == null) {
Craig Mautner15df08a2015-04-01 12:17:18 -0700910 return false;
911 }
912 String[] packages = mService.mLockTaskPackages.get(userId);
913 if (packages == null) {
914 return false;
915 }
916 for (int i = packages.length - 1; i >= 0; --i) {
Benjamin Franz6fd84cc2015-08-06 18:09:03 +0100917 if (pkg.equals(packages[i])) {
Craig Mautner15df08a2015-04-01 12:17:18 -0700918 return true;
919 }
920 }
921 return false;
922 }
Craig Mautnera82aa092013-09-13 15:34:08 -0700923 boolean isHomeTask() {
Craig Mautner84984fa2014-06-19 11:19:20 -0700924 return taskType == HOME_ACTIVITY_TYPE;
Craig Mautnera82aa092013-09-13 15:34:08 -0700925 }
926
Craig Mautner86d67a42013-05-14 10:34:38 -0700927 boolean isApplicationTask() {
Craig Mautner84984fa2014-06-19 11:19:20 -0700928 return taskType == APPLICATION_ACTIVITY_TYPE;
929 }
930
931 boolean isOverHomeStack() {
932 return mTaskToReturnTo == HOME_ACTIVITY_TYPE || mTaskToReturnTo == RECENTS_ACTIVITY_TYPE;
Craig Mautner1602ec22013-05-12 10:24:27 -0700933 }
934
Craig Mautner525f3d92013-05-07 14:01:50 -0700935 /**
936 * Find the activity in the history stack within the given task. Returns
937 * the index within the history at which it's found, or < 0 if not found.
938 */
939 final ActivityRecord findActivityInHistoryLocked(ActivityRecord r) {
940 final ComponentName realActivity = r.realActivity;
941 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
942 ActivityRecord candidate = mActivities.get(activityNdx);
943 if (candidate.finishing) {
944 continue;
945 }
946 if (candidate.realActivity.equals(realActivity)) {
947 return candidate;
948 }
949 }
950 return null;
951 }
952
Winson Chunga449dc02014-05-16 11:15:04 -0700953 /** Updates the last task description values. */
954 void updateTaskDescription() {
955 // Traverse upwards looking for any break between main task activities and
956 // utility activities.
957 int activityNdx;
958 final int numActivities = mActivities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700959 final boolean relinquish = numActivities == 0 ? false :
960 (mActivities.get(0).info.flags & ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY) != 0;
Winson Chunga449dc02014-05-16 11:15:04 -0700961 for (activityNdx = Math.min(numActivities, 1); activityNdx < numActivities;
Craig Mautner21d24a22014-04-23 11:45:37 -0700962 ++activityNdx) {
Winson Chunga449dc02014-05-16 11:15:04 -0700963 final ActivityRecord r = mActivities.get(activityNdx);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700964 if (relinquish && (r.info.flags & ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
965 // This will be the top activity for determining taskDescription. Pre-inc to
966 // overcome initial decrement below.
967 ++activityNdx;
968 break;
969 }
Winson Chunga449dc02014-05-16 11:15:04 -0700970 if (r.intent != null &&
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700971 (r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Winson Chunga449dc02014-05-16 11:15:04 -0700972 break;
973 }
974 }
975 if (activityNdx > 0) {
976 // Traverse downwards starting below break looking for set label, icon.
977 // Note that if there are activities in the task but none of them set the
978 // recent activity values, then we do not fall back to the last set
979 // values in the TaskRecord.
980 String label = null;
Craig Mautner648f69b2014-09-18 14:16:26 -0700981 String iconFilename = null;
Winson Chunga449dc02014-05-16 11:15:04 -0700982 int colorPrimary = 0;
983 for (--activityNdx; activityNdx >= 0; --activityNdx) {
984 final ActivityRecord r = mActivities.get(activityNdx);
985 if (r.taskDescription != null) {
986 if (label == null) {
987 label = r.taskDescription.getLabel();
988 }
Craig Mautner648f69b2014-09-18 14:16:26 -0700989 if (iconFilename == null) {
990 iconFilename = r.taskDescription.getIconFilename();
Winson Chunga449dc02014-05-16 11:15:04 -0700991 }
992 if (colorPrimary == 0) {
993 colorPrimary = r.taskDescription.getPrimaryColor();
Winson Chunga449dc02014-05-16 11:15:04 -0700994 }
995 }
996 }
Craig Mautner648f69b2014-09-18 14:16:26 -0700997 lastTaskDescription = new TaskDescription(label, colorPrimary, iconFilename);
Winson Chungec396d62014-08-06 17:08:00 -0700998 // Update the task affiliation color if we are the parent of the group
999 if (taskId == mAffiliatedTaskId) {
1000 mAffiliatedTaskColor = lastTaskDescription.getPrimaryColor();
1001 }
Winson Chunga449dc02014-05-16 11:15:04 -07001002 }
1003 }
1004
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001005 int findEffectiveRootIndex() {
Craig Mautner4767f4b2014-09-18 15:38:33 -07001006 int effectiveNdx = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001007 final int topActivityNdx = mActivities.size() - 1;
Dianne Hackborn39569af2014-09-23 10:56:58 -07001008 for (int activityNdx = 0; activityNdx <= topActivityNdx; ++activityNdx) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001009 final ActivityRecord r = mActivities.get(activityNdx);
1010 if (r.finishing) {
1011 continue;
1012 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001013 effectiveNdx = activityNdx;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001014 if ((r.info.flags & ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
1015 break;
1016 }
1017 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001018 return effectiveNdx;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001019 }
1020
1021 void updateEffectiveIntent() {
1022 final int effectiveRootIndex = findEffectiveRootIndex();
1023 final ActivityRecord r = mActivities.get(effectiveRootIndex);
Winson Chungfee26772014-08-05 12:21:52 -07001024 setIntent(r);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001025 }
1026
Craig Mautner21d24a22014-04-23 11:45:37 -07001027 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001028 if (DEBUG_RECENTS) Slog.i(TAG_RECENTS, "Saving task=" + this);
Craig Mautner21d24a22014-04-23 11:45:37 -07001029
1030 out.attribute(null, ATTR_TASKID, String.valueOf(taskId));
1031 if (realActivity != null) {
1032 out.attribute(null, ATTR_REALACTIVITY, realActivity.flattenToShortString());
1033 }
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001034 out.attribute(null, ATTR_REALACTIVITY_SUSPENDED, String.valueOf(realActivitySuspended));
Craig Mautner21d24a22014-04-23 11:45:37 -07001035 if (origActivity != null) {
1036 out.attribute(null, ATTR_ORIGACTIVITY, origActivity.flattenToShortString());
1037 }
Dianne Hackborn79228822014-09-16 11:11:23 -07001038 // Write affinity, and root affinity if it is different from affinity.
1039 // We use the special string "@" for a null root affinity, so we can identify
1040 // later whether we were given a root affinity or should just make it the
1041 // same as the affinity.
Craig Mautner21d24a22014-04-23 11:45:37 -07001042 if (affinity != null) {
1043 out.attribute(null, ATTR_AFFINITY, affinity);
Dianne Hackborn79228822014-09-16 11:11:23 -07001044 if (!affinity.equals(rootAffinity)) {
1045 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
1046 }
1047 } else if (rootAffinity != null) {
1048 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
Craig Mautner21d24a22014-04-23 11:45:37 -07001049 }
1050 out.attribute(null, ATTR_ROOTHASRESET, String.valueOf(rootWasReset));
Dianne Hackborn13420f22014-07-18 15:43:56 -07001051 out.attribute(null, ATTR_AUTOREMOVERECENTS, String.valueOf(autoRemoveRecents));
Craig Mautner21d24a22014-04-23 11:45:37 -07001052 out.attribute(null, ATTR_ASKEDCOMPATMODE, String.valueOf(askedCompatMode));
1053 out.attribute(null, ATTR_USERID, String.valueOf(userId));
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001054 out.attribute(null, ATTR_EFFECTIVE_UID, String.valueOf(effectiveUid));
Craig Mautner21d24a22014-04-23 11:45:37 -07001055 out.attribute(null, ATTR_TASKTYPE, String.valueOf(taskType));
Winson Chungffa2ec62014-07-03 15:54:42 -07001056 out.attribute(null, ATTR_FIRSTACTIVETIME, String.valueOf(firstActiveTime));
Winson Chungf1fbd772014-06-24 18:06:58 -07001057 out.attribute(null, ATTR_LASTACTIVETIME, String.valueOf(lastActiveTime));
Craig Mautner21d24a22014-04-23 11:45:37 -07001058 out.attribute(null, ATTR_LASTTIMEMOVED, String.valueOf(mLastTimeMoved));
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001059 out.attribute(null, ATTR_NEVERRELINQUISH, String.valueOf(mNeverRelinquishIdentity));
Craig Mautner21d24a22014-04-23 11:45:37 -07001060 if (lastDescription != null) {
1061 out.attribute(null, ATTR_LASTDESCRIPTION, lastDescription.toString());
1062 }
Winson Chung2cb86c72014-06-25 12:03:30 -07001063 if (lastTaskDescription != null) {
Craig Mautner648f69b2014-09-18 14:16:26 -07001064 lastTaskDescription.saveToXml(out);
Winson Chung2cb86c72014-06-25 12:03:30 -07001065 }
Winsonc809cbb2015-11-02 12:06:15 -08001066 mLastThumbnailInfo.saveToXml(out);
Winson Chungec396d62014-08-06 17:08:00 -07001067 out.attribute(null, ATTR_TASK_AFFILIATION_COLOR, String.valueOf(mAffiliatedTaskColor));
Craig Mautnera228ae92014-07-09 05:44:55 -07001068 out.attribute(null, ATTR_TASK_AFFILIATION, String.valueOf(mAffiliatedTaskId));
1069 out.attribute(null, ATTR_PREV_AFFILIATION, String.valueOf(mPrevAffiliateTaskId));
1070 out.attribute(null, ATTR_NEXT_AFFILIATION, String.valueOf(mNextAffiliateTaskId));
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001071 out.attribute(null, ATTR_CALLING_UID, String.valueOf(mCallingUid));
1072 out.attribute(null, ATTR_CALLING_PACKAGE, mCallingPackage == null ? "" : mCallingPackage);
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001073 out.attribute(null, ATTR_RESIZEABLE, String.valueOf(mResizeable));
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001074 out.attribute(null, ATTR_PRIVILEGED, String.valueOf(mPrivileged));
Wale Ogunwale706ed792015-08-02 10:29:44 -07001075 if (mLastNonFullscreenBounds != null) {
1076 out.attribute(
1077 null, ATTR_NON_FULLSCREEN_BOUNDS, mLastNonFullscreenBounds.flattenToString());
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001078 }
Winson Chung2cb86c72014-06-25 12:03:30 -07001079
Craig Mautner21d24a22014-04-23 11:45:37 -07001080 if (affinityIntent != null) {
1081 out.startTag(null, TAG_AFFINITYINTENT);
1082 affinityIntent.saveToXml(out);
1083 out.endTag(null, TAG_AFFINITYINTENT);
1084 }
1085
1086 out.startTag(null, TAG_INTENT);
1087 intent.saveToXml(out);
1088 out.endTag(null, TAG_INTENT);
1089
1090 final ArrayList<ActivityRecord> activities = mActivities;
1091 final int numActivities = activities.size();
1092 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1093 final ActivityRecord r = activities.get(activityNdx);
Craig Mautner43e52ed2014-06-16 17:18:52 -07001094 if (r.info.persistableMode == ActivityInfo.PERSIST_ROOT_ONLY || !r.isPersistable() ||
Wale Ogunwale843bfb92015-03-27 11:06:48 -07001095 ((r.intent.getFlags() & FLAG_ACTIVITY_NEW_DOCUMENT
1096 | FLAG_ACTIVITY_RETAIN_IN_RECENTS) == FLAG_ACTIVITY_NEW_DOCUMENT) &&
Craig Mautner43e52ed2014-06-16 17:18:52 -07001097 activityNdx > 0) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001098 // Stop at first non-persistable or first break in task (CLEAR_WHEN_TASK_RESET).
Craig Mautner21d24a22014-04-23 11:45:37 -07001099 break;
1100 }
1101 out.startTag(null, TAG_ACTIVITY);
1102 r.saveToXml(out);
1103 out.endTag(null, TAG_ACTIVITY);
1104 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001105 }
1106
1107 static TaskRecord restoreFromXml(XmlPullParser in, ActivityStackSupervisor stackSupervisor)
1108 throws IOException, XmlPullParserException {
1109 Intent intent = null;
1110 Intent affinityIntent = null;
Winsonc809cbb2015-11-02 12:06:15 -08001111 ArrayList<ActivityRecord> activities = new ArrayList<>();
Craig Mautner21d24a22014-04-23 11:45:37 -07001112 ComponentName realActivity = null;
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001113 boolean realActivitySuspended = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001114 ComponentName origActivity = null;
1115 String affinity = null;
Dianne Hackborn79228822014-09-16 11:11:23 -07001116 String rootAffinity = null;
1117 boolean hasRootAffinity = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001118 boolean rootHasReset = false;
Dianne Hackborn13420f22014-07-18 15:43:56 -07001119 boolean autoRemoveRecents = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001120 boolean askedCompatMode = false;
1121 int taskType = ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner21d24a22014-04-23 11:45:37 -07001122 int userId = 0;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001123 int effectiveUid = -1;
Craig Mautner21d24a22014-04-23 11:45:37 -07001124 String lastDescription = null;
Winson Chungffa2ec62014-07-03 15:54:42 -07001125 long firstActiveTime = -1;
Winson Chung2cb86c72014-06-25 12:03:30 -07001126 long lastActiveTime = -1;
Craig Mautner21d24a22014-04-23 11:45:37 -07001127 long lastTimeOnTop = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001128 boolean neverRelinquishIdentity = true;
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001129 int taskId = INVALID_TASK_ID;
Craig Mautner21d24a22014-04-23 11:45:37 -07001130 final int outerDepth = in.getDepth();
Craig Mautner648f69b2014-09-18 14:16:26 -07001131 TaskDescription taskDescription = new TaskDescription();
Winsonc809cbb2015-11-02 12:06:15 -08001132 TaskThumbnailInfo thumbnailInfo = new TaskThumbnailInfo();
Wale Ogunwale18795a22014-12-03 11:38:33 -08001133 int taskAffiliation = INVALID_TASK_ID;
Winson Chungec396d62014-08-06 17:08:00 -07001134 int taskAffiliationColor = 0;
Wale Ogunwale18795a22014-12-03 11:38:33 -08001135 int prevTaskId = INVALID_TASK_ID;
1136 int nextTaskId = INVALID_TASK_ID;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001137 int callingUid = -1;
1138 String callingPackage = "";
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001139 boolean resizeable = false;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001140 boolean privileged = false;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001141 Rect bounds = null;
Craig Mautner21d24a22014-04-23 11:45:37 -07001142
1143 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
1144 final String attrName = in.getAttributeName(attrNdx);
1145 final String attrValue = in.getAttributeValue(attrNdx);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001146 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: attribute name=" +
1147 attrName + " value=" + attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001148 if (ATTR_TASKID.equals(attrName)) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001149 if (taskId == INVALID_TASK_ID) taskId = Integer.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001150 } else if (ATTR_REALACTIVITY.equals(attrName)) {
1151 realActivity = ComponentName.unflattenFromString(attrValue);
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001152 } else if (ATTR_REALACTIVITY_SUSPENDED.equals(attrName)) {
1153 realActivitySuspended = Boolean.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001154 } else if (ATTR_ORIGACTIVITY.equals(attrName)) {
1155 origActivity = ComponentName.unflattenFromString(attrValue);
1156 } else if (ATTR_AFFINITY.equals(attrName)) {
1157 affinity = attrValue;
Dianne Hackborn79228822014-09-16 11:11:23 -07001158 } else if (ATTR_ROOT_AFFINITY.equals(attrName)) {
1159 rootAffinity = attrValue;
1160 hasRootAffinity = true;
Craig Mautner21d24a22014-04-23 11:45:37 -07001161 } else if (ATTR_ROOTHASRESET.equals(attrName)) {
1162 rootHasReset = Boolean.valueOf(attrValue);
Dianne Hackborn13420f22014-07-18 15:43:56 -07001163 } else if (ATTR_AUTOREMOVERECENTS.equals(attrName)) {
1164 autoRemoveRecents = Boolean.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001165 } else if (ATTR_ASKEDCOMPATMODE.equals(attrName)) {
1166 askedCompatMode = Boolean.valueOf(attrValue);
1167 } else if (ATTR_USERID.equals(attrName)) {
1168 userId = Integer.valueOf(attrValue);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001169 } else if (ATTR_EFFECTIVE_UID.equals(attrName)) {
1170 effectiveUid = Integer.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001171 } else if (ATTR_TASKTYPE.equals(attrName)) {
1172 taskType = Integer.valueOf(attrValue);
Winson Chungffa2ec62014-07-03 15:54:42 -07001173 } else if (ATTR_FIRSTACTIVETIME.equals(attrName)) {
1174 firstActiveTime = Long.valueOf(attrValue);
Winson Chungf1fbd772014-06-24 18:06:58 -07001175 } else if (ATTR_LASTACTIVETIME.equals(attrName)) {
1176 lastActiveTime = Long.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001177 } else if (ATTR_LASTDESCRIPTION.equals(attrName)) {
1178 lastDescription = attrValue;
1179 } else if (ATTR_LASTTIMEMOVED.equals(attrName)) {
1180 lastTimeOnTop = Long.valueOf(attrValue);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001181 } else if (ATTR_NEVERRELINQUISH.equals(attrName)) {
1182 neverRelinquishIdentity = Boolean.valueOf(attrValue);
Winsonc809cbb2015-11-02 12:06:15 -08001183 } else if (attrName.startsWith(TaskThumbnailInfo.ATTR_TASK_THUMBNAILINFO_PREFIX)) {
1184 thumbnailInfo.restoreFromXml(attrName, attrValue);
Craig Mautner648f69b2014-09-18 14:16:26 -07001185 } else if (attrName.startsWith(TaskDescription.ATTR_TASKDESCRIPTION_PREFIX)) {
1186 taskDescription.restoreFromXml(attrName, attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001187 } else if (ATTR_TASK_AFFILIATION.equals(attrName)) {
1188 taskAffiliation = Integer.valueOf(attrValue);
1189 } else if (ATTR_PREV_AFFILIATION.equals(attrName)) {
1190 prevTaskId = Integer.valueOf(attrValue);
1191 } else if (ATTR_NEXT_AFFILIATION.equals(attrName)) {
1192 nextTaskId = Integer.valueOf(attrValue);
Winson Chungec396d62014-08-06 17:08:00 -07001193 } else if (ATTR_TASK_AFFILIATION_COLOR.equals(attrName)) {
1194 taskAffiliationColor = Integer.valueOf(attrValue);
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001195 } else if (ATTR_CALLING_UID.equals(attrName)) {
1196 callingUid = Integer.valueOf(attrValue);
1197 } else if (ATTR_CALLING_PACKAGE.equals(attrName)) {
1198 callingPackage = attrValue;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001199 } else if (ATTR_RESIZEABLE.equals(attrName)) {
1200 resizeable = Boolean.valueOf(attrValue);
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001201 } else if (ATTR_PRIVILEGED.equals(attrName)) {
1202 privileged = Boolean.valueOf(attrValue);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001203 } else if (ATTR_NON_FULLSCREEN_BOUNDS.equals(attrName)) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001204 bounds = Rect.unflattenFromString(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001205 } else {
1206 Slog.w(TAG, "TaskRecord: Unknown attribute=" + attrName);
1207 }
1208 }
1209
1210 int event;
1211 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
Ben Kwa8814cf42015-07-08 10:54:56 -07001212 (event != XmlPullParser.END_TAG || in.getDepth() >= outerDepth)) {
Craig Mautner21d24a22014-04-23 11:45:37 -07001213 if (event == XmlPullParser.START_TAG) {
1214 final String name = in.getName();
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001215 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: START_TAG name=" +
1216 name);
Craig Mautner21d24a22014-04-23 11:45:37 -07001217 if (TAG_AFFINITYINTENT.equals(name)) {
1218 affinityIntent = Intent.restoreFromXml(in);
1219 } else if (TAG_INTENT.equals(name)) {
1220 intent = Intent.restoreFromXml(in);
1221 } else if (TAG_ACTIVITY.equals(name)) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001222 ActivityRecord activity = ActivityRecord.restoreFromXml(in, stackSupervisor);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001223 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: activity=" +
1224 activity);
Craig Mautner21d24a22014-04-23 11:45:37 -07001225 if (activity != null) {
1226 activities.add(activity);
1227 }
1228 } else {
1229 Slog.e(TAG, "restoreTask: Unexpected name=" + name);
1230 XmlUtils.skipCurrentTag(in);
1231 }
1232 }
1233 }
Dianne Hackborn79228822014-09-16 11:11:23 -07001234 if (!hasRootAffinity) {
1235 rootAffinity = affinity;
1236 } else if ("@".equals(rootAffinity)) {
1237 rootAffinity = null;
1238 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001239 if (effectiveUid <= 0) {
1240 Intent checkIntent = intent != null ? intent : affinityIntent;
1241 effectiveUid = 0;
1242 if (checkIntent != null) {
1243 IPackageManager pm = AppGlobals.getPackageManager();
1244 try {
1245 ApplicationInfo ai = pm.getApplicationInfo(
1246 checkIntent.getComponent().getPackageName(),
1247 PackageManager.GET_UNINSTALLED_PACKAGES
1248 | PackageManager.GET_DISABLED_COMPONENTS, userId);
1249 if (ai != null) {
1250 effectiveUid = ai.uid;
1251 }
1252 } catch (RemoteException e) {
1253 }
1254 }
1255 Slog.w(TAG, "Updating task #" + taskId + " for " + checkIntent
1256 + ": effectiveUid=" + effectiveUid);
1257 }
1258
Craig Mautner21d24a22014-04-23 11:45:37 -07001259 final TaskRecord task = new TaskRecord(stackSupervisor.mService, taskId, intent,
Dianne Hackborn79228822014-09-16 11:11:23 -07001260 affinityIntent, affinity, rootAffinity, realActivity, origActivity, rootHasReset,
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001261 autoRemoveRecents, askedCompatMode, taskType, userId, effectiveUid, lastDescription,
1262 activities, firstActiveTime, lastActiveTime, lastTimeOnTop, neverRelinquishIdentity,
Winsonc809cbb2015-11-02 12:06:15 -08001263 taskDescription, thumbnailInfo, taskAffiliation, prevTaskId, nextTaskId,
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001264 taskAffiliationColor, callingUid, callingPackage, resizeable, privileged,
1265 realActivitySuspended);
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07001266 task.updateOverrideConfiguration(bounds);
Craig Mautner21d24a22014-04-23 11:45:37 -07001267
1268 for (int activityNdx = activities.size() - 1; activityNdx >=0; --activityNdx) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001269 activities.get(activityNdx).task = task;
Craig Mautner21d24a22014-04-23 11:45:37 -07001270 }
1271
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001272 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "Restored task=" + task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001273 return task;
1274 }
1275
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001276 /**
1277 * Update task's override configuration based on the bounds.
1278 * @return Update configuration or null if there is no change.
1279 */
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07001280 Configuration updateOverrideConfiguration(Rect bounds) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001281 if (Objects.equals(mBounds, bounds)) {
1282 return null;
1283 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001284 final Configuration oldConfig = mOverrideConfig;
1285 final boolean oldFullscreen = mFullscreen;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001286
1287 mFullscreen = bounds == null;
Wale Ogunwale706ed792015-08-02 10:29:44 -07001288 if (mFullscreen) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001289 if (mBounds != null && StackId.persistTaskBounds(stack.mStackId)) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001290 mLastNonFullscreenBounds = mBounds;
1291 }
1292 mBounds = null;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001293 mOverrideConfig = Configuration.EMPTY;
Wale Ogunwale706ed792015-08-02 10:29:44 -07001294 } else {
Chong Zhangb15758a2015-11-17 12:12:03 -08001295 if (mBounds == null) {
1296 mBounds = new Rect(bounds);
1297 } else {
1298 mBounds.set(bounds);
1299 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001300 if (stack == null || StackId.persistTaskBounds(stack.mStackId)) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001301 mLastNonFullscreenBounds = mBounds;
1302 }
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08001303 mOverrideConfig = calculateOverrideConfig(mBounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001304 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001305
1306 if (mFullscreen != oldFullscreen) {
1307 reportMultiWindowModeChange();
1308 }
1309
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001310 return !mOverrideConfig.equals(oldConfig) ? mOverrideConfig : null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001311 }
1312
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08001313 Configuration calculateOverrideConfig(Rect bounds) {
1314 final Configuration serviceConfig = mService.mConfiguration;
1315 final Configuration config = new Configuration(Configuration.EMPTY);
1316 // TODO(multidisplay): Update Dp to that of display stack is on.
1317 final float density = serviceConfig.densityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE;
1318 config.screenWidthDp =
1319 Math.min((int)(bounds.width() / density), serviceConfig.screenWidthDp);
1320 config.screenHeightDp =
1321 Math.min((int)(bounds.height() / density), serviceConfig.screenHeightDp);
1322 config.smallestScreenWidthDp =
1323 Math.min(config.screenWidthDp, config.screenHeightDp);
1324 config.orientation = (config.screenWidthDp <= config.screenHeightDp)
1325 ? Configuration.ORIENTATION_PORTRAIT
1326 : Configuration.ORIENTATION_LANDSCAPE;
1327 final int sl = Configuration.resetScreenLayout(serviceConfig.screenLayout);
1328 config.screenLayout = Configuration.reduceScreenLayout(
1329 sl, config.screenWidthDp, config.screenHeightDp);
1330 return config;
1331 }
1332
1333 /**
1334 * Using the existing configuration {@param config}, creates a new task override config such
1335 * that all the fields that are usually set in an override config are set to the ones in
1336 * {@param config}.
1337 */
1338 Configuration extractOverrideConfig(Configuration config) {
1339 final Configuration extracted = new Configuration(Configuration.EMPTY);
1340 extracted.screenWidthDp = config.screenWidthDp;
1341 extracted.screenHeightDp = config.screenHeightDp;
1342 extracted.smallestScreenWidthDp = config.smallestScreenWidthDp;
1343 extracted.orientation = config.orientation;
1344 extracted.screenLayout = config.screenLayout;
1345 return extracted;
1346 }
1347
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001348 Rect updateOverrideConfigurationFromLaunchBounds() {
1349 final Rect bounds = validateBounds(getLaunchBounds());
1350 updateOverrideConfiguration(bounds);
1351 return bounds;
1352 }
1353
1354 static Rect validateBounds(Rect bounds) {
1355 if (bounds != null && bounds.isEmpty()) {
1356 Slog.wtf(TAG, "Received strange task bounds: " + bounds, new Throwable());
1357 return null;
1358 }
1359 return bounds;
1360 }
1361
Wale Ogunwale5f986092015-12-04 15:35:38 -08001362 private void reportMultiWindowModeChange() {
1363 for (int i = mActivities.size() - 1; i >= 0; i--) {
1364 final ActivityRecord r = mActivities.get(i);
1365 if (r.app != null && r.app.thread != null) {
1366 try {
1367 // An activity is consider to be in multi-window mode if its task isn't
1368 // fullscreen.
1369 r.app.thread.scheduleMultiWindowModeChanged(r.appToken, !mFullscreen);
1370 } catch (Exception e) {
1371 Slog.e(TAG, "TaskRecord.reportMultiWindowModeChange: ", e);
1372 }
1373 }
1374 }
1375 }
1376
Wale Ogunwaled046a012015-12-24 13:05:59 -08001377 void reportPictureInPictureModeChangeIfNeeded(ActivityStack prevStack) {
1378 if (prevStack == null || prevStack == stack
1379 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
1380 return;
1381 }
1382
Wale Ogunwale5f986092015-12-04 15:35:38 -08001383 for (int i = mActivities.size() - 1; i >= 0; i--) {
1384 final ActivityRecord r = mActivities.get(i);
1385 if (r.app != null && r.app.thread != null) {
1386 try {
1387 r.app.thread.schedulePictureInPictureModeChanged(
1388 r.appToken, stack.mStackId == PINNED_STACK_ID);
1389 } catch (Exception e) {
1390 Slog.e(TAG, "TaskRecord.reportMultiWindowModeChange: ", e);
1391 }
1392 }
1393 }
1394 }
1395
Wale Ogunwale935e5022015-11-10 12:36:10 -08001396 /** Updates the task's bounds and override configuration to match what is expected for the
1397 * input stack. */
1398 void updateOverrideConfigurationForStack(ActivityStack inStack) {
1399 if (stack != null && stack == inStack) {
1400 return;
1401 }
1402
1403 if (inStack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1404 if (!mResizeable) {
1405 throw new IllegalArgumentException("Can not position non-resizeable task="
1406 + this + " in stack=" + inStack);
1407 }
1408 if (mBounds != null) {
1409 return;
1410 }
1411 if (mLastNonFullscreenBounds != null) {
1412 updateOverrideConfiguration(mLastNonFullscreenBounds);
1413 } else {
1414 inStack.layoutTaskInStack(this, null);
1415 }
1416 } else {
1417 updateOverrideConfiguration(inStack.mBounds);
1418 }
1419 }
1420
Chong Zhang0fa656b2015-08-31 15:17:21 -07001421 /**
1422 * Returns the correct stack to use based on task type and currently set bounds,
1423 * regardless of the focused stack and current stack association of the task.
1424 * The task will be moved (and stack focus changed) later if necessary.
1425 */
1426 int getLaunchStackId() {
1427 if (!isApplicationTask()) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001428 return HOME_STACK_ID;
1429 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001430 if (mBounds != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001431 return FREEFORM_WORKSPACE_STACK_ID;
1432 }
1433 return FULLSCREEN_WORKSPACE_STACK_ID;
1434 }
1435
1436 /** Returns the bounds that should be used to launch this task. */
1437 Rect getLaunchBounds() {
Chong Zhang75b37202015-12-04 14:16:36 -08001438 // If we're over lockscreen, forget about stack bounds and use fullscreen.
1439 if (mService.mLockScreenShown == LOCK_SCREEN_SHOWN) {
1440 return null;
1441 }
1442
Chong Zhang7d5f5102016-01-13 10:29:24 -08001443 if (stack == null) {
1444 return null;
1445 }
1446
1447 final int stackId = stack.mStackId;
1448 if (stackId == HOME_STACK_ID
1449 || stackId == FULLSCREEN_WORKSPACE_STACK_ID
1450 || (stackId == DOCKED_STACK_ID && !mResizeable)) {
1451 return mResizeable ? stack.mBounds : null;
Wale Ogunwale3797c222015-10-27 14:21:58 -07001452 } else if (!StackId.persistTaskBounds(stackId)) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001453 return stack.mBounds;
Wale Ogunwale706ed792015-08-02 10:29:44 -07001454 }
1455 return mLastNonFullscreenBounds;
1456 }
1457
Wale Ogunwale39381972015-12-17 17:15:29 -08001458 boolean canMatchRootAffinity() {
1459 // We don't allow root affinity matching on the pinned stack as no other task should
1460 // be launching in it based on affinity.
1461 return rootAffinity != null && (stack == null || stack.mStackId != PINNED_STACK_ID);
1462 }
1463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001465 pw.print(prefix); pw.print("userId="); pw.print(userId);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001466 pw.print(" effectiveUid="); UserHandle.formatUid(pw, effectiveUid);
1467 pw.print(" mCallingUid="); UserHandle.formatUid(pw, mCallingUid);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001468 pw.print(" mCallingPackage="); pw.println(mCallingPackage);
Dianne Hackborn79228822014-09-16 11:11:23 -07001469 if (affinity != null || rootAffinity != null) {
1470 pw.print(prefix); pw.print("affinity="); pw.print(affinity);
1471 if (affinity == null || !affinity.equals(rootAffinity)) {
1472 pw.print(" root="); pw.println(rootAffinity);
1473 } else {
1474 pw.println();
1475 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001476 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07001477 if (voiceSession != null || voiceInteractor != null) {
1478 pw.print(prefix); pw.print("VOICE: session=0x");
1479 pw.print(Integer.toHexString(System.identityHashCode(voiceSession)));
1480 pw.print(" interactor=0x");
1481 pw.println(Integer.toHexString(System.identityHashCode(voiceInteractor)));
1482 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001483 if (intent != null) {
1484 StringBuilder sb = new StringBuilder(128);
1485 sb.append(prefix); sb.append("intent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08001486 intent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001487 sb.append('}');
1488 pw.println(sb.toString());
1489 }
1490 if (affinityIntent != null) {
1491 StringBuilder sb = new StringBuilder(128);
1492 sb.append(prefix); sb.append("affinityIntent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08001493 affinityIntent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001494 sb.append('}');
1495 pw.println(sb.toString());
1496 }
1497 if (origActivity != null) {
1498 pw.print(prefix); pw.print("origActivity=");
1499 pw.println(origActivity.flattenToShortString());
1500 }
1501 if (realActivity != null) {
1502 pw.print(prefix); pw.print("realActivity=");
1503 pw.println(realActivity.flattenToShortString());
1504 }
Dianne Hackborn852975d2014-08-22 17:42:43 -07001505 if (autoRemoveRecents || isPersistable || taskType != 0 || mTaskToReturnTo != 0
1506 || numFullscreen != 0) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001507 pw.print(prefix); pw.print("autoRemoveRecents="); pw.print(autoRemoveRecents);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001508 pw.print(" isPersistable="); pw.print(isPersistable);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001509 pw.print(" numFullscreen="); pw.print(numFullscreen);
1510 pw.print(" taskType="); pw.print(taskType);
1511 pw.print(" mTaskToReturnTo="); pw.println(mTaskToReturnTo);
1512 }
Craig Mautner432f64e2015-05-20 14:59:57 -07001513 if (rootWasReset || mNeverRelinquishIdentity || mReuseTask
1514 || mLockTaskAuth != LOCK_TASK_AUTH_PINNABLE) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001515 pw.print(prefix); pw.print("rootWasReset="); pw.print(rootWasReset);
1516 pw.print(" mNeverRelinquishIdentity="); pw.print(mNeverRelinquishIdentity);
Craig Mautner432f64e2015-05-20 14:59:57 -07001517 pw.print(" mReuseTask="); pw.print(mReuseTask);
1518 pw.print(" mLockTaskAuth="); pw.println(lockTaskAuthToString());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001519 }
Wale Ogunwale18795a22014-12-03 11:38:33 -08001520 if (mAffiliatedTaskId != taskId || mPrevAffiliateTaskId != INVALID_TASK_ID
1521 || mPrevAffiliate != null || mNextAffiliateTaskId != INVALID_TASK_ID
1522 || mNextAffiliate != null) {
Dianne Hackborn852975d2014-08-22 17:42:43 -07001523 pw.print(prefix); pw.print("affiliation="); pw.print(mAffiliatedTaskId);
1524 pw.print(" prevAffiliation="); pw.print(mPrevAffiliateTaskId);
1525 pw.print(" (");
1526 if (mPrevAffiliate == null) {
1527 pw.print("null");
1528 } else {
1529 pw.print(Integer.toHexString(System.identityHashCode(mPrevAffiliate)));
1530 }
1531 pw.print(") nextAffiliation="); pw.print(mNextAffiliateTaskId);
1532 pw.print(" (");
1533 if (mNextAffiliate == null) {
1534 pw.print("null");
1535 } else {
1536 pw.print(Integer.toHexString(System.identityHashCode(mNextAffiliate)));
1537 }
1538 pw.println(")");
1539 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001540 pw.print(prefix); pw.print("Activities="); pw.println(mActivities);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001541 if (!askedCompatMode || !inRecents || !isAvailable) {
1542 pw.print(prefix); pw.print("askedCompatMode="); pw.print(askedCompatMode);
1543 pw.print(" inRecents="); pw.print(inRecents);
1544 pw.print(" isAvailable="); pw.println(isAvailable);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07001545 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001546 pw.print(prefix); pw.print("lastThumbnail="); pw.print(mLastThumbnail);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001547 pw.print(" lastThumbnailFile="); pw.println(mLastThumbnailFile);
1548 if (lastDescription != null) {
1549 pw.print(prefix); pw.print("lastDescription="); pw.println(lastDescription);
1550 }
Wale Ogunwale069fbe42015-07-29 11:38:01 -07001551 if (stack != null) {
1552 pw.print(prefix); pw.print("stackId="); pw.println(stack.mStackId);
1553 }
Dianne Hackbornd38aed82014-06-10 21:36:35 -07001554 pw.print(prefix); pw.print("hasBeenVisible="); pw.print(hasBeenVisible);
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001555 pw.print(" mResizeable="); pw.print(mResizeable);
Winson Chungffa2ec62014-07-03 15:54:42 -07001556 pw.print(" firstActiveTime="); pw.print(lastActiveTime);
Dianne Hackbornd38aed82014-06-10 21:36:35 -07001557 pw.print(" lastActiveTime="); pw.print(lastActiveTime);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001558 pw.print(" (inactive for ");
1559 pw.print((getInactiveDuration()/1000)); pw.println("s)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 }
1561
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001562 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001564 StringBuilder sb = new StringBuilder(128);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001565 if (stringName != null) {
1566 sb.append(stringName);
1567 sb.append(" U=");
1568 sb.append(userId);
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08001569 sb.append(" StackId=");
1570 sb.append(stack != null ? stack.mStackId : INVALID_STACK_ID);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001571 sb.append(" sz=");
1572 sb.append(mActivities.size());
1573 sb.append('}');
1574 return sb.toString();
1575 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001576 sb.append("TaskRecord{");
1577 sb.append(Integer.toHexString(System.identityHashCode(this)));
1578 sb.append(" #");
1579 sb.append(taskId);
1580 if (affinity != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001581 sb.append(" A=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001582 sb.append(affinity);
1583 } else if (intent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001584 sb.append(" I=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001585 sb.append(intent.getComponent().flattenToShortString());
1586 } else if (affinityIntent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001587 sb.append(" aI=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001588 sb.append(affinityIntent.getComponent().flattenToShortString());
1589 } else {
1590 sb.append(" ??");
1591 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001592 stringName = sb.toString();
1593 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 }
1595}