blob: 9833d31181636bb4d3be114ffcb94b644c7eed33 [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
19import android.content.ComponentName;
20import android.content.Intent;
21import android.content.pm.ActivityInfo;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070022import android.os.UserHandle;
Dianne Hackborn7f96b792012-05-29 18:46:45 -070023import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25import java.io.PrintWriter;
Craig Mautner5d9c7be2013-02-15 14:02:56 -080026import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
Dianne Hackbornf26fd992011-04-08 18:14:09 -070028class TaskRecord extends ThumbnailHolder {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029 final int taskId; // Unique identifier for this task.
30 final String affinity; // The affinity name for this task, or null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031 Intent intent; // The original intent that started the task.
32 Intent affinityIntent; // Intent of affinity-moved activity that started this task.
33 ComponentName origActivity; // The non-alias activity component of the intent.
34 ComponentName realActivity; // The actual activity component that started the task.
35 int numActivities; // Current number of activities in this task.
36 long lastActiveTime; // Last time this task was active, including sleep.
37 boolean rootWasReset; // True if the intent at the root of the task had
38 // the FLAG_ACTIVITY_RESET_TASK_IF_NEEDED flag.
Dianne Hackborn36cd41f2011-05-25 21:00:46 -070039 boolean askedCompatMode;// Have asked the user about compat mode for this task.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
Dianne Hackborn1d442e02009-04-20 18:14:05 -070041 String stringName; // caching of toString() result.
Dianne Hackborn9da2d402012-03-15 13:43:08 -070042 int userId; // user for which this task was created
Craig Mautner5d9c7be2013-02-15 14:02:56 -080043
44 int numFullscreen; // Number of fullscreen activities.
45
Craig Mautnerd2328952013-03-05 12:46:26 -080046 /** List of all activities in the task arranged in history order */
Craig Mautner5d9c7be2013-02-15 14:02:56 -080047 final ArrayList<ActivityRecord> mActivities = new ArrayList<ActivityRecord>();
48
Craig Mautnerd2328952013-03-05 12:46:26 -080049 /** Current stack */
50 ActivityStack stack;
51
52 TaskRecord(int _taskId, ActivityInfo info, Intent _intent, ActivityStack _stack) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 taskId = _taskId;
54 affinity = info.taskAffinity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 setIntent(_intent, info);
Craig Mautnerd2328952013-03-05 12:46:26 -080056 stack = _stack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 }
58
59 void touchActiveTime() {
60 lastActiveTime = android.os.SystemClock.elapsedRealtime();
61 }
62
63 long getInactiveDuration() {
64 return android.os.SystemClock.elapsedRealtime() - lastActiveTime;
65 }
66
67 void setIntent(Intent _intent, ActivityInfo info) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070068 stringName = null;
Dianne Hackbornf5b86712011-12-05 17:42:41 -080069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 if (info.targetActivity == null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -080071 if (_intent != null) {
72 // If this Intent has a selector, we want to clear it for the
73 // recent task since it is not relevant if the user later wants
74 // to re-launch the app.
Dianne Hackbornd367ca82012-05-07 15:49:39 -070075 if (_intent.getSelector() != null || _intent.getSourceBounds() != null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -080076 _intent = new Intent(_intent);
77 _intent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -070078 _intent.setSourceBounds(null);
Dianne Hackbornf5b86712011-12-05 17:42:41 -080079 }
80 }
Dianne Hackborn7f96b792012-05-29 18:46:45 -070081 if (ActivityManagerService.DEBUG_TASKS) Slog.v(ActivityManagerService.TAG,
82 "Setting Intent of " + this + " to " + _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 intent = _intent;
84 realActivity = _intent != null ? _intent.getComponent() : null;
85 origActivity = null;
86 } else {
87 ComponentName targetComponent = new ComponentName(
88 info.packageName, info.targetActivity);
89 if (_intent != null) {
90 Intent targetIntent = new Intent(_intent);
91 targetIntent.setComponent(targetComponent);
Dianne Hackbornf5b86712011-12-05 17:42:41 -080092 targetIntent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -070093 targetIntent.setSourceBounds(null);
Dianne Hackborn7f96b792012-05-29 18:46:45 -070094 if (ActivityManagerService.DEBUG_TASKS) Slog.v(ActivityManagerService.TAG,
95 "Setting Intent of " + this + " to target " + targetIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 intent = targetIntent;
97 realActivity = targetComponent;
98 origActivity = _intent.getComponent();
99 } else {
100 intent = null;
101 realActivity = targetComponent;
102 origActivity = new ComponentName(info.packageName, info.name);
103 }
104 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 if (intent != null &&
107 (intent.getFlags()&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
108 // Once we are set to an Intent with this flag, we count this
109 // task as having a true root activity.
110 rootWasReset = true;
111 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700112
113 if (info.applicationInfo != null) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700114 userId = UserHandle.getUserId(info.applicationInfo.uid);
Amith Yamasani742a6712011-05-04 14:49:28 -0700115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800117
118 ActivityRecord getTopActivity() {
119 for (int i = mActivities.size() - 1; i >= 0; --i) {
120 final ActivityRecord r = mActivities.get(i);
121 if (r.finishing) {
122 continue;
123 }
124 return r;
125 }
126 return null;
127 }
128
129 void addActivityAtBottom(ActivityRecord r) {
Craig Mautner77878772013-03-04 19:46:24 -0800130 addActivityAtIndex(0, r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800131 }
132
133 void addActivityToTop(ActivityRecord r) {
134 if (!mActivities.remove(r) && r.fullscreen) {
135 // Was not previously in list.
136 numFullscreen++;
137 }
Craig Mautner9658b312013-02-28 10:55:59 -0800138 mActivities.add(r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800139 }
140
Craig Mautner77878772013-03-04 19:46:24 -0800141 void addActivityAtIndex(int index, ActivityRecord r) {
142 if (!mActivities.remove(r) && r.fullscreen) {
143 // Was not previously in list.
144 numFullscreen++;
145 }
146 mActivities.add(index, r);
147 }
148
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800149 /** @return true if this was the last activity in the task */
150 boolean removeActivity(ActivityRecord r) {
151 if (mActivities.remove(r) && r.fullscreen) {
152 // Was previously in list.
153 numFullscreen--;
154 }
155 return mActivities.size() == 0;
156 }
157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700159 if (numActivities != 0 || rootWasReset || userId != 0) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800160 pw.print(prefix); pw.print("numActivities="); pw.print(numActivities);
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700161 pw.print(" rootWasReset="); pw.print(rootWasReset);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800162 pw.print(" userId="); pw.print(userId);
163 pw.print(" numFullscreen="); pw.println(numFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700164 }
165 if (affinity != null) {
166 pw.print(prefix); pw.print("affinity="); pw.println(affinity);
167 }
168 if (intent != null) {
169 StringBuilder sb = new StringBuilder(128);
170 sb.append(prefix); sb.append("intent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800171 intent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700172 sb.append('}');
173 pw.println(sb.toString());
174 }
175 if (affinityIntent != null) {
176 StringBuilder sb = new StringBuilder(128);
177 sb.append(prefix); sb.append("affinityIntent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800178 affinityIntent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700179 sb.append('}');
180 pw.println(sb.toString());
181 }
182 if (origActivity != null) {
183 pw.print(prefix); pw.print("origActivity=");
184 pw.println(origActivity.flattenToShortString());
185 }
186 if (realActivity != null) {
187 pw.print(prefix); pw.print("realActivity=");
188 pw.println(realActivity.flattenToShortString());
189 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800190 pw.print(prefix); pw.print("Activities="); pw.println(mActivities);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700191 if (!askedCompatMode) {
192 pw.print(prefix); pw.print("askedCompatMode="); pw.println(askedCompatMode);
193 }
Dianne Hackborncfb9f2b2011-08-24 10:51:49 -0700194 pw.print(prefix); pw.print("lastThumbnail="); pw.print(lastThumbnail);
195 pw.print(" lastDescription="); pw.println(lastDescription);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700196 pw.print(prefix); pw.print("lastActiveTime="); pw.print(lastActiveTime);
197 pw.print(" (inactive for ");
198 pw.print((getInactiveDuration()/1000)); pw.println("s)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 }
200
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800201 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700203 if (stringName != null) {
204 return stringName;
205 }
206 StringBuilder sb = new StringBuilder(128);
207 sb.append("TaskRecord{");
208 sb.append(Integer.toHexString(System.identityHashCode(this)));
209 sb.append(" #");
210 sb.append(taskId);
211 if (affinity != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800212 sb.append(" A=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700213 sb.append(affinity);
214 } else if (intent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800215 sb.append(" I=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700216 sb.append(intent.getComponent().flattenToShortString());
217 } else if (affinityIntent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800218 sb.append(" aI=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700219 sb.append(affinityIntent.getComponent().flattenToShortString());
220 } else {
221 sb.append(" ??");
222 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800223 sb.append(" U=");
Amith Yamasani742a6712011-05-04 14:49:28 -0700224 sb.append(userId);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800225 sb.append(" sz=");
226 sb.append(mActivities.size());
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700227 sb.append('}');
228 return stringName = sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 }
230}