blob: 45bd6d5e7514f892c60a67dca5f694657938ae6b [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;
23import android.app.ActivityOptions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.ComponentName;
25import android.content.Intent;
26import android.content.pm.ActivityInfo;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070027import android.os.UserHandle;
Dianne Hackborn7f96b792012-05-29 18:46:45 -070028import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
30import java.io.PrintWriter;
Craig Mautner5d9c7be2013-02-15 14:02:56 -080031import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
Dianne Hackbornf26fd992011-04-08 18:14:09 -070033class TaskRecord extends ThumbnailHolder {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034 final int taskId; // Unique identifier for this task.
35 final String affinity; // The affinity name for this task, or null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036 Intent intent; // The original intent that started the task.
37 Intent affinityIntent; // Intent of affinity-moved activity that started this task.
38 ComponentName origActivity; // The non-alias activity component of the intent.
39 ComponentName realActivity; // The actual activity component that started the task.
40 int numActivities; // Current number of activities in this task.
41 long lastActiveTime; // Last time this task was active, including sleep.
42 boolean rootWasReset; // True if the intent at the root of the task had
43 // the FLAG_ACTIVITY_RESET_TASK_IF_NEEDED flag.
Dianne Hackborn36cd41f2011-05-25 21:00:46 -070044 boolean askedCompatMode;// Have asked the user about compat mode for this task.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
Dianne Hackborn1d442e02009-04-20 18:14:05 -070046 String stringName; // caching of toString() result.
Dianne Hackborn9da2d402012-03-15 13:43:08 -070047 int userId; // user for which this task was created
Craig Mautner5d9c7be2013-02-15 14:02:56 -080048
49 int numFullscreen; // Number of fullscreen activities.
50
Craig Mautnerd2328952013-03-05 12:46:26 -080051 /** List of all activities in the task arranged in history order */
Craig Mautner5d9c7be2013-02-15 14:02:56 -080052 final ArrayList<ActivityRecord> mActivities = new ArrayList<ActivityRecord>();
53
Craig Mautnerd2328952013-03-05 12:46:26 -080054 /** Current stack */
55 ActivityStack stack;
56
57 TaskRecord(int _taskId, ActivityInfo info, Intent _intent, ActivityStack _stack) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 taskId = _taskId;
59 affinity = info.taskAffinity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 setIntent(_intent, info);
Craig Mautnerd2328952013-03-05 12:46:26 -080061 stack = _stack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 }
63
64 void touchActiveTime() {
65 lastActiveTime = android.os.SystemClock.elapsedRealtime();
66 }
67
68 long getInactiveDuration() {
69 return android.os.SystemClock.elapsedRealtime() - lastActiveTime;
70 }
71
72 void setIntent(Intent _intent, ActivityInfo info) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070073 stringName = null;
Dianne Hackbornf5b86712011-12-05 17:42:41 -080074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 if (info.targetActivity == null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -080076 if (_intent != null) {
77 // If this Intent has a selector, we want to clear it for the
78 // recent task since it is not relevant if the user later wants
79 // to re-launch the app.
Dianne Hackbornd367ca82012-05-07 15:49:39 -070080 if (_intent.getSelector() != null || _intent.getSourceBounds() != null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -080081 _intent = new Intent(_intent);
82 _intent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -070083 _intent.setSourceBounds(null);
Dianne Hackbornf5b86712011-12-05 17:42:41 -080084 }
85 }
Dianne Hackborn7f96b792012-05-29 18:46:45 -070086 if (ActivityManagerService.DEBUG_TASKS) Slog.v(ActivityManagerService.TAG,
87 "Setting Intent of " + this + " to " + _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 intent = _intent;
89 realActivity = _intent != null ? _intent.getComponent() : null;
90 origActivity = null;
91 } else {
92 ComponentName targetComponent = new ComponentName(
93 info.packageName, info.targetActivity);
94 if (_intent != null) {
95 Intent targetIntent = new Intent(_intent);
96 targetIntent.setComponent(targetComponent);
Dianne Hackbornf5b86712011-12-05 17:42:41 -080097 targetIntent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -070098 targetIntent.setSourceBounds(null);
Dianne Hackborn7f96b792012-05-29 18:46:45 -070099 if (ActivityManagerService.DEBUG_TASKS) Slog.v(ActivityManagerService.TAG,
100 "Setting Intent of " + this + " to target " + targetIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 intent = targetIntent;
102 realActivity = targetComponent;
103 origActivity = _intent.getComponent();
104 } else {
105 intent = null;
106 realActivity = targetComponent;
107 origActivity = new ComponentName(info.packageName, info.name);
108 }
109 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 if (intent != null &&
112 (intent.getFlags()&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
113 // Once we are set to an Intent with this flag, we count this
114 // task as having a true root activity.
115 rootWasReset = true;
116 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700117
118 if (info.applicationInfo != null) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700119 userId = UserHandle.getUserId(info.applicationInfo.uid);
Amith Yamasani742a6712011-05-04 14:49:28 -0700120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800122
123 ActivityRecord getTopActivity() {
124 for (int i = mActivities.size() - 1; i >= 0; --i) {
125 final ActivityRecord r = mActivities.get(i);
126 if (r.finishing) {
127 continue;
128 }
129 return r;
130 }
131 return null;
132 }
133
Craig Mautnerde4ef022013-04-07 19:01:33 -0700134 /**
135 * Reorder the history stack so that the activity at the given index is
136 * brought to the front.
137 */
138 final void moveActivityToFrontLocked(ActivityRecord newTop) {
139 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + newTop
140 + " to stack at top", new RuntimeException("here").fillInStackTrace());
141
142 getTopActivity().frontOfTask = false;
143 mActivities.remove(newTop);
144 mActivities.add(newTop);
145 newTop.frontOfTask = true;
146 }
147
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800148 void addActivityAtBottom(ActivityRecord r) {
Craig Mautner77878772013-03-04 19:46:24 -0800149 addActivityAtIndex(0, r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800150 }
151
152 void addActivityToTop(ActivityRecord r) {
Craig Mautner6170f732013-04-02 13:05:23 -0700153 // 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 -0800154 if (!mActivities.remove(r) && r.fullscreen) {
155 // Was not previously in list.
156 numFullscreen++;
157 }
Craig Mautner9658b312013-02-28 10:55:59 -0800158 mActivities.add(r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800159 }
160
Craig Mautner77878772013-03-04 19:46:24 -0800161 void addActivityAtIndex(int index, ActivityRecord r) {
162 if (!mActivities.remove(r) && r.fullscreen) {
163 // Was not previously in list.
164 numFullscreen++;
165 }
166 mActivities.add(index, r);
167 }
168
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800169 /** @return true if this was the last activity in the task */
170 boolean removeActivity(ActivityRecord r) {
171 if (mActivities.remove(r) && r.fullscreen) {
172 // Was previously in list.
173 numFullscreen--;
174 }
175 return mActivities.size() == 0;
176 }
177
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700178 /**
179 * Completely remove all activities associated with an existing
180 * task starting at a specified index.
181 */
182 final void performClearTaskAtIndexLocked(int activityNdx) {
183 final ArrayList<ActivityRecord> activities = mActivities;
184 int numActivities = activities.size();
185 for ( ; activityNdx < numActivities; ++activityNdx) {
186 final ActivityRecord r = activities.get(activityNdx);
187 if (r.finishing) {
188 continue;
189 }
190 if (stack.finishActivityLocked(r, Activity.RESULT_CANCELED, null, "clear", false)) {
191 --activityNdx;
192 --numActivities;
193 }
194 }
195 }
196
197 /**
198 * Completely remove all activities associated with an existing task.
199 */
200 final void performClearTaskLocked() {
201 performClearTaskAtIndexLocked(0);
202 }
203
204 /**
205 * Perform clear operation as requested by
206 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the
207 * stack to the given task, then look for
208 * an instance of that activity in the stack and, if found, finish all
209 * activities on top of it and return the instance.
210 *
211 * @param newR Description of the new activity being started.
212 * @return Returns the old activity that should be continued to be used,
213 * or null if none was found.
214 */
215 final ActivityRecord performClearTaskLocked(ActivityRecord newR, int launchFlags) {
216 final ArrayList<ActivityRecord> activities = mActivities;
217 int numActivities = activities.size();
218 for (int activityNdx = numActivities - 1; activityNdx >= 0; --activityNdx) {
219 ActivityRecord r = activities.get(activityNdx);
220 if (r.finishing) {
221 continue;
222 }
223 if (r.realActivity.equals(newR.realActivity)) {
224 // Here it is! Now finish everything in front...
225 ActivityRecord ret = r;
226
227 for (++activityNdx; activityNdx < numActivities; ++activityNdx) {
228 r = activities.get(activityNdx);
229 if (r.finishing) {
230 continue;
231 }
232 ActivityOptions opts = r.takeOptionsLocked();
233 if (opts != null) {
234 ret.updateOptionsLocked(opts);
235 }
236 if (stack.finishActivityLocked(r, Activity.RESULT_CANCELED, null, "clear",
237 false)) {
238 --activityNdx;
239 --numActivities;
240 }
241 }
242
243 // Finally, if this is a normal launch mode (that is, not
244 // expecting onNewIntent()), then we will finish the current
245 // instance of the activity so a new fresh one can be started.
246 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
247 && (launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0) {
248 if (!ret.finishing) {
249 if (activities.contains(ret)) {
250 stack.finishActivityLocked(ret, Activity.RESULT_CANCELED, null,
251 "clear", false);
252 }
253 return null;
254 }
255 }
256
257 return ret;
258 }
259 }
260
261 return null;
262 }
263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700265 if (numActivities != 0 || rootWasReset || userId != 0) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800266 pw.print(prefix); pw.print("numActivities="); pw.print(numActivities);
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700267 pw.print(" rootWasReset="); pw.print(rootWasReset);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800268 pw.print(" userId="); pw.print(userId);
269 pw.print(" numFullscreen="); pw.println(numFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700270 }
271 if (affinity != null) {
272 pw.print(prefix); pw.print("affinity="); pw.println(affinity);
273 }
274 if (intent != null) {
275 StringBuilder sb = new StringBuilder(128);
276 sb.append(prefix); sb.append("intent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800277 intent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700278 sb.append('}');
279 pw.println(sb.toString());
280 }
281 if (affinityIntent != null) {
282 StringBuilder sb = new StringBuilder(128);
283 sb.append(prefix); sb.append("affinityIntent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800284 affinityIntent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700285 sb.append('}');
286 pw.println(sb.toString());
287 }
288 if (origActivity != null) {
289 pw.print(prefix); pw.print("origActivity=");
290 pw.println(origActivity.flattenToShortString());
291 }
292 if (realActivity != null) {
293 pw.print(prefix); pw.print("realActivity=");
294 pw.println(realActivity.flattenToShortString());
295 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800296 pw.print(prefix); pw.print("Activities="); pw.println(mActivities);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700297 if (!askedCompatMode) {
298 pw.print(prefix); pw.print("askedCompatMode="); pw.println(askedCompatMode);
299 }
Dianne Hackborncfb9f2b2011-08-24 10:51:49 -0700300 pw.print(prefix); pw.print("lastThumbnail="); pw.print(lastThumbnail);
301 pw.print(" lastDescription="); pw.println(lastDescription);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700302 pw.print(prefix); pw.print("lastActiveTime="); pw.print(lastActiveTime);
303 pw.print(" (inactive for ");
304 pw.print((getInactiveDuration()/1000)); pw.println("s)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 }
306
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800307 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700309 StringBuilder sb = new StringBuilder(128);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700310 if (stringName != null) {
311 sb.append(stringName);
312 sb.append(" U=");
313 sb.append(userId);
314 sb.append(" sz=");
315 sb.append(mActivities.size());
316 sb.append('}');
317 return sb.toString();
318 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700319 sb.append("TaskRecord{");
320 sb.append(Integer.toHexString(System.identityHashCode(this)));
321 sb.append(" #");
322 sb.append(taskId);
323 if (affinity != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800324 sb.append(" A=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700325 sb.append(affinity);
326 } else if (intent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800327 sb.append(" I=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700328 sb.append(intent.getComponent().flattenToShortString());
329 } else if (affinityIntent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800330 sb.append(" aI=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700331 sb.append(affinityIntent.getComponent().flattenToShortString());
332 } else {
333 sb.append(" ??");
334 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700335 stringName = sb.toString();
336 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 }
338}