blob: dda82ecd7e938f21b7a9f5950946531a2814e175 [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;
20import static com.android.server.am.ActivityStack.DEBUG_ADD_REMOVE;
21
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;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070030import android.os.UserHandle;
Dianne Hackborn7f96b792012-05-29 18:46:45 -070031import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
33import java.io.PrintWriter;
Craig Mautner5d9c7be2013-02-15 14:02:56 -080034import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
Dianne Hackbornf26fd992011-04-08 18:14:09 -070036class TaskRecord extends ThumbnailHolder {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037 final int taskId; // Unique identifier for this task.
38 final String affinity; // The affinity name for this task, or null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 Intent intent; // The original intent that started the task.
40 Intent affinityIntent; // Intent of affinity-moved activity that started this task.
41 ComponentName origActivity; // The non-alias activity component of the intent.
42 ComponentName realActivity; // The actual activity component that started the task.
43 int numActivities; // Current number of activities in this task.
44 long lastActiveTime; // Last time this task was active, including sleep.
45 boolean rootWasReset; // True if the intent at the root of the task had
46 // the FLAG_ACTIVITY_RESET_TASK_IF_NEEDED flag.
Dianne Hackborn36cd41f2011-05-25 21:00:46 -070047 boolean askedCompatMode;// Have asked the user about compat mode for this task.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
Dianne Hackborn1d442e02009-04-20 18:14:05 -070049 String stringName; // caching of toString() result.
Dianne Hackborn9da2d402012-03-15 13:43:08 -070050 int userId; // user for which this task was created
Craig Mautner5d9c7be2013-02-15 14:02:56 -080051
52 int numFullscreen; // Number of fullscreen activities.
53
Craig Mautnerd2328952013-03-05 12:46:26 -080054 /** List of all activities in the task arranged in history order */
Craig Mautner5d9c7be2013-02-15 14:02:56 -080055 final ArrayList<ActivityRecord> mActivities = new ArrayList<ActivityRecord>();
56
Craig Mautnerd2328952013-03-05 12:46:26 -080057 /** Current stack */
58 ActivityStack stack;
59
60 TaskRecord(int _taskId, ActivityInfo info, Intent _intent, ActivityStack _stack) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 taskId = _taskId;
62 affinity = info.taskAffinity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 setIntent(_intent, info);
Craig Mautnerd2328952013-03-05 12:46:26 -080064 stack = _stack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 }
66
67 void touchActiveTime() {
68 lastActiveTime = android.os.SystemClock.elapsedRealtime();
69 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -070070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 long getInactiveDuration() {
72 return android.os.SystemClock.elapsedRealtime() - lastActiveTime;
73 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -070074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 void setIntent(Intent _intent, ActivityInfo info) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070076 stringName = null;
Dianne Hackbornf5b86712011-12-05 17:42:41 -080077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 if (info.targetActivity == null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -080079 if (_intent != null) {
80 // If this Intent has a selector, we want to clear it for the
81 // recent task since it is not relevant if the user later wants
82 // to re-launch the app.
Dianne Hackbornd367ca82012-05-07 15:49:39 -070083 if (_intent.getSelector() != null || _intent.getSourceBounds() != null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -080084 _intent = new Intent(_intent);
85 _intent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -070086 _intent.setSourceBounds(null);
Dianne Hackbornf5b86712011-12-05 17:42:41 -080087 }
88 }
Dianne Hackborn7f96b792012-05-29 18:46:45 -070089 if (ActivityManagerService.DEBUG_TASKS) Slog.v(ActivityManagerService.TAG,
90 "Setting Intent of " + this + " to " + _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 intent = _intent;
92 realActivity = _intent != null ? _intent.getComponent() : null;
93 origActivity = null;
94 } else {
95 ComponentName targetComponent = new ComponentName(
96 info.packageName, info.targetActivity);
97 if (_intent != null) {
98 Intent targetIntent = new Intent(_intent);
99 targetIntent.setComponent(targetComponent);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800100 targetIntent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700101 targetIntent.setSourceBounds(null);
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700102 if (ActivityManagerService.DEBUG_TASKS) Slog.v(ActivityManagerService.TAG,
103 "Setting Intent of " + this + " to target " + targetIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 intent = targetIntent;
105 realActivity = targetComponent;
106 origActivity = _intent.getComponent();
107 } else {
108 intent = null;
109 realActivity = targetComponent;
110 origActivity = new ComponentName(info.packageName, info.name);
111 }
112 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 if (intent != null &&
115 (intent.getFlags()&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
116 // Once we are set to an Intent with this flag, we count this
117 // task as having a true root activity.
118 rootWasReset = true;
119 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700120
121 if (info.applicationInfo != null) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700122 userId = UserHandle.getUserId(info.applicationInfo.uid);
Amith Yamasani742a6712011-05-04 14:49:28 -0700123 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800125
126 ActivityRecord getTopActivity() {
127 for (int i = mActivities.size() - 1; i >= 0; --i) {
128 final ActivityRecord r = mActivities.get(i);
129 if (r.finishing) {
130 continue;
131 }
132 return r;
133 }
134 return null;
135 }
136
Craig Mautnerde4ef022013-04-07 19:01:33 -0700137 /**
138 * Reorder the history stack so that the activity at the given index is
139 * brought to the front.
140 */
141 final void moveActivityToFrontLocked(ActivityRecord newTop) {
142 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + newTop
143 + " to stack at top", new RuntimeException("here").fillInStackTrace());
144
145 getTopActivity().frontOfTask = false;
146 mActivities.remove(newTop);
147 mActivities.add(newTop);
148 newTop.frontOfTask = true;
149 }
150
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800151 void addActivityAtBottom(ActivityRecord r) {
Craig Mautner77878772013-03-04 19:46:24 -0800152 addActivityAtIndex(0, r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800153 }
154
155 void addActivityToTop(ActivityRecord r) {
Craig Mautner6170f732013-04-02 13:05:23 -0700156 // 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 -0800157 if (!mActivities.remove(r) && r.fullscreen) {
158 // Was not previously in list.
159 numFullscreen++;
160 }
Craig Mautner9658b312013-02-28 10:55:59 -0800161 mActivities.add(r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800162 }
163
Craig Mautner77878772013-03-04 19:46:24 -0800164 void addActivityAtIndex(int index, ActivityRecord r) {
165 if (!mActivities.remove(r) && r.fullscreen) {
166 // Was not previously in list.
167 numFullscreen++;
168 }
169 mActivities.add(index, r);
170 }
171
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800172 /** @return true if this was the last activity in the task */
173 boolean removeActivity(ActivityRecord r) {
174 if (mActivities.remove(r) && r.fullscreen) {
175 // Was previously in list.
176 numFullscreen--;
177 }
178 return mActivities.size() == 0;
179 }
180
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700181 /**
182 * Completely remove all activities associated with an existing
183 * task starting at a specified index.
184 */
185 final void performClearTaskAtIndexLocked(int activityNdx) {
186 final ArrayList<ActivityRecord> activities = mActivities;
187 int numActivities = activities.size();
188 for ( ; activityNdx < numActivities; ++activityNdx) {
189 final ActivityRecord r = activities.get(activityNdx);
190 if (r.finishing) {
191 continue;
192 }
193 if (stack.finishActivityLocked(r, Activity.RESULT_CANCELED, null, "clear", false)) {
194 --activityNdx;
195 --numActivities;
196 }
197 }
198 }
199
200 /**
201 * Completely remove all activities associated with an existing task.
202 */
203 final void performClearTaskLocked() {
204 performClearTaskAtIndexLocked(0);
205 }
206
207 /**
208 * Perform clear operation as requested by
209 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the
210 * stack to the given task, then look for
211 * an instance of that activity in the stack and, if found, finish all
212 * activities on top of it and return the instance.
213 *
214 * @param newR Description of the new activity being started.
215 * @return Returns the old activity that should be continued to be used,
216 * or null if none was found.
217 */
218 final ActivityRecord performClearTaskLocked(ActivityRecord newR, int launchFlags) {
219 final ArrayList<ActivityRecord> activities = mActivities;
220 int numActivities = activities.size();
221 for (int activityNdx = numActivities - 1; activityNdx >= 0; --activityNdx) {
222 ActivityRecord r = activities.get(activityNdx);
223 if (r.finishing) {
224 continue;
225 }
226 if (r.realActivity.equals(newR.realActivity)) {
227 // Here it is! Now finish everything in front...
228 ActivityRecord ret = r;
229
230 for (++activityNdx; activityNdx < numActivities; ++activityNdx) {
231 r = activities.get(activityNdx);
232 if (r.finishing) {
233 continue;
234 }
235 ActivityOptions opts = r.takeOptionsLocked();
236 if (opts != null) {
237 ret.updateOptionsLocked(opts);
238 }
239 if (stack.finishActivityLocked(r, Activity.RESULT_CANCELED, null, "clear",
240 false)) {
241 --activityNdx;
242 --numActivities;
243 }
244 }
245
246 // Finally, if this is a normal launch mode (that is, not
247 // expecting onNewIntent()), then we will finish the current
248 // instance of the activity so a new fresh one can be started.
249 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
250 && (launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0) {
251 if (!ret.finishing) {
252 if (activities.contains(ret)) {
253 stack.finishActivityLocked(ret, Activity.RESULT_CANCELED, null,
254 "clear", false);
255 }
256 return null;
257 }
258 }
259
260 return ret;
261 }
262 }
263
264 return null;
265 }
266
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700267 public ActivityManager.TaskThumbnails getTaskThumbnailsLocked() {
268 TaskAccessInfo info = getTaskAccessInfoLocked(true);
269 final ActivityRecord resumedActivity = stack.mResumedActivity;
270 if (resumedActivity != null && resumedActivity.thumbHolder == this) {
271 info.mainThumbnail = stack.screenshotActivities(resumedActivity);
272 }
273 if (info.mainThumbnail == null) {
274 info.mainThumbnail = lastThumbnail;
275 }
276 return info;
277 }
278
279 public Bitmap getTaskTopThumbnailLocked() {
280 final ActivityRecord resumedActivity = stack.mResumedActivity;
281 if (resumedActivity != null && resumedActivity.task == this) {
282 // This task is the current resumed task, we just need to take
283 // a screenshot of it and return that.
284 return stack.screenshotActivities(resumedActivity);
285 }
286 // Return the information about the task, to figure out the top
287 // thumbnail to return.
288 TaskAccessInfo info = getTaskAccessInfoLocked(true);
289 if (info.numSubThumbbails <= 0) {
290 return info.mainThumbnail != null ? info.mainThumbnail : lastThumbnail;
291 }
292 return info.subtasks.get(info.numSubThumbbails-1).holder.lastThumbnail;
293 }
294
295 public ActivityRecord removeTaskActivitiesLocked(int subTaskIndex,
296 boolean taskRequired) {
297 TaskAccessInfo info = getTaskAccessInfoLocked(false);
298 if (info.root == null) {
299 if (taskRequired) {
300 Slog.w(TAG, "removeTaskLocked: unknown taskId " + taskId);
301 }
302 return null;
303 }
304
305 if (subTaskIndex < 0) {
306 // Just remove the entire task.
307 performClearTaskAtIndexLocked(info.rootIndex);
308 return info.root;
309 }
310
311 if (subTaskIndex >= info.subtasks.size()) {
312 if (taskRequired) {
313 Slog.w(TAG, "removeTaskLocked: unknown subTaskIndex " + subTaskIndex);
314 }
315 return null;
316 }
317
318 // Remove all of this task's activities starting at the sub task.
319 TaskAccessInfo.SubTask subtask = info.subtasks.get(subTaskIndex);
320 performClearTaskAtIndexLocked(subtask.index);
321 return subtask.activity;
322 }
323
324 public TaskAccessInfo getTaskAccessInfoLocked(boolean inclThumbs) {
325 final TaskAccessInfo thumbs = new TaskAccessInfo();
326 // How many different sub-thumbnails?
327 final int NA = mActivities.size();
328 int j = 0;
329 ThumbnailHolder holder = null;
330 while (j < NA) {
331 ActivityRecord ar = mActivities.get(j);
332 if (!ar.finishing) {
333 thumbs.root = ar;
334 thumbs.rootIndex = j;
335 holder = ar.thumbHolder;
336 if (holder != null) {
337 thumbs.mainThumbnail = holder.lastThumbnail;
338 }
339 j++;
340 break;
341 }
342 j++;
343 }
344
345 if (j >= NA) {
346 return thumbs;
347 }
348
349 ArrayList<TaskAccessInfo.SubTask> subtasks = new ArrayList<TaskAccessInfo.SubTask>();
350 thumbs.subtasks = subtasks;
351 while (j < NA) {
352 ActivityRecord ar = mActivities.get(j);
353 j++;
354 if (ar.finishing) {
355 continue;
356 }
357 if (ar.thumbHolder != holder && holder != null) {
358 thumbs.numSubThumbbails++;
359 holder = ar.thumbHolder;
360 TaskAccessInfo.SubTask sub = new TaskAccessInfo.SubTask();
361 sub.holder = holder;
362 sub.activity = ar;
363 sub.index = j-1;
364 subtasks.add(sub);
365 }
366 }
367 if (thumbs.numSubThumbbails > 0) {
368 thumbs.retriever = new IThumbnailRetriever.Stub() {
369 @Override
370 public Bitmap getThumbnail(int index) {
371 if (index < 0 || index >= thumbs.subtasks.size()) {
372 return null;
373 }
374 TaskAccessInfo.SubTask sub = thumbs.subtasks.get(index);
375 ActivityRecord resumedActivity = stack.mResumedActivity;
376 if (resumedActivity != null && resumedActivity.thumbHolder == sub.holder) {
377 return stack.screenshotActivities(resumedActivity);
378 }
379 return sub.holder.lastThumbnail;
380 }
381 };
382 }
383 return thumbs;
384 }
385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700387 if (numActivities != 0 || rootWasReset || userId != 0) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800388 pw.print(prefix); pw.print("numActivities="); pw.print(numActivities);
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700389 pw.print(" rootWasReset="); pw.print(rootWasReset);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800390 pw.print(" userId="); pw.print(userId);
391 pw.print(" numFullscreen="); pw.println(numFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700392 }
393 if (affinity != null) {
394 pw.print(prefix); pw.print("affinity="); pw.println(affinity);
395 }
396 if (intent != null) {
397 StringBuilder sb = new StringBuilder(128);
398 sb.append(prefix); sb.append("intent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800399 intent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700400 sb.append('}');
401 pw.println(sb.toString());
402 }
403 if (affinityIntent != null) {
404 StringBuilder sb = new StringBuilder(128);
405 sb.append(prefix); sb.append("affinityIntent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800406 affinityIntent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700407 sb.append('}');
408 pw.println(sb.toString());
409 }
410 if (origActivity != null) {
411 pw.print(prefix); pw.print("origActivity=");
412 pw.println(origActivity.flattenToShortString());
413 }
414 if (realActivity != null) {
415 pw.print(prefix); pw.print("realActivity=");
416 pw.println(realActivity.flattenToShortString());
417 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800418 pw.print(prefix); pw.print("Activities="); pw.println(mActivities);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700419 if (!askedCompatMode) {
420 pw.print(prefix); pw.print("askedCompatMode="); pw.println(askedCompatMode);
421 }
Dianne Hackborncfb9f2b2011-08-24 10:51:49 -0700422 pw.print(prefix); pw.print("lastThumbnail="); pw.print(lastThumbnail);
423 pw.print(" lastDescription="); pw.println(lastDescription);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700424 pw.print(prefix); pw.print("lastActiveTime="); pw.print(lastActiveTime);
425 pw.print(" (inactive for ");
426 pw.print((getInactiveDuration()/1000)); pw.println("s)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 }
428
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800429 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700431 StringBuilder sb = new StringBuilder(128);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700432 if (stringName != null) {
433 sb.append(stringName);
434 sb.append(" U=");
435 sb.append(userId);
436 sb.append(" sz=");
437 sb.append(mActivities.size());
438 sb.append('}');
439 return sb.toString();
440 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700441 sb.append("TaskRecord{");
442 sb.append(Integer.toHexString(System.identityHashCode(this)));
443 sb.append(" #");
444 sb.append(taskId);
445 if (affinity != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800446 sb.append(" A=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700447 sb.append(affinity);
448 } else if (intent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800449 sb.append(" I=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700450 sb.append(intent.getComponent().flattenToShortString());
451 } else if (affinityIntent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800452 sb.append(" aI=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700453 sb.append(affinityIntent.getComponent().flattenToShortString());
454 } else {
455 sb.append(" ??");
456 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700457 stringName = sb.toString();
458 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 }
460}