1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.am; 18 19 import com.android.server.AttributeCache; 20 import com.android.server.am.ActivityStack.ActivityState; 21 22 import android.app.Activity; 23 import android.content.ComponentName; 24 import android.content.Intent; 25 import android.content.pm.ActivityInfo; 26 import android.content.pm.ApplicationInfo; 27 import android.content.res.CompatibilityInfo; 28 import android.content.res.Configuration; 29 import android.graphics.Bitmap; 30 import android.os.Build; 31 import android.os.Bundle; 32 import android.os.IBinder; 33 import android.os.Message; 34 import android.os.Process; 35 import android.os.RemoteException; 36 import android.os.SystemClock; 37 import android.util.EventLog; 38 import android.util.Log; 39 import android.util.Slog; 40 import android.util.TimeUtils; 41 import android.view.IApplicationToken; 42 import android.view.WindowManager; 43 44 import java.io.PrintWriter; 45 import java.lang.ref.WeakReference; 46 import java.util.ArrayList; 47 import java.util.HashSet; 48 49 /** 50 * An entry in the history stack, representing an activity. 51 */ 52 final class ActivityRecord { 53 final ActivityManagerService service; // owner 54 final ActivityStack stack; // owner 55 final IApplicationToken.Stub appToken; // window manager token 56 final ActivityInfo info; // all about me 57 final int launchedFromUid; // always the uid who started the activity. 58 final Intent intent; // the original intent that generated us 59 final ComponentName realActivity; // the intent component, or target of an alias. 60 final String shortComponentName; // the short component name of the intent 61 final String resolvedType; // as per original caller; 62 final String packageName; // the package implementing intent's component 63 final String processName; // process where this component wants to run 64 final String taskAffinity; // as per ActivityInfo.taskAffinity 65 final boolean stateNotNeeded; // As per ActivityInfo.flags 66 final boolean fullscreen; // covers the full screen? 67 final boolean noDisplay; // activity is not displayed? 68 final boolean componentSpecified; // did caller specifiy an explicit component? 69 final boolean isHomeActivity; // do we consider this to be a home activity? 70 final String baseDir; // where activity source (resources etc) located 71 final String resDir; // where public activity source (public resources etc) located 72 final String dataDir; // where activity data should go 73 CharSequence nonLocalizedLabel; // the label information from the package mgr. 74 int labelRes; // the label information from the package mgr. 75 int icon; // resource identifier of activity's icon. 76 int theme; // resource identifier of activity's theme. 77 int realTheme; // actual theme resource we will use, never 0. 78 int windowFlags; // custom window flags for preview window. 79 TaskRecord task; // the task this is in. 80 ThumbnailHolder thumbHolder; // where our thumbnails should go. 81 long launchTime; // when we starting launching this activity 82 long startTime; // last time this activity was started 83 long lastVisibleTime; // last time this activity became visible 84 long cpuTimeAtResume; // the cpu time of host process at the time of resuming activity 85 long pauseTime; // last time we started pausing the activity 86 long launchTickTime; // base time for launch tick messages 87 Configuration configuration; // configuration activity was last running in 88 CompatibilityInfo compat;// last used compatibility mode 89 ActivityRecord resultTo; // who started this entry, so will get our reply 90 final String resultWho; // additional identifier for use by resultTo. 91 final int requestCode; // code given by requester (resultTo) 92 ArrayList results; // pending ActivityResult objs we have received 93 HashSet<WeakReference<PendingIntentRecord>> pendingResults; // all pending intents for this act 94 ArrayList newIntents; // any pending new intents for single-top mode 95 HashSet<ConnectionRecord> connections; // All ConnectionRecord we hold 96 UriPermissionOwner uriPermissions; // current special URI access perms. 97 ProcessRecord app; // if non-null, hosting application 98 ActivityState state; // current state we are in 99 Bundle icicle; // last saved activity state 100 boolean frontOfTask; // is this the root activity of its task? 101 boolean launchFailed; // set if a launched failed, to abort on 2nd try 102 boolean haveState; // have we gotten the last activity state? 103 boolean stopped; // is activity pause finished? 104 boolean delayedResume; // not yet resumed because of stopped app switches? 105 boolean finishing; // activity in pending finish list? 106 boolean configDestroy; // need to destroy due to config change? 107 int configChangeFlags; // which config values have changed 108 boolean keysPaused; // has key dispatching been paused for it? 109 int launchMode; // the launch mode activity attribute. 110 boolean visible; // does this activity's window need to be shown? 111 boolean sleeping; // have we told the activity to sleep? 112 boolean waitingVisible; // true if waiting for a new act to become vis 113 boolean nowVisible; // is this activity's window visible? 114 boolean thumbnailNeeded;// has someone requested a thumbnail? 115 boolean idle; // has the activity gone idle? 116 boolean hasBeenLaunched;// has this activity ever been launched? 117 boolean frozenBeforeDestroy;// has been frozen but not yet destroyed. 118 boolean immersive; // immersive mode (don't interrupt if possible) 119 boolean forceNewConfig; // force re-create with new config next time 120 121 String stringName; // for caching of toString(). 122 123 private boolean inHistory; // are we in the history stack? 124 125 void dump(PrintWriter pw, String prefix) { 126 pw.print(prefix); pw.print("packageName="); pw.print(packageName); 127 pw.print(" processName="); pw.println(processName); 128 pw.print(prefix); pw.print("launchedFromUid="); pw.print(launchedFromUid); 129 pw.print(" app="); pw.println(app); 130 pw.print(prefix); pw.println(intent.toInsecureString()); 131 pw.print(prefix); pw.print("frontOfTask="); pw.print(frontOfTask); 132 pw.print(" task="); pw.println(task); 133 pw.print(prefix); pw.print("taskAffinity="); pw.println(taskAffinity); 134 pw.print(prefix); pw.print("realActivity="); 135 pw.println(realActivity.flattenToShortString()); 136 pw.print(prefix); pw.print("base="); pw.print(baseDir); 137 if (!resDir.equals(baseDir)) pw.print(" res="); pw.print(resDir); 138 pw.print(" data="); pw.println(dataDir); 139 pw.print(prefix); pw.print("labelRes=0x"); 140 pw.print(Integer.toHexString(labelRes)); 141 pw.print(" icon=0x"); pw.print(Integer.toHexString(icon)); 142 pw.print(" theme=0x"); pw.println(Integer.toHexString(theme)); 143 pw.print(prefix); pw.print("stateNotNeeded="); pw.print(stateNotNeeded); 144 pw.print(" componentSpecified="); pw.print(componentSpecified); 145 pw.print(" isHomeActivity="); pw.println(isHomeActivity); 146 pw.print(prefix); pw.print("config="); pw.println(configuration); 147 pw.print(prefix); pw.print("compat="); pw.println(compat); 148 if (resultTo != null || resultWho != null) { 149 pw.print(prefix); pw.print("resultTo="); pw.print(resultTo); 150 pw.print(" resultWho="); pw.print(resultWho); 151 pw.print(" resultCode="); pw.println(requestCode); 152 } 153 if (results != null) { 154 pw.print(prefix); pw.print("results="); pw.println(results); 155 } 156 if (pendingResults != null) { 157 pw.print(prefix); pw.print("pendingResults="); pw.println(pendingResults); 158 } 159 if (uriPermissions != null) { 160 if (uriPermissions.readUriPermissions != null) { 161 pw.print(prefix); pw.print("readUriPermissions="); 162 pw.println(uriPermissions.readUriPermissions); 163 } 164 if (uriPermissions.writeUriPermissions != null) { 165 pw.print(prefix); pw.print("writeUriPermissions="); 166 pw.println(uriPermissions.writeUriPermissions); 167 } 168 } 169 pw.print(prefix); pw.print("launchFailed="); pw.print(launchFailed); 170 pw.print(" haveState="); pw.print(haveState); 171 pw.print(" icicle="); pw.println(icicle); 172 pw.print(prefix); pw.print("state="); pw.print(state); 173 pw.print(" stopped="); pw.print(stopped); 174 pw.print(" delayedResume="); pw.print(delayedResume); 175 pw.print(" finishing="); pw.println(finishing); 176 pw.print(prefix); pw.print("keysPaused="); pw.print(keysPaused); 177 pw.print(" inHistory="); pw.print(inHistory); 178 pw.print(" visible="); pw.print(visible); 179 pw.print(" sleeping="); pw.print(sleeping); 180 pw.print(" idle="); pw.println(idle); 181 pw.print(prefix); pw.print("fullscreen="); pw.print(fullscreen); 182 pw.print(" noDisplay="); pw.print(noDisplay); 183 pw.print(" immersive="); pw.print(immersive); 184 pw.print(" launchMode="); pw.println(launchMode); 185 pw.print(prefix); pw.print("frozenBeforeDestroy="); pw.print(frozenBeforeDestroy); 186 pw.print(" thumbnailNeeded="); pw.print(thumbnailNeeded); 187 pw.print(" forceNewConfig="); pw.println(forceNewConfig); 188 pw.print(prefix); pw.print("thumbHolder="); pw.println(thumbHolder); 189 if (launchTime != 0 || startTime != 0) { 190 pw.print(prefix); pw.print("launchTime="); 191 TimeUtils.formatDuration(launchTime, pw); pw.print(" startTime="); 192 TimeUtils.formatDuration(startTime, pw); pw.println(""); 193 } 194 if (lastVisibleTime != 0) { 195 pw.print(prefix); pw.print("lastVisibleTime="); 196 TimeUtils.formatDuration(lastVisibleTime, pw); pw.println(""); 197 } 198 if (waitingVisible || nowVisible) { 199 pw.print(prefix); pw.print("waitingVisible="); pw.print(waitingVisible); 200 pw.print(" nowVisible="); pw.println(nowVisible); 201 } 202 if (configDestroy || configChangeFlags != 0) { 203 pw.print(prefix); pw.print("configDestroy="); pw.print(configDestroy); 204 pw.print(" configChangeFlags="); 205 pw.println(Integer.toHexString(configChangeFlags)); 206 } 207 if (connections != null) { 208 pw.print(prefix); pw.print("connections="); pw.println(connections); 209 } 210 } 211 212 static class Token extends IApplicationToken.Stub { 213 final WeakReference<ActivityRecord> weakActivity; 214 215 Token(ActivityRecord activity) { 216 weakActivity = new WeakReference<ActivityRecord>(activity); 217 } 218 219 @Override public void windowsDrawn() throws RemoteException { 220 ActivityRecord activity = weakActivity.get(); 221 if (activity != null) { 222 activity.windowsDrawn(); 223 } 224 } 225 226 @Override public void windowsVisible() throws RemoteException { 227 ActivityRecord activity = weakActivity.get(); 228 if (activity != null) { 229 activity.windowsVisible(); 230 } 231 } 232 233 @Override public void windowsGone() throws RemoteException { 234 ActivityRecord activity = weakActivity.get(); 235 if (activity != null) { 236 activity.windowsGone(); 237 } 238 } 239 240 @Override public boolean keyDispatchingTimedOut() throws RemoteException { 241 ActivityRecord activity = weakActivity.get(); 242 if (activity != null) { 243 return activity.keyDispatchingTimedOut(); 244 } 245 return false; 246 } 247 248 @Override public long getKeyDispatchingTimeout() throws RemoteException { 249 ActivityRecord activity = weakActivity.get(); 250 if (activity != null) { 251 return activity.getKeyDispatchingTimeout(); 252 } 253 return 0; 254 } 255 256 public String toString() { 257 StringBuilder sb = new StringBuilder(128); 258 sb.append("Token{"); 259 sb.append(Integer.toHexString(System.identityHashCode(this))); 260 sb.append(' '); 261 sb.append(weakActivity.get()); 262 sb.append('}'); 263 return sb.toString(); 264 } 265 } 266 267 static ActivityRecord forToken(IBinder token) { 268 try { 269 return token != null ? ((Token)token).weakActivity.get() : null; 270 } catch (ClassCastException e) { 271 Slog.w(ActivityManagerService.TAG, "Bad activity token: " + token, e); 272 return null; 273 } 274 } 275 276 ActivityRecord(ActivityManagerService _service, ActivityStack _stack, ProcessRecord _caller, 277 int _launchedFromUid, Intent _intent, String _resolvedType, 278 ActivityInfo aInfo, Configuration _configuration, 279 ActivityRecord _resultTo, String _resultWho, int _reqCode, 280 boolean _componentSpecified) { 281 service = _service; 282 stack = _stack; 283 appToken = new Token(this); 284 info = aInfo; 285 launchedFromUid = _launchedFromUid; 286 intent = _intent; 287 shortComponentName = _intent.getComponent().flattenToShortString(); 288 resolvedType = _resolvedType; 289 componentSpecified = _componentSpecified; 290 configuration = _configuration; 291 resultTo = _resultTo; 292 resultWho = _resultWho; 293 requestCode = _reqCode; 294 state = ActivityState.INITIALIZING; 295 frontOfTask = false; 296 launchFailed = false; 297 haveState = false; 298 stopped = false; 299 delayedResume = false; 300 finishing = false; 301 configDestroy = false; 302 keysPaused = false; 303 inHistory = false; 304 visible = true; 305 waitingVisible = false; 306 nowVisible = false; 307 thumbnailNeeded = false; 308 idle = false; 309 hasBeenLaunched = false; 310 311 if (aInfo != null) { 312 if (aInfo.targetActivity == null 313 || aInfo.launchMode == ActivityInfo.LAUNCH_MULTIPLE 314 || aInfo.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) { 315 realActivity = _intent.getComponent(); 316 } else { 317 realActivity = new ComponentName(aInfo.packageName, 318 aInfo.targetActivity); 319 } 320 taskAffinity = aInfo.taskAffinity; 321 stateNotNeeded = (aInfo.flags& 322 ActivityInfo.FLAG_STATE_NOT_NEEDED) != 0; 323 baseDir = aInfo.applicationInfo.sourceDir; 324 resDir = aInfo.applicationInfo.publicSourceDir; 325 dataDir = aInfo.applicationInfo.dataDir; 326 nonLocalizedLabel = aInfo.nonLocalizedLabel; 327 labelRes = aInfo.labelRes; 328 if (nonLocalizedLabel == null && labelRes == 0) { 329 ApplicationInfo app = aInfo.applicationInfo; 330 nonLocalizedLabel = app.nonLocalizedLabel; 331 labelRes = app.labelRes; 332 } 333 icon = aInfo.getIconResource(); 334 theme = aInfo.getThemeResource(); 335 realTheme = theme; 336 if (realTheme == 0) { 337 realTheme = aInfo.applicationInfo.targetSdkVersion 338 < Build.VERSION_CODES.HONEYCOMB 339 ? android.R.style.Theme 340 : android.R.style.Theme_Holo; 341 } 342 if ((aInfo.flags&ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) { 343 windowFlags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED; 344 } 345 if ((aInfo.flags&ActivityInfo.FLAG_MULTIPROCESS) != 0 346 && _caller != null 347 && (aInfo.applicationInfo.uid == Process.SYSTEM_UID 348 || aInfo.applicationInfo.uid == _caller.info.uid)) { 349 processName = _caller.processName; 350 } else { 351 processName = aInfo.processName; 352 } 353 354 if (intent != null && (aInfo.flags & ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS) != 0) { 355 intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 356 } 357 358 packageName = aInfo.applicationInfo.packageName; 359 launchMode = aInfo.launchMode; 360 361 AttributeCache.Entry ent = AttributeCache.instance().get(packageName, 362 realTheme, com.android.internal.R.styleable.Window); 363 fullscreen = ent != null && !ent.array.getBoolean( 364 com.android.internal.R.styleable.Window_windowIsFloating, false) 365 && !ent.array.getBoolean( 366 com.android.internal.R.styleable.Window_windowIsTranslucent, false); 367 noDisplay = ent != null && ent.array.getBoolean( 368 com.android.internal.R.styleable.Window_windowNoDisplay, false); 369 370 if (!_componentSpecified || _launchedFromUid == Process.myUid() 371 || _launchedFromUid == 0) { 372 // If we know the system has determined the component, then 373 // we can consider this to be a home activity... 374 if (Intent.ACTION_MAIN.equals(_intent.getAction()) && 375 _intent.hasCategory(Intent.CATEGORY_HOME) && 376 _intent.getCategories().size() == 1 && 377 _intent.getData() == null && 378 _intent.getType() == null && 379 (intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 && 380 !"android".equals(realActivity.getClassName())) { 381 // This sure looks like a home activity! 382 // Note the last check is so we don't count the resolver 383 // activity as being home... really, we don't care about 384 // doing anything special with something that comes from 385 // the core framework package. 386 isHomeActivity = true; 387 } else { 388 isHomeActivity = false; 389 } 390 } else { 391 isHomeActivity = false; 392 } 393 394 immersive = (aInfo.flags & ActivityInfo.FLAG_IMMERSIVE) != 0; 395 } else { 396 realActivity = null; 397 taskAffinity = null; 398 stateNotNeeded = false; 399 baseDir = null; 400 resDir = null; 401 dataDir = null; 402 processName = null; 403 packageName = null; 404 fullscreen = true; 405 noDisplay = false; 406 isHomeActivity = false; 407 immersive = false; 408 } 409 } 410 411 void setTask(TaskRecord newTask, ThumbnailHolder newThumbHolder, boolean isRoot) { 412 if (inHistory && !finishing) { 413 if (task != null) { 414 task.numActivities--; 415 } 416 if (newTask != null) { 417 newTask.numActivities++; 418 } 419 } 420 if (newThumbHolder == null) { 421 newThumbHolder = newTask; 422 } 423 task = newTask; 424 if (!isRoot && (intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) { 425 // This is the start of a new sub-task. 426 if (thumbHolder == null) { 427 thumbHolder = new ThumbnailHolder(); 428 } 429 } else { 430 thumbHolder = newThumbHolder; 431 } 432 } 433 434 void putInHistory() { 435 if (!inHistory) { 436 inHistory = true; 437 if (task != null && !finishing) { 438 task.numActivities++; 439 } 440 } 441 } 442 443 void takeFromHistory() { 444 if (inHistory) { 445 inHistory = false; 446 if (task != null && !finishing) { 447 task.numActivities--; 448 } 449 } 450 } 451 452 boolean isInHistory() { 453 return inHistory; 454 } 455 456 void makeFinishing() { 457 if (!finishing) { 458 finishing = true; 459 if (task != null && inHistory) { 460 task.numActivities--; 461 } 462 } 463 } 464 465 UriPermissionOwner getUriPermissionsLocked() { 466 if (uriPermissions == null) { 467 uriPermissions = new UriPermissionOwner(service, this); 468 } 469 return uriPermissions; 470 } 471 472 void addResultLocked(ActivityRecord from, String resultWho, 473 int requestCode, int resultCode, 474 Intent resultData) { 475 ActivityResult r = new ActivityResult(from, resultWho, 476 requestCode, resultCode, resultData); 477 if (results == null) { 478 results = new ArrayList(); 479 } 480 results.add(r); 481 } 482 483 void removeResultsLocked(ActivityRecord from, String resultWho, 484 int requestCode) { 485 if (results != null) { 486 for (int i=results.size()-1; i>=0; i--) { 487 ActivityResult r = (ActivityResult)results.get(i); 488 if (r.mFrom != from) continue; 489 if (r.mResultWho == null) { 490 if (resultWho != null) continue; 491 } else { 492 if (!r.mResultWho.equals(resultWho)) continue; 493 } 494 if (r.mRequestCode != requestCode) continue; 495 496 results.remove(i); 497 } 498 } 499 } 500 501 void addNewIntentLocked(Intent intent) { 502 if (newIntents == null) { 503 newIntents = new ArrayList(); 504 } 505 newIntents.add(intent); 506 } 507 508 /** 509 * Deliver a new Intent to an existing activity, so that its onNewIntent() 510 * method will be called at the proper time. 511 */ 512 final void deliverNewIntentLocked(int callingUid, Intent intent) { 513 boolean sent = false; 514 if (state == ActivityState.RESUMED 515 && app != null && app.thread != null) { 516 try { 517 ArrayList<Intent> ar = new ArrayList<Intent>(); 518 intent = new Intent(intent); 519 ar.add(intent); 520 service.grantUriPermissionFromIntentLocked(callingUid, packageName, 521 intent, getUriPermissionsLocked()); 522 app.thread.scheduleNewIntent(ar, appToken); 523 sent = true; 524 } catch (RemoteException e) { 525 Slog.w(ActivityManagerService.TAG, 526 "Exception thrown sending new intent to " + this, e); 527 } catch (NullPointerException e) { 528 Slog.w(ActivityManagerService.TAG, 529 "Exception thrown sending new intent to " + this, e); 530 } 531 } 532 if (!sent) { 533 addNewIntentLocked(new Intent(intent)); 534 } 535 } 536 537 void removeUriPermissionsLocked() { 538 if (uriPermissions != null) { 539 uriPermissions.removeUriPermissionsLocked(); 540 uriPermissions = null; 541 } 542 } 543 544 void pauseKeyDispatchingLocked() { 545 if (!keysPaused) { 546 keysPaused = true; 547 service.mWindowManager.pauseKeyDispatching(appToken); 548 } 549 } 550 551 void resumeKeyDispatchingLocked() { 552 if (keysPaused) { 553 keysPaused = false; 554 service.mWindowManager.resumeKeyDispatching(appToken); 555 } 556 } 557 558 void updateThumbnail(Bitmap newThumbnail, CharSequence description) { 559 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) { 560 // This is a logical break in the task; it repre 561 } 562 if (thumbHolder != null) { 563 if (newThumbnail != null) { 564 thumbHolder.lastThumbnail = newThumbnail; 565 } 566 thumbHolder.lastDescription = description; 567 } 568 } 569 570 void clearThumbnail() { 571 if (thumbHolder != null) { 572 thumbHolder.lastThumbnail = null; 573 thumbHolder.lastDescription = null; 574 } 575 } 576 577 void startLaunchTickingLocked() { 578 if (ActivityManagerService.IS_USER_BUILD) { 579 return; 580 } 581 if (launchTickTime == 0) { 582 launchTickTime = SystemClock.uptimeMillis(); 583 continueLaunchTickingLocked(); 584 } 585 } 586 587 boolean continueLaunchTickingLocked() { 588 if (launchTickTime != 0) { 589 Message msg = stack.mHandler.obtainMessage(ActivityStack.LAUNCH_TICK_MSG); 590 msg.obj = this; 591 stack.mHandler.removeMessages(ActivityStack.LAUNCH_TICK_MSG); 592 stack.mHandler.sendMessageDelayed(msg, ActivityStack.LAUNCH_TICK); 593 return true; 594 } 595 return false; 596 } 597 598 void finishLaunchTickingLocked() { 599 launchTickTime = 0; 600 stack.mHandler.removeMessages(ActivityStack.LAUNCH_TICK_MSG); 601 } 602 603 // IApplicationToken 604 605 public boolean mayFreezeScreenLocked(ProcessRecord app) { 606 // Only freeze the screen if this activity is currently attached to 607 // an application, and that application is not blocked or unresponding. 608 // In any other case, we can't count on getting the screen unfrozen, 609 // so it is best to leave as-is. 610 return app != null && !app.crashing && !app.notResponding; 611 } 612 613 public void startFreezingScreenLocked(ProcessRecord app, int configChanges) { 614 if (mayFreezeScreenLocked(app)) { 615 service.mWindowManager.startAppFreezingScreen(appToken, configChanges); 616 } 617 } 618 619 public void stopFreezingScreenLocked(boolean force) { 620 if (force || frozenBeforeDestroy) { 621 frozenBeforeDestroy = false; 622 service.mWindowManager.stopAppFreezingScreen(appToken, force); 623 } 624 } 625 626 public void windowsDrawn() { 627 synchronized(service) { 628 if (launchTime != 0) { 629 final long curTime = SystemClock.uptimeMillis(); 630 final long thisTime = curTime - launchTime; 631 final long totalTime = stack.mInitialStartTime != 0 632 ? (curTime - stack.mInitialStartTime) : thisTime; 633 if (ActivityManagerService.SHOW_ACTIVITY_START_TIME) { 634 EventLog.writeEvent(EventLogTags.ACTIVITY_LAUNCH_TIME, 635 System.identityHashCode(this), shortComponentName, 636 thisTime, totalTime); 637 StringBuilder sb = service.mStringBuilder; 638 sb.setLength(0); 639 sb.append("Displayed "); 640 sb.append(shortComponentName); 641 sb.append(": "); 642 TimeUtils.formatDuration(thisTime, sb); 643 if (thisTime != totalTime) { 644 sb.append(" (total "); 645 TimeUtils.formatDuration(totalTime, sb); 646 sb.append(")"); 647 } 648 Log.i(ActivityManagerService.TAG, sb.toString()); 649 } 650 stack.reportActivityLaunchedLocked(false, this, thisTime, totalTime); 651 if (totalTime > 0) { 652 service.mUsageStatsService.noteLaunchTime(realActivity, (int)totalTime); 653 } 654 launchTime = 0; 655 stack.mInitialStartTime = 0; 656 } 657 startTime = 0; 658 finishLaunchTickingLocked(); 659 } 660 } 661 662 public void windowsVisible() { 663 synchronized(service) { 664 stack.reportActivityVisibleLocked(this); 665 if (ActivityManagerService.DEBUG_SWITCH) Log.v( 666 ActivityManagerService.TAG, "windowsVisible(): " + this); 667 if (!nowVisible) { 668 nowVisible = true; 669 lastVisibleTime = SystemClock.uptimeMillis(); 670 if (!idle) { 671 // Instead of doing the full stop routine here, let's just 672 // hide any activities we now can, and let them stop when 673 // the normal idle happens. 674 stack.processStoppingActivitiesLocked(false); 675 } else { 676 // If this activity was already idle, then we now need to 677 // make sure we perform the full stop of any activities 678 // that are waiting to do so. This is because we won't 679 // do that while they are still waiting for this one to 680 // become visible. 681 final int N = stack.mWaitingVisibleActivities.size(); 682 if (N > 0) { 683 for (int i=0; i<N; i++) { 684 ActivityRecord r = (ActivityRecord) 685 stack.mWaitingVisibleActivities.get(i); 686 r.waitingVisible = false; 687 if (ActivityManagerService.DEBUG_SWITCH) Log.v( 688 ActivityManagerService.TAG, 689 "Was waiting for visible: " + r); 690 } 691 stack.mWaitingVisibleActivities.clear(); 692 Message msg = Message.obtain(); 693 msg.what = ActivityStack.IDLE_NOW_MSG; 694 stack.mHandler.sendMessage(msg); 695 } 696 } 697 service.scheduleAppGcsLocked(); 698 } 699 } 700 } 701 702 public void windowsGone() { 703 if (ActivityManagerService.DEBUG_SWITCH) Log.v( 704 ActivityManagerService.TAG, "windowsGone(): " + this); 705 nowVisible = false; 706 } 707 708 private ActivityRecord getWaitingHistoryRecordLocked() { 709 // First find the real culprit... if we are waiting 710 // for another app to start, then we have paused dispatching 711 // for this activity. 712 ActivityRecord r = this; 713 if (r.waitingVisible) { 714 // Hmmm, who might we be waiting for? 715 r = stack.mResumedActivity; 716 if (r == null) { 717 r = stack.mPausingActivity; 718 } 719 // Both of those null? Fall back to 'this' again 720 if (r == null) { 721 r = this; 722 } 723 } 724 725 return r; 726 } 727 728 public boolean keyDispatchingTimedOut() { 729 ActivityRecord r; 730 ProcessRecord anrApp = null; 731 synchronized(service) { 732 r = getWaitingHistoryRecordLocked(); 733 if (r != null && r.app != null) { 734 if (r.app.debugging) { 735 return false; 736 } 737 738 if (service.mDidDexOpt) { 739 // Give more time since we were dexopting. 740 service.mDidDexOpt = false; 741 return false; 742 } 743 744 if (r.app.instrumentationClass == null) { 745 anrApp = r.app; 746 } else { 747 Bundle info = new Bundle(); 748 info.putString("shortMsg", "keyDispatchingTimedOut"); 749 info.putString("longMsg", "Timed out while dispatching key event"); 750 service.finishInstrumentationLocked( 751 r.app, Activity.RESULT_CANCELED, info); 752 } 753 } 754 } 755 756 if (anrApp != null) { 757 service.appNotResponding(anrApp, r, this, 758 "keyDispatchingTimedOut"); 759 } 760 761 return true; 762 } 763 764 /** Returns the key dispatching timeout for this application token. */ 765 public long getKeyDispatchingTimeout() { 766 synchronized(service) { 767 ActivityRecord r = getWaitingHistoryRecordLocked(); 768 if (r != null && r.app != null 769 && (r.app.instrumentationClass != null || r.app.usingWrapper)) { 770 return ActivityManagerService.INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT; 771 } 772 773 return ActivityManagerService.KEY_DISPATCHING_TIMEOUT; 774 } 775 } 776 777 /** 778 * This method will return true if the activity is either visible, is becoming visible, is 779 * currently pausing, or is resumed. 780 */ 781 public boolean isInterestingToUserLocked() { 782 return visible || nowVisible || state == ActivityState.PAUSING || 783 state == ActivityState.RESUMED; 784 } 785 786 public void setSleeping(boolean _sleeping) { 787 if (sleeping == _sleeping) { 788 return; 789 } 790 if (app != null && app.thread != null) { 791 try { 792 app.thread.scheduleSleeping(appToken, _sleeping); 793 if (sleeping && !stack.mGoingToSleepActivities.contains(this)) { 794 stack.mGoingToSleepActivities.add(this); 795 } 796 sleeping = _sleeping; 797 } catch (RemoteException e) { 798 Slog.w(ActivityStack.TAG, "Exception thrown when sleeping: " 799 + intent.getComponent(), e); 800 } 801 } 802 } 803 804 public String toString() { 805 if (stringName != null) { 806 return stringName; 807 } 808 StringBuilder sb = new StringBuilder(128); 809 sb.append("ActivityRecord{"); 810 sb.append(Integer.toHexString(System.identityHashCode(this))); 811 sb.append(' '); 812 sb.append(intent.getComponent().flattenToShortString()); 813 sb.append('}'); 814 return stringName = sb.toString(); 815 } 816 } 817