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 | |
Craig Mautner | b0f7dc7 | 2013-04-01 16:34:45 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.app.ActivityOptions; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.content.ComponentName; |
| 22 | import android.content.Intent; |
| 23 | import android.content.pm.ActivityInfo; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 24 | import android.os.UserHandle; |
Dianne Hackborn | 7f96b79 | 2012-05-29 18:46:45 -0700 | [diff] [blame] | 25 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
| 27 | import java.io.PrintWriter; |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 28 | import java.util.ArrayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 30 | class TaskRecord extends ThumbnailHolder { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | final int taskId; // Unique identifier for this task. |
| 32 | 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] | 33 | 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 Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 41 | 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] | 42 | |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 43 | String stringName; // caching of toString() result. |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 44 | int userId; // user for which this task was created |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 45 | |
| 46 | int numFullscreen; // Number of fullscreen activities. |
| 47 | |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 48 | /** List of all activities in the task arranged in history order */ |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 49 | final ArrayList<ActivityRecord> mActivities = new ArrayList<ActivityRecord>(); |
| 50 | |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 51 | /** Current stack */ |
| 52 | ActivityStack stack; |
| 53 | |
| 54 | TaskRecord(int _taskId, ActivityInfo info, Intent _intent, ActivityStack _stack) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | taskId = _taskId; |
| 56 | affinity = info.taskAffinity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | setIntent(_intent, info); |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 58 | stack = _stack; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | } |
| 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 Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 70 | stringName = null; |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 71 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | if (info.targetActivity == null) { |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 73 | 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 Hackborn | d367ca8 | 2012-05-07 15:49:39 -0700 | [diff] [blame] | 77 | if (_intent.getSelector() != null || _intent.getSourceBounds() != null) { |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 78 | _intent = new Intent(_intent); |
| 79 | _intent.setSelector(null); |
Dianne Hackborn | d367ca8 | 2012-05-07 15:49:39 -0700 | [diff] [blame] | 80 | _intent.setSourceBounds(null); |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 81 | } |
| 82 | } |
Dianne Hackborn | 7f96b79 | 2012-05-29 18:46:45 -0700 | [diff] [blame] | 83 | if (ActivityManagerService.DEBUG_TASKS) Slog.v(ActivityManagerService.TAG, |
| 84 | "Setting Intent of " + this + " to " + _intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | 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 Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 94 | targetIntent.setSelector(null); |
Dianne Hackborn | d367ca8 | 2012-05-07 15:49:39 -0700 | [diff] [blame] | 95 | targetIntent.setSourceBounds(null); |
Dianne Hackborn | 7f96b79 | 2012-05-29 18:46:45 -0700 | [diff] [blame] | 96 | if (ActivityManagerService.DEBUG_TASKS) Slog.v(ActivityManagerService.TAG, |
| 97 | "Setting Intent of " + this + " to target " + targetIntent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | 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 Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 107 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | 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 Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 114 | |
| 115 | if (info.applicationInfo != null) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 116 | userId = UserHandle.getUserId(info.applicationInfo.uid); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 117 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | } |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 119 | |
| 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 Mautner | 7787877 | 2013-03-04 19:46:24 -0800 | [diff] [blame] | 132 | addActivityAtIndex(0, r); |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void addActivityToTop(ActivityRecord r) { |
Craig Mautner | 6170f73 | 2013-04-02 13:05:23 -0700 | [diff] [blame] | 136 | // Remove r first, and if it wasn't already in the list and it's fullscreen, count it. |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 137 | if (!mActivities.remove(r) && r.fullscreen) { |
| 138 | // Was not previously in list. |
| 139 | numFullscreen++; |
| 140 | } |
Craig Mautner | 9658b31 | 2013-02-28 10:55:59 -0800 | [diff] [blame] | 141 | mActivities.add(r); |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Craig Mautner | 7787877 | 2013-03-04 19:46:24 -0800 | [diff] [blame] | 144 | 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 Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 152 | /** @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 Mautner | b0f7dc7 | 2013-04-01 16:34:45 -0700 | [diff] [blame] | 161 | /** |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | void dump(PrintWriter pw, String prefix) { |
Dianne Hackborn | 7f96b79 | 2012-05-29 18:46:45 -0700 | [diff] [blame] | 248 | if (numActivities != 0 || rootWasReset || userId != 0) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 249 | pw.print(prefix); pw.print("numActivities="); pw.print(numActivities); |
Dianne Hackborn | 7f96b79 | 2012-05-29 18:46:45 -0700 | [diff] [blame] | 250 | pw.print(" rootWasReset="); pw.print(rootWasReset); |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 251 | pw.print(" userId="); pw.print(userId); |
| 252 | pw.print(" numFullscreen="); pw.println(numFullscreen); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 253 | } |
| 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 Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 260 | intent.toShortString(sb, false, true, false, true); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 261 | 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 Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 267 | affinityIntent.toShortString(sb, false, true, false, true); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 268 | 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 Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 279 | pw.print(prefix); pw.print("Activities="); pw.println(mActivities); |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 280 | if (!askedCompatMode) { |
| 281 | pw.print(prefix); pw.print("askedCompatMode="); pw.println(askedCompatMode); |
| 282 | } |
Dianne Hackborn | cfb9f2b | 2011-08-24 10:51:49 -0700 | [diff] [blame] | 283 | pw.print(prefix); pw.print("lastThumbnail="); pw.print(lastThumbnail); |
| 284 | pw.print(" lastDescription="); pw.println(lastDescription); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 285 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | } |
| 289 | |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 290 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | public String toString() { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 292 | 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 Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 301 | sb.append(" A="); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 302 | sb.append(affinity); |
| 303 | } else if (intent != null) { |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 304 | sb.append(" I="); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 305 | sb.append(intent.getComponent().flattenToShortString()); |
| 306 | } else if (affinityIntent != null) { |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 307 | sb.append(" aI="); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 308 | sb.append(affinityIntent.getComponent().flattenToShortString()); |
| 309 | } else { |
| 310 | sb.append(" ??"); |
| 311 | } |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 312 | sb.append(" U="); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 313 | sb.append(userId); |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 314 | sb.append(" sz="); |
| 315 | sb.append(mActivities.size()); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 316 | sb.append('}'); |
| 317 | return stringName = sb.toString(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | } |
| 319 | } |