blob: dd3d8aa5eca286bb4243de6574b529a24827ec33 [file] [log] [blame]
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001/*
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.util.ArrayList;
22
Dianne Hackborn7d19e022012-08-07 19:12:33 -070023import android.app.ActivityManager;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080024import android.app.AppGlobals;
Dianne Hackbornf51f6122013-02-04 18:23:34 -080025import android.app.AppOpsManager;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080026import android.content.ComponentName;
27import android.content.IIntentReceiver;
28import android.content.Intent;
Dianne Hackborn7d19e022012-08-07 19:12:33 -070029import android.content.pm.ActivityInfo;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080030import android.content.pm.PackageManager;
31import android.content.pm.ResolveInfo;
32import android.os.Bundle;
33import android.os.Handler;
34import android.os.IBinder;
35import android.os.Message;
36import android.os.Process;
37import android.os.RemoteException;
38import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070039import android.os.UserHandle;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080040import android.util.EventLog;
Dianne Hackborn6c5406a2012-11-29 16:18:01 -080041import android.util.Log;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080042import android.util.Slog;
43
44/**
45 * BROADCASTS
46 *
47 * We keep two broadcast queues and associated bookkeeping, one for those at
48 * foreground priority, and one for normal (background-priority) broadcasts.
49 */
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070050public final class BroadcastQueue {
Dianne Hackborn40c8db52012-02-10 18:59:48 -080051 static final String TAG = "BroadcastQueue";
52 static final String TAG_MU = ActivityManagerService.TAG_MU;
53 static final boolean DEBUG_BROADCAST = ActivityManagerService.DEBUG_BROADCAST;
54 static final boolean DEBUG_BROADCAST_LIGHT = ActivityManagerService.DEBUG_BROADCAST_LIGHT;
55 static final boolean DEBUG_MU = ActivityManagerService.DEBUG_MU;
56
Dianne Hackborn4c51de42013-10-16 23:34:35 -070057 static final int MAX_BROADCAST_HISTORY = ActivityManager.isLowRamDeviceStatic() ? 10 : 50;
Dianne Hackborn6285a322013-09-18 12:09:47 -070058 static final int MAX_BROADCAST_SUMMARY_HISTORY
Dianne Hackborn4c51de42013-10-16 23:34:35 -070059 = ActivityManager.isLowRamDeviceStatic() ? 25 : 300;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080060
61 final ActivityManagerService mService;
62
63 /**
64 * Recognizable moniker for this queue
65 */
66 final String mQueueName;
67
68 /**
69 * Timeout period for this queue's broadcasts
70 */
71 final long mTimeoutPeriod;
72
73 /**
Dianne Hackborn6285a322013-09-18 12:09:47 -070074 * If true, we can delay broadcasts while waiting services to finish in the previous
75 * receiver's process.
76 */
77 final boolean mDelayBehindServices;
78
79 /**
Dianne Hackborn40c8db52012-02-10 18:59:48 -080080 * Lists of all active broadcasts that are to be executed immediately
81 * (without waiting for another broadcast to finish). Currently this only
82 * contains broadcasts to registered receivers, to avoid spinning up
83 * a bunch of processes to execute IntentReceiver components. Background-
84 * and foreground-priority broadcasts are queued separately.
85 */
Dianne Hackborn6285a322013-09-18 12:09:47 -070086 final ArrayList<BroadcastRecord> mParallelBroadcasts = new ArrayList<BroadcastRecord>();
87
Dianne Hackborn40c8db52012-02-10 18:59:48 -080088 /**
89 * List of all active broadcasts that are to be executed one at a time.
90 * The object at the top of the list is the currently activity broadcasts;
91 * those after it are waiting for the top to finish. As with parallel
92 * broadcasts, separate background- and foreground-priority queues are
93 * maintained.
94 */
Dianne Hackborn6285a322013-09-18 12:09:47 -070095 final ArrayList<BroadcastRecord> mOrderedBroadcasts = new ArrayList<BroadcastRecord>();
Dianne Hackborn40c8db52012-02-10 18:59:48 -080096
97 /**
98 * Historical data of past broadcasts, for debugging.
99 */
Dianne Hackborn6285a322013-09-18 12:09:47 -0700100 final BroadcastRecord[] mBroadcastHistory = new BroadcastRecord[MAX_BROADCAST_HISTORY];
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800101
102 /**
Dianne Hackbornc0bd7472012-10-09 14:00:30 -0700103 * Summary of historical data of past broadcasts, for debugging.
104 */
Dianne Hackborn6285a322013-09-18 12:09:47 -0700105 final Intent[] mBroadcastSummaryHistory = new Intent[MAX_BROADCAST_SUMMARY_HISTORY];
Dianne Hackbornc0bd7472012-10-09 14:00:30 -0700106
107 /**
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800108 * Set when we current have a BROADCAST_INTENT_MSG in flight.
109 */
110 boolean mBroadcastsScheduled = false;
111
112 /**
113 * True if we have a pending unexpired BROADCAST_TIMEOUT_MSG posted to our handler.
114 */
115 boolean mPendingBroadcastTimeoutMessage;
116
117 /**
118 * Intent broadcasts that we have tried to start, but are
119 * waiting for the application's process to be created. We only
120 * need one per scheduling class (instead of a list) because we always
121 * process broadcasts one at a time, so no others can be started while
122 * waiting for this one.
123 */
124 BroadcastRecord mPendingBroadcast = null;
125
126 /**
127 * The receiver index that is pending, to restart the broadcast if needed.
128 */
129 int mPendingBroadcastRecvIndex;
130
131 static final int BROADCAST_INTENT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG;
132 static final int BROADCAST_TIMEOUT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG + 1;
133
134 final Handler mHandler = new Handler() {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800135 public void handleMessage(Message msg) {
136 switch (msg.what) {
137 case BROADCAST_INTENT_MSG: {
138 if (DEBUG_BROADCAST) Slog.v(
139 TAG, "Received BROADCAST_INTENT_MSG");
140 processNextBroadcast(true);
141 } break;
142 case BROADCAST_TIMEOUT_MSG: {
143 synchronized (mService) {
144 broadcastTimeoutLocked(true);
145 }
146 } break;
147 }
148 }
149 };
150
151 private final class AppNotResponding implements Runnable {
152 private final ProcessRecord mApp;
153 private final String mAnnotation;
154
155 public AppNotResponding(ProcessRecord app, String annotation) {
156 mApp = app;
157 mAnnotation = annotation;
158 }
159
160 @Override
161 public void run() {
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700162 mService.appNotResponding(mApp, null, null, false, mAnnotation);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800163 }
164 }
165
Dianne Hackborn6285a322013-09-18 12:09:47 -0700166 BroadcastQueue(ActivityManagerService service, String name, long timeoutPeriod,
167 boolean allowDelayBehindServices) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800168 mService = service;
169 mQueueName = name;
170 mTimeoutPeriod = timeoutPeriod;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700171 mDelayBehindServices = allowDelayBehindServices;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800172 }
173
174 public boolean isPendingBroadcastProcessLocked(int pid) {
175 return mPendingBroadcast != null && mPendingBroadcast.curApp.pid == pid;
176 }
177
178 public void enqueueParallelBroadcastLocked(BroadcastRecord r) {
179 mParallelBroadcasts.add(r);
180 }
181
182 public void enqueueOrderedBroadcastLocked(BroadcastRecord r) {
183 mOrderedBroadcasts.add(r);
184 }
185
186 public final boolean replaceParallelBroadcastLocked(BroadcastRecord r) {
187 for (int i=mParallelBroadcasts.size()-1; i>=0; i--) {
188 if (r.intent.filterEquals(mParallelBroadcasts.get(i).intent)) {
189 if (DEBUG_BROADCAST) Slog.v(TAG,
190 "***** DROPPING PARALLEL ["
191 + mQueueName + "]: " + r.intent);
192 mParallelBroadcasts.set(i, r);
193 return true;
194 }
195 }
196 return false;
197 }
198
199 public final boolean replaceOrderedBroadcastLocked(BroadcastRecord r) {
200 for (int i=mOrderedBroadcasts.size()-1; i>0; i--) {
201 if (r.intent.filterEquals(mOrderedBroadcasts.get(i).intent)) {
202 if (DEBUG_BROADCAST) Slog.v(TAG,
203 "***** DROPPING ORDERED ["
204 + mQueueName + "]: " + r.intent);
205 mOrderedBroadcasts.set(i, r);
206 return true;
207 }
208 }
209 return false;
210 }
211
212 private final void processCurBroadcastLocked(BroadcastRecord r,
213 ProcessRecord app) throws RemoteException {
214 if (DEBUG_BROADCAST) Slog.v(TAG,
215 "Process cur broadcast " + r + " for app " + app);
216 if (app.thread == null) {
217 throw new RemoteException();
218 }
219 r.receiver = app.thread.asBinder();
220 r.curApp = app;
221 app.curReceiver = r;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700222 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_RECEIVER);
Dianne Hackborndb926082013-10-31 16:32:44 -0700223 mService.updateLruProcessLocked(app, false, null);
224 mService.updateOomAdjLocked();
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800225
226 // Tell the application to launch this receiver.
227 r.intent.setComponent(r.curComponent);
228
229 boolean started = false;
230 try {
231 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG,
232 "Delivering to component " + r.curComponent
233 + ": " + r);
234 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
235 app.thread.scheduleReceiver(new Intent(r.intent), r.curReceiver,
236 mService.compatibilityInfoForPackageLocked(r.curReceiver.applicationInfo),
Dianne Hackborna413dc02013-07-12 12:02:55 -0700237 r.resultCode, r.resultData, r.resultExtras, r.ordered, r.userId,
238 app.repProcState);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800239 if (DEBUG_BROADCAST) Slog.v(TAG,
240 "Process cur broadcast " + r + " DELIVERED for app " + app);
241 started = true;
242 } finally {
243 if (!started) {
244 if (DEBUG_BROADCAST) Slog.v(TAG,
245 "Process cur broadcast " + r + ": NOT STARTED!");
246 r.receiver = null;
247 r.curApp = null;
248 app.curReceiver = null;
249 }
250 }
251 }
252
253 public boolean sendPendingBroadcastsLocked(ProcessRecord app) {
254 boolean didSomething = false;
255 final BroadcastRecord br = mPendingBroadcast;
256 if (br != null && br.curApp.pid == app.pid) {
257 try {
258 mPendingBroadcast = null;
259 processCurBroadcastLocked(br, app);
260 didSomething = true;
261 } catch (Exception e) {
262 Slog.w(TAG, "Exception in new application when starting receiver "
263 + br.curComponent.flattenToShortString(), e);
264 logBroadcastReceiverDiscardLocked(br);
265 finishReceiverLocked(br, br.resultCode, br.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700266 br.resultExtras, br.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800267 scheduleBroadcastsLocked();
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700268 // We need to reset the state if we failed to start the receiver.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800269 br.state = BroadcastRecord.IDLE;
270 throw new RuntimeException(e.getMessage());
271 }
272 }
273 return didSomething;
274 }
275
276 public void skipPendingBroadcastLocked(int pid) {
277 final BroadcastRecord br = mPendingBroadcast;
278 if (br != null && br.curApp.pid == pid) {
279 br.state = BroadcastRecord.IDLE;
280 br.nextReceiver = mPendingBroadcastRecvIndex;
281 mPendingBroadcast = null;
282 scheduleBroadcastsLocked();
283 }
284 }
285
286 public void skipCurrentReceiverLocked(ProcessRecord app) {
287 boolean reschedule = false;
288 BroadcastRecord r = app.curReceiver;
289 if (r != null) {
290 // The current broadcast is waiting for this app's receiver
291 // to be finished. Looks like that's not going to happen, so
292 // let the broadcast continue.
293 logBroadcastReceiverDiscardLocked(r);
294 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700295 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800296 reschedule = true;
297 }
298
299 r = mPendingBroadcast;
300 if (r != null && r.curApp == app) {
301 if (DEBUG_BROADCAST) Slog.v(TAG,
302 "[" + mQueueName + "] skip & discard pending app " + r);
303 logBroadcastReceiverDiscardLocked(r);
304 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700305 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800306 reschedule = true;
307 }
308 if (reschedule) {
309 scheduleBroadcastsLocked();
310 }
311 }
312
313 public void scheduleBroadcastsLocked() {
314 if (DEBUG_BROADCAST) Slog.v(TAG, "Schedule broadcasts ["
315 + mQueueName + "]: current="
316 + mBroadcastsScheduled);
317
318 if (mBroadcastsScheduled) {
319 return;
320 }
321 mHandler.sendMessage(mHandler.obtainMessage(BROADCAST_INTENT_MSG, this));
322 mBroadcastsScheduled = true;
323 }
324
325 public BroadcastRecord getMatchingOrderedReceiver(IBinder receiver) {
326 if (mOrderedBroadcasts.size() > 0) {
327 final BroadcastRecord r = mOrderedBroadcasts.get(0);
328 if (r != null && r.receiver == receiver) {
329 return r;
330 }
331 }
332 return null;
333 }
334
335 public boolean finishReceiverLocked(BroadcastRecord r, int resultCode,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700336 String resultData, Bundle resultExtras, boolean resultAbort, boolean waitForServices) {
337 final int state = r.state;
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700338 final ActivityInfo receiver = r.curReceiver;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800339 r.state = BroadcastRecord.IDLE;
340 if (state == BroadcastRecord.IDLE) {
Dianne Hackborn6285a322013-09-18 12:09:47 -0700341 Slog.w(TAG, "finishReceiver [" + mQueueName + "] called but state is IDLE");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800342 }
343 r.receiver = null;
344 r.intent.setComponent(null);
345 if (r.curApp != null) {
346 r.curApp.curReceiver = null;
347 }
348 if (r.curFilter != null) {
349 r.curFilter.receiverList.curBroadcast = null;
350 }
351 r.curFilter = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800352 r.curReceiver = null;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700353 r.curApp = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800354 mPendingBroadcast = null;
355
356 r.resultCode = resultCode;
357 r.resultData = resultData;
358 r.resultExtras = resultExtras;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700359 if (resultAbort && (r.intent.getFlags()&Intent.FLAG_RECEIVER_NO_ABORT) == 0) {
360 r.resultAbort = resultAbort;
361 } else {
362 r.resultAbort = false;
363 }
364
365 if (waitForServices && r.curComponent != null && r.queue.mDelayBehindServices
366 && r.queue.mOrderedBroadcasts.size() > 0
367 && r.queue.mOrderedBroadcasts.get(0) == r) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700368 ActivityInfo nextReceiver;
369 if (r.nextReceiver < r.receivers.size()) {
370 Object obj = r.receivers.get(r.nextReceiver);
371 nextReceiver = (obj instanceof ActivityInfo) ? (ActivityInfo)obj : null;
372 } else {
373 nextReceiver = null;
374 }
375 // Don't do this if the next receive is in the same process as the current one.
376 if (receiver == null || nextReceiver == null
377 || receiver.applicationInfo.uid != nextReceiver.applicationInfo.uid
378 || !receiver.processName.equals(nextReceiver.processName)) {
379 // In this case, we are ready to process the next receiver for the current broadcast,
380 // but are on a queue that would like to wait for services to finish before moving
381 // on. If there are background services currently starting, then we will go into a
382 // special state where we hold off on continuing this broadcast until they are done.
383 if (mService.mServices.hasBackgroundServices(r.userId)) {
384 Slog.i(ActivityManagerService.TAG, "Delay finish: "
385 + r.curComponent.flattenToShortString());
386 r.state = BroadcastRecord.WAITING_SERVICES;
387 return false;
388 }
Dianne Hackborn6285a322013-09-18 12:09:47 -0700389 }
390 }
391
392 r.curComponent = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800393
394 // We will process the next receiver right now if this is finishing
395 // an app receiver (which is always asynchronous) or after we have
396 // come back from calling a receiver.
397 return state == BroadcastRecord.APP_RECEIVE
398 || state == BroadcastRecord.CALL_DONE_RECEIVE;
399 }
400
Dianne Hackborn6285a322013-09-18 12:09:47 -0700401 public void backgroundServicesFinishedLocked(int userId) {
402 if (mOrderedBroadcasts.size() > 0) {
403 BroadcastRecord br = mOrderedBroadcasts.get(0);
404 if (br.userId == userId && br.state == BroadcastRecord.WAITING_SERVICES) {
405 Slog.i(ActivityManagerService.TAG, "Resuming delayed broadcast");
406 br.curComponent = null;
407 br.state = BroadcastRecord.IDLE;
408 processNextBroadcast(false);
409 }
410 }
411 }
412
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800413 private static void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver,
414 Intent intent, int resultCode, String data, Bundle extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700415 boolean ordered, boolean sticky, int sendingUser) throws RemoteException {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800416 // Send the intent to the receiver asynchronously using one-way binder calls.
417 if (app != null && app.thread != null) {
418 // If we have an app thread, do the call through that so it is
419 // correctly ordered with other one-way calls.
420 app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700421 data, extras, ordered, sticky, sendingUser, app.repProcState);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800422 } else {
Dianne Hackborn20e80982012-08-31 19:00:44 -0700423 receiver.performReceive(intent, resultCode, data, extras, ordered,
424 sticky, sendingUser);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800425 }
426 }
427
428 private final void deliverToRegisteredReceiverLocked(BroadcastRecord r,
429 BroadcastFilter filter, boolean ordered) {
430 boolean skip = false;
Amith Yamasani8bf06ed2012-08-27 19:30:30 -0700431 if (filter.requiredPermission != null) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800432 int perm = mService.checkComponentPermission(filter.requiredPermission,
433 r.callingPid, r.callingUid, -1, true);
434 if (perm != PackageManager.PERMISSION_GRANTED) {
435 Slog.w(TAG, "Permission Denial: broadcasting "
436 + r.intent.toString()
437 + " from " + r.callerPackage + " (pid="
438 + r.callingPid + ", uid=" + r.callingUid + ")"
439 + " requires " + filter.requiredPermission
440 + " due to registered receiver " + filter);
441 skip = true;
442 }
443 }
Dianne Hackbornb4163a62012-08-02 18:31:26 -0700444 if (!skip && r.requiredPermission != null) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800445 int perm = mService.checkComponentPermission(r.requiredPermission,
446 filter.receiverList.pid, filter.receiverList.uid, -1, true);
447 if (perm != PackageManager.PERMISSION_GRANTED) {
448 Slog.w(TAG, "Permission Denial: receiving "
449 + r.intent.toString()
450 + " to " + filter.receiverList.app
451 + " (pid=" + filter.receiverList.pid
452 + ", uid=" + filter.receiverList.uid + ")"
453 + " requires " + r.requiredPermission
454 + " due to sender " + r.callerPackage
455 + " (uid " + r.callingUid + ")");
456 skip = true;
457 }
458 }
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800459 if (r.appOp != AppOpsManager.OP_NONE) {
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700460 int mode = mService.mAppOpsService.noteOperation(r.appOp,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800461 filter.receiverList.uid, filter.packageName);
462 if (mode != AppOpsManager.MODE_ALLOWED) {
463 if (DEBUG_BROADCAST) Slog.v(TAG,
464 "App op " + r.appOp + " not allowed for broadcast to uid "
465 + filter.receiverList.uid + " pkg " + filter.packageName);
466 skip = true;
467 }
468 }
Ben Gruver49660c72013-08-06 19:54:08 -0700469 if (!skip) {
470 skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,
471 r.callingPid, r.resolvedType, filter.receiverList.uid);
472 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800473
Dianne Hackborn9357b112013-10-03 18:27:48 -0700474 if (filter.receiverList.app == null || filter.receiverList.app.crashing) {
475 Slog.w(TAG, "Skipping deliver [" + mQueueName + "] " + r
476 + " to " + filter.receiverList + ": process crashing");
477 skip = true;
478 }
479
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800480 if (!skip) {
481 // If this is not being sent as an ordered broadcast, then we
482 // don't want to touch the fields that keep track of the current
483 // state of ordered broadcasts.
484 if (ordered) {
485 r.receiver = filter.receiverList.receiver.asBinder();
486 r.curFilter = filter;
487 filter.receiverList.curBroadcast = r;
488 r.state = BroadcastRecord.CALL_IN_RECEIVE;
489 if (filter.receiverList.app != null) {
490 // Bump hosting application to no longer be in background
491 // scheduling class. Note that we can't do that if there
492 // isn't an app... but we can only be in that case for
493 // things that directly call the IActivityManager API, which
494 // are already core system stuff so don't matter for this.
495 r.curApp = filter.receiverList.app;
496 filter.receiverList.app.curReceiver = r;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700497 mService.updateOomAdjLocked(r.curApp, true);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800498 }
499 }
500 try {
501 if (DEBUG_BROADCAST_LIGHT) {
502 int seq = r.intent.getIntExtra("seq", -1);
503 Slog.i(TAG, "Delivering to " + filter
504 + " (seq=" + seq + "): " + r);
505 }
506 performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700507 new Intent(r.intent), r.resultCode, r.resultData,
508 r.resultExtras, r.ordered, r.initialSticky, r.userId);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800509 if (ordered) {
510 r.state = BroadcastRecord.CALL_DONE_RECEIVE;
511 }
512 } catch (RemoteException e) {
513 Slog.w(TAG, "Failure sending broadcast " + r.intent, e);
514 if (ordered) {
515 r.receiver = null;
516 r.curFilter = null;
517 filter.receiverList.curBroadcast = null;
518 if (filter.receiverList.app != null) {
519 filter.receiverList.app.curReceiver = null;
520 }
521 }
522 }
523 }
524 }
525
526 final void processNextBroadcast(boolean fromMsg) {
527 synchronized(mService) {
528 BroadcastRecord r;
529
530 if (DEBUG_BROADCAST) Slog.v(TAG, "processNextBroadcast ["
531 + mQueueName + "]: "
532 + mParallelBroadcasts.size() + " broadcasts, "
533 + mOrderedBroadcasts.size() + " ordered broadcasts");
534
535 mService.updateCpuStats();
536
537 if (fromMsg) {
538 mBroadcastsScheduled = false;
539 }
540
541 // First, deliver any non-serialized broadcasts right away.
542 while (mParallelBroadcasts.size() > 0) {
543 r = mParallelBroadcasts.remove(0);
544 r.dispatchTime = SystemClock.uptimeMillis();
545 r.dispatchClockTime = System.currentTimeMillis();
546 final int N = r.receivers.size();
547 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing parallel broadcast ["
548 + mQueueName + "] " + r);
549 for (int i=0; i<N; i++) {
550 Object target = r.receivers.get(i);
551 if (DEBUG_BROADCAST) Slog.v(TAG,
552 "Delivering non-ordered on [" + mQueueName + "] to registered "
553 + target + ": " + r);
554 deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false);
555 }
556 addBroadcastToHistoryLocked(r);
557 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Done with parallel broadcast ["
558 + mQueueName + "] " + r);
559 }
560
561 // Now take care of the next serialized one...
562
563 // If we are waiting for a process to come up to handle the next
564 // broadcast, then do nothing at this point. Just in case, we
565 // check that the process we're waiting for still exists.
566 if (mPendingBroadcast != null) {
567 if (DEBUG_BROADCAST_LIGHT) {
568 Slog.v(TAG, "processNextBroadcast ["
569 + mQueueName + "]: waiting for "
570 + mPendingBroadcast.curApp);
571 }
572
573 boolean isDead;
574 synchronized (mService.mPidsSelfLocked) {
Dianne Hackborn9357b112013-10-03 18:27:48 -0700575 ProcessRecord proc = mService.mPidsSelfLocked.get(mPendingBroadcast.curApp.pid);
576 isDead = proc == null || proc.crashing;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800577 }
578 if (!isDead) {
579 // It's still alive, so keep waiting
580 return;
581 } else {
582 Slog.w(TAG, "pending app ["
583 + mQueueName + "]" + mPendingBroadcast.curApp
584 + " died before responding to broadcast");
585 mPendingBroadcast.state = BroadcastRecord.IDLE;
586 mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex;
587 mPendingBroadcast = null;
588 }
589 }
590
591 boolean looped = false;
592
593 do {
594 if (mOrderedBroadcasts.size() == 0) {
595 // No more broadcasts pending, so all done!
596 mService.scheduleAppGcsLocked();
597 if (looped) {
598 // If we had finished the last ordered broadcast, then
599 // make sure all processes have correct oom and sched
600 // adjustments.
601 mService.updateOomAdjLocked();
602 }
603 return;
604 }
605 r = mOrderedBroadcasts.get(0);
606 boolean forceReceive = false;
607
608 // Ensure that even if something goes awry with the timeout
609 // detection, we catch "hung" broadcasts here, discard them,
610 // and continue to make progress.
611 //
612 // This is only done if the system is ready so that PRE_BOOT_COMPLETED
613 // receivers don't get executed with timeouts. They're intended for
614 // one time heavy lifting after system upgrades and can take
615 // significant amounts of time.
616 int numReceivers = (r.receivers != null) ? r.receivers.size() : 0;
617 if (mService.mProcessesReady && r.dispatchTime > 0) {
618 long now = SystemClock.uptimeMillis();
619 if ((numReceivers > 0) &&
620 (now > r.dispatchTime + (2*mTimeoutPeriod*numReceivers))) {
621 Slog.w(TAG, "Hung broadcast ["
622 + mQueueName + "] discarded after timeout failure:"
623 + " now=" + now
624 + " dispatchTime=" + r.dispatchTime
625 + " startTime=" + r.receiverTime
626 + " intent=" + r.intent
627 + " numReceivers=" + numReceivers
628 + " nextReceiver=" + r.nextReceiver
629 + " state=" + r.state);
630 broadcastTimeoutLocked(false); // forcibly finish this broadcast
631 forceReceive = true;
632 r.state = BroadcastRecord.IDLE;
633 }
634 }
635
636 if (r.state != BroadcastRecord.IDLE) {
637 if (DEBUG_BROADCAST) Slog.d(TAG,
638 "processNextBroadcast("
639 + mQueueName + ") called when not idle (state="
640 + r.state + ")");
641 return;
642 }
643
644 if (r.receivers == null || r.nextReceiver >= numReceivers
645 || r.resultAbort || forceReceive) {
646 // No more receivers for this broadcast! Send the final
647 // result if requested...
648 if (r.resultTo != null) {
649 try {
650 if (DEBUG_BROADCAST) {
651 int seq = r.intent.getIntExtra("seq", -1);
652 Slog.i(TAG, "Finishing broadcast ["
653 + mQueueName + "] " + r.intent.getAction()
654 + " seq=" + seq + " app=" + r.callerApp);
655 }
656 performReceiveLocked(r.callerApp, r.resultTo,
657 new Intent(r.intent), r.resultCode,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700658 r.resultData, r.resultExtras, false, false, r.userId);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800659 // Set this to null so that the reference
Dianne Hackborn9357b112013-10-03 18:27:48 -0700660 // (local and remote) isn't kept in the mBroadcastHistory.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800661 r.resultTo = null;
662 } catch (RemoteException e) {
663 Slog.w(TAG, "Failure ["
664 + mQueueName + "] sending broadcast result of "
665 + r.intent, e);
666 }
667 }
668
669 if (DEBUG_BROADCAST) Slog.v(TAG, "Cancelling BROADCAST_TIMEOUT_MSG");
670 cancelBroadcastTimeoutLocked();
671
672 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Finished with ordered broadcast "
673 + r);
674
675 // ... and on to the next...
676 addBroadcastToHistoryLocked(r);
677 mOrderedBroadcasts.remove(0);
678 r = null;
679 looped = true;
680 continue;
681 }
682 } while (r == null);
683
684 // Get the next receiver...
685 int recIdx = r.nextReceiver++;
686
687 // Keep track of when this receiver started, and make sure there
688 // is a timeout message pending to kill it if need be.
689 r.receiverTime = SystemClock.uptimeMillis();
690 if (recIdx == 0) {
691 r.dispatchTime = r.receiverTime;
692 r.dispatchClockTime = System.currentTimeMillis();
693 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing ordered broadcast ["
694 + mQueueName + "] " + r);
695 }
696 if (! mPendingBroadcastTimeoutMessage) {
697 long timeoutTime = r.receiverTime + mTimeoutPeriod;
698 if (DEBUG_BROADCAST) Slog.v(TAG,
699 "Submitting BROADCAST_TIMEOUT_MSG ["
700 + mQueueName + "] for " + r + " at " + timeoutTime);
701 setBroadcastTimeoutLocked(timeoutTime);
702 }
703
704 Object nextReceiver = r.receivers.get(recIdx);
705 if (nextReceiver instanceof BroadcastFilter) {
706 // Simple case: this is a registered receiver who gets
707 // a direct call.
708 BroadcastFilter filter = (BroadcastFilter)nextReceiver;
709 if (DEBUG_BROADCAST) Slog.v(TAG,
710 "Delivering ordered ["
711 + mQueueName + "] to registered "
712 + filter + ": " + r);
713 deliverToRegisteredReceiverLocked(r, filter, r.ordered);
714 if (r.receiver == null || !r.ordered) {
715 // The receiver has already finished, so schedule to
716 // process the next one.
717 if (DEBUG_BROADCAST) Slog.v(TAG, "Quick finishing ["
718 + mQueueName + "]: ordered="
719 + r.ordered + " receiver=" + r.receiver);
720 r.state = BroadcastRecord.IDLE;
721 scheduleBroadcastsLocked();
722 }
723 return;
724 }
725
726 // Hard case: need to instantiate the receiver, possibly
727 // starting its application process to host it.
728
729 ResolveInfo info =
730 (ResolveInfo)nextReceiver;
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700731 ComponentName component = new ComponentName(
732 info.activityInfo.applicationInfo.packageName,
733 info.activityInfo.name);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800734
735 boolean skip = false;
736 int perm = mService.checkComponentPermission(info.activityInfo.permission,
737 r.callingPid, r.callingUid, info.activityInfo.applicationInfo.uid,
738 info.activityInfo.exported);
739 if (perm != PackageManager.PERMISSION_GRANTED) {
740 if (!info.activityInfo.exported) {
741 Slog.w(TAG, "Permission Denial: broadcasting "
742 + r.intent.toString()
743 + " from " + r.callerPackage + " (pid=" + r.callingPid
744 + ", uid=" + r.callingUid + ")"
745 + " is not exported from uid " + info.activityInfo.applicationInfo.uid
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700746 + " due to receiver " + component.flattenToShortString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800747 } else {
748 Slog.w(TAG, "Permission Denial: broadcasting "
749 + r.intent.toString()
750 + " from " + r.callerPackage + " (pid=" + r.callingPid
751 + ", uid=" + r.callingUid + ")"
752 + " requires " + info.activityInfo.permission
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700753 + " due to receiver " + component.flattenToShortString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800754 }
755 skip = true;
756 }
757 if (info.activityInfo.applicationInfo.uid != Process.SYSTEM_UID &&
758 r.requiredPermission != null) {
759 try {
760 perm = AppGlobals.getPackageManager().
761 checkPermission(r.requiredPermission,
762 info.activityInfo.applicationInfo.packageName);
763 } catch (RemoteException e) {
764 perm = PackageManager.PERMISSION_DENIED;
765 }
766 if (perm != PackageManager.PERMISSION_GRANTED) {
767 Slog.w(TAG, "Permission Denial: receiving "
768 + r.intent + " to "
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700769 + component.flattenToShortString()
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800770 + " requires " + r.requiredPermission
771 + " due to sender " + r.callerPackage
772 + " (uid " + r.callingUid + ")");
773 skip = true;
774 }
775 }
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800776 if (r.appOp != AppOpsManager.OP_NONE) {
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700777 int mode = mService.mAppOpsService.noteOperation(r.appOp,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800778 info.activityInfo.applicationInfo.uid, info.activityInfo.packageName);
779 if (mode != AppOpsManager.MODE_ALLOWED) {
780 if (DEBUG_BROADCAST) Slog.v(TAG,
781 "App op " + r.appOp + " not allowed for broadcast to uid "
782 + info.activityInfo.applicationInfo.uid + " pkg "
783 + info.activityInfo.packageName);
784 skip = true;
785 }
786 }
Ben Gruver49660c72013-08-06 19:54:08 -0700787 if (!skip) {
788 skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,
789 r.callingPid, r.resolvedType, info.activityInfo.applicationInfo.uid);
790 }
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700791 boolean isSingleton = false;
792 try {
793 isSingleton = mService.isSingleton(info.activityInfo.processName,
794 info.activityInfo.applicationInfo,
795 info.activityInfo.name, info.activityInfo.flags);
796 } catch (SecurityException e) {
797 Slog.w(TAG, e.getMessage());
798 skip = true;
799 }
800 if ((info.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) {
801 if (ActivityManager.checkUidPermission(
802 android.Manifest.permission.INTERACT_ACROSS_USERS,
803 info.activityInfo.applicationInfo.uid)
804 != PackageManager.PERMISSION_GRANTED) {
805 Slog.w(TAG, "Permission Denial: Receiver " + component.flattenToShortString()
806 + " requests FLAG_SINGLE_USER, but app does not hold "
807 + android.Manifest.permission.INTERACT_ACROSS_USERS);
808 skip = true;
809 }
810 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800811 if (r.curApp != null && r.curApp.crashing) {
812 // If the target process is crashing, just skip it.
Dianne Hackborn9357b112013-10-03 18:27:48 -0700813 Slog.w(TAG, "Skipping deliver ordered [" + mQueueName + "] " + r
814 + " to " + r.curApp + ": process crashing");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800815 skip = true;
816 }
817
818 if (skip) {
819 if (DEBUG_BROADCAST) Slog.v(TAG,
820 "Skipping delivery of ordered ["
821 + mQueueName + "] " + r + " for whatever reason");
822 r.receiver = null;
823 r.curFilter = null;
824 r.state = BroadcastRecord.IDLE;
825 scheduleBroadcastsLocked();
826 return;
827 }
828
829 r.state = BroadcastRecord.APP_RECEIVE;
830 String targetProcess = info.activityInfo.processName;
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700831 r.curComponent = component;
832 if (r.callingUid != Process.SYSTEM_UID && isSingleton) {
833 info.activityInfo = mService.getActivityInfoForUser(info.activityInfo, 0);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800834 }
835 r.curReceiver = info.activityInfo;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700836 if (DEBUG_MU && r.callingUid > UserHandle.PER_USER_RANGE) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800837 Slog.v(TAG_MU, "Updated broadcast record activity info for secondary user, "
838 + info.activityInfo + ", callingUid = " + r.callingUid + ", uid = "
839 + info.activityInfo.applicationInfo.uid);
840 }
841
842 // Broadcast is being executed, its package can't be stopped.
843 try {
844 AppGlobals.getPackageManager().setPackageStoppedState(
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700845 r.curComponent.getPackageName(), false, UserHandle.getUserId(r.callingUid));
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800846 } catch (RemoteException e) {
847 } catch (IllegalArgumentException e) {
848 Slog.w(TAG, "Failed trying to unstop package "
849 + r.curComponent.getPackageName() + ": " + e);
850 }
851
852 // Is this receiver's application already running?
853 ProcessRecord app = mService.getProcessRecordLocked(targetProcess,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700854 info.activityInfo.applicationInfo.uid, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800855 if (app != null && app.thread != null) {
856 try {
Dianne Hackbornd2932242013-08-05 18:18:42 -0700857 app.addPackage(info.activityInfo.packageName, mService.mProcessStats);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800858 processCurBroadcastLocked(r, app);
859 return;
860 } catch (RemoteException e) {
861 Slog.w(TAG, "Exception when sending broadcast to "
862 + r.curComponent, e);
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800863 } catch (RuntimeException e) {
864 Log.wtf(TAG, "Failed sending broadcast to "
865 + r.curComponent + " with " + r.intent, e);
866 // If some unexpected exception happened, just skip
867 // this broadcast. At this point we are not in the call
868 // from a client, so throwing an exception out from here
869 // will crash the entire system instead of just whoever
870 // sent the broadcast.
871 logBroadcastReceiverDiscardLocked(r);
872 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700873 r.resultExtras, r.resultAbort, false);
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800874 scheduleBroadcastsLocked();
875 // We need to reset the state if we failed to start the receiver.
876 r.state = BroadcastRecord.IDLE;
877 return;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800878 }
879
880 // If a dead object exception was thrown -- fall through to
881 // restart the application.
882 }
883
884 // Not running -- get it started, to be executed when the app comes up.
885 if (DEBUG_BROADCAST) Slog.v(TAG,
886 "Need to start app ["
887 + mQueueName + "] " + targetProcess + " for broadcast " + r);
888 if ((r.curApp=mService.startProcessLocked(targetProcess,
889 info.activityInfo.applicationInfo, true,
890 r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND,
891 "broadcast", r.curComponent,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700892 (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0, false, false))
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800893 == null) {
894 // Ah, this recipient is unavailable. Finish it if necessary,
895 // and mark the broadcast record as ready for the next.
896 Slog.w(TAG, "Unable to launch app "
897 + info.activityInfo.applicationInfo.packageName + "/"
898 + info.activityInfo.applicationInfo.uid + " for broadcast "
899 + r.intent + ": process is bad");
900 logBroadcastReceiverDiscardLocked(r);
901 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700902 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800903 scheduleBroadcastsLocked();
904 r.state = BroadcastRecord.IDLE;
905 return;
906 }
907
908 mPendingBroadcast = r;
909 mPendingBroadcastRecvIndex = recIdx;
910 }
911 }
912
913 final void setBroadcastTimeoutLocked(long timeoutTime) {
914 if (! mPendingBroadcastTimeoutMessage) {
915 Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG, this);
916 mHandler.sendMessageAtTime(msg, timeoutTime);
917 mPendingBroadcastTimeoutMessage = true;
918 }
919 }
920
921 final void cancelBroadcastTimeoutLocked() {
922 if (mPendingBroadcastTimeoutMessage) {
923 mHandler.removeMessages(BROADCAST_TIMEOUT_MSG, this);
924 mPendingBroadcastTimeoutMessage = false;
925 }
926 }
927
928 final void broadcastTimeoutLocked(boolean fromMsg) {
929 if (fromMsg) {
930 mPendingBroadcastTimeoutMessage = false;
931 }
932
933 if (mOrderedBroadcasts.size() == 0) {
934 return;
935 }
936
937 long now = SystemClock.uptimeMillis();
938 BroadcastRecord r = mOrderedBroadcasts.get(0);
939 if (fromMsg) {
940 if (mService.mDidDexOpt) {
941 // Delay timeouts until dexopt finishes.
942 mService.mDidDexOpt = false;
943 long timeoutTime = SystemClock.uptimeMillis() + mTimeoutPeriod;
944 setBroadcastTimeoutLocked(timeoutTime);
945 return;
946 }
947 if (!mService.mProcessesReady) {
948 // Only process broadcast timeouts if the system is ready. That way
949 // PRE_BOOT_COMPLETED broadcasts can't timeout as they are intended
950 // to do heavy lifting for system up.
951 return;
952 }
953
954 long timeoutTime = r.receiverTime + mTimeoutPeriod;
955 if (timeoutTime > now) {
956 // We can observe premature timeouts because we do not cancel and reset the
957 // broadcast timeout message after each receiver finishes. Instead, we set up
958 // an initial timeout then kick it down the road a little further as needed
959 // when it expires.
960 if (DEBUG_BROADCAST) Slog.v(TAG,
961 "Premature timeout ["
962 + mQueueName + "] @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for "
963 + timeoutTime);
964 setBroadcastTimeoutLocked(timeoutTime);
965 return;
966 }
967 }
968
Dianne Hackborn6285a322013-09-18 12:09:47 -0700969 BroadcastRecord br = mOrderedBroadcasts.get(0);
970 if (br.state == BroadcastRecord.WAITING_SERVICES) {
971 // In this case the broadcast had already finished, but we had decided to wait
972 // for started services to finish as well before going on. So if we have actually
973 // waited long enough time timeout the broadcast, let's give up on the whole thing
974 // and just move on to the next.
975 Slog.i(ActivityManagerService.TAG, "Waited long enough for: " + (br.curComponent != null
976 ? br.curComponent.flattenToShortString() : "(null)"));
977 br.curComponent = null;
978 br.state = BroadcastRecord.IDLE;
979 processNextBroadcast(false);
980 return;
981 }
982
983 Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r. receiver
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800984 + ", started " + (now - r.receiverTime) + "ms ago");
985 r.receiverTime = now;
986 r.anrCount++;
987
988 // Current receiver has passed its expiration date.
989 if (r.nextReceiver <= 0) {
990 Slog.w(TAG, "Timeout on receiver with nextReceiver <= 0");
991 return;
992 }
993
994 ProcessRecord app = null;
995 String anrMessage = null;
996
997 Object curReceiver = r.receivers.get(r.nextReceiver-1);
998 Slog.w(TAG, "Receiver during timeout: " + curReceiver);
999 logBroadcastReceiverDiscardLocked(r);
1000 if (curReceiver instanceof BroadcastFilter) {
1001 BroadcastFilter bf = (BroadcastFilter)curReceiver;
1002 if (bf.receiverList.pid != 0
1003 && bf.receiverList.pid != ActivityManagerService.MY_PID) {
1004 synchronized (mService.mPidsSelfLocked) {
1005 app = mService.mPidsSelfLocked.get(
1006 bf.receiverList.pid);
1007 }
1008 }
1009 } else {
1010 app = r.curApp;
1011 }
1012
1013 if (app != null) {
1014 anrMessage = "Broadcast of " + r.intent.toString();
1015 }
1016
1017 if (mPendingBroadcast == r) {
1018 mPendingBroadcast = null;
1019 }
1020
1021 // Move on to the next receiver.
1022 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -07001023 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001024 scheduleBroadcastsLocked();
1025
1026 if (anrMessage != null) {
1027 // Post the ANR to the handler since we do not want to process ANRs while
1028 // potentially holding our lock.
1029 mHandler.post(new AppNotResponding(app, anrMessage));
1030 }
1031 }
1032
1033 private final void addBroadcastToHistoryLocked(BroadcastRecord r) {
1034 if (r.callingUid < 0) {
1035 // This was from a registerReceiver() call; ignore it.
1036 return;
1037 }
1038 System.arraycopy(mBroadcastHistory, 0, mBroadcastHistory, 1,
1039 MAX_BROADCAST_HISTORY-1);
1040 r.finishTime = SystemClock.uptimeMillis();
1041 mBroadcastHistory[0] = r;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001042 System.arraycopy(mBroadcastSummaryHistory, 0, mBroadcastSummaryHistory, 1,
1043 MAX_BROADCAST_SUMMARY_HISTORY-1);
1044 mBroadcastSummaryHistory[0] = r.intent;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001045 }
1046
1047 final void logBroadcastReceiverDiscardLocked(BroadcastRecord r) {
1048 if (r.nextReceiver > 0) {
1049 Object curReceiver = r.receivers.get(r.nextReceiver-1);
1050 if (curReceiver instanceof BroadcastFilter) {
1051 BroadcastFilter bf = (BroadcastFilter) curReceiver;
1052 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001053 bf.owningUserId, System.identityHashCode(r),
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001054 r.intent.getAction(),
1055 r.nextReceiver - 1,
1056 System.identityHashCode(bf));
1057 } else {
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001058 ResolveInfo ri = (ResolveInfo)curReceiver;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001059 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001060 UserHandle.getUserId(ri.activityInfo.applicationInfo.uid),
1061 System.identityHashCode(r), r.intent.getAction(),
1062 r.nextReceiver - 1, ri.toString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001063 }
1064 } else {
1065 Slog.w(TAG, "Discarding broadcast before first receiver is invoked: "
1066 + r);
1067 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001068 -1, System.identityHashCode(r),
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001069 r.intent.getAction(),
1070 r.nextReceiver,
1071 "NONE");
1072 }
1073 }
1074
1075 final boolean dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args,
1076 int opti, boolean dumpAll, String dumpPackage, boolean needSep) {
1077 if (mParallelBroadcasts.size() > 0 || mOrderedBroadcasts.size() > 0
1078 || mPendingBroadcast != null) {
1079 boolean printed = false;
1080 for (int i=mParallelBroadcasts.size()-1; i>=0; i--) {
1081 BroadcastRecord br = mParallelBroadcasts.get(i);
1082 if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) {
1083 continue;
1084 }
1085 if (!printed) {
1086 if (needSep) {
1087 pw.println();
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001088 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001089 needSep = true;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001090 printed = true;
1091 pw.println(" Active broadcasts [" + mQueueName + "]:");
1092 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001093 pw.println(" Active Broadcast " + mQueueName + " #" + i + ":");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001094 br.dump(pw, " ");
1095 }
1096 printed = false;
1097 needSep = true;
1098 for (int i=mOrderedBroadcasts.size()-1; i>=0; i--) {
1099 BroadcastRecord br = mOrderedBroadcasts.get(i);
1100 if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) {
1101 continue;
1102 }
1103 if (!printed) {
1104 if (needSep) {
1105 pw.println();
1106 }
1107 needSep = true;
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001108 printed = true;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001109 pw.println(" Active ordered broadcasts [" + mQueueName + "]:");
1110 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001111 pw.println(" Active Ordered Broadcast " + mQueueName + " #" + i + ":");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001112 mOrderedBroadcasts.get(i).dump(pw, " ");
1113 }
1114 if (dumpPackage == null || (mPendingBroadcast != null
1115 && dumpPackage.equals(mPendingBroadcast.callerPackage))) {
1116 if (needSep) {
1117 pw.println();
1118 }
1119 pw.println(" Pending broadcast [" + mQueueName + "]:");
1120 if (mPendingBroadcast != null) {
1121 mPendingBroadcast.dump(pw, " ");
1122 } else {
1123 pw.println(" (null)");
1124 }
1125 needSep = true;
1126 }
1127 }
1128
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001129 int i;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001130 boolean printed = false;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001131 for (i=0; i<MAX_BROADCAST_HISTORY; i++) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001132 BroadcastRecord r = mBroadcastHistory[i];
1133 if (r == null) {
1134 break;
1135 }
1136 if (dumpPackage != null && !dumpPackage.equals(r.callerPackage)) {
1137 continue;
1138 }
1139 if (!printed) {
1140 if (needSep) {
1141 pw.println();
1142 }
1143 needSep = true;
1144 pw.println(" Historical broadcasts [" + mQueueName + "]:");
1145 printed = true;
1146 }
1147 if (dumpAll) {
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001148 pw.print(" Historical Broadcast " + mQueueName + " #");
1149 pw.print(i); pw.println(":");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001150 r.dump(pw, " ");
1151 } else {
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001152 pw.print(" #"); pw.print(i); pw.print(": "); pw.println(r);
1153 pw.print(" ");
1154 pw.println(r.intent.toShortString(false, true, true, false));
Dianne Hackborna40cfeb2013-03-25 17:49:36 -07001155 if (r.targetComp != null && r.targetComp != r.intent.getComponent()) {
1156 pw.print(" targetComp: "); pw.println(r.targetComp.toShortString());
1157 }
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001158 Bundle bundle = r.intent.getExtras();
1159 if (bundle != null) {
1160 pw.print(" extras: "); pw.println(bundle.toString());
1161 }
1162 }
1163 }
1164
1165 if (dumpPackage == null) {
1166 if (dumpAll) {
1167 i = 0;
1168 printed = false;
1169 }
1170 for (; i<MAX_BROADCAST_SUMMARY_HISTORY; i++) {
1171 Intent intent = mBroadcastSummaryHistory[i];
1172 if (intent == null) {
1173 break;
1174 }
1175 if (!printed) {
1176 if (needSep) {
1177 pw.println();
1178 }
1179 needSep = true;
1180 pw.println(" Historical broadcasts summary [" + mQueueName + "]:");
1181 printed = true;
1182 }
1183 if (!dumpAll && i >= 50) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001184 pw.println(" ...");
1185 break;
1186 }
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001187 pw.print(" #"); pw.print(i); pw.print(": ");
1188 pw.println(intent.toShortString(false, true, true, false));
1189 Bundle bundle = intent.getExtras();
1190 if (bundle != null) {
1191 pw.print(" extras: "); pw.println(bundle.toString());
1192 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001193 }
1194 }
1195
1196 return needSep;
1197 }
1198}