blob: ce83ae6fb0e7fa7fe10b0a98519d2d57a6a3bdf2 [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
Craig Mautnerde4ef022013-04-07 19:01:33 -070019import static com.android.server.am.ActivityManagerService.TAG;
Craig Mautner0eea92c2013-05-16 13:35:39 -070020import static com.android.server.am.ActivityStackSupervisor.DEBUG_ADD_REMOVE;
Craig Mautnerde4ef022013-04-07 19:01:33 -070021
Craig Mautnerb0f7dc72013-04-01 16:34:45 -070022import android.app.Activity;
Craig Mautner9db9a0b2013-04-29 17:05:56 -070023import android.app.ActivityManager;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -070024import android.app.ActivityOptions;
Craig Mautner9db9a0b2013-04-29 17:05:56 -070025import android.app.IThumbnailRetriever;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.ComponentName;
27import android.content.Intent;
28import android.content.pm.ActivityInfo;
Craig Mautner9db9a0b2013-04-29 17:05:56 -070029import android.graphics.Bitmap;
Craig Mautner21d24a22014-04-23 11:45:37 -070030import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070031import android.os.UserHandle;
Dianne Hackborn91097de2014-04-04 18:02:06 -070032import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn7f96b792012-05-29 18:46:45 -070033import android.util.Slog;
Dianne Hackborn91097de2014-04-04 18:02:06 -070034import com.android.internal.app.IVoiceInteractor;
Craig Mautner21d24a22014-04-23 11:45:37 -070035import com.android.internal.util.XmlUtils;
36import org.xmlpull.v1.XmlPullParser;
37import org.xmlpull.v1.XmlPullParserException;
38import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
Craig Mautner21d24a22014-04-23 11:45:37 -070040import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import java.io.PrintWriter;
Craig Mautner5d9c7be2013-02-15 14:02:56 -080042import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070044final class TaskRecord extends ThumbnailHolder {
Craig Mautner21d24a22014-04-23 11:45:37 -070045 private static final String TAG_TASK = "task";
46 private static final String ATTR_TASKID = "task_id";
47 private static final String TAG_INTENT = "intent";
48 private static final String TAG_AFFINITYINTENT = "affinity_intent";
49 private static final String ATTR_REALACTIVITY = "real_activity";
50 private static final String ATTR_ORIGACTIVITY = "orig_activity";
51 private static final String TAG_ACTIVITY = "activity";
52 private static final String ATTR_AFFINITY = "affinity";
53 private static final String ATTR_ROOTHASRESET = "root_has_reset";
54 private static final String ATTR_ASKEDCOMPATMODE = "asked_compat_mode";
55 private static final String ATTR_USERID = "user_id";
56 private static final String ATTR_TASKTYPE = "task_type";
57 private static final String ATTR_ONTOPOFHOME = "on_top_of_home";
58 private static final String ATTR_LASTDESCRIPTION = "last_description";
59 private static final String ATTR_LASTTIMEMOVED = "last_time_moved";
60
61 private static final String TASK_THUMBNAIL_SUFFIX = "_task_thumbnail";
62
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 final int taskId; // Unique identifier for this task.
64 final String affinity; // The affinity name for this task, or null.
Dianne Hackborn91097de2014-04-04 18:02:06 -070065 final IVoiceInteractionSession voiceSession; // Voice interaction session driving task
66 final IVoiceInteractor voiceInteractor; // Associated interactor to provide to app
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 Intent intent; // The original intent that started the task.
68 Intent affinityIntent; // Intent of affinity-moved activity that started this task.
69 ComponentName origActivity; // The non-alias activity component of the intent.
70 ComponentName realActivity; // The actual activity component that started the task.
71 int numActivities; // Current number of activities in this task.
72 long lastActiveTime; // Last time this task was active, including sleep.
73 boolean rootWasReset; // True if the intent at the root of the task had
74 // the FLAG_ACTIVITY_RESET_TASK_IF_NEEDED flag.
Dianne Hackborn36cd41f2011-05-25 21:00:46 -070075 boolean askedCompatMode;// Have asked the user about compat mode for this task.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076
Dianne Hackborn1d442e02009-04-20 18:14:05 -070077 String stringName; // caching of toString() result.
Dianne Hackborn9da2d402012-03-15 13:43:08 -070078 int userId; // user for which this task was created
Dianne Hackborn09233282014-04-30 11:33:59 -070079 int creatorUid; // The app uid that originally created the task
Craig Mautner5d9c7be2013-02-15 14:02:56 -080080
81 int numFullscreen; // Number of fullscreen activities.
82
Winson Chung03a9bae2014-05-02 09:56:12 -070083 // This represents the last resolved activity values for this task
84 // NOTE: This value needs to be persisted with each task
Winson Chunga449dc02014-05-16 11:15:04 -070085 ActivityManager.TaskDescription lastTaskDescription =
86 new ActivityManager.TaskDescription();
Winson Chung03a9bae2014-05-02 09:56:12 -070087
Craig Mautnerd2328952013-03-05 12:46:26 -080088 /** List of all activities in the task arranged in history order */
Craig Mautner21d24a22014-04-23 11:45:37 -070089 final ArrayList<ActivityRecord> mActivities;
Craig Mautner5d9c7be2013-02-15 14:02:56 -080090
Craig Mautnerd2328952013-03-05 12:46:26 -080091 /** Current stack */
92 ActivityStack stack;
93
Craig Mautner2c1faed2013-07-23 12:56:02 -070094 /** Takes on same set of values as ActivityRecord.mActivityType */
Craig Mautner21d24a22014-04-23 11:45:37 -070095 int taskType;
Craig Mautner1602ec22013-05-12 10:24:27 -070096
Craig Mautner21d24a22014-04-23 11:45:37 -070097 /** Takes on same value as first root activity */
98 boolean isPersistable = false;
99
100 /** Only used for persistable tasks, otherwise 0. The last time this task was moved. Used for
101 * determining the order when restoring. Sign indicates whether last task movement was to front
102 * (positive) or back (negative). Absolute value indicates time. */
103 long mLastTimeMoved = System.currentTimeMillis();
104
105 /** True if persistable, has changed, and has not yet been persisted */
106 boolean needsPersisting = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800107 /** Launch the home activity when leaving this task. Will be false for tasks that are not on
108 * Display.DEFAULT_DISPLAY. */
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700109 boolean mOnTopOfHome = false;
110
Craig Mautner21d24a22014-04-23 11:45:37 -0700111 final ActivityManagerService mService;
112
113 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700114 IVoiceInteractionSession _voiceSession, IVoiceInteractor _voiceInteractor) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700115 mService = service;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 taskId = _taskId;
117 affinity = info.taskAffinity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700118 voiceSession = _voiceSession;
119 voiceInteractor = _voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 setIntent(_intent, info);
Craig Mautner21d24a22014-04-23 11:45:37 -0700121 mActivities = new ArrayList<ActivityRecord>();
122 }
123
124 TaskRecord(ActivityManagerService service, int _taskId, Intent _intent, Intent _affinityIntent,
125 String _affinity, ComponentName _realActivity, ComponentName _origActivity,
126 boolean _rootWasReset, boolean _askedCompatMode, int _taskType, boolean _onTopOfHome,
127 int _userId, String _lastDescription, ArrayList<ActivityRecord> activities,
128 long lastTimeMoved) {
129 mService = service;
130 taskId = _taskId;
131 intent = _intent;
132 affinityIntent = _affinityIntent;
133 affinity = _affinity;
134 voiceSession = null;
135 voiceInteractor = null;
136 realActivity = _realActivity;
137 origActivity = _origActivity;
138 rootWasReset = _rootWasReset;
139 askedCompatMode = _askedCompatMode;
140 taskType = _taskType;
141 mOnTopOfHome = _onTopOfHome;
142 userId = _userId;
143 lastDescription = _lastDescription;
144 mActivities = activities;
145 mLastTimeMoved = lastTimeMoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 }
147
148 void touchActiveTime() {
149 lastActiveTime = android.os.SystemClock.elapsedRealtime();
150 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 long getInactiveDuration() {
153 return android.os.SystemClock.elapsedRealtime() - lastActiveTime;
154 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 void setIntent(Intent _intent, ActivityInfo info) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700157 stringName = null;
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 if (info.targetActivity == null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800160 if (_intent != null) {
161 // If this Intent has a selector, we want to clear it for the
162 // recent task since it is not relevant if the user later wants
163 // to re-launch the app.
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700164 if (_intent.getSelector() != null || _intent.getSourceBounds() != null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800165 _intent = new Intent(_intent);
166 _intent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700167 _intent.setSourceBounds(null);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800168 }
169 }
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700170 if (ActivityManagerService.DEBUG_TASKS) Slog.v(ActivityManagerService.TAG,
171 "Setting Intent of " + this + " to " + _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 intent = _intent;
173 realActivity = _intent != null ? _intent.getComponent() : null;
174 origActivity = null;
175 } else {
176 ComponentName targetComponent = new ComponentName(
177 info.packageName, info.targetActivity);
178 if (_intent != null) {
179 Intent targetIntent = new Intent(_intent);
180 targetIntent.setComponent(targetComponent);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800181 targetIntent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700182 targetIntent.setSourceBounds(null);
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700183 if (ActivityManagerService.DEBUG_TASKS) Slog.v(ActivityManagerService.TAG,
184 "Setting Intent of " + this + " to target " + targetIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 intent = targetIntent;
186 realActivity = targetComponent;
187 origActivity = _intent.getComponent();
188 } else {
189 intent = null;
190 realActivity = targetComponent;
191 origActivity = new ComponentName(info.packageName, info.name);
192 }
193 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 if (intent != null &&
196 (intent.getFlags()&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
197 // Once we are set to an Intent with this flag, we count this
198 // task as having a true root activity.
199 rootWasReset = true;
200 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700201
Dianne Hackborn09233282014-04-30 11:33:59 -0700202 userId = UserHandle.getUserId(info.applicationInfo.uid);
203 creatorUid = info.applicationInfo.uid;
Craig Mautner41db4a72014-05-07 17:20:56 -0700204 if ((info.flags & ActivityInfo.FLAG_AUTO_REMOVE_FROM_RECENTS) != 0) {
205 intent.addFlags(Intent.FLAG_ACTIVITY_AUTO_REMOVE_FROM_RECENTS);
206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800208
Dianne Hackborn9844d292013-10-04 16:44:22 -0700209 void disposeThumbnail() {
210 super.disposeThumbnail();
211 for (int i=mActivities.size()-1; i>=0; i--) {
212 ThumbnailHolder thumb = mActivities.get(i).thumbHolder;
213 if (thumb != this) {
214 thumb.disposeThumbnail();
215 }
216 }
217 }
218
Winson Chung1147c402014-05-14 11:05:00 -0700219 /** Returns the intent for the root activity for this task */
220 Intent getBaseIntent() {
221 return intent != null ? intent : affinityIntent;
222 }
223
Winson Chung3b3f4642014-04-22 10:08:18 -0700224 /** Returns the first non-finishing activity from the root. */
225 ActivityRecord getRootActivity() {
226 for (int i = 0; i < mActivities.size(); i++) {
227 final ActivityRecord r = mActivities.get(i);
228 if (r.finishing) {
229 continue;
230 }
231 return r;
232 }
233 return null;
234 }
235
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800236 ActivityRecord getTopActivity() {
237 for (int i = mActivities.size() - 1; i >= 0; --i) {
238 final ActivityRecord r = mActivities.get(i);
239 if (r.finishing) {
240 continue;
241 }
242 return r;
243 }
244 return null;
245 }
246
Craig Mautner6b74cb52013-09-27 17:02:21 -0700247 ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
248 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
249 ActivityRecord r = mActivities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800250 if (!r.finishing && r != notTop && stack.okToShowLocked(r)) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700251 return r;
252 }
253 }
254 return null;
255 }
256
Craig Mautner3b475fe2013-12-16 15:58:31 -0800257 /** Call after activity movement or finish to make sure that frontOfTask is set correctly */
258 final void setFrontOfTask() {
259 boolean foundFront = false;
260 final int numActivities = mActivities.size();
Craig Mautner704e40b2013-12-18 16:43:51 -0800261 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
Craig Mautner3b475fe2013-12-16 15:58:31 -0800262 final ActivityRecord r = mActivities.get(activityNdx);
263 if (foundFront || r.finishing) {
264 r.frontOfTask = false;
265 } else {
266 r.frontOfTask = true;
267 // Set frontOfTask false for every following activity.
268 foundFront = true;
269 }
270 }
271 }
272
Craig Mautnerde4ef022013-04-07 19:01:33 -0700273 /**
Craig Mautner3b475fe2013-12-16 15:58:31 -0800274 * Reorder the history stack so that the passed activity is brought to the front.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700275 */
276 final void moveActivityToFrontLocked(ActivityRecord newTop) {
277 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + newTop
278 + " to stack at top", new RuntimeException("here").fillInStackTrace());
279
Craig Mautnerde4ef022013-04-07 19:01:33 -0700280 mActivities.remove(newTop);
281 mActivities.add(newTop);
Craig Mautner3b475fe2013-12-16 15:58:31 -0800282
283 setFrontOfTask();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700284 }
285
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800286 void addActivityAtBottom(ActivityRecord r) {
Craig Mautner77878772013-03-04 19:46:24 -0800287 addActivityAtIndex(0, r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800288 }
289
290 void addActivityToTop(ActivityRecord r) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700291 addActivityAtIndex(mActivities.size(), r);
292 }
293
294 void addActivityAtIndex(int index, ActivityRecord r) {
Craig Mautner6170f732013-04-02 13:05:23 -0700295 // 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 -0800296 if (!mActivities.remove(r) && r.fullscreen) {
297 // Was not previously in list.
298 numFullscreen++;
299 }
Craig Mautner2c1faed2013-07-23 12:56:02 -0700300 // Only set this based on the first activity
301 if (mActivities.isEmpty()) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700302 taskType = r.mActivityType;
303 isPersistable = r.isPersistable();
Craig Mautner2c1faed2013-07-23 12:56:02 -0700304 } else {
305 // Otherwise make all added activities match this one.
Craig Mautner21d24a22014-04-23 11:45:37 -0700306 r.mActivityType = taskType;
Craig Mautner78733002013-06-10 13:54:49 -0700307 }
Craig Mautner77878772013-03-04 19:46:24 -0800308 mActivities.add(index, r);
Craig Mautner21d24a22014-04-23 11:45:37 -0700309 if (r.isPersistable()) {
310 mService.notifyTaskPersisterLocked(this, false);
311 }
Craig Mautner77878772013-03-04 19:46:24 -0800312 }
313
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800314 /** @return true if this was the last activity in the task */
315 boolean removeActivity(ActivityRecord r) {
316 if (mActivities.remove(r) && r.fullscreen) {
317 // Was previously in list.
318 numFullscreen--;
319 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700320 if (r.isPersistable()) {
321 mService.notifyTaskPersisterLocked(this, false);
322 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800323 return mActivities.size() == 0;
324 }
325
Craig Mautner41db4a72014-05-07 17:20:56 -0700326 boolean autoRemoveFromRecents() {
327 return intent != null &&
328 (intent.getFlags() & Intent.FLAG_ACTIVITY_AUTO_REMOVE_FROM_RECENTS) != 0;
329 }
330
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700331 /**
332 * Completely remove all activities associated with an existing
333 * task starting at a specified index.
334 */
335 final void performClearTaskAtIndexLocked(int activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700336 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700337 for ( ; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700338 final ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700339 if (r.finishing) {
340 continue;
341 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700342 if (stack == null) {
343 // Task was restored from persistent storage.
344 r.takeFromHistory();
345 mActivities.remove(activityNdx);
346 --activityNdx;
347 --numActivities;
348 } else if (stack.finishActivityLocked(r, Activity.RESULT_CANCELED, null, "clear",
349 false)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700350 --activityNdx;
351 --numActivities;
352 }
353 }
354 }
355
356 /**
357 * Completely remove all activities associated with an existing task.
358 */
359 final void performClearTaskLocked() {
360 performClearTaskAtIndexLocked(0);
361 }
362
363 /**
364 * Perform clear operation as requested by
365 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the
366 * stack to the given task, then look for
367 * an instance of that activity in the stack and, if found, finish all
368 * activities on top of it and return the instance.
369 *
370 * @param newR Description of the new activity being started.
371 * @return Returns the old activity that should be continued to be used,
372 * or null if none was found.
373 */
374 final ActivityRecord performClearTaskLocked(ActivityRecord newR, int launchFlags) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700375 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700376 for (int activityNdx = numActivities - 1; activityNdx >= 0; --activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700377 ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700378 if (r.finishing) {
379 continue;
380 }
381 if (r.realActivity.equals(newR.realActivity)) {
382 // Here it is! Now finish everything in front...
Craig Mautner1602ec22013-05-12 10:24:27 -0700383 final ActivityRecord ret = r;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700384
385 for (++activityNdx; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700386 r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700387 if (r.finishing) {
388 continue;
389 }
390 ActivityOptions opts = r.takeOptionsLocked();
391 if (opts != null) {
392 ret.updateOptionsLocked(opts);
393 }
394 if (stack.finishActivityLocked(r, Activity.RESULT_CANCELED, null, "clear",
395 false)) {
396 --activityNdx;
397 --numActivities;
398 }
399 }
400
401 // Finally, if this is a normal launch mode (that is, not
402 // expecting onNewIntent()), then we will finish the current
403 // instance of the activity so a new fresh one can be started.
404 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
405 && (launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0) {
406 if (!ret.finishing) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700407 stack.finishActivityLocked(ret, Activity.RESULT_CANCELED, null,
408 "clear", false);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700409 return null;
410 }
411 }
412
413 return ret;
414 }
415 }
416
417 return null;
418 }
419
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700420 public ActivityManager.TaskThumbnails getTaskThumbnailsLocked() {
Winson Chung3b3f4642014-04-22 10:08:18 -0700421 TaskAccessInfo info = getTaskAccessInfoLocked();
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700422 final ActivityRecord resumedActivity = stack.mResumedActivity;
423 if (resumedActivity != null && resumedActivity.thumbHolder == this) {
424 info.mainThumbnail = stack.screenshotActivities(resumedActivity);
425 }
426 if (info.mainThumbnail == null) {
427 info.mainThumbnail = lastThumbnail;
428 }
429 return info;
430 }
431
432 public Bitmap getTaskTopThumbnailLocked() {
Craig Mautner21d24a22014-04-23 11:45:37 -0700433 if (stack != null) {
434 final ActivityRecord resumedActivity = stack.mResumedActivity;
435 if (resumedActivity != null && resumedActivity.task == this) {
436 // This task is the current resumed task, we just need to take
437 // a screenshot of it and return that.
438 return stack.screenshotActivities(resumedActivity);
439 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700440 }
441 // Return the information about the task, to figure out the top
442 // thumbnail to return.
Winson Chung3b3f4642014-04-22 10:08:18 -0700443 TaskAccessInfo info = getTaskAccessInfoLocked();
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700444 if (info.numSubThumbbails <= 0) {
445 return info.mainThumbnail != null ? info.mainThumbnail : lastThumbnail;
446 }
447 return info.subtasks.get(info.numSubThumbbails-1).holder.lastThumbnail;
448 }
449
450 public ActivityRecord removeTaskActivitiesLocked(int subTaskIndex,
451 boolean taskRequired) {
Winson Chung3b3f4642014-04-22 10:08:18 -0700452 TaskAccessInfo info = getTaskAccessInfoLocked();
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700453 if (info.root == null) {
454 if (taskRequired) {
455 Slog.w(TAG, "removeTaskLocked: unknown taskId " + taskId);
456 }
457 return null;
458 }
459
460 if (subTaskIndex < 0) {
461 // Just remove the entire task.
462 performClearTaskAtIndexLocked(info.rootIndex);
463 return info.root;
464 }
465
466 if (subTaskIndex >= info.subtasks.size()) {
467 if (taskRequired) {
468 Slog.w(TAG, "removeTaskLocked: unknown subTaskIndex " + subTaskIndex);
469 }
470 return null;
471 }
472
473 // Remove all of this task's activities starting at the sub task.
474 TaskAccessInfo.SubTask subtask = info.subtasks.get(subTaskIndex);
475 performClearTaskAtIndexLocked(subtask.index);
476 return subtask.activity;
477 }
478
Craig Mautnera82aa092013-09-13 15:34:08 -0700479 boolean isHomeTask() {
Craig Mautner21d24a22014-04-23 11:45:37 -0700480 return taskType == ActivityRecord.HOME_ACTIVITY_TYPE;
Craig Mautnera82aa092013-09-13 15:34:08 -0700481 }
482
Craig Mautner86d67a42013-05-14 10:34:38 -0700483 boolean isApplicationTask() {
Craig Mautner21d24a22014-04-23 11:45:37 -0700484 return taskType == ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner1602ec22013-05-12 10:24:27 -0700485 }
486
Winson Chung3b3f4642014-04-22 10:08:18 -0700487 public TaskAccessInfo getTaskAccessInfoLocked() {
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700488 final TaskAccessInfo thumbs = new TaskAccessInfo();
489 // How many different sub-thumbnails?
490 final int NA = mActivities.size();
491 int j = 0;
492 ThumbnailHolder holder = null;
493 while (j < NA) {
494 ActivityRecord ar = mActivities.get(j);
495 if (!ar.finishing) {
496 thumbs.root = ar;
497 thumbs.rootIndex = j;
498 holder = ar.thumbHolder;
499 if (holder != null) {
500 thumbs.mainThumbnail = holder.lastThumbnail;
501 }
502 j++;
503 break;
504 }
505 j++;
506 }
507
508 if (j >= NA) {
509 return thumbs;
510 }
511
512 ArrayList<TaskAccessInfo.SubTask> subtasks = new ArrayList<TaskAccessInfo.SubTask>();
513 thumbs.subtasks = subtasks;
514 while (j < NA) {
515 ActivityRecord ar = mActivities.get(j);
516 j++;
517 if (ar.finishing) {
518 continue;
519 }
520 if (ar.thumbHolder != holder && holder != null) {
521 thumbs.numSubThumbbails++;
522 holder = ar.thumbHolder;
523 TaskAccessInfo.SubTask sub = new TaskAccessInfo.SubTask();
524 sub.holder = holder;
525 sub.activity = ar;
526 sub.index = j-1;
527 subtasks.add(sub);
528 }
529 }
530 if (thumbs.numSubThumbbails > 0) {
531 thumbs.retriever = new IThumbnailRetriever.Stub() {
532 @Override
533 public Bitmap getThumbnail(int index) {
534 if (index < 0 || index >= thumbs.subtasks.size()) {
535 return null;
536 }
537 TaskAccessInfo.SubTask sub = thumbs.subtasks.get(index);
538 ActivityRecord resumedActivity = stack.mResumedActivity;
539 if (resumedActivity != null && resumedActivity.thumbHolder == sub.holder) {
540 return stack.screenshotActivities(resumedActivity);
541 }
542 return sub.holder.lastThumbnail;
543 }
544 };
545 }
546 return thumbs;
547 }
548
Craig Mautner525f3d92013-05-07 14:01:50 -0700549 /**
550 * Find the activity in the history stack within the given task. Returns
551 * the index within the history at which it's found, or < 0 if not found.
552 */
553 final ActivityRecord findActivityInHistoryLocked(ActivityRecord r) {
554 final ComponentName realActivity = r.realActivity;
555 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
556 ActivityRecord candidate = mActivities.get(activityNdx);
557 if (candidate.finishing) {
558 continue;
559 }
560 if (candidate.realActivity.equals(realActivity)) {
561 return candidate;
562 }
563 }
564 return null;
565 }
566
Winson Chunga449dc02014-05-16 11:15:04 -0700567 /** Updates the last task description values. */
568 void updateTaskDescription() {
569 // Traverse upwards looking for any break between main task activities and
570 // utility activities.
571 int activityNdx;
572 final int numActivities = mActivities.size();
573 for (activityNdx = Math.min(numActivities, 1); activityNdx < numActivities;
Craig Mautner21d24a22014-04-23 11:45:37 -0700574 ++activityNdx) {
Winson Chunga449dc02014-05-16 11:15:04 -0700575 final ActivityRecord r = mActivities.get(activityNdx);
576 if (r.intent != null &&
577 (r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
578 != 0) {
579 break;
580 }
581 }
582 if (activityNdx > 0) {
583 // Traverse downwards starting below break looking for set label, icon.
584 // Note that if there are activities in the task but none of them set the
585 // recent activity values, then we do not fall back to the last set
586 // values in the TaskRecord.
587 String label = null;
588 Bitmap icon = null;
589 int colorPrimary = 0;
590 for (--activityNdx; activityNdx >= 0; --activityNdx) {
591 final ActivityRecord r = mActivities.get(activityNdx);
592 if (r.taskDescription != null) {
593 if (label == null) {
594 label = r.taskDescription.getLabel();
595 }
596 if (icon == null) {
597 icon = r.taskDescription.getIcon();
598 }
599 if (colorPrimary == 0) {
600 colorPrimary = r.taskDescription.getPrimaryColor();
601
602 }
603 }
604 }
605 lastTaskDescription = new ActivityManager.TaskDescription(label, icon, colorPrimary);
606 }
607 }
608
Craig Mautner21d24a22014-04-23 11:45:37 -0700609 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
610 Slog.i(TAG, "Saving task=" + this);
611
612 out.attribute(null, ATTR_TASKID, String.valueOf(taskId));
613 if (realActivity != null) {
614 out.attribute(null, ATTR_REALACTIVITY, realActivity.flattenToShortString());
615 }
616 if (origActivity != null) {
617 out.attribute(null, ATTR_ORIGACTIVITY, origActivity.flattenToShortString());
618 }
619 if (affinity != null) {
620 out.attribute(null, ATTR_AFFINITY, affinity);
621 }
622 out.attribute(null, ATTR_ROOTHASRESET, String.valueOf(rootWasReset));
623 out.attribute(null, ATTR_ASKEDCOMPATMODE, String.valueOf(askedCompatMode));
624 out.attribute(null, ATTR_USERID, String.valueOf(userId));
625 out.attribute(null, ATTR_TASKTYPE, String.valueOf(taskType));
626 out.attribute(null, ATTR_ONTOPOFHOME, String.valueOf(mOnTopOfHome));
627 out.attribute(null, ATTR_LASTTIMEMOVED, String.valueOf(mLastTimeMoved));
628 if (lastDescription != null) {
629 out.attribute(null, ATTR_LASTDESCRIPTION, lastDescription.toString());
630 }
631
632 if (affinityIntent != null) {
633 out.startTag(null, TAG_AFFINITYINTENT);
634 affinityIntent.saveToXml(out);
635 out.endTag(null, TAG_AFFINITYINTENT);
636 }
637
638 out.startTag(null, TAG_INTENT);
639 intent.saveToXml(out);
640 out.endTag(null, TAG_INTENT);
641
642 final ArrayList<ActivityRecord> activities = mActivities;
643 final int numActivities = activities.size();
644 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
645 final ActivityRecord r = activities.get(activityNdx);
646 if (!r.isPersistable() || (r.intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) ==
647 Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) {
648 break;
649 }
650 out.startTag(null, TAG_ACTIVITY);
651 r.saveToXml(out);
652 out.endTag(null, TAG_ACTIVITY);
653 }
654
655 final Bitmap thumbnail = getTaskTopThumbnailLocked();
656 if (thumbnail != null) {
657 TaskPersister.saveImage(thumbnail, String.valueOf(taskId) + TASK_THUMBNAIL_SUFFIX);
658 }
659 }
660
661 static TaskRecord restoreFromXml(XmlPullParser in, ActivityStackSupervisor stackSupervisor)
662 throws IOException, XmlPullParserException {
663 Intent intent = null;
664 Intent affinityIntent = null;
665 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
666 ComponentName realActivity = null;
667 ComponentName origActivity = null;
668 String affinity = null;
669 boolean rootHasReset = false;
670 boolean askedCompatMode = false;
671 int taskType = ActivityRecord.APPLICATION_ACTIVITY_TYPE;
672 boolean onTopOfHome = true;
673 int userId = 0;
674 String lastDescription = null;
675 long lastTimeOnTop = 0;
676 int taskId = -1;
677 final int outerDepth = in.getDepth();
678
679 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
680 final String attrName = in.getAttributeName(attrNdx);
681 final String attrValue = in.getAttributeValue(attrNdx);
682 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: attribute name=" +
683 attrName + " value=" + attrValue);
684 if (ATTR_TASKID.equals(attrName)) {
685 taskId = Integer.valueOf(attrValue);
686 } else if (ATTR_REALACTIVITY.equals(attrName)) {
687 realActivity = ComponentName.unflattenFromString(attrValue);
688 } else if (ATTR_ORIGACTIVITY.equals(attrName)) {
689 origActivity = ComponentName.unflattenFromString(attrValue);
690 } else if (ATTR_AFFINITY.equals(attrName)) {
691 affinity = attrValue;
692 } else if (ATTR_ROOTHASRESET.equals(attrName)) {
693 rootHasReset = Boolean.valueOf(attrValue);
694 } else if (ATTR_ASKEDCOMPATMODE.equals(attrName)) {
695 askedCompatMode = Boolean.valueOf(attrValue);
696 } else if (ATTR_USERID.equals(attrName)) {
697 userId = Integer.valueOf(attrValue);
698 } else if (ATTR_TASKTYPE.equals(attrName)) {
699 taskType = Integer.valueOf(attrValue);
700 } else if (ATTR_ONTOPOFHOME.equals(attrName)) {
701 onTopOfHome = Boolean.valueOf(attrValue);
702 } else if (ATTR_LASTDESCRIPTION.equals(attrName)) {
703 lastDescription = attrValue;
704 } else if (ATTR_LASTTIMEMOVED.equals(attrName)) {
705 lastTimeOnTop = Long.valueOf(attrValue);
706 } else {
707 Slog.w(TAG, "TaskRecord: Unknown attribute=" + attrName);
708 }
709 }
710
711 int event;
712 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
713 (event != XmlPullParser.END_TAG || in.getDepth() < outerDepth)) {
714 if (event == XmlPullParser.START_TAG) {
715 final String name = in.getName();
716 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: START_TAG name=" +
717 name);
718 if (TAG_AFFINITYINTENT.equals(name)) {
719 affinityIntent = Intent.restoreFromXml(in);
720 } else if (TAG_INTENT.equals(name)) {
721 intent = Intent.restoreFromXml(in);
722 } else if (TAG_ACTIVITY.equals(name)) {
723 ActivityRecord activity =
724 ActivityRecord.restoreFromXml(in, taskId, stackSupervisor);
725 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: activity=" +
726 activity);
727 if (activity != null) {
728 activities.add(activity);
729 }
730 } else {
731 Slog.e(TAG, "restoreTask: Unexpected name=" + name);
732 XmlUtils.skipCurrentTag(in);
733 }
734 }
735 }
736
737 final TaskRecord task = new TaskRecord(stackSupervisor.mService, taskId, intent,
738 affinityIntent, affinity, realActivity, origActivity, rootHasReset,
739 askedCompatMode, taskType, onTopOfHome, userId, lastDescription, activities,
740 lastTimeOnTop);
741
742 for (int activityNdx = activities.size() - 1; activityNdx >=0; --activityNdx) {
743 final ActivityRecord r = activities.get(activityNdx);
744 r.thumbHolder = r.task = task;
745 }
746
747 task.lastThumbnail = TaskPersister.restoreImage(taskId + TASK_THUMBNAIL_SUFFIX);
748
749 Slog.i(TAG, "Restored task=" + task);
750 return task;
751 }
752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 void dump(PrintWriter pw, String prefix) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700754 if (rootWasReset || userId != 0 || numFullscreen != 0) {
755 pw.print(prefix); pw.print(" rootWasReset="); pw.print(rootWasReset);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800756 pw.print(" userId="); pw.print(userId);
Craig Mautner21d24a22014-04-23 11:45:37 -0700757 pw.print(" taskType="); pw.print(taskType);
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700758 pw.print(" numFullscreen="); pw.print(numFullscreen);
759 pw.print(" mOnTopOfHome="); pw.println(mOnTopOfHome);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700760 }
761 if (affinity != null) {
762 pw.print(prefix); pw.print("affinity="); pw.println(affinity);
763 }
Dianne Hackborn91097de2014-04-04 18:02:06 -0700764 if (voiceSession != null || voiceInteractor != null) {
765 pw.print(prefix); pw.print("VOICE: session=0x");
766 pw.print(Integer.toHexString(System.identityHashCode(voiceSession)));
767 pw.print(" interactor=0x");
768 pw.println(Integer.toHexString(System.identityHashCode(voiceInteractor)));
769 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700770 if (intent != null) {
771 StringBuilder sb = new StringBuilder(128);
772 sb.append(prefix); sb.append("intent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800773 intent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700774 sb.append('}');
775 pw.println(sb.toString());
776 }
777 if (affinityIntent != null) {
778 StringBuilder sb = new StringBuilder(128);
779 sb.append(prefix); sb.append("affinityIntent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800780 affinityIntent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700781 sb.append('}');
782 pw.println(sb.toString());
783 }
784 if (origActivity != null) {
785 pw.print(prefix); pw.print("origActivity=");
786 pw.println(origActivity.flattenToShortString());
787 }
788 if (realActivity != null) {
789 pw.print(prefix); pw.print("realActivity=");
790 pw.println(realActivity.flattenToShortString());
791 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800792 pw.print(prefix); pw.print("Activities="); pw.println(mActivities);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700793 if (!askedCompatMode) {
794 pw.print(prefix); pw.print("askedCompatMode="); pw.println(askedCompatMode);
795 }
Dianne Hackborncfb9f2b2011-08-24 10:51:49 -0700796 pw.print(prefix); pw.print("lastThumbnail="); pw.print(lastThumbnail);
797 pw.print(" lastDescription="); pw.println(lastDescription);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700798 pw.print(prefix); pw.print("lastActiveTime="); pw.print(lastActiveTime);
799 pw.print(" (inactive for ");
800 pw.print((getInactiveDuration()/1000)); pw.println("s)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 }
802
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800803 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700805 StringBuilder sb = new StringBuilder(128);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700806 if (stringName != null) {
807 sb.append(stringName);
808 sb.append(" U=");
809 sb.append(userId);
810 sb.append(" sz=");
811 sb.append(mActivities.size());
812 sb.append('}');
813 return sb.toString();
814 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700815 sb.append("TaskRecord{");
816 sb.append(Integer.toHexString(System.identityHashCode(this)));
817 sb.append(" #");
818 sb.append(taskId);
819 if (affinity != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800820 sb.append(" A=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700821 sb.append(affinity);
822 } else if (intent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800823 sb.append(" I=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700824 sb.append(intent.getComponent().flattenToShortString());
825 } else if (affinityIntent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800826 sb.append(" aI=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700827 sb.append(affinityIntent.getComponent().flattenToShortString());
828 } else {
829 sb.append(" ??");
830 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700831 stringName = sb.toString();
832 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 }
834}