ActivityRecord.java revision 8a985d24ce9a38f40ed88fecbdcd0e75e3a68f44
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 android.os.Trace; 20import com.android.internal.R.styleable; 21import com.android.internal.app.ResolverActivity; 22import com.android.server.AttributeCache; 23import com.android.server.am.ActivityStack.ActivityState; 24import com.android.server.am.ActivityStackSupervisor.ActivityContainer; 25 26import android.app.ActivityOptions; 27import android.app.ResultInfo; 28import android.content.ComponentName; 29import android.content.Intent; 30import android.content.pm.ActivityInfo; 31import android.content.pm.ApplicationInfo; 32import android.content.res.CompatibilityInfo; 33import android.content.res.Configuration; 34import android.graphics.Bitmap; 35import android.graphics.Rect; 36import android.os.Build; 37import android.os.Bundle; 38import android.os.IBinder; 39import android.os.Message; 40import android.os.Process; 41import android.os.RemoteException; 42import android.os.SystemClock; 43import android.os.UserHandle; 44import android.util.EventLog; 45import android.util.Log; 46import android.util.Slog; 47import android.util.TimeUtils; 48import android.view.IApplicationToken; 49import android.view.WindowManager; 50 51import java.io.PrintWriter; 52import java.lang.ref.WeakReference; 53import java.util.ArrayList; 54import java.util.HashSet; 55 56/** 57 * An entry in the history stack, representing an activity. 58 */ 59final class ActivityRecord { 60 static final String TAG = ActivityManagerService.TAG; 61 static final boolean DEBUG_SAVED_STATE = ActivityStackSupervisor.DEBUG_SAVED_STATE; 62 final public static String RECENTS_PACKAGE_NAME = "com.android.systemui.recent"; 63 64 final ActivityManagerService service; // owner 65 final IApplicationToken.Stub appToken; // window manager token 66 final ActivityInfo info; // all about me 67 final int launchedFromUid; // always the uid who started the activity. 68 final String launchedFromPackage; // always the package who started the activity. 69 final int userId; // Which user is this running for? 70 final Intent intent; // the original intent that generated us 71 final ComponentName realActivity; // the intent component, or target of an alias. 72 final String shortComponentName; // the short component name of the intent 73 final String resolvedType; // as per original caller; 74 final String packageName; // the package implementing intent's component 75 final String processName; // process where this component wants to run 76 final String taskAffinity; // as per ActivityInfo.taskAffinity 77 final boolean stateNotNeeded; // As per ActivityInfo.flags 78 boolean fullscreen; // covers the full screen? 79 final boolean noDisplay; // activity is not displayed? 80 final boolean componentSpecified; // did caller specifiy an explicit component? 81 82 static final int APPLICATION_ACTIVITY_TYPE = 0; 83 static final int HOME_ACTIVITY_TYPE = 1; 84 static final int RECENTS_ACTIVITY_TYPE = 2; 85 int mActivityType; 86 87 final String baseDir; // where activity source (resources etc) located 88 final String resDir; // where public activity source (public resources etc) located 89 final String dataDir; // where activity data should go 90 CharSequence nonLocalizedLabel; // the label information from the package mgr. 91 int labelRes; // the label information from the package mgr. 92 int icon; // resource identifier of activity's icon. 93 int logo; // resource identifier of activity's logo. 94 int theme; // resource identifier of activity's theme. 95 int realTheme; // actual theme resource we will use, never 0. 96 int windowFlags; // custom window flags for preview window. 97 TaskRecord task; // the task this is in. 98 ThumbnailHolder thumbHolder; // where our thumbnails should go. 99 long displayStartTime; // when we started launching this activity 100 long fullyDrawnStartTime; // when we started launching this activity 101 long startTime; // last time this activity was started 102 long lastVisibleTime; // last time this activity became visible 103 long cpuTimeAtResume; // the cpu time of host process at the time of resuming activity 104 long pauseTime; // last time we started pausing the activity 105 long launchTickTime; // base time for launch tick messages 106 Configuration configuration; // configuration activity was last running in 107 CompatibilityInfo compat;// last used compatibility mode 108 ActivityRecord resultTo; // who started this entry, so will get our reply 109 final String resultWho; // additional identifier for use by resultTo. 110 final int requestCode; // code given by requester (resultTo) 111 ArrayList<ResultInfo> results; // pending ActivityResult objs we have received 112 HashSet<WeakReference<PendingIntentRecord>> pendingResults; // all pending intents for this act 113 ArrayList<Intent> newIntents; // any pending new intents for single-top mode 114 ActivityOptions pendingOptions; // most recently given options 115 HashSet<ConnectionRecord> connections; // All ConnectionRecord we hold 116 UriPermissionOwner uriPermissions; // current special URI access perms. 117 ProcessRecord app; // if non-null, hosting application 118 ActivityState state; // current state we are in 119 Bundle icicle; // last saved activity state 120 boolean frontOfTask; // is this the root activity of its task? 121 boolean launchFailed; // set if a launched failed, to abort on 2nd try 122 boolean haveState; // have we gotten the last activity state? 123 boolean stopped; // is activity pause finished? 124 boolean delayedResume; // not yet resumed because of stopped app switches? 125 boolean finishing; // activity in pending finish list? 126 boolean configDestroy; // need to destroy due to config change? 127 int configChangeFlags; // which config values have changed 128 boolean keysPaused; // has key dispatching been paused for it? 129 int launchMode; // the launch mode activity attribute. 130 boolean visible; // does this activity's window need to be shown? 131 boolean sleeping; // have we told the activity to sleep? 132 boolean waitingVisible; // true if waiting for a new act to become vis 133 boolean nowVisible; // is this activity's window visible? 134 boolean thumbnailNeeded;// has someone requested a thumbnail? 135 boolean idle; // has the activity gone idle? 136 boolean hasBeenLaunched;// has this activity ever been launched? 137 boolean frozenBeforeDestroy;// has been frozen but not yet destroyed. 138 boolean immersive; // immersive mode (don't interrupt if possible) 139 boolean forceNewConfig; // force re-create with new config next time 140 int launchCount; // count of launches since last state 141 long lastLaunchTime; // time of last lauch of this activity 142 ArrayList<ActivityStack> mChildContainers = new ArrayList<ActivityStack>(); 143 144 String stringName; // for caching of toString(). 145 146 private boolean inHistory; // are we in the history stack? 147 final ActivityStackSupervisor mStackSupervisor; 148 boolean mStartingWindowShown = false; 149 ActivityContainer mInitialActivityContainer; 150 151 void dump(PrintWriter pw, String prefix) { 152 final long now = SystemClock.uptimeMillis(); 153 pw.print(prefix); pw.print("packageName="); pw.print(packageName); 154 pw.print(" processName="); pw.println(processName); 155 pw.print(prefix); pw.print("launchedFromUid="); pw.print(launchedFromUid); 156 pw.print(" launchedFromPackage="); pw.print(launchedFromPackage); 157 pw.print(" userId="); pw.println(userId); 158 pw.print(prefix); pw.print("app="); pw.println(app); 159 pw.print(prefix); pw.println(intent.toInsecureStringWithClip()); 160 pw.print(prefix); pw.print("frontOfTask="); pw.print(frontOfTask); 161 pw.print(" task="); pw.println(task); 162 pw.print(prefix); pw.print("taskAffinity="); pw.println(taskAffinity); 163 pw.print(prefix); pw.print("realActivity="); 164 pw.println(realActivity.flattenToShortString()); 165 pw.print(prefix); pw.print("baseDir="); pw.println(baseDir); 166 if (!resDir.equals(baseDir)) { 167 pw.print(prefix); pw.print("resDir="); pw.println(resDir); 168 } 169 pw.print(prefix); pw.print("dataDir="); pw.println(dataDir); 170 pw.print(prefix); pw.print("stateNotNeeded="); pw.print(stateNotNeeded); 171 pw.print(" componentSpecified="); pw.print(componentSpecified); 172 pw.print(" mActivityType="); pw.println(mActivityType); 173 pw.print(prefix); pw.print("compat="); pw.print(compat); 174 pw.print(" labelRes=0x"); pw.print(Integer.toHexString(labelRes)); 175 pw.print(" icon=0x"); pw.print(Integer.toHexString(icon)); 176 pw.print(" theme=0x"); pw.println(Integer.toHexString(theme)); 177 pw.print(prefix); pw.print("config="); pw.println(configuration); 178 if (resultTo != null || resultWho != null) { 179 pw.print(prefix); pw.print("resultTo="); pw.print(resultTo); 180 pw.print(" resultWho="); pw.print(resultWho); 181 pw.print(" resultCode="); pw.println(requestCode); 182 } 183 if (results != null) { 184 pw.print(prefix); pw.print("results="); pw.println(results); 185 } 186 if (pendingResults != null && pendingResults.size() > 0) { 187 pw.print(prefix); pw.println("Pending Results:"); 188 for (WeakReference<PendingIntentRecord> wpir : pendingResults) { 189 PendingIntentRecord pir = wpir != null ? wpir.get() : null; 190 pw.print(prefix); pw.print(" - "); 191 if (pir == null) { 192 pw.println("null"); 193 } else { 194 pw.println(pir); 195 pir.dump(pw, prefix + " "); 196 } 197 } 198 } 199 if (newIntents != null && newIntents.size() > 0) { 200 pw.print(prefix); pw.println("Pending New Intents:"); 201 for (int i=0; i<newIntents.size(); i++) { 202 Intent intent = newIntents.get(i); 203 pw.print(prefix); pw.print(" - "); 204 if (intent == null) { 205 pw.println("null"); 206 } else { 207 pw.println(intent.toShortString(false, true, false, true)); 208 } 209 } 210 } 211 if (pendingOptions != null) { 212 pw.print(prefix); pw.print("pendingOptions="); pw.println(pendingOptions); 213 } 214 if (uriPermissions != null) { 215 if (uriPermissions.readUriPermissions != null) { 216 pw.print(prefix); pw.print("readUriPermissions="); 217 pw.println(uriPermissions.readUriPermissions); 218 } 219 if (uriPermissions.writeUriPermissions != null) { 220 pw.print(prefix); pw.print("writeUriPermissions="); 221 pw.println(uriPermissions.writeUriPermissions); 222 } 223 } 224 pw.print(prefix); pw.print("launchFailed="); pw.print(launchFailed); 225 pw.print(" launchCount="); pw.print(launchCount); 226 pw.print(" lastLaunchTime="); 227 if (lastLaunchTime == 0) pw.print("0"); 228 else TimeUtils.formatDuration(lastLaunchTime, now, pw); 229 pw.println(); 230 pw.print(prefix); pw.print("haveState="); pw.print(haveState); 231 pw.print(" icicle="); pw.println(icicle); 232 pw.print(prefix); pw.print("state="); pw.print(state); 233 pw.print(" stopped="); pw.print(stopped); 234 pw.print(" delayedResume="); pw.print(delayedResume); 235 pw.print(" finishing="); pw.println(finishing); 236 pw.print(prefix); pw.print("keysPaused="); pw.print(keysPaused); 237 pw.print(" inHistory="); pw.print(inHistory); 238 pw.print(" visible="); pw.print(visible); 239 pw.print(" sleeping="); pw.print(sleeping); 240 pw.print(" idle="); pw.println(idle); 241 pw.print(prefix); pw.print("fullscreen="); pw.print(fullscreen); 242 pw.print(" noDisplay="); pw.print(noDisplay); 243 pw.print(" immersive="); pw.print(immersive); 244 pw.print(" launchMode="); pw.println(launchMode); 245 pw.print(prefix); pw.print("frozenBeforeDestroy="); pw.print(frozenBeforeDestroy); 246 pw.print(" thumbnailNeeded="); pw.print(thumbnailNeeded); 247 pw.print(" forceNewConfig="); pw.println(forceNewConfig); 248 pw.print(prefix); pw.print("mActivityType="); 249 pw.println(activityTypeToString(mActivityType)); 250 pw.print(prefix); pw.print("thumbHolder: "); 251 pw.print(Integer.toHexString(System.identityHashCode(thumbHolder))); 252 if (thumbHolder != null) { 253 pw.print(" bm="); pw.print(thumbHolder.lastThumbnail); 254 pw.print(" desc="); pw.print(thumbHolder.lastDescription); 255 } 256 pw.println(); 257 if (displayStartTime != 0 || startTime != 0) { 258 pw.print(prefix); pw.print("displayStartTime="); 259 if (displayStartTime == 0) pw.print("0"); 260 else TimeUtils.formatDuration(displayStartTime, now, pw); 261 pw.print(" startTime="); 262 if (startTime == 0) pw.print("0"); 263 else TimeUtils.formatDuration(startTime, now, pw); 264 pw.println(); 265 } 266 if (lastVisibleTime != 0 || waitingVisible || nowVisible) { 267 pw.print(prefix); pw.print("waitingVisible="); pw.print(waitingVisible); 268 pw.print(" nowVisible="); pw.print(nowVisible); 269 pw.print(" lastVisibleTime="); 270 if (lastVisibleTime == 0) pw.print("0"); 271 else TimeUtils.formatDuration(lastVisibleTime, now, pw); 272 pw.println(); 273 } 274 if (configDestroy || configChangeFlags != 0) { 275 pw.print(prefix); pw.print("configDestroy="); pw.print(configDestroy); 276 pw.print(" configChangeFlags="); 277 pw.println(Integer.toHexString(configChangeFlags)); 278 } 279 if (connections != null) { 280 pw.print(prefix); pw.print("connections="); pw.println(connections); 281 } 282 } 283 284 static class Token extends IApplicationToken.Stub { 285 final WeakReference<ActivityRecord> weakActivity; 286 287 Token(ActivityRecord activity) { 288 weakActivity = new WeakReference<ActivityRecord>(activity); 289 } 290 291 @Override public void windowsDrawn() { 292 ActivityRecord activity = weakActivity.get(); 293 if (activity != null) { 294 activity.windowsDrawn(); 295 } 296 } 297 298 @Override public void windowsVisible() { 299 ActivityRecord activity = weakActivity.get(); 300 if (activity != null) { 301 activity.windowsVisible(); 302 } 303 } 304 305 @Override public void windowsGone() { 306 ActivityRecord activity = weakActivity.get(); 307 if (activity != null) { 308 activity.windowsGone(); 309 } 310 } 311 312 @Override public boolean keyDispatchingTimedOut(String reason) { 313 ActivityRecord activity = weakActivity.get(); 314 return activity != null && activity.keyDispatchingTimedOut(reason); 315 } 316 317 @Override public long getKeyDispatchingTimeout() { 318 ActivityRecord activity = weakActivity.get(); 319 if (activity != null) { 320 return activity.getKeyDispatchingTimeout(); 321 } 322 return 0; 323 } 324 325 @Override 326 public String toString() { 327 StringBuilder sb = new StringBuilder(128); 328 sb.append("Token{"); 329 sb.append(Integer.toHexString(System.identityHashCode(this))); 330 sb.append(' '); 331 sb.append(weakActivity.get()); 332 sb.append('}'); 333 return sb.toString(); 334 } 335 } 336 337 static ActivityRecord forToken(IBinder token) { 338 try { 339 return token != null ? ((Token)token).weakActivity.get() : null; 340 } catch (ClassCastException e) { 341 Slog.w(ActivityManagerService.TAG, "Bad activity token: " + token, e); 342 return null; 343 } 344 } 345 346 boolean isNotResolverActivity() { 347 return !ResolverActivity.class.getName().equals(realActivity.getClassName()); 348 } 349 350 ActivityRecord(ActivityManagerService _service, ProcessRecord _caller, 351 int _launchedFromUid, String _launchedFromPackage, Intent _intent, String _resolvedType, 352 ActivityInfo aInfo, Configuration _configuration, 353 ActivityRecord _resultTo, String _resultWho, int _reqCode, 354 boolean _componentSpecified, ActivityStackSupervisor supervisor, 355 ActivityContainer container) { 356 service = _service; 357 appToken = new Token(this); 358 info = aInfo; 359 launchedFromUid = _launchedFromUid; 360 launchedFromPackage = _launchedFromPackage; 361 userId = UserHandle.getUserId(aInfo.applicationInfo.uid); 362 intent = _intent; 363 shortComponentName = _intent.getComponent().flattenToShortString(); 364 resolvedType = _resolvedType; 365 componentSpecified = _componentSpecified; 366 configuration = _configuration; 367 resultTo = _resultTo; 368 resultWho = _resultWho; 369 requestCode = _reqCode; 370 state = ActivityState.INITIALIZING; 371 frontOfTask = false; 372 launchFailed = false; 373 stopped = false; 374 delayedResume = false; 375 finishing = false; 376 configDestroy = false; 377 keysPaused = false; 378 inHistory = false; 379 visible = true; 380 waitingVisible = false; 381 nowVisible = false; 382 thumbnailNeeded = false; 383 idle = false; 384 hasBeenLaunched = false; 385 mStackSupervisor = supervisor; 386 mInitialActivityContainer = container; 387 388 // This starts out true, since the initial state of an activity 389 // is that we have everything, and we shouldn't never consider it 390 // lacking in state to be removed if it dies. 391 haveState = true; 392 393 if (aInfo != null) { 394 if (aInfo.targetActivity == null 395 || aInfo.launchMode == ActivityInfo.LAUNCH_MULTIPLE 396 || aInfo.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) { 397 realActivity = _intent.getComponent(); 398 } else { 399 realActivity = new ComponentName(aInfo.packageName, 400 aInfo.targetActivity); 401 } 402 taskAffinity = aInfo.taskAffinity; 403 stateNotNeeded = (aInfo.flags& 404 ActivityInfo.FLAG_STATE_NOT_NEEDED) != 0; 405 baseDir = aInfo.applicationInfo.sourceDir; 406 resDir = aInfo.applicationInfo.publicSourceDir; 407 dataDir = aInfo.applicationInfo.dataDir; 408 nonLocalizedLabel = aInfo.nonLocalizedLabel; 409 labelRes = aInfo.labelRes; 410 if (nonLocalizedLabel == null && labelRes == 0) { 411 ApplicationInfo app = aInfo.applicationInfo; 412 nonLocalizedLabel = app.nonLocalizedLabel; 413 labelRes = app.labelRes; 414 } 415 icon = aInfo.getIconResource(); 416 logo = aInfo.getLogoResource(); 417 theme = aInfo.getThemeResource(); 418 realTheme = theme; 419 if (realTheme == 0) { 420 realTheme = aInfo.applicationInfo.targetSdkVersion 421 < Build.VERSION_CODES.HONEYCOMB 422 ? android.R.style.Theme 423 : android.R.style.Theme_Holo; 424 } 425 if ((aInfo.flags&ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) { 426 windowFlags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED; 427 } 428 if ((aInfo.flags&ActivityInfo.FLAG_MULTIPROCESS) != 0 429 && _caller != null 430 && (aInfo.applicationInfo.uid == Process.SYSTEM_UID 431 || aInfo.applicationInfo.uid == _caller.info.uid)) { 432 processName = _caller.processName; 433 } else { 434 processName = aInfo.processName; 435 } 436 437 if (intent != null && (aInfo.flags & ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS) != 0) { 438 intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 439 } 440 441 packageName = aInfo.applicationInfo.packageName; 442 launchMode = aInfo.launchMode; 443 444 AttributeCache.Entry ent = AttributeCache.instance().get(packageName, 445 realTheme, com.android.internal.R.styleable.Window, userId); 446 fullscreen = ent != null && !ent.array.getBoolean( 447 com.android.internal.R.styleable.Window_windowIsFloating, false) 448 && !ent.array.getBoolean( 449 com.android.internal.R.styleable.Window_windowIsTranslucent, false); 450 noDisplay = ent != null && ent.array.getBoolean( 451 com.android.internal.R.styleable.Window_windowNoDisplay, false); 452 453 if ((!_componentSpecified || _launchedFromUid == Process.myUid() 454 || _launchedFromUid == 0) && 455 Intent.ACTION_MAIN.equals(_intent.getAction()) && 456 _intent.hasCategory(Intent.CATEGORY_HOME) && 457 _intent.getCategories().size() == 1 && 458 _intent.getData() == null && 459 _intent.getType() == null && 460 (intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 && 461 isNotResolverActivity()) { 462 // This sure looks like a home activity! 463 mActivityType = HOME_ACTIVITY_TYPE; 464 } else if (realActivity.getClassName().contains(RECENTS_PACKAGE_NAME)) { 465 mActivityType = RECENTS_ACTIVITY_TYPE; 466 } else { 467 mActivityType = APPLICATION_ACTIVITY_TYPE; 468 } 469 470 immersive = (aInfo.flags & ActivityInfo.FLAG_IMMERSIVE) != 0; 471 } else { 472 realActivity = null; 473 taskAffinity = null; 474 stateNotNeeded = false; 475 baseDir = null; 476 resDir = null; 477 dataDir = null; 478 processName = null; 479 packageName = null; 480 fullscreen = true; 481 noDisplay = false; 482 mActivityType = APPLICATION_ACTIVITY_TYPE; 483 immersive = false; 484 } 485 } 486 487 void setTask(TaskRecord newTask, ThumbnailHolder newThumbHolder, boolean isRoot) { 488 if (task != null && task.removeActivity(this)) { 489 if (task != newTask) { 490 task.stack.removeTask(task); 491 } else { 492 Slog.d(TAG, "!!! REMOVE THIS LOG !!! setTask: nearly removed stack=" + 493 (newTask == null ? null : newTask.stack)); 494 } 495 } 496 if (inHistory && !finishing) { 497 if (task != null) { 498 task.numActivities--; 499 } 500 if (newTask != null) { 501 newTask.numActivities++; 502 } 503 } 504 if (newThumbHolder == null) { 505 newThumbHolder = newTask; 506 } 507 task = newTask; 508 if (!isRoot && (intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) { 509 // This is the start of a new sub-task. 510 if (thumbHolder == null) { 511 thumbHolder = new ThumbnailHolder(); 512 } 513 } else { 514 thumbHolder = newThumbHolder; 515 } 516 } 517 518 boolean changeWindowTranslucency(boolean toOpaque) { 519 if (fullscreen == toOpaque) { 520 return false; 521 } 522 AttributeCache.Entry ent = 523 AttributeCache.instance().get(packageName, realTheme, styleable.Window, userId); 524 if (ent == null 525 || !ent.array.getBoolean(styleable.Window_windowIsTranslucent, false) 526 || ent.array.getBoolean(styleable.Window_windowIsFloating, false)) { 527 return false; 528 } 529 530 // Keep track of the number of fullscreen activities in this task. 531 task.numFullscreen += toOpaque ? +1 : -1; 532 533 fullscreen = toOpaque; 534 return true; 535 } 536 537 void putInHistory() { 538 if (!inHistory) { 539 inHistory = true; 540 if (task != null && !finishing) { 541 task.numActivities++; 542 } 543 } 544 } 545 546 void takeFromHistory() { 547 if (inHistory) { 548 inHistory = false; 549 if (task != null && !finishing) { 550 task.numActivities--; 551 task = null; 552 } 553 clearOptionsLocked(); 554 } 555 } 556 557 boolean isInHistory() { 558 return inHistory; 559 } 560 561 boolean isHomeActivity() { 562 return mActivityType == HOME_ACTIVITY_TYPE; 563 } 564 565 boolean isRecentsActivity() { 566 return mActivityType == RECENTS_ACTIVITY_TYPE; 567 } 568 569 boolean isApplicationActivity() { 570 return mActivityType == APPLICATION_ACTIVITY_TYPE; 571 } 572 573 void makeFinishing() { 574 if (!finishing) { 575 finishing = true; 576 if (task != null && inHistory) { 577 task.numActivities--; 578 } 579 if (stopped) { 580 clearOptionsLocked(); 581 } 582 } 583 } 584 585 boolean isRootActivity() { 586 final ArrayList<ActivityRecord> activities = task.mActivities; 587 return activities.size() == 0 || this == activities.get(0); 588 } 589 590 UriPermissionOwner getUriPermissionsLocked() { 591 if (uriPermissions == null) { 592 uriPermissions = new UriPermissionOwner(service, this); 593 } 594 return uriPermissions; 595 } 596 597 void addResultLocked(ActivityRecord from, String resultWho, 598 int requestCode, int resultCode, 599 Intent resultData) { 600 ActivityResult r = new ActivityResult(from, resultWho, 601 requestCode, resultCode, resultData); 602 if (results == null) { 603 results = new ArrayList<ResultInfo>(); 604 } 605 results.add(r); 606 } 607 608 void removeResultsLocked(ActivityRecord from, String resultWho, 609 int requestCode) { 610 if (results != null) { 611 for (int i=results.size()-1; i>=0; i--) { 612 ActivityResult r = (ActivityResult)results.get(i); 613 if (r.mFrom != from) continue; 614 if (r.mResultWho == null) { 615 if (resultWho != null) continue; 616 } else { 617 if (!r.mResultWho.equals(resultWho)) continue; 618 } 619 if (r.mRequestCode != requestCode) continue; 620 621 results.remove(i); 622 } 623 } 624 } 625 626 void addNewIntentLocked(Intent intent) { 627 if (newIntents == null) { 628 newIntents = new ArrayList<Intent>(); 629 } 630 newIntents.add(intent); 631 } 632 633 /** 634 * Deliver a new Intent to an existing activity, so that its onNewIntent() 635 * method will be called at the proper time. 636 */ 637 final void deliverNewIntentLocked(int callingUid, Intent intent) { 638 // The activity now gets access to the data associated with this Intent. 639 service.grantUriPermissionFromIntentLocked(callingUid, packageName, 640 intent, getUriPermissionsLocked()); 641 // We want to immediately deliver the intent to the activity if 642 // it is currently the top resumed activity... however, if the 643 // device is sleeping, then all activities are stopped, so in that 644 // case we will deliver it if this is the current top activity on its 645 // stack. 646 boolean unsent = true; 647 if ((state == ActivityState.RESUMED || (service.mSleeping 648 && task.stack.topRunningActivityLocked(null) == this)) 649 && app != null && app.thread != null) { 650 try { 651 ArrayList<Intent> ar = new ArrayList<Intent>(); 652 intent = new Intent(intent); 653 ar.add(intent); 654 app.thread.scheduleNewIntent(ar, appToken); 655 unsent = false; 656 } catch (RemoteException e) { 657 Slog.w(ActivityManagerService.TAG, 658 "Exception thrown sending new intent to " + this, e); 659 } catch (NullPointerException e) { 660 Slog.w(ActivityManagerService.TAG, 661 "Exception thrown sending new intent to " + this, e); 662 } 663 } 664 if (unsent) { 665 addNewIntentLocked(new Intent(intent)); 666 } 667 } 668 669 void updateOptionsLocked(Bundle options) { 670 if (options != null) { 671 if (pendingOptions != null) { 672 pendingOptions.abort(); 673 } 674 pendingOptions = new ActivityOptions(options); 675 } 676 } 677 678 void updateOptionsLocked(ActivityOptions options) { 679 if (options != null) { 680 if (pendingOptions != null) { 681 pendingOptions.abort(); 682 } 683 pendingOptions = options; 684 } 685 } 686 687 void applyOptionsLocked() { 688 if (pendingOptions != null) { 689 final int animationType = pendingOptions.getAnimationType(); 690 switch (animationType) { 691 case ActivityOptions.ANIM_CUSTOM: 692 service.mWindowManager.overridePendingAppTransition( 693 pendingOptions.getPackageName(), 694 pendingOptions.getCustomEnterResId(), 695 pendingOptions.getCustomExitResId(), 696 pendingOptions.getOnAnimationStartListener()); 697 break; 698 case ActivityOptions.ANIM_SCALE_UP: 699 service.mWindowManager.overridePendingAppTransitionScaleUp( 700 pendingOptions.getStartX(), pendingOptions.getStartY(), 701 pendingOptions.getStartWidth(), pendingOptions.getStartHeight()); 702 if (intent.getSourceBounds() == null) { 703 intent.setSourceBounds(new Rect(pendingOptions.getStartX(), 704 pendingOptions.getStartY(), 705 pendingOptions.getStartX()+pendingOptions.getStartWidth(), 706 pendingOptions.getStartY()+pendingOptions.getStartHeight())); 707 } 708 break; 709 case ActivityOptions.ANIM_THUMBNAIL_SCALE_UP: 710 case ActivityOptions.ANIM_THUMBNAIL_SCALE_DOWN: 711 boolean scaleUp = (animationType == ActivityOptions.ANIM_THUMBNAIL_SCALE_UP); 712 service.mWindowManager.overridePendingAppTransitionThumb( 713 pendingOptions.getThumbnail(), 714 pendingOptions.getStartX(), pendingOptions.getStartY(), 715 pendingOptions.getOnAnimationStartListener(), 716 scaleUp); 717 if (intent.getSourceBounds() == null) { 718 intent.setSourceBounds(new Rect(pendingOptions.getStartX(), 719 pendingOptions.getStartY(), 720 pendingOptions.getStartX() 721 + pendingOptions.getThumbnail().getWidth(), 722 pendingOptions.getStartY() 723 + pendingOptions.getThumbnail().getHeight())); 724 } 725 break; 726 } 727 pendingOptions = null; 728 } 729 } 730 731 ActivityOptions getOptionsForTargetActivityLocked() { 732 return pendingOptions != null ? pendingOptions.forTargetActivity() : null; 733 } 734 735 void clearOptionsLocked() { 736 if (pendingOptions != null) { 737 pendingOptions.abort(); 738 pendingOptions = null; 739 } 740 } 741 742 ActivityOptions takeOptionsLocked() { 743 ActivityOptions opts = pendingOptions; 744 pendingOptions = null; 745 return opts; 746 } 747 748 void removeUriPermissionsLocked() { 749 if (uriPermissions != null) { 750 uriPermissions.removeUriPermissionsLocked(); 751 uriPermissions = null; 752 } 753 } 754 755 void pauseKeyDispatchingLocked() { 756 if (!keysPaused) { 757 keysPaused = true; 758 service.mWindowManager.pauseKeyDispatching(appToken); 759 } 760 } 761 762 void resumeKeyDispatchingLocked() { 763 if (keysPaused) { 764 keysPaused = false; 765 service.mWindowManager.resumeKeyDispatching(appToken); 766 } 767 } 768 769 void updateThumbnail(Bitmap newThumbnail, CharSequence description) { 770 if (thumbHolder != null) { 771 if (newThumbnail != null) { 772 if (ActivityManagerService.DEBUG_THUMBNAILS) Slog.i(ActivityManagerService.TAG, 773 "Setting thumbnail of " + this + " holder " + thumbHolder 774 + " to " + newThumbnail); 775 thumbHolder.lastThumbnail = newThumbnail; 776 } 777 thumbHolder.lastDescription = description; 778 } 779 } 780 781 void startLaunchTickingLocked() { 782 if (ActivityManagerService.IS_USER_BUILD) { 783 return; 784 } 785 if (launchTickTime == 0) { 786 launchTickTime = SystemClock.uptimeMillis(); 787 continueLaunchTickingLocked(); 788 } 789 } 790 791 boolean continueLaunchTickingLocked() { 792 if (launchTickTime != 0) { 793 final ActivityStack stack = task.stack; 794 Message msg = stack.mHandler.obtainMessage(ActivityStack.LAUNCH_TICK_MSG, this); 795 stack.mHandler.removeMessages(ActivityStack.LAUNCH_TICK_MSG); 796 stack.mHandler.sendMessageDelayed(msg, ActivityStack.LAUNCH_TICK); 797 return true; 798 } 799 return false; 800 } 801 802 void finishLaunchTickingLocked() { 803 launchTickTime = 0; 804 task.stack.mHandler.removeMessages(ActivityStack.LAUNCH_TICK_MSG); 805 } 806 807 // IApplicationToken 808 809 public boolean mayFreezeScreenLocked(ProcessRecord app) { 810 // Only freeze the screen if this activity is currently attached to 811 // an application, and that application is not blocked or unresponding. 812 // In any other case, we can't count on getting the screen unfrozen, 813 // so it is best to leave as-is. 814 return app != null && !app.crashing && !app.notResponding; 815 } 816 817 public void startFreezingScreenLocked(ProcessRecord app, int configChanges) { 818 if (mayFreezeScreenLocked(app)) { 819 service.mWindowManager.startAppFreezingScreen(appToken, configChanges); 820 } 821 } 822 823 public void stopFreezingScreenLocked(boolean force) { 824 if (force || frozenBeforeDestroy) { 825 frozenBeforeDestroy = false; 826 service.mWindowManager.stopAppFreezingScreen(appToken, force); 827 } 828 } 829 830 public void reportFullyDrawnLocked() { 831 final long curTime = SystemClock.uptimeMillis(); 832 if (displayStartTime != 0) { 833 reportLaunchTimeLocked(curTime); 834 } 835 if (fullyDrawnStartTime != 0) { 836 final ActivityStack stack = task.stack; 837 final long thisTime = curTime - fullyDrawnStartTime; 838 final long totalTime = stack.mFullyDrawnStartTime != 0 839 ? (curTime - stack.mFullyDrawnStartTime) : thisTime; 840 if (ActivityManagerService.SHOW_ACTIVITY_START_TIME) { 841 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0); 842 EventLog.writeEvent(EventLogTags.AM_ACTIVITY_FULLY_DRAWN_TIME, 843 userId, System.identityHashCode(this), shortComponentName, 844 thisTime, totalTime); 845 StringBuilder sb = service.mStringBuilder; 846 sb.setLength(0); 847 sb.append("Fully drawn "); 848 sb.append(shortComponentName); 849 sb.append(": "); 850 TimeUtils.formatDuration(thisTime, sb); 851 if (thisTime != totalTime) { 852 sb.append(" (total "); 853 TimeUtils.formatDuration(totalTime, sb); 854 sb.append(")"); 855 } 856 Log.i(ActivityManagerService.TAG, sb.toString()); 857 } 858 if (totalTime > 0) { 859 service.mUsageStatsService.noteFullyDrawnTime(realActivity, (int) totalTime); 860 } 861 fullyDrawnStartTime = 0; 862 stack.mFullyDrawnStartTime = 0; 863 } 864 } 865 866 private void reportLaunchTimeLocked(final long curTime) { 867 final ActivityStack stack = task.stack; 868 final long thisTime = curTime - displayStartTime; 869 final long totalTime = stack.mLaunchStartTime != 0 870 ? (curTime - stack.mLaunchStartTime) : thisTime; 871 if (ActivityManagerService.SHOW_ACTIVITY_START_TIME) { 872 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0); 873 EventLog.writeEvent(EventLogTags.AM_ACTIVITY_LAUNCH_TIME, 874 userId, System.identityHashCode(this), shortComponentName, 875 thisTime, totalTime); 876 StringBuilder sb = service.mStringBuilder; 877 sb.setLength(0); 878 sb.append("Displayed "); 879 sb.append(shortComponentName); 880 sb.append(": "); 881 TimeUtils.formatDuration(thisTime, sb); 882 if (thisTime != totalTime) { 883 sb.append(" (total "); 884 TimeUtils.formatDuration(totalTime, sb); 885 sb.append(")"); 886 } 887 Log.i(ActivityManagerService.TAG, sb.toString()); 888 } 889 mStackSupervisor.reportActivityLaunchedLocked(false, this, thisTime, totalTime); 890 if (totalTime > 0) { 891 service.mUsageStatsService.noteLaunchTime(realActivity, (int)totalTime); 892 } 893 displayStartTime = 0; 894 stack.mLaunchStartTime = 0; 895 } 896 897 public void windowsDrawn() { 898 synchronized(service) { 899 if (displayStartTime != 0) { 900 reportLaunchTimeLocked(SystemClock.uptimeMillis()); 901 } 902 startTime = 0; 903 finishLaunchTickingLocked(); 904 } 905 } 906 907 public void windowsVisible() { 908 synchronized(service) { 909 mStackSupervisor.reportActivityVisibleLocked(this); 910 if (ActivityManagerService.DEBUG_SWITCH) Log.v( 911 ActivityManagerService.TAG, "windowsVisible(): " + this); 912 if (!nowVisible) { 913 nowVisible = true; 914 lastVisibleTime = SystemClock.uptimeMillis(); 915 if (!idle) { 916 // Instead of doing the full stop routine here, let's just 917 // hide any activities we now can, and let them stop when 918 // the normal idle happens. 919 mStackSupervisor.processStoppingActivitiesLocked(false); 920 } else { 921 // If this activity was already idle, then we now need to 922 // make sure we perform the full stop of any activities 923 // that are waiting to do so. This is because we won't 924 // do that while they are still waiting for this one to 925 // become visible. 926 final int N = mStackSupervisor.mWaitingVisibleActivities.size(); 927 if (N > 0) { 928 for (int i=0; i<N; i++) { 929 ActivityRecord r = mStackSupervisor.mWaitingVisibleActivities.get(i); 930 r.waitingVisible = false; 931 if (ActivityManagerService.DEBUG_SWITCH) Log.v( 932 ActivityManagerService.TAG, 933 "Was waiting for visible: " + r); 934 } 935 mStackSupervisor.mWaitingVisibleActivities.clear(); 936 mStackSupervisor.scheduleIdleLocked(); 937 } 938 } 939 service.scheduleAppGcsLocked(); 940 } 941 } 942 } 943 944 public void windowsGone() { 945 if (ActivityManagerService.DEBUG_SWITCH) Log.v( 946 ActivityManagerService.TAG, "windowsGone(): " + this); 947 nowVisible = false; 948 } 949 950 private ActivityRecord getWaitingHistoryRecordLocked() { 951 // First find the real culprit... if we are waiting 952 // for another app to start, then we have paused dispatching 953 // for this activity. 954 ActivityRecord r = this; 955 final ActivityStack stack = task.stack; 956 if (r.waitingVisible) { 957 // Hmmm, who might we be waiting for? 958 r = stack.mResumedActivity; 959 if (r == null) { 960 r = stack.mPausingActivity; 961 } 962 // Both of those null? Fall back to 'this' again 963 if (r == null) { 964 r = this; 965 } 966 } 967 968 return r; 969 } 970 971 public boolean keyDispatchingTimedOut(String reason) { 972 ActivityRecord r; 973 ProcessRecord anrApp; 974 synchronized(service) { 975 r = getWaitingHistoryRecordLocked(); 976 anrApp = r != null ? r.app : null; 977 } 978 return service.inputDispatchingTimedOut(anrApp, r, this, false, reason); 979 } 980 981 /** Returns the key dispatching timeout for this application token. */ 982 public long getKeyDispatchingTimeout() { 983 synchronized(service) { 984 ActivityRecord r = getWaitingHistoryRecordLocked(); 985 return ActivityManagerService.getInputDispatchingTimeoutLocked(r); 986 } 987 } 988 989 /** 990 * This method will return true if the activity is either visible, is becoming visible, is 991 * currently pausing, or is resumed. 992 */ 993 public boolean isInterestingToUserLocked() { 994 return visible || nowVisible || state == ActivityState.PAUSING || 995 state == ActivityState.RESUMED; 996 } 997 998 public void setSleeping(boolean _sleeping) { 999 if (sleeping == _sleeping) { 1000 return; 1001 } 1002 if (app != null && app.thread != null) { 1003 try { 1004 app.thread.scheduleSleeping(appToken, _sleeping); 1005 if (_sleeping && !mStackSupervisor.mGoingToSleepActivities.contains(this)) { 1006 mStackSupervisor.mGoingToSleepActivities.add(this); 1007 } 1008 sleeping = _sleeping; 1009 } catch (RemoteException e) { 1010 Slog.w(TAG, "Exception thrown when sleeping: " + intent.getComponent(), e); 1011 } 1012 } 1013 } 1014 1015 static void activityResumedLocked(IBinder token) { 1016 final ActivityRecord r = ActivityRecord.forToken(token); 1017 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Resumed activity; dropping state of: " + r); 1018 r.icicle = null; 1019 r.haveState = false; 1020 } 1021 1022 static int getTaskForActivityLocked(IBinder token, boolean onlyRoot) { 1023 final ActivityRecord r = ActivityRecord.forToken(token); 1024 if (r == null) { 1025 return -1; 1026 } 1027 final TaskRecord task = r.task; 1028 switch (task.mActivities.indexOf(r)) { 1029 case -1: return -1; 1030 case 0: return task.taskId; 1031 default: return onlyRoot ? -1 : task.taskId; 1032 } 1033 } 1034 1035 static ActivityRecord isInStackLocked(IBinder token) { 1036 final ActivityRecord r = ActivityRecord.forToken(token); 1037 if (r != null) { 1038 return r.task.stack.isInStackLocked(token); 1039 } 1040 return null; 1041 } 1042 1043 static ActivityStack getStackLocked(IBinder token) { 1044 final ActivityRecord r = ActivityRecord.isInStackLocked(token); 1045 if (r != null) { 1046 return r.task.stack; 1047 } 1048 return null; 1049 } 1050 1051 private String activityTypeToString(int type) { 1052 switch (type) { 1053 case APPLICATION_ACTIVITY_TYPE: return "APPLICATION_ACTIVITY_TYPE"; 1054 case HOME_ACTIVITY_TYPE: return "HOME_ACTIVITY_TYPE"; 1055 case RECENTS_ACTIVITY_TYPE: return "RECENTS_ACTIVITY_TYPE"; 1056 default: return Integer.toString(type); 1057 } 1058 } 1059 1060 @Override 1061 public String toString() { 1062 if (stringName != null) { 1063 return stringName + " t" + (task == null ? -1 : task.taskId) + 1064 (finishing ? " f}" : "}"); 1065 } 1066 StringBuilder sb = new StringBuilder(128); 1067 sb.append("ActivityRecord{"); 1068 sb.append(Integer.toHexString(System.identityHashCode(this))); 1069 sb.append(" u"); 1070 sb.append(userId); 1071 sb.append(' '); 1072 sb.append(intent.getComponent().flattenToShortString()); 1073 stringName = sb.toString(); 1074 return toString(); 1075 } 1076} 1077