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