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 | de4ef02 | 2013-04-07 19:01:33 -0700 | [diff] [blame^] | 19 | import static com.android.server.am.ActivityManagerService.TAG; |
| 20 | import static com.android.server.am.ActivityStack.DEBUG_ADD_REMOVE; |
| 21 | |
Craig Mautner | b0f7dc7 | 2013-04-01 16:34:45 -0700 | [diff] [blame] | 22 | import android.app.Activity; |
| 23 | import android.app.ActivityOptions; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.content.ComponentName; |
| 25 | import android.content.Intent; |
| 26 | import android.content.pm.ActivityInfo; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 27 | import android.os.UserHandle; |
Dianne Hackborn | 7f96b79 | 2012-05-29 18:46:45 -0700 | [diff] [blame] | 28 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
| 30 | import java.io.PrintWriter; |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 31 | import java.util.ArrayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 33 | class TaskRecord extends ThumbnailHolder { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | final int taskId; // Unique identifier for this task. |
| 35 | 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] | 36 | 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 Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 44 | 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] | 45 | |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 46 | String stringName; // caching of toString() result. |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 47 | int userId; // user for which this task was created |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 48 | |
| 49 | int numFullscreen; // Number of fullscreen activities. |
| 50 | |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 51 | /** List of all activities in the task arranged in history order */ |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 52 | final ArrayList<ActivityRecord> mActivities = new ArrayList<ActivityRecord>(); |
| 53 | |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 54 | /** Current stack */ |
| 55 | ActivityStack stack; |
| 56 | |
| 57 | TaskRecord(int _taskId, ActivityInfo info, Intent _intent, ActivityStack _stack) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | taskId = _taskId; |
| 59 | affinity = info.taskAffinity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | setIntent(_intent, info); |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 61 | stack = _stack; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | } |
| 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 Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 73 | stringName = null; |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 74 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | if (info.targetActivity == null) { |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 76 | 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 Hackborn | d367ca8 | 2012-05-07 15:49:39 -0700 | [diff] [blame] | 80 | if (_intent.getSelector() != null || _intent.getSourceBounds() != null) { |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 81 | _intent = new Intent(_intent); |
| 82 | _intent.setSelector(null); |
Dianne Hackborn | d367ca8 | 2012-05-07 15:49:39 -0700 | [diff] [blame] | 83 | _intent.setSourceBounds(null); |
Dianne Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 84 | } |
| 85 | } |
Dianne Hackborn | 7f96b79 | 2012-05-29 18:46:45 -0700 | [diff] [blame] | 86 | if (ActivityManagerService.DEBUG_TASKS) Slog.v(ActivityManagerService.TAG, |
| 87 | "Setting Intent of " + this + " to " + _intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | 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 Hackborn | f5b8671 | 2011-12-05 17:42:41 -0800 | [diff] [blame] | 97 | targetIntent.setSelector(null); |
Dianne Hackborn | d367ca8 | 2012-05-07 15:49:39 -0700 | [diff] [blame] | 98 | targetIntent.setSourceBounds(null); |
Dianne Hackborn | 7f96b79 | 2012-05-29 18:46:45 -0700 | [diff] [blame] | 99 | if (ActivityManagerService.DEBUG_TASKS) Slog.v(ActivityManagerService.TAG, |
| 100 | "Setting Intent of " + this + " to target " + targetIntent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | 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 Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 110 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | 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 Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 117 | |
| 118 | if (info.applicationInfo != null) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 119 | userId = UserHandle.getUserId(info.applicationInfo.uid); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 120 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | } |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 122 | |
| 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 Mautner | de4ef02 | 2013-04-07 19:01:33 -0700 | [diff] [blame^] | 134 | /** |
| 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 Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 148 | void addActivityAtBottom(ActivityRecord r) { |
Craig Mautner | 7787877 | 2013-03-04 19:46:24 -0800 | [diff] [blame] | 149 | addActivityAtIndex(0, r); |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void addActivityToTop(ActivityRecord r) { |
Craig Mautner | 6170f73 | 2013-04-02 13:05:23 -0700 | [diff] [blame] | 153 | // 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] | 154 | if (!mActivities.remove(r) && r.fullscreen) { |
| 155 | // Was not previously in list. |
| 156 | numFullscreen++; |
| 157 | } |
Craig Mautner | 9658b31 | 2013-02-28 10:55:59 -0800 | [diff] [blame] | 158 | mActivities.add(r); |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Craig Mautner | 7787877 | 2013-03-04 19:46:24 -0800 | [diff] [blame] | 161 | 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 Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 169 | /** @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 Mautner | b0f7dc7 | 2013-04-01 16:34:45 -0700 | [diff] [blame] | 178 | /** |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | void dump(PrintWriter pw, String prefix) { |
Dianne Hackborn | 7f96b79 | 2012-05-29 18:46:45 -0700 | [diff] [blame] | 265 | if (numActivities != 0 || rootWasReset || userId != 0) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 266 | pw.print(prefix); pw.print("numActivities="); pw.print(numActivities); |
Dianne Hackborn | 7f96b79 | 2012-05-29 18:46:45 -0700 | [diff] [blame] | 267 | pw.print(" rootWasReset="); pw.print(rootWasReset); |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 268 | pw.print(" userId="); pw.print(userId); |
| 269 | pw.print(" numFullscreen="); pw.println(numFullscreen); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 270 | } |
| 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 Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 277 | intent.toShortString(sb, false, true, false, true); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 278 | 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 Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 284 | affinityIntent.toShortString(sb, false, true, false, true); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 285 | 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 Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 296 | pw.print(prefix); pw.print("Activities="); pw.println(mActivities); |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 297 | if (!askedCompatMode) { |
| 298 | pw.print(prefix); pw.print("askedCompatMode="); pw.println(askedCompatMode); |
| 299 | } |
Dianne Hackborn | cfb9f2b | 2011-08-24 10:51:49 -0700 | [diff] [blame] | 300 | pw.print(prefix); pw.print("lastThumbnail="); pw.print(lastThumbnail); |
| 301 | pw.print(" lastDescription="); pw.println(lastDescription); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 302 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 307 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | public String toString() { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 309 | StringBuilder sb = new StringBuilder(128); |
Craig Mautner | de4ef02 | 2013-04-07 19:01:33 -0700 | [diff] [blame^] | 310 | 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 Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 319 | sb.append("TaskRecord{"); |
| 320 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 321 | sb.append(" #"); |
| 322 | sb.append(taskId); |
| 323 | if (affinity != null) { |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 324 | sb.append(" A="); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 325 | sb.append(affinity); |
| 326 | } else if (intent != null) { |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 327 | sb.append(" I="); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 328 | sb.append(intent.getComponent().flattenToShortString()); |
| 329 | } else if (affinityIntent != null) { |
Craig Mautner | 5d9c7be | 2013-02-15 14:02:56 -0800 | [diff] [blame] | 330 | sb.append(" aI="); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 331 | sb.append(affinityIntent.getComponent().flattenToShortString()); |
| 332 | } else { |
| 333 | sb.append(" ??"); |
| 334 | } |
Craig Mautner | de4ef02 | 2013-04-07 19:01:33 -0700 | [diff] [blame^] | 335 | stringName = sb.toString(); |
| 336 | return toString(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | } |
| 338 | } |