blob: 7ab37948711764632d4be5744fa4da6109003761 [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;
Amith Yamasani4b9d79c2014-05-21 19:14:21 -070030import android.content.pm.ApplicationInfo;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080031import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
33import android.os.Bundle;
34import android.os.Handler;
35import android.os.IBinder;
Jeff Brown6f357d32014-01-15 20:40:55 -080036import android.os.Looper;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080037import android.os.Message;
38import android.os.Process;
39import android.os.RemoteException;
40import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070041import android.os.UserHandle;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080042import android.util.EventLog;
Dianne Hackborn6c5406a2012-11-29 16:18:01 -080043import android.util.Log;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080044import android.util.Slog;
45
46/**
47 * BROADCASTS
48 *
49 * We keep two broadcast queues and associated bookkeeping, one for those at
50 * foreground priority, and one for normal (background-priority) broadcasts.
51 */
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070052public final class BroadcastQueue {
Dianne Hackborn40c8db52012-02-10 18:59:48 -080053 static final String TAG = "BroadcastQueue";
54 static final String TAG_MU = ActivityManagerService.TAG_MU;
55 static final boolean DEBUG_BROADCAST = ActivityManagerService.DEBUG_BROADCAST;
56 static final boolean DEBUG_BROADCAST_LIGHT = ActivityManagerService.DEBUG_BROADCAST_LIGHT;
57 static final boolean DEBUG_MU = ActivityManagerService.DEBUG_MU;
58
Dianne Hackborn4c51de42013-10-16 23:34:35 -070059 static final int MAX_BROADCAST_HISTORY = ActivityManager.isLowRamDeviceStatic() ? 10 : 50;
Dianne Hackborn6285a322013-09-18 12:09:47 -070060 static final int MAX_BROADCAST_SUMMARY_HISTORY
Dianne Hackborn4c51de42013-10-16 23:34:35 -070061 = ActivityManager.isLowRamDeviceStatic() ? 25 : 300;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080062
63 final ActivityManagerService mService;
64
65 /**
66 * Recognizable moniker for this queue
67 */
68 final String mQueueName;
69
70 /**
71 * Timeout period for this queue's broadcasts
72 */
73 final long mTimeoutPeriod;
74
75 /**
Dianne Hackborn6285a322013-09-18 12:09:47 -070076 * If true, we can delay broadcasts while waiting services to finish in the previous
77 * receiver's process.
78 */
79 final boolean mDelayBehindServices;
80
81 /**
Dianne Hackborn40c8db52012-02-10 18:59:48 -080082 * Lists of all active broadcasts that are to be executed immediately
83 * (without waiting for another broadcast to finish). Currently this only
84 * contains broadcasts to registered receivers, to avoid spinning up
85 * a bunch of processes to execute IntentReceiver components. Background-
86 * and foreground-priority broadcasts are queued separately.
87 */
Dianne Hackborn6285a322013-09-18 12:09:47 -070088 final ArrayList<BroadcastRecord> mParallelBroadcasts = new ArrayList<BroadcastRecord>();
89
Dianne Hackborn40c8db52012-02-10 18:59:48 -080090 /**
91 * List of all active broadcasts that are to be executed one at a time.
92 * The object at the top of the list is the currently activity broadcasts;
93 * those after it are waiting for the top to finish. As with parallel
94 * broadcasts, separate background- and foreground-priority queues are
95 * maintained.
96 */
Dianne Hackborn6285a322013-09-18 12:09:47 -070097 final ArrayList<BroadcastRecord> mOrderedBroadcasts = new ArrayList<BroadcastRecord>();
Dianne Hackborn40c8db52012-02-10 18:59:48 -080098
99 /**
100 * Historical data of past broadcasts, for debugging.
101 */
Dianne Hackborn6285a322013-09-18 12:09:47 -0700102 final BroadcastRecord[] mBroadcastHistory = new BroadcastRecord[MAX_BROADCAST_HISTORY];
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800103
104 /**
Dianne Hackbornc0bd7472012-10-09 14:00:30 -0700105 * Summary of historical data of past broadcasts, for debugging.
106 */
Dianne Hackborn6285a322013-09-18 12:09:47 -0700107 final Intent[] mBroadcastSummaryHistory = new Intent[MAX_BROADCAST_SUMMARY_HISTORY];
Dianne Hackbornc0bd7472012-10-09 14:00:30 -0700108
109 /**
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800110 * Set when we current have a BROADCAST_INTENT_MSG in flight.
111 */
112 boolean mBroadcastsScheduled = false;
113
114 /**
115 * True if we have a pending unexpired BROADCAST_TIMEOUT_MSG posted to our handler.
116 */
117 boolean mPendingBroadcastTimeoutMessage;
118
119 /**
120 * Intent broadcasts that we have tried to start, but are
121 * waiting for the application's process to be created. We only
122 * need one per scheduling class (instead of a list) because we always
123 * process broadcasts one at a time, so no others can be started while
124 * waiting for this one.
125 */
126 BroadcastRecord mPendingBroadcast = null;
127
128 /**
129 * The receiver index that is pending, to restart the broadcast if needed.
130 */
131 int mPendingBroadcastRecvIndex;
132
133 static final int BROADCAST_INTENT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG;
134 static final int BROADCAST_TIMEOUT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG + 1;
135
Jeff Brown6f357d32014-01-15 20:40:55 -0800136 final BroadcastHandler mHandler;
137
138 private final class BroadcastHandler extends Handler {
139 public BroadcastHandler(Looper looper) {
140 super(looper, null, true);
141 }
142
143 @Override
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800144 public void handleMessage(Message msg) {
145 switch (msg.what) {
146 case BROADCAST_INTENT_MSG: {
147 if (DEBUG_BROADCAST) Slog.v(
148 TAG, "Received BROADCAST_INTENT_MSG");
149 processNextBroadcast(true);
150 } break;
151 case BROADCAST_TIMEOUT_MSG: {
152 synchronized (mService) {
153 broadcastTimeoutLocked(true);
154 }
155 } break;
156 }
157 }
158 };
159
160 private final class AppNotResponding implements Runnable {
161 private final ProcessRecord mApp;
162 private final String mAnnotation;
163
164 public AppNotResponding(ProcessRecord app, String annotation) {
165 mApp = app;
166 mAnnotation = annotation;
167 }
168
169 @Override
170 public void run() {
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700171 mService.appNotResponding(mApp, null, null, false, mAnnotation);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800172 }
173 }
174
Jeff Brown6f357d32014-01-15 20:40:55 -0800175 BroadcastQueue(ActivityManagerService service, Handler handler,
176 String name, long timeoutPeriod, boolean allowDelayBehindServices) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800177 mService = service;
Jeff Brown6f357d32014-01-15 20:40:55 -0800178 mHandler = new BroadcastHandler(handler.getLooper());
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800179 mQueueName = name;
180 mTimeoutPeriod = timeoutPeriod;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700181 mDelayBehindServices = allowDelayBehindServices;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800182 }
183
184 public boolean isPendingBroadcastProcessLocked(int pid) {
185 return mPendingBroadcast != null && mPendingBroadcast.curApp.pid == pid;
186 }
187
188 public void enqueueParallelBroadcastLocked(BroadcastRecord r) {
189 mParallelBroadcasts.add(r);
190 }
191
192 public void enqueueOrderedBroadcastLocked(BroadcastRecord r) {
193 mOrderedBroadcasts.add(r);
194 }
195
196 public final boolean replaceParallelBroadcastLocked(BroadcastRecord r) {
197 for (int i=mParallelBroadcasts.size()-1; i>=0; i--) {
198 if (r.intent.filterEquals(mParallelBroadcasts.get(i).intent)) {
199 if (DEBUG_BROADCAST) Slog.v(TAG,
200 "***** DROPPING PARALLEL ["
201 + mQueueName + "]: " + r.intent);
202 mParallelBroadcasts.set(i, r);
203 return true;
204 }
205 }
206 return false;
207 }
208
209 public final boolean replaceOrderedBroadcastLocked(BroadcastRecord r) {
210 for (int i=mOrderedBroadcasts.size()-1; i>0; i--) {
211 if (r.intent.filterEquals(mOrderedBroadcasts.get(i).intent)) {
212 if (DEBUG_BROADCAST) Slog.v(TAG,
213 "***** DROPPING ORDERED ["
214 + mQueueName + "]: " + r.intent);
215 mOrderedBroadcasts.set(i, r);
216 return true;
217 }
218 }
219 return false;
220 }
221
222 private final void processCurBroadcastLocked(BroadcastRecord r,
223 ProcessRecord app) throws RemoteException {
224 if (DEBUG_BROADCAST) Slog.v(TAG,
225 "Process cur broadcast " + r + " for app " + app);
226 if (app.thread == null) {
227 throw new RemoteException();
228 }
229 r.receiver = app.thread.asBinder();
230 r.curApp = app;
231 app.curReceiver = r;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700232 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_RECEIVER);
Dianne Hackborndb926082013-10-31 16:32:44 -0700233 mService.updateLruProcessLocked(app, false, null);
234 mService.updateOomAdjLocked();
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800235
236 // Tell the application to launch this receiver.
237 r.intent.setComponent(r.curComponent);
238
239 boolean started = false;
240 try {
241 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG,
242 "Delivering to component " + r.curComponent
243 + ": " + r);
244 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
245 app.thread.scheduleReceiver(new Intent(r.intent), r.curReceiver,
246 mService.compatibilityInfoForPackageLocked(r.curReceiver.applicationInfo),
Dianne Hackborna413dc02013-07-12 12:02:55 -0700247 r.resultCode, r.resultData, r.resultExtras, r.ordered, r.userId,
248 app.repProcState);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800249 if (DEBUG_BROADCAST) Slog.v(TAG,
250 "Process cur broadcast " + r + " DELIVERED for app " + app);
251 started = true;
252 } finally {
253 if (!started) {
254 if (DEBUG_BROADCAST) Slog.v(TAG,
255 "Process cur broadcast " + r + ": NOT STARTED!");
256 r.receiver = null;
257 r.curApp = null;
258 app.curReceiver = null;
259 }
260 }
261 }
262
263 public boolean sendPendingBroadcastsLocked(ProcessRecord app) {
264 boolean didSomething = false;
265 final BroadcastRecord br = mPendingBroadcast;
266 if (br != null && br.curApp.pid == app.pid) {
267 try {
268 mPendingBroadcast = null;
269 processCurBroadcastLocked(br, app);
270 didSomething = true;
271 } catch (Exception e) {
272 Slog.w(TAG, "Exception in new application when starting receiver "
273 + br.curComponent.flattenToShortString(), e);
274 logBroadcastReceiverDiscardLocked(br);
275 finishReceiverLocked(br, br.resultCode, br.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700276 br.resultExtras, br.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800277 scheduleBroadcastsLocked();
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700278 // We need to reset the state if we failed to start the receiver.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800279 br.state = BroadcastRecord.IDLE;
280 throw new RuntimeException(e.getMessage());
281 }
282 }
283 return didSomething;
284 }
285
286 public void skipPendingBroadcastLocked(int pid) {
287 final BroadcastRecord br = mPendingBroadcast;
288 if (br != null && br.curApp.pid == pid) {
289 br.state = BroadcastRecord.IDLE;
290 br.nextReceiver = mPendingBroadcastRecvIndex;
291 mPendingBroadcast = null;
292 scheduleBroadcastsLocked();
293 }
294 }
295
296 public void skipCurrentReceiverLocked(ProcessRecord app) {
297 boolean reschedule = false;
298 BroadcastRecord r = app.curReceiver;
Kenji Sugimoto4472fa972014-07-17 14:50:41 +0900299 if (r != null && r.queue == this) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800300 // The current broadcast is waiting for this app's receiver
301 // to be finished. Looks like that's not going to happen, so
302 // let the broadcast continue.
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
309 r = mPendingBroadcast;
310 if (r != null && r.curApp == app) {
311 if (DEBUG_BROADCAST) Slog.v(TAG,
312 "[" + mQueueName + "] skip & discard pending app " + r);
313 logBroadcastReceiverDiscardLocked(r);
314 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700315 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800316 reschedule = true;
317 }
318 if (reschedule) {
319 scheduleBroadcastsLocked();
320 }
321 }
322
323 public void scheduleBroadcastsLocked() {
324 if (DEBUG_BROADCAST) Slog.v(TAG, "Schedule broadcasts ["
325 + mQueueName + "]: current="
326 + mBroadcastsScheduled);
327
328 if (mBroadcastsScheduled) {
329 return;
330 }
331 mHandler.sendMessage(mHandler.obtainMessage(BROADCAST_INTENT_MSG, this));
332 mBroadcastsScheduled = true;
333 }
334
335 public BroadcastRecord getMatchingOrderedReceiver(IBinder receiver) {
336 if (mOrderedBroadcasts.size() > 0) {
337 final BroadcastRecord r = mOrderedBroadcasts.get(0);
338 if (r != null && r.receiver == receiver) {
339 return r;
340 }
341 }
342 return null;
343 }
344
345 public boolean finishReceiverLocked(BroadcastRecord r, int resultCode,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700346 String resultData, Bundle resultExtras, boolean resultAbort, boolean waitForServices) {
347 final int state = r.state;
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700348 final ActivityInfo receiver = r.curReceiver;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800349 r.state = BroadcastRecord.IDLE;
350 if (state == BroadcastRecord.IDLE) {
Dianne Hackborn6285a322013-09-18 12:09:47 -0700351 Slog.w(TAG, "finishReceiver [" + mQueueName + "] called but state is IDLE");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800352 }
353 r.receiver = null;
354 r.intent.setComponent(null);
Guobin Zhang04d0bb62014-03-07 17:47:10 +0800355 if (r.curApp != null && r.curApp.curReceiver == r) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800356 r.curApp.curReceiver = null;
357 }
358 if (r.curFilter != null) {
359 r.curFilter.receiverList.curBroadcast = null;
360 }
361 r.curFilter = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800362 r.curReceiver = null;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700363 r.curApp = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800364 mPendingBroadcast = null;
365
366 r.resultCode = resultCode;
367 r.resultData = resultData;
368 r.resultExtras = resultExtras;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700369 if (resultAbort && (r.intent.getFlags()&Intent.FLAG_RECEIVER_NO_ABORT) == 0) {
370 r.resultAbort = resultAbort;
371 } else {
372 r.resultAbort = false;
373 }
374
375 if (waitForServices && r.curComponent != null && r.queue.mDelayBehindServices
376 && r.queue.mOrderedBroadcasts.size() > 0
377 && r.queue.mOrderedBroadcasts.get(0) == r) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700378 ActivityInfo nextReceiver;
379 if (r.nextReceiver < r.receivers.size()) {
380 Object obj = r.receivers.get(r.nextReceiver);
381 nextReceiver = (obj instanceof ActivityInfo) ? (ActivityInfo)obj : null;
382 } else {
383 nextReceiver = null;
384 }
385 // Don't do this if the next receive is in the same process as the current one.
386 if (receiver == null || nextReceiver == null
387 || receiver.applicationInfo.uid != nextReceiver.applicationInfo.uid
388 || !receiver.processName.equals(nextReceiver.processName)) {
389 // In this case, we are ready to process the next receiver for the current broadcast,
390 // but are on a queue that would like to wait for services to finish before moving
391 // on. If there are background services currently starting, then we will go into a
392 // special state where we hold off on continuing this broadcast until they are done.
393 if (mService.mServices.hasBackgroundServices(r.userId)) {
394 Slog.i(ActivityManagerService.TAG, "Delay finish: "
395 + r.curComponent.flattenToShortString());
396 r.state = BroadcastRecord.WAITING_SERVICES;
397 return false;
398 }
Dianne Hackborn6285a322013-09-18 12:09:47 -0700399 }
400 }
401
402 r.curComponent = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800403
404 // We will process the next receiver right now if this is finishing
405 // an app receiver (which is always asynchronous) or after we have
406 // come back from calling a receiver.
407 return state == BroadcastRecord.APP_RECEIVE
408 || state == BroadcastRecord.CALL_DONE_RECEIVE;
409 }
410
Dianne Hackborn6285a322013-09-18 12:09:47 -0700411 public void backgroundServicesFinishedLocked(int userId) {
412 if (mOrderedBroadcasts.size() > 0) {
413 BroadcastRecord br = mOrderedBroadcasts.get(0);
414 if (br.userId == userId && br.state == BroadcastRecord.WAITING_SERVICES) {
415 Slog.i(ActivityManagerService.TAG, "Resuming delayed broadcast");
416 br.curComponent = null;
417 br.state = BroadcastRecord.IDLE;
418 processNextBroadcast(false);
419 }
420 }
421 }
422
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800423 private static void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver,
424 Intent intent, int resultCode, String data, Bundle extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700425 boolean ordered, boolean sticky, int sendingUser) throws RemoteException {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800426 // Send the intent to the receiver asynchronously using one-way binder calls.
Craig Mautner38c1a5f2014-07-21 15:36:37 +0000427 if (app != null) {
428 if (app.thread != null) {
429 // If we have an app thread, do the call through that so it is
430 // correctly ordered with other one-way calls.
431 app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode,
432 data, extras, ordered, sticky, sendingUser, app.repProcState);
433 } else {
434 // Application has died. Receiver doesn't exist.
435 throw new RemoteException("app.thread must not be null");
436 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800437 } else {
Dianne Hackborn20e80982012-08-31 19:00:44 -0700438 receiver.performReceive(intent, resultCode, data, extras, ordered,
439 sticky, sendingUser);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800440 }
441 }
442
443 private final void deliverToRegisteredReceiverLocked(BroadcastRecord r,
444 BroadcastFilter filter, boolean ordered) {
445 boolean skip = false;
Amith Yamasani8bf06ed2012-08-27 19:30:30 -0700446 if (filter.requiredPermission != null) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800447 int perm = mService.checkComponentPermission(filter.requiredPermission,
448 r.callingPid, r.callingUid, -1, true);
449 if (perm != PackageManager.PERMISSION_GRANTED) {
450 Slog.w(TAG, "Permission Denial: broadcasting "
451 + r.intent.toString()
452 + " from " + r.callerPackage + " (pid="
453 + r.callingPid + ", uid=" + r.callingUid + ")"
454 + " requires " + filter.requiredPermission
455 + " due to registered receiver " + filter);
456 skip = true;
457 }
458 }
Dianne Hackbornb4163a62012-08-02 18:31:26 -0700459 if (!skip && r.requiredPermission != null) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800460 int perm = mService.checkComponentPermission(r.requiredPermission,
461 filter.receiverList.pid, filter.receiverList.uid, -1, true);
462 if (perm != PackageManager.PERMISSION_GRANTED) {
463 Slog.w(TAG, "Permission Denial: receiving "
464 + r.intent.toString()
465 + " to " + filter.receiverList.app
466 + " (pid=" + filter.receiverList.pid
467 + ", uid=" + filter.receiverList.uid + ")"
468 + " requires " + r.requiredPermission
469 + " due to sender " + r.callerPackage
470 + " (uid " + r.callingUid + ")");
471 skip = true;
472 }
473 }
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800474 if (r.appOp != AppOpsManager.OP_NONE) {
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700475 int mode = mService.mAppOpsService.noteOperation(r.appOp,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800476 filter.receiverList.uid, filter.packageName);
477 if (mode != AppOpsManager.MODE_ALLOWED) {
478 if (DEBUG_BROADCAST) Slog.v(TAG,
479 "App op " + r.appOp + " not allowed for broadcast to uid "
480 + filter.receiverList.uid + " pkg " + filter.packageName);
481 skip = true;
482 }
483 }
Ben Gruver49660c72013-08-06 19:54:08 -0700484 if (!skip) {
485 skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,
486 r.callingPid, r.resolvedType, filter.receiverList.uid);
487 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800488
Dianne Hackborn9357b112013-10-03 18:27:48 -0700489 if (filter.receiverList.app == null || filter.receiverList.app.crashing) {
490 Slog.w(TAG, "Skipping deliver [" + mQueueName + "] " + r
491 + " to " + filter.receiverList + ": process crashing");
492 skip = true;
493 }
494
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800495 if (!skip) {
496 // If this is not being sent as an ordered broadcast, then we
497 // don't want to touch the fields that keep track of the current
498 // state of ordered broadcasts.
499 if (ordered) {
500 r.receiver = filter.receiverList.receiver.asBinder();
501 r.curFilter = filter;
502 filter.receiverList.curBroadcast = r;
503 r.state = BroadcastRecord.CALL_IN_RECEIVE;
504 if (filter.receiverList.app != null) {
505 // Bump hosting application to no longer be in background
506 // scheduling class. Note that we can't do that if there
507 // isn't an app... but we can only be in that case for
508 // things that directly call the IActivityManager API, which
509 // are already core system stuff so don't matter for this.
510 r.curApp = filter.receiverList.app;
511 filter.receiverList.app.curReceiver = r;
Dianne Hackborn684bf342014-04-29 17:56:57 -0700512 mService.updateOomAdjLocked(r.curApp);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800513 }
514 }
515 try {
Todd Kennedyd2f15112015-01-21 15:25:56 -0800516 if (DEBUG_BROADCAST_LIGHT) Slog.i(TAG, "Delivering to " + filter + " : " + r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800517 performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700518 new Intent(r.intent), r.resultCode, r.resultData,
519 r.resultExtras, r.ordered, r.initialSticky, r.userId);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800520 if (ordered) {
521 r.state = BroadcastRecord.CALL_DONE_RECEIVE;
522 }
523 } catch (RemoteException e) {
524 Slog.w(TAG, "Failure sending broadcast " + r.intent, e);
525 if (ordered) {
526 r.receiver = null;
527 r.curFilter = null;
528 filter.receiverList.curBroadcast = null;
529 if (filter.receiverList.app != null) {
530 filter.receiverList.app.curReceiver = null;
531 }
532 }
533 }
534 }
535 }
536
537 final void processNextBroadcast(boolean fromMsg) {
538 synchronized(mService) {
539 BroadcastRecord r;
540
541 if (DEBUG_BROADCAST) Slog.v(TAG, "processNextBroadcast ["
542 + mQueueName + "]: "
543 + mParallelBroadcasts.size() + " broadcasts, "
544 + mOrderedBroadcasts.size() + " ordered broadcasts");
545
546 mService.updateCpuStats();
547
548 if (fromMsg) {
549 mBroadcastsScheduled = false;
550 }
551
552 // First, deliver any non-serialized broadcasts right away.
553 while (mParallelBroadcasts.size() > 0) {
554 r = mParallelBroadcasts.remove(0);
555 r.dispatchTime = SystemClock.uptimeMillis();
556 r.dispatchClockTime = System.currentTimeMillis();
557 final int N = r.receivers.size();
558 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing parallel broadcast ["
559 + mQueueName + "] " + r);
560 for (int i=0; i<N; i++) {
561 Object target = r.receivers.get(i);
562 if (DEBUG_BROADCAST) Slog.v(TAG,
563 "Delivering non-ordered on [" + mQueueName + "] to registered "
564 + target + ": " + r);
565 deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false);
566 }
567 addBroadcastToHistoryLocked(r);
568 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Done with parallel broadcast ["
569 + mQueueName + "] " + r);
570 }
571
572 // Now take care of the next serialized one...
573
574 // If we are waiting for a process to come up to handle the next
575 // broadcast, then do nothing at this point. Just in case, we
576 // check that the process we're waiting for still exists.
577 if (mPendingBroadcast != null) {
578 if (DEBUG_BROADCAST_LIGHT) {
579 Slog.v(TAG, "processNextBroadcast ["
580 + mQueueName + "]: waiting for "
581 + mPendingBroadcast.curApp);
582 }
583
584 boolean isDead;
585 synchronized (mService.mPidsSelfLocked) {
Dianne Hackborn9357b112013-10-03 18:27:48 -0700586 ProcessRecord proc = mService.mPidsSelfLocked.get(mPendingBroadcast.curApp.pid);
587 isDead = proc == null || proc.crashing;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800588 }
589 if (!isDead) {
590 // It's still alive, so keep waiting
591 return;
592 } else {
593 Slog.w(TAG, "pending app ["
594 + mQueueName + "]" + mPendingBroadcast.curApp
595 + " died before responding to broadcast");
596 mPendingBroadcast.state = BroadcastRecord.IDLE;
597 mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex;
598 mPendingBroadcast = null;
599 }
600 }
601
602 boolean looped = false;
603
604 do {
605 if (mOrderedBroadcasts.size() == 0) {
606 // No more broadcasts pending, so all done!
607 mService.scheduleAppGcsLocked();
608 if (looped) {
609 // If we had finished the last ordered broadcast, then
610 // make sure all processes have correct oom and sched
611 // adjustments.
612 mService.updateOomAdjLocked();
613 }
614 return;
615 }
616 r = mOrderedBroadcasts.get(0);
617 boolean forceReceive = false;
618
619 // Ensure that even if something goes awry with the timeout
620 // detection, we catch "hung" broadcasts here, discard them,
621 // and continue to make progress.
622 //
623 // This is only done if the system is ready so that PRE_BOOT_COMPLETED
624 // receivers don't get executed with timeouts. They're intended for
625 // one time heavy lifting after system upgrades and can take
626 // significant amounts of time.
627 int numReceivers = (r.receivers != null) ? r.receivers.size() : 0;
628 if (mService.mProcessesReady && r.dispatchTime > 0) {
629 long now = SystemClock.uptimeMillis();
630 if ((numReceivers > 0) &&
631 (now > r.dispatchTime + (2*mTimeoutPeriod*numReceivers))) {
632 Slog.w(TAG, "Hung broadcast ["
633 + mQueueName + "] discarded after timeout failure:"
634 + " now=" + now
635 + " dispatchTime=" + r.dispatchTime
636 + " startTime=" + r.receiverTime
637 + " intent=" + r.intent
638 + " numReceivers=" + numReceivers
639 + " nextReceiver=" + r.nextReceiver
640 + " state=" + r.state);
641 broadcastTimeoutLocked(false); // forcibly finish this broadcast
642 forceReceive = true;
643 r.state = BroadcastRecord.IDLE;
644 }
645 }
646
647 if (r.state != BroadcastRecord.IDLE) {
648 if (DEBUG_BROADCAST) Slog.d(TAG,
649 "processNextBroadcast("
650 + mQueueName + ") called when not idle (state="
651 + r.state + ")");
652 return;
653 }
654
655 if (r.receivers == null || r.nextReceiver >= numReceivers
656 || r.resultAbort || forceReceive) {
657 // No more receivers for this broadcast! Send the final
658 // result if requested...
659 if (r.resultTo != null) {
660 try {
Todd Kennedyd2f15112015-01-21 15:25:56 -0800661 if (DEBUG_BROADCAST) Slog.i(TAG,
662 "Finishing broadcast [" + mQueueName + "] "
663 + r.intent.getAction() + " app=" + r.callerApp);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800664 performReceiveLocked(r.callerApp, r.resultTo,
665 new Intent(r.intent), r.resultCode,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700666 r.resultData, r.resultExtras, false, false, r.userId);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800667 // Set this to null so that the reference
Dianne Hackborn9357b112013-10-03 18:27:48 -0700668 // (local and remote) isn't kept in the mBroadcastHistory.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800669 r.resultTo = null;
670 } catch (RemoteException e) {
Craig Mautner38c1a5f2014-07-21 15:36:37 +0000671 r.resultTo = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800672 Slog.w(TAG, "Failure ["
673 + mQueueName + "] sending broadcast result of "
674 + r.intent, e);
675 }
676 }
677
678 if (DEBUG_BROADCAST) Slog.v(TAG, "Cancelling BROADCAST_TIMEOUT_MSG");
679 cancelBroadcastTimeoutLocked();
680
681 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Finished with ordered broadcast "
682 + r);
683
684 // ... and on to the next...
685 addBroadcastToHistoryLocked(r);
686 mOrderedBroadcasts.remove(0);
687 r = null;
688 looped = true;
689 continue;
690 }
691 } while (r == null);
692
693 // Get the next receiver...
694 int recIdx = r.nextReceiver++;
695
696 // Keep track of when this receiver started, and make sure there
697 // is a timeout message pending to kill it if need be.
698 r.receiverTime = SystemClock.uptimeMillis();
699 if (recIdx == 0) {
700 r.dispatchTime = r.receiverTime;
701 r.dispatchClockTime = System.currentTimeMillis();
702 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing ordered broadcast ["
703 + mQueueName + "] " + r);
704 }
705 if (! mPendingBroadcastTimeoutMessage) {
706 long timeoutTime = r.receiverTime + mTimeoutPeriod;
707 if (DEBUG_BROADCAST) Slog.v(TAG,
708 "Submitting BROADCAST_TIMEOUT_MSG ["
709 + mQueueName + "] for " + r + " at " + timeoutTime);
710 setBroadcastTimeoutLocked(timeoutTime);
711 }
712
713 Object nextReceiver = r.receivers.get(recIdx);
714 if (nextReceiver instanceof BroadcastFilter) {
715 // Simple case: this is a registered receiver who gets
716 // a direct call.
717 BroadcastFilter filter = (BroadcastFilter)nextReceiver;
718 if (DEBUG_BROADCAST) Slog.v(TAG,
719 "Delivering ordered ["
720 + mQueueName + "] to registered "
721 + filter + ": " + r);
722 deliverToRegisteredReceiverLocked(r, filter, r.ordered);
723 if (r.receiver == null || !r.ordered) {
724 // The receiver has already finished, so schedule to
725 // process the next one.
726 if (DEBUG_BROADCAST) Slog.v(TAG, "Quick finishing ["
727 + mQueueName + "]: ordered="
728 + r.ordered + " receiver=" + r.receiver);
729 r.state = BroadcastRecord.IDLE;
730 scheduleBroadcastsLocked();
731 }
732 return;
733 }
734
735 // Hard case: need to instantiate the receiver, possibly
736 // starting its application process to host it.
737
738 ResolveInfo info =
739 (ResolveInfo)nextReceiver;
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700740 ComponentName component = new ComponentName(
741 info.activityInfo.applicationInfo.packageName,
742 info.activityInfo.name);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800743
744 boolean skip = false;
745 int perm = mService.checkComponentPermission(info.activityInfo.permission,
746 r.callingPid, r.callingUid, info.activityInfo.applicationInfo.uid,
747 info.activityInfo.exported);
748 if (perm != PackageManager.PERMISSION_GRANTED) {
749 if (!info.activityInfo.exported) {
750 Slog.w(TAG, "Permission Denial: broadcasting "
751 + r.intent.toString()
752 + " from " + r.callerPackage + " (pid=" + r.callingPid
753 + ", uid=" + r.callingUid + ")"
754 + " is not exported from uid " + info.activityInfo.applicationInfo.uid
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700755 + " due to receiver " + component.flattenToShortString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800756 } else {
757 Slog.w(TAG, "Permission Denial: broadcasting "
758 + r.intent.toString()
759 + " from " + r.callerPackage + " (pid=" + r.callingPid
760 + ", uid=" + r.callingUid + ")"
761 + " requires " + info.activityInfo.permission
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700762 + " due to receiver " + component.flattenToShortString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800763 }
764 skip = true;
765 }
766 if (info.activityInfo.applicationInfo.uid != Process.SYSTEM_UID &&
767 r.requiredPermission != null) {
768 try {
769 perm = AppGlobals.getPackageManager().
770 checkPermission(r.requiredPermission,
771 info.activityInfo.applicationInfo.packageName);
772 } catch (RemoteException e) {
773 perm = PackageManager.PERMISSION_DENIED;
774 }
775 if (perm != PackageManager.PERMISSION_GRANTED) {
776 Slog.w(TAG, "Permission Denial: receiving "
777 + r.intent + " to "
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700778 + component.flattenToShortString()
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800779 + " requires " + r.requiredPermission
780 + " due to sender " + r.callerPackage
781 + " (uid " + r.callingUid + ")");
782 skip = true;
783 }
784 }
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800785 if (r.appOp != AppOpsManager.OP_NONE) {
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700786 int mode = mService.mAppOpsService.noteOperation(r.appOp,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800787 info.activityInfo.applicationInfo.uid, info.activityInfo.packageName);
788 if (mode != AppOpsManager.MODE_ALLOWED) {
789 if (DEBUG_BROADCAST) Slog.v(TAG,
790 "App op " + r.appOp + " not allowed for broadcast to uid "
791 + info.activityInfo.applicationInfo.uid + " pkg "
792 + info.activityInfo.packageName);
793 skip = true;
794 }
795 }
Ben Gruver49660c72013-08-06 19:54:08 -0700796 if (!skip) {
797 skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,
798 r.callingPid, r.resolvedType, info.activityInfo.applicationInfo.uid);
799 }
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700800 boolean isSingleton = false;
801 try {
802 isSingleton = mService.isSingleton(info.activityInfo.processName,
803 info.activityInfo.applicationInfo,
804 info.activityInfo.name, info.activityInfo.flags);
805 } catch (SecurityException e) {
806 Slog.w(TAG, e.getMessage());
807 skip = true;
808 }
809 if ((info.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) {
810 if (ActivityManager.checkUidPermission(
811 android.Manifest.permission.INTERACT_ACROSS_USERS,
812 info.activityInfo.applicationInfo.uid)
813 != PackageManager.PERMISSION_GRANTED) {
814 Slog.w(TAG, "Permission Denial: Receiver " + component.flattenToShortString()
815 + " requests FLAG_SINGLE_USER, but app does not hold "
816 + android.Manifest.permission.INTERACT_ACROSS_USERS);
817 skip = true;
818 }
819 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800820 if (r.curApp != null && r.curApp.crashing) {
821 // If the target process is crashing, just skip it.
Dianne Hackborn9357b112013-10-03 18:27:48 -0700822 Slog.w(TAG, "Skipping deliver ordered [" + mQueueName + "] " + r
823 + " to " + r.curApp + ": process crashing");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800824 skip = true;
825 }
Christopher Tateba629da2013-11-13 17:42:28 -0800826 if (!skip) {
827 boolean isAvailable = false;
828 try {
829 isAvailable = AppGlobals.getPackageManager().isPackageAvailable(
830 info.activityInfo.packageName,
831 UserHandle.getUserId(info.activityInfo.applicationInfo.uid));
832 } catch (Exception e) {
833 // all such failures mean we skip this receiver
834 Slog.w(TAG, "Exception getting recipient info for "
835 + info.activityInfo.packageName, e);
836 }
837 if (!isAvailable) {
838 if (DEBUG_BROADCAST) {
839 Slog.v(TAG, "Skipping delivery to " + info.activityInfo.packageName
840 + " / " + info.activityInfo.applicationInfo.uid
841 + " : package no longer available");
842 }
843 skip = true;
844 }
845 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800846
847 if (skip) {
848 if (DEBUG_BROADCAST) Slog.v(TAG,
849 "Skipping delivery of ordered ["
850 + mQueueName + "] " + r + " for whatever reason");
851 r.receiver = null;
852 r.curFilter = null;
853 r.state = BroadcastRecord.IDLE;
854 scheduleBroadcastsLocked();
855 return;
856 }
857
858 r.state = BroadcastRecord.APP_RECEIVE;
859 String targetProcess = info.activityInfo.processName;
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700860 r.curComponent = component;
Amith Yamasani4b9d79c2014-05-21 19:14:21 -0700861 final int receiverUid = info.activityInfo.applicationInfo.uid;
862 // If it's a singleton, it needs to be the same app or a special app
863 if (r.callingUid != Process.SYSTEM_UID && isSingleton
864 && mService.isValidSingletonCall(r.callingUid, receiverUid)) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700865 info.activityInfo = mService.getActivityInfoForUser(info.activityInfo, 0);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800866 }
867 r.curReceiver = info.activityInfo;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700868 if (DEBUG_MU && r.callingUid > UserHandle.PER_USER_RANGE) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800869 Slog.v(TAG_MU, "Updated broadcast record activity info for secondary user, "
870 + info.activityInfo + ", callingUid = " + r.callingUid + ", uid = "
871 + info.activityInfo.applicationInfo.uid);
872 }
873
874 // Broadcast is being executed, its package can't be stopped.
875 try {
876 AppGlobals.getPackageManager().setPackageStoppedState(
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700877 r.curComponent.getPackageName(), false, UserHandle.getUserId(r.callingUid));
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800878 } catch (RemoteException e) {
879 } catch (IllegalArgumentException e) {
880 Slog.w(TAG, "Failed trying to unstop package "
881 + r.curComponent.getPackageName() + ": " + e);
882 }
883
884 // Is this receiver's application already running?
885 ProcessRecord app = mService.getProcessRecordLocked(targetProcess,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700886 info.activityInfo.applicationInfo.uid, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800887 if (app != null && app.thread != null) {
888 try {
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700889 app.addPackage(info.activityInfo.packageName,
890 info.activityInfo.applicationInfo.versionCode, mService.mProcessStats);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800891 processCurBroadcastLocked(r, app);
892 return;
893 } catch (RemoteException e) {
894 Slog.w(TAG, "Exception when sending broadcast to "
895 + r.curComponent, e);
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800896 } catch (RuntimeException e) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700897 Slog.wtf(TAG, "Failed sending broadcast to "
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800898 + r.curComponent + " with " + r.intent, e);
899 // If some unexpected exception happened, just skip
900 // this broadcast. At this point we are not in the call
901 // from a client, so throwing an exception out from here
902 // will crash the entire system instead of just whoever
903 // sent the broadcast.
904 logBroadcastReceiverDiscardLocked(r);
905 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700906 r.resultExtras, r.resultAbort, false);
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800907 scheduleBroadcastsLocked();
908 // We need to reset the state if we failed to start the receiver.
909 r.state = BroadcastRecord.IDLE;
910 return;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800911 }
912
913 // If a dead object exception was thrown -- fall through to
914 // restart the application.
915 }
916
917 // Not running -- get it started, to be executed when the app comes up.
918 if (DEBUG_BROADCAST) Slog.v(TAG,
919 "Need to start app ["
920 + mQueueName + "] " + targetProcess + " for broadcast " + r);
921 if ((r.curApp=mService.startProcessLocked(targetProcess,
922 info.activityInfo.applicationInfo, true,
923 r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND,
924 "broadcast", r.curComponent,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700925 (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0, false, false))
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800926 == null) {
927 // Ah, this recipient is unavailable. Finish it if necessary,
928 // and mark the broadcast record as ready for the next.
929 Slog.w(TAG, "Unable to launch app "
930 + info.activityInfo.applicationInfo.packageName + "/"
931 + info.activityInfo.applicationInfo.uid + " for broadcast "
932 + r.intent + ": process is bad");
933 logBroadcastReceiverDiscardLocked(r);
934 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700935 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800936 scheduleBroadcastsLocked();
937 r.state = BroadcastRecord.IDLE;
938 return;
939 }
940
941 mPendingBroadcast = r;
942 mPendingBroadcastRecvIndex = recIdx;
943 }
944 }
945
946 final void setBroadcastTimeoutLocked(long timeoutTime) {
947 if (! mPendingBroadcastTimeoutMessage) {
948 Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG, this);
949 mHandler.sendMessageAtTime(msg, timeoutTime);
950 mPendingBroadcastTimeoutMessage = true;
951 }
952 }
953
954 final void cancelBroadcastTimeoutLocked() {
955 if (mPendingBroadcastTimeoutMessage) {
956 mHandler.removeMessages(BROADCAST_TIMEOUT_MSG, this);
957 mPendingBroadcastTimeoutMessage = false;
958 }
959 }
960
961 final void broadcastTimeoutLocked(boolean fromMsg) {
962 if (fromMsg) {
963 mPendingBroadcastTimeoutMessage = false;
964 }
965
966 if (mOrderedBroadcasts.size() == 0) {
967 return;
968 }
969
970 long now = SystemClock.uptimeMillis();
971 BroadcastRecord r = mOrderedBroadcasts.get(0);
972 if (fromMsg) {
973 if (mService.mDidDexOpt) {
974 // Delay timeouts until dexopt finishes.
975 mService.mDidDexOpt = false;
976 long timeoutTime = SystemClock.uptimeMillis() + mTimeoutPeriod;
977 setBroadcastTimeoutLocked(timeoutTime);
978 return;
979 }
980 if (!mService.mProcessesReady) {
981 // Only process broadcast timeouts if the system is ready. That way
982 // PRE_BOOT_COMPLETED broadcasts can't timeout as they are intended
983 // to do heavy lifting for system up.
984 return;
985 }
986
987 long timeoutTime = r.receiverTime + mTimeoutPeriod;
988 if (timeoutTime > now) {
989 // We can observe premature timeouts because we do not cancel and reset the
990 // broadcast timeout message after each receiver finishes. Instead, we set up
991 // an initial timeout then kick it down the road a little further as needed
992 // when it expires.
993 if (DEBUG_BROADCAST) Slog.v(TAG,
994 "Premature timeout ["
995 + mQueueName + "] @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for "
996 + timeoutTime);
997 setBroadcastTimeoutLocked(timeoutTime);
998 return;
999 }
1000 }
1001
Dianne Hackborn6285a322013-09-18 12:09:47 -07001002 BroadcastRecord br = mOrderedBroadcasts.get(0);
1003 if (br.state == BroadcastRecord.WAITING_SERVICES) {
1004 // In this case the broadcast had already finished, but we had decided to wait
1005 // for started services to finish as well before going on. So if we have actually
1006 // waited long enough time timeout the broadcast, let's give up on the whole thing
1007 // and just move on to the next.
1008 Slog.i(ActivityManagerService.TAG, "Waited long enough for: " + (br.curComponent != null
1009 ? br.curComponent.flattenToShortString() : "(null)"));
1010 br.curComponent = null;
1011 br.state = BroadcastRecord.IDLE;
1012 processNextBroadcast(false);
1013 return;
1014 }
1015
1016 Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r. receiver
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001017 + ", started " + (now - r.receiverTime) + "ms ago");
1018 r.receiverTime = now;
1019 r.anrCount++;
1020
1021 // Current receiver has passed its expiration date.
1022 if (r.nextReceiver <= 0) {
1023 Slog.w(TAG, "Timeout on receiver with nextReceiver <= 0");
1024 return;
1025 }
1026
1027 ProcessRecord app = null;
1028 String anrMessage = null;
1029
1030 Object curReceiver = r.receivers.get(r.nextReceiver-1);
1031 Slog.w(TAG, "Receiver during timeout: " + curReceiver);
1032 logBroadcastReceiverDiscardLocked(r);
1033 if (curReceiver instanceof BroadcastFilter) {
1034 BroadcastFilter bf = (BroadcastFilter)curReceiver;
1035 if (bf.receiverList.pid != 0
1036 && bf.receiverList.pid != ActivityManagerService.MY_PID) {
1037 synchronized (mService.mPidsSelfLocked) {
1038 app = mService.mPidsSelfLocked.get(
1039 bf.receiverList.pid);
1040 }
1041 }
1042 } else {
1043 app = r.curApp;
1044 }
1045
1046 if (app != null) {
1047 anrMessage = "Broadcast of " + r.intent.toString();
1048 }
1049
1050 if (mPendingBroadcast == r) {
1051 mPendingBroadcast = null;
1052 }
1053
1054 // Move on to the next receiver.
1055 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -07001056 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001057 scheduleBroadcastsLocked();
1058
1059 if (anrMessage != null) {
1060 // Post the ANR to the handler since we do not want to process ANRs while
1061 // potentially holding our lock.
1062 mHandler.post(new AppNotResponding(app, anrMessage));
1063 }
1064 }
1065
1066 private final void addBroadcastToHistoryLocked(BroadcastRecord r) {
1067 if (r.callingUid < 0) {
1068 // This was from a registerReceiver() call; ignore it.
1069 return;
1070 }
1071 System.arraycopy(mBroadcastHistory, 0, mBroadcastHistory, 1,
1072 MAX_BROADCAST_HISTORY-1);
1073 r.finishTime = SystemClock.uptimeMillis();
1074 mBroadcastHistory[0] = r;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001075 System.arraycopy(mBroadcastSummaryHistory, 0, mBroadcastSummaryHistory, 1,
1076 MAX_BROADCAST_SUMMARY_HISTORY-1);
1077 mBroadcastSummaryHistory[0] = r.intent;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001078 }
1079
1080 final void logBroadcastReceiverDiscardLocked(BroadcastRecord r) {
1081 if (r.nextReceiver > 0) {
1082 Object curReceiver = r.receivers.get(r.nextReceiver-1);
1083 if (curReceiver instanceof BroadcastFilter) {
1084 BroadcastFilter bf = (BroadcastFilter) curReceiver;
1085 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001086 bf.owningUserId, System.identityHashCode(r),
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001087 r.intent.getAction(),
1088 r.nextReceiver - 1,
1089 System.identityHashCode(bf));
1090 } else {
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001091 ResolveInfo ri = (ResolveInfo)curReceiver;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001092 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001093 UserHandle.getUserId(ri.activityInfo.applicationInfo.uid),
1094 System.identityHashCode(r), r.intent.getAction(),
1095 r.nextReceiver - 1, ri.toString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001096 }
1097 } else {
1098 Slog.w(TAG, "Discarding broadcast before first receiver is invoked: "
1099 + r);
1100 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001101 -1, System.identityHashCode(r),
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001102 r.intent.getAction(),
1103 r.nextReceiver,
1104 "NONE");
1105 }
1106 }
1107
1108 final boolean dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args,
1109 int opti, boolean dumpAll, String dumpPackage, boolean needSep) {
1110 if (mParallelBroadcasts.size() > 0 || mOrderedBroadcasts.size() > 0
1111 || mPendingBroadcast != null) {
1112 boolean printed = false;
1113 for (int i=mParallelBroadcasts.size()-1; i>=0; i--) {
1114 BroadcastRecord br = mParallelBroadcasts.get(i);
1115 if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) {
1116 continue;
1117 }
1118 if (!printed) {
1119 if (needSep) {
1120 pw.println();
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001121 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001122 needSep = true;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001123 printed = true;
1124 pw.println(" Active broadcasts [" + mQueueName + "]:");
1125 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001126 pw.println(" Active Broadcast " + mQueueName + " #" + i + ":");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001127 br.dump(pw, " ");
1128 }
1129 printed = false;
1130 needSep = true;
1131 for (int i=mOrderedBroadcasts.size()-1; i>=0; i--) {
1132 BroadcastRecord br = mOrderedBroadcasts.get(i);
1133 if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) {
1134 continue;
1135 }
1136 if (!printed) {
1137 if (needSep) {
1138 pw.println();
1139 }
1140 needSep = true;
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001141 printed = true;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001142 pw.println(" Active ordered broadcasts [" + mQueueName + "]:");
1143 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001144 pw.println(" Active Ordered Broadcast " + mQueueName + " #" + i + ":");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001145 mOrderedBroadcasts.get(i).dump(pw, " ");
1146 }
1147 if (dumpPackage == null || (mPendingBroadcast != null
1148 && dumpPackage.equals(mPendingBroadcast.callerPackage))) {
1149 if (needSep) {
1150 pw.println();
1151 }
1152 pw.println(" Pending broadcast [" + mQueueName + "]:");
1153 if (mPendingBroadcast != null) {
1154 mPendingBroadcast.dump(pw, " ");
1155 } else {
1156 pw.println(" (null)");
1157 }
1158 needSep = true;
1159 }
1160 }
1161
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001162 int i;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001163 boolean printed = false;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001164 for (i=0; i<MAX_BROADCAST_HISTORY; i++) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001165 BroadcastRecord r = mBroadcastHistory[i];
1166 if (r == null) {
1167 break;
1168 }
1169 if (dumpPackage != null && !dumpPackage.equals(r.callerPackage)) {
1170 continue;
1171 }
1172 if (!printed) {
1173 if (needSep) {
1174 pw.println();
1175 }
1176 needSep = true;
1177 pw.println(" Historical broadcasts [" + mQueueName + "]:");
1178 printed = true;
1179 }
1180 if (dumpAll) {
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001181 pw.print(" Historical Broadcast " + mQueueName + " #");
1182 pw.print(i); pw.println(":");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001183 r.dump(pw, " ");
1184 } else {
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001185 pw.print(" #"); pw.print(i); pw.print(": "); pw.println(r);
1186 pw.print(" ");
1187 pw.println(r.intent.toShortString(false, true, true, false));
Dianne Hackborna40cfeb2013-03-25 17:49:36 -07001188 if (r.targetComp != null && r.targetComp != r.intent.getComponent()) {
1189 pw.print(" targetComp: "); pw.println(r.targetComp.toShortString());
1190 }
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001191 Bundle bundle = r.intent.getExtras();
1192 if (bundle != null) {
1193 pw.print(" extras: "); pw.println(bundle.toString());
1194 }
1195 }
1196 }
1197
1198 if (dumpPackage == null) {
1199 if (dumpAll) {
1200 i = 0;
1201 printed = false;
1202 }
1203 for (; i<MAX_BROADCAST_SUMMARY_HISTORY; i++) {
1204 Intent intent = mBroadcastSummaryHistory[i];
1205 if (intent == null) {
1206 break;
1207 }
1208 if (!printed) {
1209 if (needSep) {
1210 pw.println();
1211 }
1212 needSep = true;
1213 pw.println(" Historical broadcasts summary [" + mQueueName + "]:");
1214 printed = true;
1215 }
1216 if (!dumpAll && i >= 50) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001217 pw.println(" ...");
1218 break;
1219 }
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001220 pw.print(" #"); pw.print(i); pw.print(": ");
1221 pw.println(intent.toShortString(false, true, true, false));
1222 Bundle bundle = intent.getExtras();
1223 if (bundle != null) {
1224 pw.print(" extras: "); pw.println(bundle.toString());
1225 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001226 }
1227 }
1228
1229 return needSep;
1230 }
1231}