BroadcastQueue.java revision a750a63d639f6936af456df904fa6b9ba941885e
1/* 2 * Copyright (C) 2012 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 java.io.FileDescriptor; 20import java.io.PrintWriter; 21import java.text.SimpleDateFormat; 22import java.util.ArrayList; 23import java.util.Date; 24import java.util.Set; 25 26import android.app.ActivityManager; 27import android.app.AppGlobals; 28import android.app.AppOpsManager; 29import android.app.BroadcastOptions; 30import android.content.ComponentName; 31import android.content.IIntentReceiver; 32import android.content.Intent; 33import android.content.pm.ActivityInfo; 34import android.content.pm.PackageManager; 35import android.content.pm.ResolveInfo; 36import android.os.Bundle; 37import android.os.Handler; 38import android.os.IBinder; 39import android.os.Looper; 40import android.os.Message; 41import android.os.Process; 42import android.os.RemoteException; 43import android.os.SystemClock; 44import android.os.UserHandle; 45import android.util.EventLog; 46import android.util.Slog; 47import com.android.server.DeviceIdleController; 48 49import static com.android.server.am.ActivityManagerDebugConfig.*; 50 51/** 52 * BROADCASTS 53 * 54 * We keep two broadcast queues and associated bookkeeping, one for those at 55 * foreground priority, and one for normal (background-priority) broadcasts. 56 */ 57public final class BroadcastQueue { 58 private static final String TAG = "BroadcastQueue"; 59 private static final String TAG_MU = TAG + POSTFIX_MU; 60 private static final String TAG_BROADCAST = TAG + POSTFIX_BROADCAST; 61 62 static final int MAX_BROADCAST_HISTORY = ActivityManager.isLowRamDeviceStatic() ? 10 : 50; 63 static final int MAX_BROADCAST_SUMMARY_HISTORY 64 = ActivityManager.isLowRamDeviceStatic() ? 25 : 300; 65 66 final ActivityManagerService mService; 67 68 /** 69 * Recognizable moniker for this queue 70 */ 71 final String mQueueName; 72 73 /** 74 * Timeout period for this queue's broadcasts 75 */ 76 final long mTimeoutPeriod; 77 78 /** 79 * If true, we can delay broadcasts while waiting services to finish in the previous 80 * receiver's process. 81 */ 82 final boolean mDelayBehindServices; 83 84 /** 85 * Lists of all active broadcasts that are to be executed immediately 86 * (without waiting for another broadcast to finish). Currently this only 87 * contains broadcasts to registered receivers, to avoid spinning up 88 * a bunch of processes to execute IntentReceiver components. Background- 89 * and foreground-priority broadcasts are queued separately. 90 */ 91 final ArrayList<BroadcastRecord> mParallelBroadcasts = new ArrayList<>(); 92 93 /** 94 * List of all active broadcasts that are to be executed one at a time. 95 * The object at the top of the list is the currently activity broadcasts; 96 * those after it are waiting for the top to finish. As with parallel 97 * broadcasts, separate background- and foreground-priority queues are 98 * maintained. 99 */ 100 final ArrayList<BroadcastRecord> mOrderedBroadcasts = new ArrayList<>(); 101 102 /** 103 * Historical data of past broadcasts, for debugging. This is a ring buffer 104 * whose last element is at mHistoryNext. 105 */ 106 final BroadcastRecord[] mBroadcastHistory = new BroadcastRecord[MAX_BROADCAST_HISTORY]; 107 int mHistoryNext = 0; 108 109 /** 110 * Summary of historical data of past broadcasts, for debugging. This is a 111 * ring buffer whose last element is at mSummaryHistoryNext. 112 */ 113 final Intent[] mBroadcastSummaryHistory = new Intent[MAX_BROADCAST_SUMMARY_HISTORY]; 114 int mSummaryHistoryNext = 0; 115 116 /** 117 * Various milestone timestamps of entries in the mBroadcastSummaryHistory ring 118 * buffer, also tracked via the mSummaryHistoryNext index. These are all in wall 119 * clock time, not elapsed. 120 */ 121 final long[] mSummaryHistoryEnqueueTime = new long[MAX_BROADCAST_SUMMARY_HISTORY]; 122 final long[] mSummaryHistoryDispatchTime = new long[MAX_BROADCAST_SUMMARY_HISTORY]; 123 final long[] mSummaryHistoryFinishTime = new long[MAX_BROADCAST_SUMMARY_HISTORY]; 124 125 /** 126 * Set when we current have a BROADCAST_INTENT_MSG in flight. 127 */ 128 boolean mBroadcastsScheduled = false; 129 130 /** 131 * True if we have a pending unexpired BROADCAST_TIMEOUT_MSG posted to our handler. 132 */ 133 boolean mPendingBroadcastTimeoutMessage; 134 135 /** 136 * Intent broadcasts that we have tried to start, but are 137 * waiting for the application's process to be created. We only 138 * need one per scheduling class (instead of a list) because we always 139 * process broadcasts one at a time, so no others can be started while 140 * waiting for this one. 141 */ 142 BroadcastRecord mPendingBroadcast = null; 143 144 /** 145 * The receiver index that is pending, to restart the broadcast if needed. 146 */ 147 int mPendingBroadcastRecvIndex; 148 149 static final int BROADCAST_INTENT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG; 150 static final int BROADCAST_TIMEOUT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG + 1; 151 static final int SCHEDULE_TEMP_WHITELIST_MSG 152 = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG + 2; 153 154 final BroadcastHandler mHandler; 155 156 private final class BroadcastHandler extends Handler { 157 public BroadcastHandler(Looper looper) { 158 super(looper, null, true); 159 } 160 161 @Override 162 public void handleMessage(Message msg) { 163 switch (msg.what) { 164 case BROADCAST_INTENT_MSG: { 165 if (DEBUG_BROADCAST) Slog.v( 166 TAG_BROADCAST, "Received BROADCAST_INTENT_MSG"); 167 processNextBroadcast(true); 168 } break; 169 case BROADCAST_TIMEOUT_MSG: { 170 synchronized (mService) { 171 broadcastTimeoutLocked(true); 172 } 173 } break; 174 case SCHEDULE_TEMP_WHITELIST_MSG: { 175 DeviceIdleController.LocalService dic = mService.mLocalDeviceIdleController; 176 if (dic != null) { 177 dic.addPowerSaveTempWhitelistAppDirect(UserHandle.getAppId(msg.arg1), 178 msg.arg2); 179 } 180 } break; 181 } 182 } 183 }; 184 185 private final class AppNotResponding implements Runnable { 186 private final ProcessRecord mApp; 187 private final String mAnnotation; 188 189 public AppNotResponding(ProcessRecord app, String annotation) { 190 mApp = app; 191 mAnnotation = annotation; 192 } 193 194 @Override 195 public void run() { 196 mService.appNotResponding(mApp, null, null, false, mAnnotation); 197 } 198 } 199 200 BroadcastQueue(ActivityManagerService service, Handler handler, 201 String name, long timeoutPeriod, boolean allowDelayBehindServices) { 202 mService = service; 203 mHandler = new BroadcastHandler(handler.getLooper()); 204 mQueueName = name; 205 mTimeoutPeriod = timeoutPeriod; 206 mDelayBehindServices = allowDelayBehindServices; 207 } 208 209 public boolean isPendingBroadcastProcessLocked(int pid) { 210 return mPendingBroadcast != null && mPendingBroadcast.curApp.pid == pid; 211 } 212 213 public void enqueueParallelBroadcastLocked(BroadcastRecord r) { 214 mParallelBroadcasts.add(r); 215 r.enqueueClockTime = System.currentTimeMillis(); 216 } 217 218 public void enqueueOrderedBroadcastLocked(BroadcastRecord r) { 219 mOrderedBroadcasts.add(r); 220 r.enqueueClockTime = System.currentTimeMillis(); 221 } 222 223 public final boolean replaceParallelBroadcastLocked(BroadcastRecord r) { 224 for (int i = mParallelBroadcasts.size() - 1; i >= 0; i--) { 225 if (r.intent.filterEquals(mParallelBroadcasts.get(i).intent)) { 226 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, 227 "***** DROPPING PARALLEL [" 228 + mQueueName + "]: " + r.intent); 229 mParallelBroadcasts.set(i, r); 230 return true; 231 } 232 } 233 return false; 234 } 235 236 public final boolean replaceOrderedBroadcastLocked(BroadcastRecord r) { 237 for (int i = mOrderedBroadcasts.size() - 1; i > 0; i--) { 238 if (r.intent.filterEquals(mOrderedBroadcasts.get(i).intent)) { 239 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, 240 "***** DROPPING ORDERED [" 241 + mQueueName + "]: " + r.intent); 242 mOrderedBroadcasts.set(i, r); 243 return true; 244 } 245 } 246 return false; 247 } 248 249 private final void processCurBroadcastLocked(BroadcastRecord r, 250 ProcessRecord app) throws RemoteException { 251 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, 252 "Process cur broadcast " + r + " for app " + app); 253 if (app.thread == null) { 254 throw new RemoteException(); 255 } 256 r.receiver = app.thread.asBinder(); 257 r.curApp = app; 258 app.curReceiver = r; 259 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_RECEIVER); 260 mService.updateLruProcessLocked(app, false, null); 261 mService.updateOomAdjLocked(); 262 263 // Tell the application to launch this receiver. 264 r.intent.setComponent(r.curComponent); 265 266 boolean started = false; 267 try { 268 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, 269 "Delivering to component " + r.curComponent 270 + ": " + r); 271 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName()); 272 app.thread.scheduleReceiver(new Intent(r.intent), r.curReceiver, 273 mService.compatibilityInfoForPackageLocked(r.curReceiver.applicationInfo), 274 r.resultCode, r.resultData, r.resultExtras, r.ordered, r.userId, 275 app.repProcState); 276 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, 277 "Process cur broadcast " + r + " DELIVERED for app " + app); 278 started = true; 279 } finally { 280 if (!started) { 281 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, 282 "Process cur broadcast " + r + ": NOT STARTED!"); 283 r.receiver = null; 284 r.curApp = null; 285 app.curReceiver = null; 286 } 287 } 288 } 289 290 public boolean sendPendingBroadcastsLocked(ProcessRecord app) { 291 boolean didSomething = false; 292 final BroadcastRecord br = mPendingBroadcast; 293 if (br != null && br.curApp.pid == app.pid) { 294 try { 295 mPendingBroadcast = null; 296 processCurBroadcastLocked(br, app); 297 didSomething = true; 298 } catch (Exception e) { 299 Slog.w(TAG, "Exception in new application when starting receiver " 300 + br.curComponent.flattenToShortString(), e); 301 logBroadcastReceiverDiscardLocked(br); 302 finishReceiverLocked(br, br.resultCode, br.resultData, 303 br.resultExtras, br.resultAbort, false); 304 scheduleBroadcastsLocked(); 305 // We need to reset the state if we failed to start the receiver. 306 br.state = BroadcastRecord.IDLE; 307 throw new RuntimeException(e.getMessage()); 308 } 309 } 310 return didSomething; 311 } 312 313 public void skipPendingBroadcastLocked(int pid) { 314 final BroadcastRecord br = mPendingBroadcast; 315 if (br != null && br.curApp.pid == pid) { 316 br.state = BroadcastRecord.IDLE; 317 br.nextReceiver = mPendingBroadcastRecvIndex; 318 mPendingBroadcast = null; 319 scheduleBroadcastsLocked(); 320 } 321 } 322 323 public void skipCurrentReceiverLocked(ProcessRecord app) { 324 BroadcastRecord r = app.curReceiver; 325 if (r != null && r.queue == this) { 326 // The current broadcast is waiting for this app's receiver 327 // to be finished. Looks like that's not going to happen, so 328 // let the broadcast continue. 329 logBroadcastReceiverDiscardLocked(r); 330 finishReceiverLocked(r, r.resultCode, r.resultData, 331 r.resultExtras, r.resultAbort, false); 332 } 333 if (r == null && mPendingBroadcast != null && mPendingBroadcast.curApp == app) { 334 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, 335 "[" + mQueueName + "] skip & discard pending app " + r); 336 r = mPendingBroadcast; 337 } 338 339 if (r != null) { 340 logBroadcastReceiverDiscardLocked(r); 341 finishReceiverLocked(r, r.resultCode, r.resultData, 342 r.resultExtras, r.resultAbort, false); 343 scheduleBroadcastsLocked(); 344 } 345 } 346 347 public void scheduleBroadcastsLocked() { 348 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Schedule broadcasts [" 349 + mQueueName + "]: current=" 350 + mBroadcastsScheduled); 351 352 if (mBroadcastsScheduled) { 353 return; 354 } 355 mHandler.sendMessage(mHandler.obtainMessage(BROADCAST_INTENT_MSG, this)); 356 mBroadcastsScheduled = true; 357 } 358 359 public BroadcastRecord getMatchingOrderedReceiver(IBinder receiver) { 360 if (mOrderedBroadcasts.size() > 0) { 361 final BroadcastRecord r = mOrderedBroadcasts.get(0); 362 if (r != null && r.receiver == receiver) { 363 return r; 364 } 365 } 366 return null; 367 } 368 369 public boolean finishReceiverLocked(BroadcastRecord r, int resultCode, 370 String resultData, Bundle resultExtras, boolean resultAbort, boolean waitForServices) { 371 final int state = r.state; 372 final ActivityInfo receiver = r.curReceiver; 373 r.state = BroadcastRecord.IDLE; 374 if (state == BroadcastRecord.IDLE) { 375 Slog.w(TAG, "finishReceiver [" + mQueueName + "] called but state is IDLE"); 376 } 377 r.receiver = null; 378 r.intent.setComponent(null); 379 if (r.curApp != null && r.curApp.curReceiver == r) { 380 r.curApp.curReceiver = null; 381 } 382 if (r.curFilter != null) { 383 r.curFilter.receiverList.curBroadcast = null; 384 } 385 r.curFilter = null; 386 r.curReceiver = null; 387 r.curApp = null; 388 mPendingBroadcast = null; 389 390 r.resultCode = resultCode; 391 r.resultData = resultData; 392 r.resultExtras = resultExtras; 393 if (resultAbort && (r.intent.getFlags()&Intent.FLAG_RECEIVER_NO_ABORT) == 0) { 394 r.resultAbort = resultAbort; 395 } else { 396 r.resultAbort = false; 397 } 398 399 if (waitForServices && r.curComponent != null && r.queue.mDelayBehindServices 400 && r.queue.mOrderedBroadcasts.size() > 0 401 && r.queue.mOrderedBroadcasts.get(0) == r) { 402 ActivityInfo nextReceiver; 403 if (r.nextReceiver < r.receivers.size()) { 404 Object obj = r.receivers.get(r.nextReceiver); 405 nextReceiver = (obj instanceof ActivityInfo) ? (ActivityInfo)obj : null; 406 } else { 407 nextReceiver = null; 408 } 409 // Don't do this if the next receive is in the same process as the current one. 410 if (receiver == null || nextReceiver == null 411 || receiver.applicationInfo.uid != nextReceiver.applicationInfo.uid 412 || !receiver.processName.equals(nextReceiver.processName)) { 413 // In this case, we are ready to process the next receiver for the current broadcast, 414 // but are on a queue that would like to wait for services to finish before moving 415 // on. If there are background services currently starting, then we will go into a 416 // special state where we hold off on continuing this broadcast until they are done. 417 if (mService.mServices.hasBackgroundServices(r.userId)) { 418 Slog.i(TAG, "Delay finish: " + r.curComponent.flattenToShortString()); 419 r.state = BroadcastRecord.WAITING_SERVICES; 420 return false; 421 } 422 } 423 } 424 425 r.curComponent = null; 426 427 // We will process the next receiver right now if this is finishing 428 // an app receiver (which is always asynchronous) or after we have 429 // come back from calling a receiver. 430 return state == BroadcastRecord.APP_RECEIVE 431 || state == BroadcastRecord.CALL_DONE_RECEIVE; 432 } 433 434 public void backgroundServicesFinishedLocked(int userId) { 435 if (mOrderedBroadcasts.size() > 0) { 436 BroadcastRecord br = mOrderedBroadcasts.get(0); 437 if (br.userId == userId && br.state == BroadcastRecord.WAITING_SERVICES) { 438 Slog.i(TAG, "Resuming delayed broadcast"); 439 br.curComponent = null; 440 br.state = BroadcastRecord.IDLE; 441 processNextBroadcast(false); 442 } 443 } 444 } 445 446 private static void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver, 447 Intent intent, int resultCode, String data, Bundle extras, 448 boolean ordered, boolean sticky, int sendingUser) throws RemoteException { 449 // Send the intent to the receiver asynchronously using one-way binder calls. 450 if (app != null) { 451 if (app.thread != null) { 452 // If we have an app thread, do the call through that so it is 453 // correctly ordered with other one-way calls. 454 app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode, 455 data, extras, ordered, sticky, sendingUser, app.repProcState); 456 } else { 457 // Application has died. Receiver doesn't exist. 458 throw new RemoteException("app.thread must not be null"); 459 } 460 } else { 461 receiver.performReceive(intent, resultCode, data, extras, ordered, 462 sticky, sendingUser); 463 } 464 } 465 466 private final void deliverToRegisteredReceiverLocked(BroadcastRecord r, 467 BroadcastFilter filter, boolean ordered) { 468 boolean skip = false; 469 if (filter.requiredPermission != null) { 470 int perm = mService.checkComponentPermission(filter.requiredPermission, 471 r.callingPid, r.callingUid, -1, true); 472 if (perm != PackageManager.PERMISSION_GRANTED) { 473 Slog.w(TAG, "Permission Denial: broadcasting " 474 + r.intent.toString() 475 + " from " + r.callerPackage + " (pid=" 476 + r.callingPid + ", uid=" + r.callingUid + ")" 477 + " requires " + filter.requiredPermission 478 + " due to registered receiver " + filter); 479 skip = true; 480 } 481 } 482 if (!skip && r.requiredPermission != null) { 483 int perm = mService.checkComponentPermission(r.requiredPermission, 484 filter.receiverList.pid, filter.receiverList.uid, -1, true); 485 if (perm != PackageManager.PERMISSION_GRANTED) { 486 Slog.w(TAG, "Permission Denial: receiving " 487 + r.intent.toString() 488 + " to " + filter.receiverList.app 489 + " (pid=" + filter.receiverList.pid 490 + ", uid=" + filter.receiverList.uid + ")" 491 + " requires " + r.requiredPermission 492 + " due to sender " + r.callerPackage 493 + " (uid " + r.callingUid + ")"); 494 skip = true; 495 } 496 } 497 if (r.appOp != AppOpsManager.OP_NONE) { 498 int mode = mService.mAppOpsService.noteOperation(r.appOp, 499 filter.receiverList.uid, filter.packageName); 500 if (mode != AppOpsManager.MODE_ALLOWED) { 501 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, 502 "App op " + r.appOp + " not allowed for broadcast to uid " 503 + filter.receiverList.uid + " pkg " + filter.packageName); 504 skip = true; 505 } 506 } 507 if (!skip) { 508 skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid, 509 r.callingPid, r.resolvedType, filter.receiverList.uid); 510 } 511 512 if (filter.receiverList.app == null || filter.receiverList.app.crashing) { 513 Slog.w(TAG, "Skipping deliver [" + mQueueName + "] " + r 514 + " to " + filter.receiverList + ": process crashing"); 515 skip = true; 516 } 517 518 if (!skip) { 519 // If this is not being sent as an ordered broadcast, then we 520 // don't want to touch the fields that keep track of the current 521 // state of ordered broadcasts. 522 if (ordered) { 523 r.receiver = filter.receiverList.receiver.asBinder(); 524 r.curFilter = filter; 525 filter.receiverList.curBroadcast = r; 526 r.state = BroadcastRecord.CALL_IN_RECEIVE; 527 if (filter.receiverList.app != null) { 528 // Bump hosting application to no longer be in background 529 // scheduling class. Note that we can't do that if there 530 // isn't an app... but we can only be in that case for 531 // things that directly call the IActivityManager API, which 532 // are already core system stuff so don't matter for this. 533 r.curApp = filter.receiverList.app; 534 filter.receiverList.app.curReceiver = r; 535 mService.updateOomAdjLocked(r.curApp); 536 } 537 } 538 try { 539 if (DEBUG_BROADCAST_LIGHT) Slog.i(TAG_BROADCAST, 540 "Delivering to " + filter + " : " + r); 541 performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver, 542 new Intent(r.intent), r.resultCode, r.resultData, 543 r.resultExtras, r.ordered, r.initialSticky, r.userId); 544 if (ordered) { 545 r.state = BroadcastRecord.CALL_DONE_RECEIVE; 546 } 547 } catch (RemoteException e) { 548 Slog.w(TAG, "Failure sending broadcast " + r.intent, e); 549 if (ordered) { 550 r.receiver = null; 551 r.curFilter = null; 552 filter.receiverList.curBroadcast = null; 553 if (filter.receiverList.app != null) { 554 filter.receiverList.app.curReceiver = null; 555 } 556 } 557 } 558 } 559 } 560 561 final void scheduleTempWhitelistLocked(int uid, long duration) { 562 if (duration > Integer.MAX_VALUE) { 563 duration = Integer.MAX_VALUE; 564 } 565 // XXX ideally we should pause the broadcast until everything behind this is done, 566 // or else we will likely start dispatching the broadcast before we have opened 567 // access to the app (there is a lot of asynchronicity behind this). It is probably 568 // not that big a deal, however, because the main purpose here is to allow apps 569 // to hold wake locks, and they will be able to acquire their wake lock immediately 570 // it just won't be enabled until we get through this work. 571 mHandler.obtainMessage(SCHEDULE_TEMP_WHITELIST_MSG, uid, (int)duration).sendToTarget(); 572 } 573 574 final void processNextBroadcast(boolean fromMsg) { 575 synchronized(mService) { 576 BroadcastRecord r; 577 578 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "processNextBroadcast [" 579 + mQueueName + "]: " 580 + mParallelBroadcasts.size() + " broadcasts, " 581 + mOrderedBroadcasts.size() + " ordered broadcasts"); 582 583 mService.updateCpuStats(); 584 585 if (fromMsg) { 586 mBroadcastsScheduled = false; 587 } 588 589 // First, deliver any non-serialized broadcasts right away. 590 while (mParallelBroadcasts.size() > 0) { 591 r = mParallelBroadcasts.remove(0); 592 r.dispatchTime = SystemClock.uptimeMillis(); 593 r.dispatchClockTime = System.currentTimeMillis(); 594 final int N = r.receivers.size(); 595 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Processing parallel broadcast [" 596 + mQueueName + "] " + r); 597 for (int i=0; i<N; i++) { 598 Object target = r.receivers.get(i); 599 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, 600 "Delivering non-ordered on [" + mQueueName + "] to registered " 601 + target + ": " + r); 602 deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false); 603 } 604 addBroadcastToHistoryLocked(r); 605 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Done with parallel broadcast [" 606 + mQueueName + "] " + r); 607 } 608 609 // Now take care of the next serialized one... 610 611 // If we are waiting for a process to come up to handle the next 612 // broadcast, then do nothing at this point. Just in case, we 613 // check that the process we're waiting for still exists. 614 if (mPendingBroadcast != null) { 615 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, 616 "processNextBroadcast [" + mQueueName + "]: waiting for " 617 + mPendingBroadcast.curApp); 618 619 boolean isDead; 620 synchronized (mService.mPidsSelfLocked) { 621 ProcessRecord proc = mService.mPidsSelfLocked.get(mPendingBroadcast.curApp.pid); 622 isDead = proc == null || proc.crashing; 623 } 624 if (!isDead) { 625 // It's still alive, so keep waiting 626 return; 627 } else { 628 Slog.w(TAG, "pending app [" 629 + mQueueName + "]" + mPendingBroadcast.curApp 630 + " died before responding to broadcast"); 631 mPendingBroadcast.state = BroadcastRecord.IDLE; 632 mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex; 633 mPendingBroadcast = null; 634 } 635 } 636 637 boolean looped = false; 638 639 do { 640 if (mOrderedBroadcasts.size() == 0) { 641 // No more broadcasts pending, so all done! 642 mService.scheduleAppGcsLocked(); 643 if (looped) { 644 // If we had finished the last ordered broadcast, then 645 // make sure all processes have correct oom and sched 646 // adjustments. 647 mService.updateOomAdjLocked(); 648 } 649 return; 650 } 651 r = mOrderedBroadcasts.get(0); 652 boolean forceReceive = false; 653 654 // Ensure that even if something goes awry with the timeout 655 // detection, we catch "hung" broadcasts here, discard them, 656 // and continue to make progress. 657 // 658 // This is only done if the system is ready so that PRE_BOOT_COMPLETED 659 // receivers don't get executed with timeouts. They're intended for 660 // one time heavy lifting after system upgrades and can take 661 // significant amounts of time. 662 int numReceivers = (r.receivers != null) ? r.receivers.size() : 0; 663 if (mService.mProcessesReady && r.dispatchTime > 0) { 664 long now = SystemClock.uptimeMillis(); 665 if ((numReceivers > 0) && 666 (now > r.dispatchTime + (2*mTimeoutPeriod*numReceivers))) { 667 Slog.w(TAG, "Hung broadcast [" 668 + mQueueName + "] discarded after timeout failure:" 669 + " now=" + now 670 + " dispatchTime=" + r.dispatchTime 671 + " startTime=" + r.receiverTime 672 + " intent=" + r.intent 673 + " numReceivers=" + numReceivers 674 + " nextReceiver=" + r.nextReceiver 675 + " state=" + r.state); 676 broadcastTimeoutLocked(false); // forcibly finish this broadcast 677 forceReceive = true; 678 r.state = BroadcastRecord.IDLE; 679 } 680 } 681 682 if (r.state != BroadcastRecord.IDLE) { 683 if (DEBUG_BROADCAST) Slog.d(TAG_BROADCAST, 684 "processNextBroadcast(" 685 + mQueueName + ") called when not idle (state=" 686 + r.state + ")"); 687 return; 688 } 689 690 if (r.receivers == null || r.nextReceiver >= numReceivers 691 || r.resultAbort || forceReceive) { 692 // No more receivers for this broadcast! Send the final 693 // result if requested... 694 if (r.resultTo != null) { 695 try { 696 if (DEBUG_BROADCAST) Slog.i(TAG_BROADCAST, 697 "Finishing broadcast [" + mQueueName + "] " 698 + r.intent.getAction() + " app=" + r.callerApp); 699 performReceiveLocked(r.callerApp, r.resultTo, 700 new Intent(r.intent), r.resultCode, 701 r.resultData, r.resultExtras, false, false, r.userId); 702 // Set this to null so that the reference 703 // (local and remote) isn't kept in the mBroadcastHistory. 704 r.resultTo = null; 705 } catch (RemoteException e) { 706 r.resultTo = null; 707 Slog.w(TAG, "Failure [" 708 + mQueueName + "] sending broadcast result of " 709 + r.intent, e); 710 } 711 } 712 713 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Cancelling BROADCAST_TIMEOUT_MSG"); 714 cancelBroadcastTimeoutLocked(); 715 716 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, 717 "Finished with ordered broadcast " + r); 718 719 // ... and on to the next... 720 addBroadcastToHistoryLocked(r); 721 mOrderedBroadcasts.remove(0); 722 r = null; 723 looped = true; 724 continue; 725 } 726 } while (r == null); 727 728 // Get the next receiver... 729 int recIdx = r.nextReceiver++; 730 731 // Keep track of when this receiver started, and make sure there 732 // is a timeout message pending to kill it if need be. 733 r.receiverTime = SystemClock.uptimeMillis(); 734 if (recIdx == 0) { 735 r.dispatchTime = r.receiverTime; 736 r.dispatchClockTime = System.currentTimeMillis(); 737 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Processing ordered broadcast [" 738 + mQueueName + "] " + r); 739 } 740 if (! mPendingBroadcastTimeoutMessage) { 741 long timeoutTime = r.receiverTime + mTimeoutPeriod; 742 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, 743 "Submitting BROADCAST_TIMEOUT_MSG [" 744 + mQueueName + "] for " + r + " at " + timeoutTime); 745 setBroadcastTimeoutLocked(timeoutTime); 746 } 747 748 final BroadcastOptions brOptions = r.options; 749 final Object nextReceiver = r.receivers.get(recIdx); 750 751 if (nextReceiver instanceof BroadcastFilter) { 752 // Simple case: this is a registered receiver who gets 753 // a direct call. 754 BroadcastFilter filter = (BroadcastFilter)nextReceiver; 755 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, 756 "Delivering ordered [" 757 + mQueueName + "] to registered " 758 + filter + ": " + r); 759 deliverToRegisteredReceiverLocked(r, filter, r.ordered); 760 if (r.receiver == null || !r.ordered) { 761 // The receiver has already finished, so schedule to 762 // process the next one. 763 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Quick finishing [" 764 + mQueueName + "]: ordered=" 765 + r.ordered + " receiver=" + r.receiver); 766 r.state = BroadcastRecord.IDLE; 767 scheduleBroadcastsLocked(); 768 } else { 769 if (brOptions != null && brOptions.getTemporaryAppWhitelistDuration() > 0) { 770 scheduleTempWhitelistLocked(filter.owningUid, 771 brOptions.getTemporaryAppWhitelistDuration()); 772 } 773 } 774 return; 775 } 776 777 // Hard case: need to instantiate the receiver, possibly 778 // starting its application process to host it. 779 780 ResolveInfo info = 781 (ResolveInfo)nextReceiver; 782 ComponentName component = new ComponentName( 783 info.activityInfo.applicationInfo.packageName, 784 info.activityInfo.name); 785 786 boolean skip = false; 787 int perm = mService.checkComponentPermission(info.activityInfo.permission, 788 r.callingPid, r.callingUid, info.activityInfo.applicationInfo.uid, 789 info.activityInfo.exported); 790 if (perm != PackageManager.PERMISSION_GRANTED) { 791 if (!info.activityInfo.exported) { 792 Slog.w(TAG, "Permission Denial: broadcasting " 793 + r.intent.toString() 794 + " from " + r.callerPackage + " (pid=" + r.callingPid 795 + ", uid=" + r.callingUid + ")" 796 + " is not exported from uid " + info.activityInfo.applicationInfo.uid 797 + " due to receiver " + component.flattenToShortString()); 798 } else { 799 Slog.w(TAG, "Permission Denial: broadcasting " 800 + r.intent.toString() 801 + " from " + r.callerPackage + " (pid=" + r.callingPid 802 + ", uid=" + r.callingUid + ")" 803 + " requires " + info.activityInfo.permission 804 + " due to receiver " + component.flattenToShortString()); 805 } 806 skip = true; 807 } 808 if (info.activityInfo.applicationInfo.uid != Process.SYSTEM_UID && 809 r.requiredPermission != null) { 810 try { 811 perm = AppGlobals.getPackageManager(). 812 checkPermission(r.requiredPermission, 813 info.activityInfo.applicationInfo.packageName, 814 UserHandle.getUserId(info.activityInfo.applicationInfo.uid)); 815 } catch (RemoteException e) { 816 perm = PackageManager.PERMISSION_DENIED; 817 } 818 if (perm != PackageManager.PERMISSION_GRANTED) { 819 Slog.w(TAG, "Permission Denial: receiving " 820 + r.intent + " to " 821 + component.flattenToShortString() 822 + " requires " + r.requiredPermission 823 + " due to sender " + r.callerPackage 824 + " (uid " + r.callingUid + ")"); 825 skip = true; 826 } 827 } 828 if (r.appOp != AppOpsManager.OP_NONE) { 829 int mode = mService.mAppOpsService.noteOperation(r.appOp, 830 info.activityInfo.applicationInfo.uid, info.activityInfo.packageName); 831 if (mode != AppOpsManager.MODE_ALLOWED) { 832 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, 833 "App op " + r.appOp + " not allowed for broadcast to uid " 834 + info.activityInfo.applicationInfo.uid + " pkg " 835 + info.activityInfo.packageName); 836 skip = true; 837 } 838 } 839 if (!skip) { 840 skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid, 841 r.callingPid, r.resolvedType, info.activityInfo.applicationInfo.uid); 842 } 843 boolean isSingleton = false; 844 try { 845 isSingleton = mService.isSingleton(info.activityInfo.processName, 846 info.activityInfo.applicationInfo, 847 info.activityInfo.name, info.activityInfo.flags); 848 } catch (SecurityException e) { 849 Slog.w(TAG, e.getMessage()); 850 skip = true; 851 } 852 if ((info.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) { 853 if (ActivityManager.checkUidPermission( 854 android.Manifest.permission.INTERACT_ACROSS_USERS, 855 info.activityInfo.applicationInfo.uid) 856 != PackageManager.PERMISSION_GRANTED) { 857 Slog.w(TAG, "Permission Denial: Receiver " + component.flattenToShortString() 858 + " requests FLAG_SINGLE_USER, but app does not hold " 859 + android.Manifest.permission.INTERACT_ACROSS_USERS); 860 skip = true; 861 } 862 } 863 if (r.curApp != null && r.curApp.crashing) { 864 // If the target process is crashing, just skip it. 865 Slog.w(TAG, "Skipping deliver ordered [" + mQueueName + "] " + r 866 + " to " + r.curApp + ": process crashing"); 867 skip = true; 868 } 869 if (!skip) { 870 boolean isAvailable = false; 871 try { 872 isAvailable = AppGlobals.getPackageManager().isPackageAvailable( 873 info.activityInfo.packageName, 874 UserHandle.getUserId(info.activityInfo.applicationInfo.uid)); 875 } catch (Exception e) { 876 // all such failures mean we skip this receiver 877 Slog.w(TAG, "Exception getting recipient info for " 878 + info.activityInfo.packageName, e); 879 } 880 if (!isAvailable) { 881 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, 882 "Skipping delivery to " + info.activityInfo.packageName + " / " 883 + info.activityInfo.applicationInfo.uid 884 + " : package no longer available"); 885 skip = true; 886 } 887 } 888 889 if (skip) { 890 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, 891 "Skipping delivery of ordered [" + mQueueName + "] " 892 + r + " for whatever reason"); 893 r.receiver = null; 894 r.curFilter = null; 895 r.state = BroadcastRecord.IDLE; 896 scheduleBroadcastsLocked(); 897 return; 898 } 899 900 r.state = BroadcastRecord.APP_RECEIVE; 901 String targetProcess = info.activityInfo.processName; 902 r.curComponent = component; 903 final int receiverUid = info.activityInfo.applicationInfo.uid; 904 // If it's a singleton, it needs to be the same app or a special app 905 if (r.callingUid != Process.SYSTEM_UID && isSingleton 906 && mService.isValidSingletonCall(r.callingUid, receiverUid)) { 907 info.activityInfo = mService.getActivityInfoForUser(info.activityInfo, 0); 908 } 909 r.curReceiver = info.activityInfo; 910 if (DEBUG_MU && r.callingUid > UserHandle.PER_USER_RANGE) { 911 Slog.v(TAG_MU, "Updated broadcast record activity info for secondary user, " 912 + info.activityInfo + ", callingUid = " + r.callingUid + ", uid = " 913 + info.activityInfo.applicationInfo.uid); 914 } 915 916 if (brOptions != null && brOptions.getTemporaryAppWhitelistDuration() > 0) { 917 scheduleTempWhitelistLocked(receiverUid, 918 brOptions.getTemporaryAppWhitelistDuration()); 919 } 920 921 // Broadcast is being executed, its package can't be stopped. 922 try { 923 AppGlobals.getPackageManager().setPackageStoppedState( 924 r.curComponent.getPackageName(), false, UserHandle.getUserId(r.callingUid)); 925 } catch (RemoteException e) { 926 } catch (IllegalArgumentException e) { 927 Slog.w(TAG, "Failed trying to unstop package " 928 + r.curComponent.getPackageName() + ": " + e); 929 } 930 931 // Is this receiver's application already running? 932 ProcessRecord app = mService.getProcessRecordLocked(targetProcess, 933 info.activityInfo.applicationInfo.uid, false); 934 if (app != null && app.thread != null) { 935 try { 936 app.addPackage(info.activityInfo.packageName, 937 info.activityInfo.applicationInfo.versionCode, mService.mProcessStats); 938 processCurBroadcastLocked(r, app); 939 return; 940 } catch (RemoteException e) { 941 Slog.w(TAG, "Exception when sending broadcast to " 942 + r.curComponent, e); 943 } catch (RuntimeException e) { 944 Slog.wtf(TAG, "Failed sending broadcast to " 945 + r.curComponent + " with " + r.intent, e); 946 // If some unexpected exception happened, just skip 947 // this broadcast. At this point we are not in the call 948 // from a client, so throwing an exception out from here 949 // will crash the entire system instead of just whoever 950 // sent the broadcast. 951 logBroadcastReceiverDiscardLocked(r); 952 finishReceiverLocked(r, r.resultCode, r.resultData, 953 r.resultExtras, r.resultAbort, false); 954 scheduleBroadcastsLocked(); 955 // We need to reset the state if we failed to start the receiver. 956 r.state = BroadcastRecord.IDLE; 957 return; 958 } 959 960 // If a dead object exception was thrown -- fall through to 961 // restart the application. 962 } 963 964 // Not running -- get it started, to be executed when the app comes up. 965 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, 966 "Need to start app [" 967 + mQueueName + "] " + targetProcess + " for broadcast " + r); 968 if ((r.curApp=mService.startProcessLocked(targetProcess, 969 info.activityInfo.applicationInfo, true, 970 r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND, 971 "broadcast", r.curComponent, 972 (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0, false, false)) 973 == null) { 974 // Ah, this recipient is unavailable. Finish it if necessary, 975 // and mark the broadcast record as ready for the next. 976 Slog.w(TAG, "Unable to launch app " 977 + info.activityInfo.applicationInfo.packageName + "/" 978 + info.activityInfo.applicationInfo.uid + " for broadcast " 979 + r.intent + ": process is bad"); 980 logBroadcastReceiverDiscardLocked(r); 981 finishReceiverLocked(r, r.resultCode, r.resultData, 982 r.resultExtras, r.resultAbort, false); 983 scheduleBroadcastsLocked(); 984 r.state = BroadcastRecord.IDLE; 985 return; 986 } 987 988 mPendingBroadcast = r; 989 mPendingBroadcastRecvIndex = recIdx; 990 } 991 } 992 993 final void setBroadcastTimeoutLocked(long timeoutTime) { 994 if (! mPendingBroadcastTimeoutMessage) { 995 Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG, this); 996 mHandler.sendMessageAtTime(msg, timeoutTime); 997 mPendingBroadcastTimeoutMessage = true; 998 } 999 } 1000 1001 final void cancelBroadcastTimeoutLocked() { 1002 if (mPendingBroadcastTimeoutMessage) { 1003 mHandler.removeMessages(BROADCAST_TIMEOUT_MSG, this); 1004 mPendingBroadcastTimeoutMessage = false; 1005 } 1006 } 1007 1008 final void broadcastTimeoutLocked(boolean fromMsg) { 1009 if (fromMsg) { 1010 mPendingBroadcastTimeoutMessage = false; 1011 } 1012 1013 if (mOrderedBroadcasts.size() == 0) { 1014 return; 1015 } 1016 1017 long now = SystemClock.uptimeMillis(); 1018 BroadcastRecord r = mOrderedBroadcasts.get(0); 1019 if (fromMsg) { 1020 if (mService.mDidDexOpt) { 1021 // Delay timeouts until dexopt finishes. 1022 mService.mDidDexOpt = false; 1023 long timeoutTime = SystemClock.uptimeMillis() + mTimeoutPeriod; 1024 setBroadcastTimeoutLocked(timeoutTime); 1025 return; 1026 } 1027 if (!mService.mProcessesReady) { 1028 // Only process broadcast timeouts if the system is ready. That way 1029 // PRE_BOOT_COMPLETED broadcasts can't timeout as they are intended 1030 // to do heavy lifting for system up. 1031 return; 1032 } 1033 1034 long timeoutTime = r.receiverTime + mTimeoutPeriod; 1035 if (timeoutTime > now) { 1036 // We can observe premature timeouts because we do not cancel and reset the 1037 // broadcast timeout message after each receiver finishes. Instead, we set up 1038 // an initial timeout then kick it down the road a little further as needed 1039 // when it expires. 1040 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, 1041 "Premature timeout [" 1042 + mQueueName + "] @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for " 1043 + timeoutTime); 1044 setBroadcastTimeoutLocked(timeoutTime); 1045 return; 1046 } 1047 } 1048 1049 BroadcastRecord br = mOrderedBroadcasts.get(0); 1050 if (br.state == BroadcastRecord.WAITING_SERVICES) { 1051 // In this case the broadcast had already finished, but we had decided to wait 1052 // for started services to finish as well before going on. So if we have actually 1053 // waited long enough time timeout the broadcast, let's give up on the whole thing 1054 // and just move on to the next. 1055 Slog.i(TAG, "Waited long enough for: " + (br.curComponent != null 1056 ? br.curComponent.flattenToShortString() : "(null)")); 1057 br.curComponent = null; 1058 br.state = BroadcastRecord.IDLE; 1059 processNextBroadcast(false); 1060 return; 1061 } 1062 1063 Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r. receiver 1064 + ", started " + (now - r.receiverTime) + "ms ago"); 1065 r.receiverTime = now; 1066 r.anrCount++; 1067 1068 // Current receiver has passed its expiration date. 1069 if (r.nextReceiver <= 0) { 1070 Slog.w(TAG, "Timeout on receiver with nextReceiver <= 0"); 1071 return; 1072 } 1073 1074 ProcessRecord app = null; 1075 String anrMessage = null; 1076 1077 Object curReceiver = r.receivers.get(r.nextReceiver-1); 1078 Slog.w(TAG, "Receiver during timeout: " + curReceiver); 1079 logBroadcastReceiverDiscardLocked(r); 1080 if (curReceiver instanceof BroadcastFilter) { 1081 BroadcastFilter bf = (BroadcastFilter)curReceiver; 1082 if (bf.receiverList.pid != 0 1083 && bf.receiverList.pid != ActivityManagerService.MY_PID) { 1084 synchronized (mService.mPidsSelfLocked) { 1085 app = mService.mPidsSelfLocked.get( 1086 bf.receiverList.pid); 1087 } 1088 } 1089 } else { 1090 app = r.curApp; 1091 } 1092 1093 if (app != null) { 1094 anrMessage = "Broadcast of " + r.intent.toString(); 1095 } 1096 1097 if (mPendingBroadcast == r) { 1098 mPendingBroadcast = null; 1099 } 1100 1101 // Move on to the next receiver. 1102 finishReceiverLocked(r, r.resultCode, r.resultData, 1103 r.resultExtras, r.resultAbort, false); 1104 scheduleBroadcastsLocked(); 1105 1106 if (anrMessage != null) { 1107 // Post the ANR to the handler since we do not want to process ANRs while 1108 // potentially holding our lock. 1109 mHandler.post(new AppNotResponding(app, anrMessage)); 1110 } 1111 } 1112 1113 private final int ringAdvance(int x, final int increment, final int ringSize) { 1114 x += increment; 1115 if (x < 0) return (ringSize - 1); 1116 else if (x >= ringSize) return 0; 1117 else return x; 1118 } 1119 1120 private final void addBroadcastToHistoryLocked(BroadcastRecord r) { 1121 if (r.callingUid < 0) { 1122 // This was from a registerReceiver() call; ignore it. 1123 return; 1124 } 1125 r.finishTime = SystemClock.uptimeMillis(); 1126 1127 mBroadcastHistory[mHistoryNext] = r; 1128 mHistoryNext = ringAdvance(mHistoryNext, 1, MAX_BROADCAST_HISTORY); 1129 1130 mBroadcastSummaryHistory[mSummaryHistoryNext] = r.intent; 1131 mSummaryHistoryEnqueueTime[mSummaryHistoryNext] = r.enqueueClockTime; 1132 mSummaryHistoryDispatchTime[mSummaryHistoryNext] = r.dispatchClockTime; 1133 mSummaryHistoryFinishTime[mSummaryHistoryNext] = System.currentTimeMillis(); 1134 mSummaryHistoryNext = ringAdvance(mSummaryHistoryNext, 1, MAX_BROADCAST_SUMMARY_HISTORY); 1135 } 1136 1137 boolean cleanupDisabledPackageReceiversLocked( 1138 String packageName, Set<String> filterByClasses, int userId, boolean doit) { 1139 boolean didSomething = false; 1140 for (int i = mParallelBroadcasts.size() - 1; i >= 0; i--) { 1141 didSomething |= mParallelBroadcasts.get(i).cleanupDisabledPackageReceiversLocked( 1142 packageName, filterByClasses, userId, doit); 1143 if (!doit && didSomething) { 1144 return true; 1145 } 1146 } 1147 1148 for (int i = mOrderedBroadcasts.size() - 1; i >= 0; i--) { 1149 didSomething |= mOrderedBroadcasts.get(i).cleanupDisabledPackageReceiversLocked( 1150 packageName, filterByClasses, userId, doit); 1151 if (!doit && didSomething) { 1152 return true; 1153 } 1154 } 1155 1156 return didSomething; 1157 } 1158 1159 final void logBroadcastReceiverDiscardLocked(BroadcastRecord r) { 1160 final int logIndex = r.nextReceiver - 1; 1161 if (logIndex >= 0 && logIndex < r.receivers.size()) { 1162 Object curReceiver = r.receivers.get(logIndex); 1163 if (curReceiver instanceof BroadcastFilter) { 1164 BroadcastFilter bf = (BroadcastFilter) curReceiver; 1165 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER, 1166 bf.owningUserId, System.identityHashCode(r), 1167 r.intent.getAction(), logIndex, System.identityHashCode(bf)); 1168 } else { 1169 ResolveInfo ri = (ResolveInfo) curReceiver; 1170 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP, 1171 UserHandle.getUserId(ri.activityInfo.applicationInfo.uid), 1172 System.identityHashCode(r), r.intent.getAction(), logIndex, ri.toString()); 1173 } 1174 } else { 1175 if (logIndex < 0) Slog.w(TAG, 1176 "Discarding broadcast before first receiver is invoked: " + r); 1177 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP, 1178 -1, System.identityHashCode(r), 1179 r.intent.getAction(), 1180 r.nextReceiver, 1181 "NONE"); 1182 } 1183 } 1184 1185 final boolean dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args, 1186 int opti, boolean dumpAll, String dumpPackage, boolean needSep) { 1187 if (mParallelBroadcasts.size() > 0 || mOrderedBroadcasts.size() > 0 1188 || mPendingBroadcast != null) { 1189 boolean printed = false; 1190 for (int i = mParallelBroadcasts.size() - 1; i >= 0; i--) { 1191 BroadcastRecord br = mParallelBroadcasts.get(i); 1192 if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) { 1193 continue; 1194 } 1195 if (!printed) { 1196 if (needSep) { 1197 pw.println(); 1198 } 1199 needSep = true; 1200 printed = true; 1201 pw.println(" Active broadcasts [" + mQueueName + "]:"); 1202 } 1203 pw.println(" Active Broadcast " + mQueueName + " #" + i + ":"); 1204 br.dump(pw, " "); 1205 } 1206 printed = false; 1207 needSep = true; 1208 for (int i = mOrderedBroadcasts.size() - 1; i >= 0; i--) { 1209 BroadcastRecord br = mOrderedBroadcasts.get(i); 1210 if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) { 1211 continue; 1212 } 1213 if (!printed) { 1214 if (needSep) { 1215 pw.println(); 1216 } 1217 needSep = true; 1218 printed = true; 1219 pw.println(" Active ordered broadcasts [" + mQueueName + "]:"); 1220 } 1221 pw.println(" Active Ordered Broadcast " + mQueueName + " #" + i + ":"); 1222 mOrderedBroadcasts.get(i).dump(pw, " "); 1223 } 1224 if (dumpPackage == null || (mPendingBroadcast != null 1225 && dumpPackage.equals(mPendingBroadcast.callerPackage))) { 1226 if (needSep) { 1227 pw.println(); 1228 } 1229 pw.println(" Pending broadcast [" + mQueueName + "]:"); 1230 if (mPendingBroadcast != null) { 1231 mPendingBroadcast.dump(pw, " "); 1232 } else { 1233 pw.println(" (null)"); 1234 } 1235 needSep = true; 1236 } 1237 } 1238 1239 int i; 1240 boolean printed = false; 1241 1242 i = -1; 1243 int lastIndex = mHistoryNext; 1244 int ringIndex = lastIndex; 1245 do { 1246 // increasing index = more recent entry, and we want to print the most 1247 // recent first and work backwards, so we roll through the ring backwards. 1248 ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_HISTORY); 1249 BroadcastRecord r = mBroadcastHistory[ringIndex]; 1250 if (r == null) { 1251 continue; 1252 } 1253 1254 i++; // genuine record of some sort even if we're filtering it out 1255 if (dumpPackage != null && !dumpPackage.equals(r.callerPackage)) { 1256 continue; 1257 } 1258 if (!printed) { 1259 if (needSep) { 1260 pw.println(); 1261 } 1262 needSep = true; 1263 pw.println(" Historical broadcasts [" + mQueueName + "]:"); 1264 printed = true; 1265 } 1266 if (dumpAll) { 1267 pw.print(" Historical Broadcast " + mQueueName + " #"); 1268 pw.print(i); pw.println(":"); 1269 r.dump(pw, " "); 1270 } else { 1271 pw.print(" #"); pw.print(i); pw.print(": "); pw.println(r); 1272 pw.print(" "); 1273 pw.println(r.intent.toShortString(false, true, true, false)); 1274 if (r.targetComp != null && r.targetComp != r.intent.getComponent()) { 1275 pw.print(" targetComp: "); pw.println(r.targetComp.toShortString()); 1276 } 1277 Bundle bundle = r.intent.getExtras(); 1278 if (bundle != null) { 1279 pw.print(" extras: "); pw.println(bundle.toString()); 1280 } 1281 } 1282 } while (ringIndex != lastIndex); 1283 1284 if (dumpPackage == null) { 1285 lastIndex = ringIndex = mSummaryHistoryNext; 1286 if (dumpAll) { 1287 printed = false; 1288 i = -1; 1289 } else { 1290 // roll over the 'i' full dumps that have already been issued 1291 for (int j = i; 1292 j > 0 && ringIndex != lastIndex;) { 1293 ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_SUMMARY_HISTORY); 1294 BroadcastRecord r = mBroadcastHistory[ringIndex]; 1295 if (r == null) { 1296 continue; 1297 } 1298 j--; 1299 } 1300 } 1301 // done skipping; dump the remainder of the ring. 'i' is still the ordinal within 1302 // the overall broadcast history. 1303 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 1304 do { 1305 ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_SUMMARY_HISTORY); 1306 Intent intent = mBroadcastSummaryHistory[ringIndex]; 1307 if (intent == null) { 1308 continue; 1309 } 1310 if (!printed) { 1311 if (needSep) { 1312 pw.println(); 1313 } 1314 needSep = true; 1315 pw.println(" Historical broadcasts summary [" + mQueueName + "]:"); 1316 printed = true; 1317 } 1318 if (!dumpAll && i >= 50) { 1319 pw.println(" ..."); 1320 break; 1321 } 1322 i++; 1323 pw.print(" #"); pw.print(i); pw.print(": "); 1324 pw.println(intent.toShortString(false, true, true, false)); 1325 pw.print(" enq="); pw.print(sdf.format(new Date(mSummaryHistoryEnqueueTime[ringIndex]))); 1326 pw.print(" disp="); pw.print(sdf.format(new Date(mSummaryHistoryDispatchTime[ringIndex]))); 1327 pw.print(" fin="); pw.println(sdf.format(new Date(mSummaryHistoryFinishTime[ringIndex]))); 1328 Bundle bundle = intent.getExtras(); 1329 if (bundle != null) { 1330 pw.print(" extras: "); pw.println(bundle.toString()); 1331 } 1332 } while (ringIndex != lastIndex); 1333 } 1334 1335 return needSep; 1336 } 1337} 1338