TaskRecord.java revision 734983fff35d9ed2b7a9848bdfbca401887d0dd8
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 17package com.android.server.am; 18 19import static com.android.server.am.ActivityManagerService.TAG; 20import static com.android.server.am.ActivityStackSupervisor.DEBUG_ADD_REMOVE; 21 22import android.app.Activity; 23import android.app.ActivityManager; 24import android.app.ActivityOptions; 25import android.app.IThumbnailRetriever; 26import android.content.ComponentName; 27import android.content.Intent; 28import android.content.pm.ActivityInfo; 29import android.graphics.Bitmap; 30import android.os.UserHandle; 31import android.util.Slog; 32 33import java.io.PrintWriter; 34import java.util.ArrayList; 35 36final class TaskRecord extends ThumbnailHolder { 37 final int taskId; // Unique identifier for this task. 38 final String affinity; // The affinity name for this task, or null. 39 Intent intent; // The original intent that started the task. 40 Intent affinityIntent; // Intent of affinity-moved activity that started this task. 41 ComponentName origActivity; // The non-alias activity component of the intent. 42 ComponentName realActivity; // The actual activity component that started the task. 43 int numActivities; // Current number of activities in this task. 44 long lastActiveTime; // Last time this task was active, including sleep. 45 boolean rootWasReset; // True if the intent at the root of the task had 46 // the FLAG_ACTIVITY_RESET_TASK_IF_NEEDED flag. 47 boolean askedCompatMode;// Have asked the user about compat mode for this task. 48 49 String stringName; // caching of toString() result. 50 int userId; // user for which this task was created 51 52 int numFullscreen; // Number of fullscreen activities. 53 54 /** List of all activities in the task arranged in history order */ 55 final ArrayList<ActivityRecord> mActivities = new ArrayList<ActivityRecord>(); 56 57 /** Current stack */ 58 ActivityStack stack; 59 60 /** Takes on same set of values as ActivityRecord.mActivityType */ 61 private int mTaskType; 62 63 /** Launch the home activity when leaving this task. Will be false for tasks that are not on 64 * Display.DEFAULT_DISPLAY. */ 65 boolean mOnTopOfHome = false; 66 67 TaskRecord(int _taskId, ActivityInfo info, Intent _intent) { 68 taskId = _taskId; 69 affinity = info.taskAffinity; 70 setIntent(_intent, info); 71 } 72 73 void touchActiveTime() { 74 lastActiveTime = android.os.SystemClock.elapsedRealtime(); 75 } 76 77 long getInactiveDuration() { 78 return android.os.SystemClock.elapsedRealtime() - lastActiveTime; 79 } 80 81 void setIntent(Intent _intent, ActivityInfo info) { 82 stringName = null; 83 84 if (info.targetActivity == null) { 85 if (_intent != null) { 86 // If this Intent has a selector, we want to clear it for the 87 // recent task since it is not relevant if the user later wants 88 // to re-launch the app. 89 if (_intent.getSelector() != null || _intent.getSourceBounds() != null) { 90 _intent = new Intent(_intent); 91 _intent.setSelector(null); 92 _intent.setSourceBounds(null); 93 } 94 } 95 if (ActivityManagerService.DEBUG_TASKS) Slog.v(ActivityManagerService.TAG, 96 "Setting Intent of " + this + " to " + _intent); 97 intent = _intent; 98 realActivity = _intent != null ? _intent.getComponent() : null; 99 origActivity = null; 100 } else { 101 ComponentName targetComponent = new ComponentName( 102 info.packageName, info.targetActivity); 103 if (_intent != null) { 104 Intent targetIntent = new Intent(_intent); 105 targetIntent.setComponent(targetComponent); 106 targetIntent.setSelector(null); 107 targetIntent.setSourceBounds(null); 108 if (ActivityManagerService.DEBUG_TASKS) Slog.v(ActivityManagerService.TAG, 109 "Setting Intent of " + this + " to target " + targetIntent); 110 intent = targetIntent; 111 realActivity = targetComponent; 112 origActivity = _intent.getComponent(); 113 } else { 114 intent = null; 115 realActivity = targetComponent; 116 origActivity = new ComponentName(info.packageName, info.name); 117 } 118 } 119 120 if (intent != null && 121 (intent.getFlags()&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) { 122 // Once we are set to an Intent with this flag, we count this 123 // task as having a true root activity. 124 rootWasReset = true; 125 } 126 127 if (info.applicationInfo != null) { 128 userId = UserHandle.getUserId(info.applicationInfo.uid); 129 } 130 } 131 132 void disposeThumbnail() { 133 super.disposeThumbnail(); 134 for (int i=mActivities.size()-1; i>=0; i--) { 135 ThumbnailHolder thumb = mActivities.get(i).thumbHolder; 136 if (thumb != this) { 137 thumb.disposeThumbnail(); 138 } 139 } 140 } 141 142 ActivityRecord getTopActivity() { 143 for (int i = mActivities.size() - 1; i >= 0; --i) { 144 final ActivityRecord r = mActivities.get(i); 145 if (r.finishing) { 146 continue; 147 } 148 return r; 149 } 150 return null; 151 } 152 153 ActivityRecord topRunningActivityLocked(ActivityRecord notTop) { 154 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) { 155 ActivityRecord r = mActivities.get(activityNdx); 156 if (!r.finishing && r != notTop && stack.okToShowLocked(r)) { 157 return r; 158 } 159 } 160 return null; 161 } 162 163 /** Call after activity movement or finish to make sure that frontOfTask is set correctly */ 164 final void setFrontOfTask() { 165 boolean foundFront = false; 166 final int numActivities = mActivities.size(); 167 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) { 168 final ActivityRecord r = mActivities.get(activityNdx); 169 if (foundFront || r.finishing) { 170 r.frontOfTask = false; 171 } else { 172 r.frontOfTask = true; 173 // Set frontOfTask false for every following activity. 174 foundFront = true; 175 } 176 } 177 } 178 179 /** 180 * Reorder the history stack so that the passed activity is brought to the front. 181 */ 182 final void moveActivityToFrontLocked(ActivityRecord newTop) { 183 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + newTop 184 + " to stack at top", new RuntimeException("here").fillInStackTrace()); 185 186 mActivities.remove(newTop); 187 mActivities.add(newTop); 188 189 setFrontOfTask(); 190 } 191 192 void addActivityAtBottom(ActivityRecord r) { 193 addActivityAtIndex(0, r); 194 } 195 196 void addActivityToTop(ActivityRecord r) { 197 addActivityAtIndex(mActivities.size(), r); 198 } 199 200 void addActivityAtIndex(int index, ActivityRecord r) { 201 // Remove r first, and if it wasn't already in the list and it's fullscreen, count it. 202 if (!mActivities.remove(r) && r.fullscreen) { 203 // Was not previously in list. 204 numFullscreen++; 205 } 206 // Only set this based on the first activity 207 if (mActivities.isEmpty()) { 208 mTaskType = r.mActivityType; 209 } else { 210 // Otherwise make all added activities match this one. 211 r.mActivityType = mTaskType; 212 } 213 mActivities.add(index, r); 214 } 215 216 /** @return true if this was the last activity in the task */ 217 boolean removeActivity(ActivityRecord r) { 218 if (mActivities.remove(r) && r.fullscreen) { 219 // Was previously in list. 220 numFullscreen--; 221 } 222 return mActivities.size() == 0; 223 } 224 225 /** 226 * Completely remove all activities associated with an existing 227 * task starting at a specified index. 228 */ 229 final void performClearTaskAtIndexLocked(int activityNdx) { 230 int numActivities = mActivities.size(); 231 for ( ; activityNdx < numActivities; ++activityNdx) { 232 final ActivityRecord r = mActivities.get(activityNdx); 233 if (r.finishing) { 234 continue; 235 } 236 if (stack.finishActivityLocked(r, Activity.RESULT_CANCELED, null, "clear", false)) { 237 --activityNdx; 238 --numActivities; 239 } 240 } 241 } 242 243 /** 244 * Completely remove all activities associated with an existing task. 245 */ 246 final void performClearTaskLocked() { 247 performClearTaskAtIndexLocked(0); 248 } 249 250 /** 251 * Perform clear operation as requested by 252 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the 253 * stack to the given task, then look for 254 * an instance of that activity in the stack and, if found, finish all 255 * activities on top of it and return the instance. 256 * 257 * @param newR Description of the new activity being started. 258 * @return Returns the old activity that should be continued to be used, 259 * or null if none was found. 260 */ 261 final ActivityRecord performClearTaskLocked(ActivityRecord newR, int launchFlags) { 262 int numActivities = mActivities.size(); 263 for (int activityNdx = numActivities - 1; activityNdx >= 0; --activityNdx) { 264 ActivityRecord r = mActivities.get(activityNdx); 265 if (r.finishing) { 266 continue; 267 } 268 if (r.realActivity.equals(newR.realActivity)) { 269 // Here it is! Now finish everything in front... 270 final ActivityRecord ret = r; 271 272 for (++activityNdx; activityNdx < numActivities; ++activityNdx) { 273 r = mActivities.get(activityNdx); 274 if (r.finishing) { 275 continue; 276 } 277 ActivityOptions opts = r.takeOptionsLocked(); 278 if (opts != null) { 279 ret.updateOptionsLocked(opts); 280 } 281 if (stack.finishActivityLocked(r, Activity.RESULT_CANCELED, null, "clear", 282 false)) { 283 --activityNdx; 284 --numActivities; 285 } 286 } 287 288 // Finally, if this is a normal launch mode (that is, not 289 // expecting onNewIntent()), then we will finish the current 290 // instance of the activity so a new fresh one can be started. 291 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE 292 && (launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0) { 293 if (!ret.finishing) { 294 stack.finishActivityLocked(ret, Activity.RESULT_CANCELED, null, 295 "clear", false); 296 return null; 297 } 298 } 299 300 return ret; 301 } 302 } 303 304 return null; 305 } 306 307 public ActivityManager.TaskThumbnails getTaskThumbnailsLocked() { 308 TaskAccessInfo info = getTaskAccessInfoLocked(true); 309 final ActivityRecord resumedActivity = stack.mResumedActivity; 310 if (resumedActivity != null && resumedActivity.thumbHolder == this) { 311 info.mainThumbnail = stack.screenshotActivities(resumedActivity); 312 } 313 if (info.mainThumbnail == null) { 314 info.mainThumbnail = lastThumbnail; 315 } 316 return info; 317 } 318 319 public Bitmap getTaskTopThumbnailLocked() { 320 final ActivityRecord resumedActivity = stack.mResumedActivity; 321 if (resumedActivity != null && resumedActivity.task == this) { 322 // This task is the current resumed task, we just need to take 323 // a screenshot of it and return that. 324 return stack.screenshotActivities(resumedActivity); 325 } 326 // Return the information about the task, to figure out the top 327 // thumbnail to return. 328 TaskAccessInfo info = getTaskAccessInfoLocked(true); 329 if (info.numSubThumbbails <= 0) { 330 return info.mainThumbnail != null ? info.mainThumbnail : lastThumbnail; 331 } 332 return info.subtasks.get(info.numSubThumbbails-1).holder.lastThumbnail; 333 } 334 335 public ActivityRecord removeTaskActivitiesLocked(int subTaskIndex, 336 boolean taskRequired) { 337 TaskAccessInfo info = getTaskAccessInfoLocked(false); 338 if (info.root == null) { 339 if (taskRequired) { 340 Slog.w(TAG, "removeTaskLocked: unknown taskId " + taskId); 341 } 342 return null; 343 } 344 345 if (subTaskIndex < 0) { 346 // Just remove the entire task. 347 performClearTaskAtIndexLocked(info.rootIndex); 348 return info.root; 349 } 350 351 if (subTaskIndex >= info.subtasks.size()) { 352 if (taskRequired) { 353 Slog.w(TAG, "removeTaskLocked: unknown subTaskIndex " + subTaskIndex); 354 } 355 return null; 356 } 357 358 // Remove all of this task's activities starting at the sub task. 359 TaskAccessInfo.SubTask subtask = info.subtasks.get(subTaskIndex); 360 performClearTaskAtIndexLocked(subtask.index); 361 return subtask.activity; 362 } 363 364 boolean isHomeTask() { 365 return mTaskType == ActivityRecord.HOME_ACTIVITY_TYPE; 366 } 367 368 boolean isApplicationTask() { 369 return mTaskType == ActivityRecord.APPLICATION_ACTIVITY_TYPE; 370 } 371 372 public TaskAccessInfo getTaskAccessInfoLocked(boolean inclThumbs) { 373 final TaskAccessInfo thumbs = new TaskAccessInfo(); 374 // How many different sub-thumbnails? 375 final int NA = mActivities.size(); 376 int j = 0; 377 ThumbnailHolder holder = null; 378 while (j < NA) { 379 ActivityRecord ar = mActivities.get(j); 380 if (!ar.finishing) { 381 thumbs.root = ar; 382 thumbs.rootIndex = j; 383 holder = ar.thumbHolder; 384 if (holder != null) { 385 thumbs.mainThumbnail = holder.lastThumbnail; 386 } 387 j++; 388 break; 389 } 390 j++; 391 } 392 393 if (j >= NA) { 394 return thumbs; 395 } 396 397 ArrayList<TaskAccessInfo.SubTask> subtasks = new ArrayList<TaskAccessInfo.SubTask>(); 398 thumbs.subtasks = subtasks; 399 while (j < NA) { 400 ActivityRecord ar = mActivities.get(j); 401 j++; 402 if (ar.finishing) { 403 continue; 404 } 405 if (ar.thumbHolder != holder && holder != null) { 406 thumbs.numSubThumbbails++; 407 holder = ar.thumbHolder; 408 TaskAccessInfo.SubTask sub = new TaskAccessInfo.SubTask(); 409 sub.holder = holder; 410 sub.activity = ar; 411 sub.index = j-1; 412 subtasks.add(sub); 413 } 414 } 415 if (thumbs.numSubThumbbails > 0) { 416 thumbs.retriever = new IThumbnailRetriever.Stub() { 417 @Override 418 public Bitmap getThumbnail(int index) { 419 if (index < 0 || index >= thumbs.subtasks.size()) { 420 return null; 421 } 422 TaskAccessInfo.SubTask sub = thumbs.subtasks.get(index); 423 ActivityRecord resumedActivity = stack.mResumedActivity; 424 if (resumedActivity != null && resumedActivity.thumbHolder == sub.holder) { 425 return stack.screenshotActivities(resumedActivity); 426 } 427 return sub.holder.lastThumbnail; 428 } 429 }; 430 } 431 return thumbs; 432 } 433 434 /** 435 * Find the activity in the history stack within the given task. Returns 436 * the index within the history at which it's found, or < 0 if not found. 437 */ 438 final ActivityRecord findActivityInHistoryLocked(ActivityRecord r) { 439 final ComponentName realActivity = r.realActivity; 440 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) { 441 ActivityRecord candidate = mActivities.get(activityNdx); 442 if (candidate.finishing) { 443 continue; 444 } 445 if (candidate.realActivity.equals(realActivity)) { 446 return candidate; 447 } 448 } 449 return null; 450 } 451 452 void dump(PrintWriter pw, String prefix) { 453 if (numActivities != 0 || rootWasReset || userId != 0 || numFullscreen != 0) { 454 pw.print(prefix); pw.print("numActivities="); pw.print(numActivities); 455 pw.print(" rootWasReset="); pw.print(rootWasReset); 456 pw.print(" userId="); pw.print(userId); 457 pw.print(" mTaskType="); pw.print(mTaskType); 458 pw.print(" numFullscreen="); pw.print(numFullscreen); 459 pw.print(" mOnTopOfHome="); pw.println(mOnTopOfHome); 460 } 461 if (affinity != null) { 462 pw.print(prefix); pw.print("affinity="); pw.println(affinity); 463 } 464 if (intent != null) { 465 StringBuilder sb = new StringBuilder(128); 466 sb.append(prefix); sb.append("intent={"); 467 intent.toShortString(sb, false, true, false, true); 468 sb.append('}'); 469 pw.println(sb.toString()); 470 } 471 if (affinityIntent != null) { 472 StringBuilder sb = new StringBuilder(128); 473 sb.append(prefix); sb.append("affinityIntent={"); 474 affinityIntent.toShortString(sb, false, true, false, true); 475 sb.append('}'); 476 pw.println(sb.toString()); 477 } 478 if (origActivity != null) { 479 pw.print(prefix); pw.print("origActivity="); 480 pw.println(origActivity.flattenToShortString()); 481 } 482 if (realActivity != null) { 483 pw.print(prefix); pw.print("realActivity="); 484 pw.println(realActivity.flattenToShortString()); 485 } 486 pw.print(prefix); pw.print("Activities="); pw.println(mActivities); 487 if (!askedCompatMode) { 488 pw.print(prefix); pw.print("askedCompatMode="); pw.println(askedCompatMode); 489 } 490 pw.print(prefix); pw.print("lastThumbnail="); pw.print(lastThumbnail); 491 pw.print(" lastDescription="); pw.println(lastDescription); 492 pw.print(prefix); pw.print("lastActiveTime="); pw.print(lastActiveTime); 493 pw.print(" (inactive for "); 494 pw.print((getInactiveDuration()/1000)); pw.println("s)"); 495 } 496 497 @Override 498 public String toString() { 499 StringBuilder sb = new StringBuilder(128); 500 if (stringName != null) { 501 sb.append(stringName); 502 sb.append(" U="); 503 sb.append(userId); 504 sb.append(" sz="); 505 sb.append(mActivities.size()); 506 sb.append('}'); 507 return sb.toString(); 508 } 509 sb.append("TaskRecord{"); 510 sb.append(Integer.toHexString(System.identityHashCode(this))); 511 sb.append(" #"); 512 sb.append(taskId); 513 if (affinity != null) { 514 sb.append(" A="); 515 sb.append(affinity); 516 } else if (intent != null) { 517 sb.append(" I="); 518 sb.append(intent.getComponent().flattenToShortString()); 519 } else if (affinityIntent != null) { 520 sb.append(" aI="); 521 sb.append(affinityIntent.getComponent().flattenToShortString()); 522 } else { 523 sb.append(" ??"); 524 } 525 stringName = sb.toString(); 526 return toString(); 527 } 528} 529