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