blob: 94da7e5d1ef4ca109dddf1b413e7136c9aca89cb [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 Mautnerb0f7dc72013-04-01 16:34:45 -070019import android.app.Activity;
20import android.app.ActivityOptions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.ComponentName;
22import android.content.Intent;
23import android.content.pm.ActivityInfo;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070024import android.os.UserHandle;
Dianne Hackborn7f96b792012-05-29 18:46:45 -070025import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
27import java.io.PrintWriter;
Craig Mautner5d9c7be2013-02-15 14:02:56 -080028import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
Dianne Hackbornf26fd992011-04-08 18:14:09 -070030class TaskRecord extends ThumbnailHolder {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031 final int taskId; // Unique identifier for this task.
32 final String affinity; // The affinity name for this task, or null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033 Intent intent; // The original intent that started the task.
34 Intent affinityIntent; // Intent of affinity-moved activity that started this task.
35 ComponentName origActivity; // The non-alias activity component of the intent.
36 ComponentName realActivity; // The actual activity component that started the task.
37 int numActivities; // Current number of activities in this task.
38 long lastActiveTime; // Last time this task was active, including sleep.
39 boolean rootWasReset; // True if the intent at the root of the task had
40 // the FLAG_ACTIVITY_RESET_TASK_IF_NEEDED flag.
Dianne Hackborn36cd41f2011-05-25 21:00:46 -070041 boolean askedCompatMode;// Have asked the user about compat mode for this task.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
Dianne Hackborn1d442e02009-04-20 18:14:05 -070043 String stringName; // caching of toString() result.
Dianne Hackborn9da2d402012-03-15 13:43:08 -070044 int userId; // user for which this task was created
Craig Mautner5d9c7be2013-02-15 14:02:56 -080045
46 int numFullscreen; // Number of fullscreen activities.
47
Craig Mautnerd2328952013-03-05 12:46:26 -080048 /** List of all activities in the task arranged in history order */
Craig Mautner5d9c7be2013-02-15 14:02:56 -080049 final ArrayList<ActivityRecord> mActivities = new ArrayList<ActivityRecord>();
50
Craig Mautnerd2328952013-03-05 12:46:26 -080051 /** Current stack */
52 ActivityStack stack;
53
54 TaskRecord(int _taskId, ActivityInfo info, Intent _intent, ActivityStack _stack) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 taskId = _taskId;
56 affinity = info.taskAffinity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 setIntent(_intent, info);
Craig Mautnerd2328952013-03-05 12:46:26 -080058 stack = _stack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 }
60
61 void touchActiveTime() {
62 lastActiveTime = android.os.SystemClock.elapsedRealtime();
63 }
64
65 long getInactiveDuration() {
66 return android.os.SystemClock.elapsedRealtime() - lastActiveTime;
67 }
68
69 void setIntent(Intent _intent, ActivityInfo info) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070070 stringName = null;
Dianne Hackbornf5b86712011-12-05 17:42:41 -080071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 if (info.targetActivity == null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -080073 if (_intent != null) {
74 // If this Intent has a selector, we want to clear it for the
75 // recent task since it is not relevant if the user later wants
76 // to re-launch the app.
Dianne Hackbornd367ca82012-05-07 15:49:39 -070077 if (_intent.getSelector() != null || _intent.getSourceBounds() != null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -080078 _intent = new Intent(_intent);
79 _intent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -070080 _intent.setSourceBounds(null);
Dianne Hackbornf5b86712011-12-05 17:42:41 -080081 }
82 }
Dianne Hackborn7f96b792012-05-29 18:46:45 -070083 if (ActivityManagerService.DEBUG_TASKS) Slog.v(ActivityManagerService.TAG,
84 "Setting Intent of " + this + " to " + _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 intent = _intent;
86 realActivity = _intent != null ? _intent.getComponent() : null;
87 origActivity = null;
88 } else {
89 ComponentName targetComponent = new ComponentName(
90 info.packageName, info.targetActivity);
91 if (_intent != null) {
92 Intent targetIntent = new Intent(_intent);
93 targetIntent.setComponent(targetComponent);
Dianne Hackbornf5b86712011-12-05 17:42:41 -080094 targetIntent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -070095 targetIntent.setSourceBounds(null);
Dianne Hackborn7f96b792012-05-29 18:46:45 -070096 if (ActivityManagerService.DEBUG_TASKS) Slog.v(ActivityManagerService.TAG,
97 "Setting Intent of " + this + " to target " + targetIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 intent = targetIntent;
99 realActivity = targetComponent;
100 origActivity = _intent.getComponent();
101 } else {
102 intent = null;
103 realActivity = targetComponent;
104 origActivity = new ComponentName(info.packageName, info.name);
105 }
106 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 if (intent != null &&
109 (intent.getFlags()&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
110 // Once we are set to an Intent with this flag, we count this
111 // task as having a true root activity.
112 rootWasReset = true;
113 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700114
115 if (info.applicationInfo != null) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700116 userId = UserHandle.getUserId(info.applicationInfo.uid);
Amith Yamasani742a6712011-05-04 14:49:28 -0700117 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800119
120 ActivityRecord getTopActivity() {
121 for (int i = mActivities.size() - 1; i >= 0; --i) {
122 final ActivityRecord r = mActivities.get(i);
123 if (r.finishing) {
124 continue;
125 }
126 return r;
127 }
128 return null;
129 }
130
131 void addActivityAtBottom(ActivityRecord r) {
Craig Mautner77878772013-03-04 19:46:24 -0800132 addActivityAtIndex(0, r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800133 }
134
135 void addActivityToTop(ActivityRecord r) {
Craig Mautner6170f732013-04-02 13:05:23 -0700136 // 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 -0800137 if (!mActivities.remove(r) && r.fullscreen) {
138 // Was not previously in list.
139 numFullscreen++;
140 }
Craig Mautner9658b312013-02-28 10:55:59 -0800141 mActivities.add(r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800142 }
143
Craig Mautner77878772013-03-04 19:46:24 -0800144 void addActivityAtIndex(int index, ActivityRecord r) {
145 if (!mActivities.remove(r) && r.fullscreen) {
146 // Was not previously in list.
147 numFullscreen++;
148 }
149 mActivities.add(index, r);
150 }
151
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800152 /** @return true if this was the last activity in the task */
153 boolean removeActivity(ActivityRecord r) {
154 if (mActivities.remove(r) && r.fullscreen) {
155 // Was previously in list.
156 numFullscreen--;
157 }
158 return mActivities.size() == 0;
159 }
160
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700161 /**
162 * Completely remove all activities associated with an existing
163 * task starting at a specified index.
164 */
165 final void performClearTaskAtIndexLocked(int activityNdx) {
166 final ArrayList<ActivityRecord> activities = mActivities;
167 int numActivities = activities.size();
168 for ( ; activityNdx < numActivities; ++activityNdx) {
169 final ActivityRecord r = activities.get(activityNdx);
170 if (r.finishing) {
171 continue;
172 }
173 if (stack.finishActivityLocked(r, Activity.RESULT_CANCELED, null, "clear", false)) {
174 --activityNdx;
175 --numActivities;
176 }
177 }
178 }
179
180 /**
181 * Completely remove all activities associated with an existing task.
182 */
183 final void performClearTaskLocked() {
184 performClearTaskAtIndexLocked(0);
185 }
186
187 /**
188 * Perform clear operation as requested by
189 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the
190 * stack to the given task, then look for
191 * an instance of that activity in the stack and, if found, finish all
192 * activities on top of it and return the instance.
193 *
194 * @param newR Description of the new activity being started.
195 * @return Returns the old activity that should be continued to be used,
196 * or null if none was found.
197 */
198 final ActivityRecord performClearTaskLocked(ActivityRecord newR, int launchFlags) {
199 final ArrayList<ActivityRecord> activities = mActivities;
200 int numActivities = activities.size();
201 for (int activityNdx = numActivities - 1; activityNdx >= 0; --activityNdx) {
202 ActivityRecord r = activities.get(activityNdx);
203 if (r.finishing) {
204 continue;
205 }
206 if (r.realActivity.equals(newR.realActivity)) {
207 // Here it is! Now finish everything in front...
208 ActivityRecord ret = r;
209
210 for (++activityNdx; activityNdx < numActivities; ++activityNdx) {
211 r = activities.get(activityNdx);
212 if (r.finishing) {
213 continue;
214 }
215 ActivityOptions opts = r.takeOptionsLocked();
216 if (opts != null) {
217 ret.updateOptionsLocked(opts);
218 }
219 if (stack.finishActivityLocked(r, Activity.RESULT_CANCELED, null, "clear",
220 false)) {
221 --activityNdx;
222 --numActivities;
223 }
224 }
225
226 // Finally, if this is a normal launch mode (that is, not
227 // expecting onNewIntent()), then we will finish the current
228 // instance of the activity so a new fresh one can be started.
229 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
230 && (launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0) {
231 if (!ret.finishing) {
232 if (activities.contains(ret)) {
233 stack.finishActivityLocked(ret, Activity.RESULT_CANCELED, null,
234 "clear", false);
235 }
236 return null;
237 }
238 }
239
240 return ret;
241 }
242 }
243
244 return null;
245 }
246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700248 if (numActivities != 0 || rootWasReset || userId != 0) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800249 pw.print(prefix); pw.print("numActivities="); pw.print(numActivities);
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700250 pw.print(" rootWasReset="); pw.print(rootWasReset);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800251 pw.print(" userId="); pw.print(userId);
252 pw.print(" numFullscreen="); pw.println(numFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700253 }
254 if (affinity != null) {
255 pw.print(prefix); pw.print("affinity="); pw.println(affinity);
256 }
257 if (intent != null) {
258 StringBuilder sb = new StringBuilder(128);
259 sb.append(prefix); sb.append("intent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800260 intent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700261 sb.append('}');
262 pw.println(sb.toString());
263 }
264 if (affinityIntent != null) {
265 StringBuilder sb = new StringBuilder(128);
266 sb.append(prefix); sb.append("affinityIntent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800267 affinityIntent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700268 sb.append('}');
269 pw.println(sb.toString());
270 }
271 if (origActivity != null) {
272 pw.print(prefix); pw.print("origActivity=");
273 pw.println(origActivity.flattenToShortString());
274 }
275 if (realActivity != null) {
276 pw.print(prefix); pw.print("realActivity=");
277 pw.println(realActivity.flattenToShortString());
278 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800279 pw.print(prefix); pw.print("Activities="); pw.println(mActivities);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700280 if (!askedCompatMode) {
281 pw.print(prefix); pw.print("askedCompatMode="); pw.println(askedCompatMode);
282 }
Dianne Hackborncfb9f2b2011-08-24 10:51:49 -0700283 pw.print(prefix); pw.print("lastThumbnail="); pw.print(lastThumbnail);
284 pw.print(" lastDescription="); pw.println(lastDescription);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700285 pw.print(prefix); pw.print("lastActiveTime="); pw.print(lastActiveTime);
286 pw.print(" (inactive for ");
287 pw.print((getInactiveDuration()/1000)); pw.println("s)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 }
289
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800290 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700292 if (stringName != null) {
293 return stringName;
294 }
295 StringBuilder sb = new StringBuilder(128);
296 sb.append("TaskRecord{");
297 sb.append(Integer.toHexString(System.identityHashCode(this)));
298 sb.append(" #");
299 sb.append(taskId);
300 if (affinity != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800301 sb.append(" A=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700302 sb.append(affinity);
303 } else if (intent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800304 sb.append(" I=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700305 sb.append(intent.getComponent().flattenToShortString());
306 } else if (affinityIntent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800307 sb.append(" aI=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700308 sb.append(affinityIntent.getComponent().flattenToShortString());
309 } else {
310 sb.append(" ??");
311 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800312 sb.append(" U=");
Amith Yamasani742a6712011-05-04 14:49:28 -0700313 sb.append(userId);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800314 sb.append(" sz=");
315 sb.append(mActivities.size());
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700316 sb.append('}');
317 return stringName = sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 }
319}