The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.am; |
| 18 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.Intent; |
| 21 | import android.content.pm.ActivityInfo; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 22 | import android.os.UserHandle; |
Dianne Hackborn | 7f96b79 | 2012-05-29 18:46:45 -0700 | [diff] [blame] | 23 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
| 25 | import java.io.PrintWriter; |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 26 | import java.util.ArrayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 28 | class TaskRecord extends ThumbnailHolder { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | final int taskId; // Unique identifier for this task. |
| 30 | final String affinity; // The affinity name for this task, or null. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | 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 Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 39 | boolean askedCompatMode;// Have asked the user about compat mode for this task. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 41 | String stringName; // caching of toString() result. |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 42 | int userId; // user for which this task was created |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 43 | |
| 44 | int numFullscreen; // Number of fullscreen activities. |
| 45 | |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame^] | 46 | /** List of all activities in the task arranged in history order */ |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 47 | final ArrayList<ActivityRecord> mActivities = new ArrayList<ActivityRecord>(); |
| 48 | |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame^] | 49 | /** Current stack */ |
| 50 | ActivityStack stack; |
| 51 | |
| 52 | TaskRecord(int _taskId, ActivityInfo info, Intent _intent, ActivityStack _stack) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | taskId = _taskId; |
| 54 | affinity = info.taskAffinity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | setIntent(_intent, info); |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame^] | 56 | stack = _stack; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | } |
| 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 Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 68 | stringName = null; |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 69 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | if (info.targetActivity == null) { |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 71 | 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 Hackborn | d367ca8 | 2012-05-07 15:49:39 -0700 | [diff] [blame] | 75 | if (_intent.getSelector() != null || _intent.getSourceBounds() != null) { |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 76 | _intent = new Intent(_intent); |
| 77 | _intent.setSelector(null); |
Dianne Hackborn | d367ca8 | 2012-05-07 15:49:39 -0700 | [diff] [blame] | 78 | _intent.setSourceBounds(null); |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 79 | } |
| 80 | } |
Dianne Hackborn | 7f96b79 | 2012-05-29 18:46:45 -0700 | [diff] [blame] | 81 | if (ActivityManagerService.DEBUG_TASKS) Slog.v(ActivityManagerService.TAG, |
| 82 | "Setting Intent of " + this + " to " + _intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | 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 Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 92 | targetIntent.setSelector(null); |
Dianne Hackborn | d367ca8 | 2012-05-07 15:49:39 -0700 | [diff] [blame] | 93 | targetIntent.setSourceBounds(null); |
Dianne Hackborn | 7f96b79 | 2012-05-29 18:46:45 -0700 | [diff] [blame] | 94 | if (ActivityManagerService.DEBUG_TASKS) Slog.v(ActivityManagerService.TAG, |
| 95 | "Setting Intent of " + this + " to target " + targetIntent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | 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 Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 105 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | 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 Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 112 | |
| 113 | if (info.applicationInfo != null) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 114 | userId = UserHandle.getUserId(info.applicationInfo.uid); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 115 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | } |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 117 | |
| 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 Mautner | 7787877 | 2013-03-04 19:46:24 -0800 | [diff] [blame] | 130 | addActivityAtIndex(0, r); |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void addActivityToTop(ActivityRecord r) { |
| 134 | if (!mActivities.remove(r) && r.fullscreen) { |
| 135 | // Was not previously in list. |
| 136 | numFullscreen++; |
| 137 | } |
Craig Mautner | 9658b31 | 2013-02-28 10:55:59 -0800 | [diff] [blame] | 138 | mActivities.add(r); |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Craig Mautner | 7787877 | 2013-03-04 19:46:24 -0800 | [diff] [blame] | 141 | 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 Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 149 | /** @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | void dump(PrintWriter pw, String prefix) { |
Dianne Hackborn | 7f96b79 | 2012-05-29 18:46:45 -0700 | [diff] [blame] | 159 | if (numActivities != 0 || rootWasReset || userId != 0) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 160 | pw.print(prefix); pw.print("numActivities="); pw.print(numActivities); |
Dianne Hackborn | 7f96b79 | 2012-05-29 18:46:45 -0700 | [diff] [blame] | 161 | pw.print(" rootWasReset="); pw.print(rootWasReset); |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 162 | pw.print(" userId="); pw.print(userId); |
| 163 | pw.print(" numFullscreen="); pw.println(numFullscreen); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 164 | } |
| 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 Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 171 | intent.toShortString(sb, false, true, false, true); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 172 | 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 Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 178 | affinityIntent.toShortString(sb, false, true, false, true); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 179 | 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 Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 190 | pw.print(prefix); pw.print("Activities="); pw.println(mActivities); |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 191 | if (!askedCompatMode) { |
| 192 | pw.print(prefix); pw.print("askedCompatMode="); pw.println(askedCompatMode); |
| 193 | } |
Dianne Hackborn | cfb9f2b | 2011-08-24 10:51:49 -0700 | [diff] [blame] | 194 | pw.print(prefix); pw.print("lastThumbnail="); pw.print(lastThumbnail); |
| 195 | pw.print(" lastDescription="); pw.println(lastDescription); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 196 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 201 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | public String toString() { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 203 | 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 Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 212 | sb.append(" A="); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 213 | sb.append(affinity); |
| 214 | } else if (intent != null) { |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 215 | sb.append(" I="); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 216 | sb.append(intent.getComponent().flattenToShortString()); |
| 217 | } else if (affinityIntent != null) { |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 218 | sb.append(" aI="); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 219 | sb.append(affinityIntent.getComponent().flattenToShortString()); |
| 220 | } else { |
| 221 | sb.append(" ??"); |
| 222 | } |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 223 | sb.append(" U="); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 224 | sb.append(userId); |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 225 | sb.append(" sz="); |
| 226 | sb.append(mActivities.size()); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 227 | sb.append('}'); |
| 228 | return stringName = sb.toString(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | } |
| 230 | } |