blob: 5d969c6548391d8375905a05e3a7216e45b0150f [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) {
riddle_hsu01eb7fa2015-03-04 17:27:05 +0800297 BroadcastRecord r = null;
298 if (mOrderedBroadcasts.size() > 0) {
299 BroadcastRecord br = mOrderedBroadcasts.get(0);
300 if (br.curApp == app) {
301 r = br;
302 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800303 }
riddle_hsu01eb7fa2015-03-04 17:27:05 +0800304 if (r == null && mPendingBroadcast != null && mPendingBroadcast.curApp == app) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800305 if (DEBUG_BROADCAST) Slog.v(TAG,
306 "[" + mQueueName + "] skip & discard pending app " + r);
riddle_hsu01eb7fa2015-03-04 17:27:05 +0800307 r = mPendingBroadcast;
308 }
309
310 if (r != null) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800311 logBroadcastReceiverDiscardLocked(r);
312 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700313 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800314 scheduleBroadcastsLocked();
315 }
316 }
317
318 public void scheduleBroadcastsLocked() {
319 if (DEBUG_BROADCAST) Slog.v(TAG, "Schedule broadcasts ["
320 + mQueueName + "]: current="
321 + mBroadcastsScheduled);
322
323 if (mBroadcastsScheduled) {
324 return;
325 }
326 mHandler.sendMessage(mHandler.obtainMessage(BROADCAST_INTENT_MSG, this));
327 mBroadcastsScheduled = true;
328 }
329
330 public BroadcastRecord getMatchingOrderedReceiver(IBinder receiver) {
331 if (mOrderedBroadcasts.size() > 0) {
332 final BroadcastRecord r = mOrderedBroadcasts.get(0);
333 if (r != null && r.receiver == receiver) {
334 return r;
335 }
336 }
337 return null;
338 }
339
340 public boolean finishReceiverLocked(BroadcastRecord r, int resultCode,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700341 String resultData, Bundle resultExtras, boolean resultAbort, boolean waitForServices) {
342 final int state = r.state;
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700343 final ActivityInfo receiver = r.curReceiver;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800344 r.state = BroadcastRecord.IDLE;
345 if (state == BroadcastRecord.IDLE) {
Dianne Hackborn6285a322013-09-18 12:09:47 -0700346 Slog.w(TAG, "finishReceiver [" + mQueueName + "] called but state is IDLE");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800347 }
348 r.receiver = null;
349 r.intent.setComponent(null);
Guobin Zhang04d0bb62014-03-07 17:47:10 +0800350 if (r.curApp != null && r.curApp.curReceiver == r) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800351 r.curApp.curReceiver = null;
352 }
353 if (r.curFilter != null) {
354 r.curFilter.receiverList.curBroadcast = null;
355 }
356 r.curFilter = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800357 r.curReceiver = null;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700358 r.curApp = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800359 mPendingBroadcast = null;
360
361 r.resultCode = resultCode;
362 r.resultData = resultData;
363 r.resultExtras = resultExtras;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700364 if (resultAbort && (r.intent.getFlags()&Intent.FLAG_RECEIVER_NO_ABORT) == 0) {
365 r.resultAbort = resultAbort;
366 } else {
367 r.resultAbort = false;
368 }
369
370 if (waitForServices && r.curComponent != null && r.queue.mDelayBehindServices
371 && r.queue.mOrderedBroadcasts.size() > 0
372 && r.queue.mOrderedBroadcasts.get(0) == r) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700373 ActivityInfo nextReceiver;
374 if (r.nextReceiver < r.receivers.size()) {
375 Object obj = r.receivers.get(r.nextReceiver);
376 nextReceiver = (obj instanceof ActivityInfo) ? (ActivityInfo)obj : null;
377 } else {
378 nextReceiver = null;
379 }
380 // Don't do this if the next receive is in the same process as the current one.
381 if (receiver == null || nextReceiver == null
382 || receiver.applicationInfo.uid != nextReceiver.applicationInfo.uid
383 || !receiver.processName.equals(nextReceiver.processName)) {
384 // In this case, we are ready to process the next receiver for the current broadcast,
385 // but are on a queue that would like to wait for services to finish before moving
386 // on. If there are background services currently starting, then we will go into a
387 // special state where we hold off on continuing this broadcast until they are done.
388 if (mService.mServices.hasBackgroundServices(r.userId)) {
389 Slog.i(ActivityManagerService.TAG, "Delay finish: "
390 + r.curComponent.flattenToShortString());
391 r.state = BroadcastRecord.WAITING_SERVICES;
392 return false;
393 }
Dianne Hackborn6285a322013-09-18 12:09:47 -0700394 }
395 }
396
397 r.curComponent = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800398
399 // We will process the next receiver right now if this is finishing
400 // an app receiver (which is always asynchronous) or after we have
401 // come back from calling a receiver.
402 return state == BroadcastRecord.APP_RECEIVE
403 || state == BroadcastRecord.CALL_DONE_RECEIVE;
404 }
405
Dianne Hackborn6285a322013-09-18 12:09:47 -0700406 public void backgroundServicesFinishedLocked(int userId) {
407 if (mOrderedBroadcasts.size() > 0) {
408 BroadcastRecord br = mOrderedBroadcasts.get(0);
409 if (br.userId == userId && br.state == BroadcastRecord.WAITING_SERVICES) {
410 Slog.i(ActivityManagerService.TAG, "Resuming delayed broadcast");
411 br.curComponent = null;
412 br.state = BroadcastRecord.IDLE;
413 processNextBroadcast(false);
414 }
415 }
416 }
417
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800418 private static void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver,
419 Intent intent, int resultCode, String data, Bundle extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700420 boolean ordered, boolean sticky, int sendingUser) throws RemoteException {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800421 // Send the intent to the receiver asynchronously using one-way binder calls.
Craig Mautner38c1a5f2014-07-21 15:36:37 +0000422 if (app != null) {
423 if (app.thread != null) {
424 // If we have an app thread, do the call through that so it is
425 // correctly ordered with other one-way calls.
426 app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode,
427 data, extras, ordered, sticky, sendingUser, app.repProcState);
428 } else {
429 // Application has died. Receiver doesn't exist.
430 throw new RemoteException("app.thread must not be null");
431 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800432 } else {
Dianne Hackborn20e80982012-08-31 19:00:44 -0700433 receiver.performReceive(intent, resultCode, data, extras, ordered,
434 sticky, sendingUser);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800435 }
436 }
437
438 private final void deliverToRegisteredReceiverLocked(BroadcastRecord r,
439 BroadcastFilter filter, boolean ordered) {
440 boolean skip = false;
Amith Yamasani8bf06ed2012-08-27 19:30:30 -0700441 if (filter.requiredPermission != null) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800442 int perm = mService.checkComponentPermission(filter.requiredPermission,
443 r.callingPid, r.callingUid, -1, true);
444 if (perm != PackageManager.PERMISSION_GRANTED) {
445 Slog.w(TAG, "Permission Denial: broadcasting "
446 + r.intent.toString()
447 + " from " + r.callerPackage + " (pid="
448 + r.callingPid + ", uid=" + r.callingUid + ")"
449 + " requires " + filter.requiredPermission
450 + " due to registered receiver " + filter);
451 skip = true;
452 }
453 }
Dianne Hackbornb4163a62012-08-02 18:31:26 -0700454 if (!skip && r.requiredPermission != null) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800455 int perm = mService.checkComponentPermission(r.requiredPermission,
456 filter.receiverList.pid, filter.receiverList.uid, -1, true);
457 if (perm != PackageManager.PERMISSION_GRANTED) {
458 Slog.w(TAG, "Permission Denial: receiving "
459 + r.intent.toString()
460 + " to " + filter.receiverList.app
461 + " (pid=" + filter.receiverList.pid
462 + ", uid=" + filter.receiverList.uid + ")"
463 + " requires " + r.requiredPermission
464 + " due to sender " + r.callerPackage
465 + " (uid " + r.callingUid + ")");
466 skip = true;
467 }
468 }
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800469 if (r.appOp != AppOpsManager.OP_NONE) {
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700470 int mode = mService.mAppOpsService.noteOperation(r.appOp,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800471 filter.receiverList.uid, filter.packageName);
472 if (mode != AppOpsManager.MODE_ALLOWED) {
473 if (DEBUG_BROADCAST) Slog.v(TAG,
474 "App op " + r.appOp + " not allowed for broadcast to uid "
475 + filter.receiverList.uid + " pkg " + filter.packageName);
476 skip = true;
477 }
478 }
Ben Gruver49660c72013-08-06 19:54:08 -0700479 if (!skip) {
480 skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,
481 r.callingPid, r.resolvedType, filter.receiverList.uid);
482 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800483
Dianne Hackborn9357b112013-10-03 18:27:48 -0700484 if (filter.receiverList.app == null || filter.receiverList.app.crashing) {
485 Slog.w(TAG, "Skipping deliver [" + mQueueName + "] " + r
486 + " to " + filter.receiverList + ": process crashing");
487 skip = true;
488 }
489
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800490 if (!skip) {
491 // If this is not being sent as an ordered broadcast, then we
492 // don't want to touch the fields that keep track of the current
493 // state of ordered broadcasts.
494 if (ordered) {
495 r.receiver = filter.receiverList.receiver.asBinder();
496 r.curFilter = filter;
497 filter.receiverList.curBroadcast = r;
498 r.state = BroadcastRecord.CALL_IN_RECEIVE;
499 if (filter.receiverList.app != null) {
500 // Bump hosting application to no longer be in background
501 // scheduling class. Note that we can't do that if there
502 // isn't an app... but we can only be in that case for
503 // things that directly call the IActivityManager API, which
504 // are already core system stuff so don't matter for this.
505 r.curApp = filter.receiverList.app;
506 filter.receiverList.app.curReceiver = r;
Dianne Hackborn684bf342014-04-29 17:56:57 -0700507 mService.updateOomAdjLocked(r.curApp);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800508 }
509 }
510 try {
511 if (DEBUG_BROADCAST_LIGHT) {
512 int seq = r.intent.getIntExtra("seq", -1);
513 Slog.i(TAG, "Delivering to " + filter
514 + " (seq=" + seq + "): " + r);
515 }
516 performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700517 new Intent(r.intent), r.resultCode, r.resultData,
518 r.resultExtras, r.ordered, r.initialSticky, r.userId);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800519 if (ordered) {
520 r.state = BroadcastRecord.CALL_DONE_RECEIVE;
521 }
522 } catch (RemoteException e) {
523 Slog.w(TAG, "Failure sending broadcast " + r.intent, e);
524 if (ordered) {
525 r.receiver = null;
526 r.curFilter = null;
527 filter.receiverList.curBroadcast = null;
528 if (filter.receiverList.app != null) {
529 filter.receiverList.app.curReceiver = null;
530 }
531 }
532 }
533 }
534 }
535
536 final void processNextBroadcast(boolean fromMsg) {
537 synchronized(mService) {
538 BroadcastRecord r;
539
540 if (DEBUG_BROADCAST) Slog.v(TAG, "processNextBroadcast ["
541 + mQueueName + "]: "
542 + mParallelBroadcasts.size() + " broadcasts, "
543 + mOrderedBroadcasts.size() + " ordered broadcasts");
544
545 mService.updateCpuStats();
546
547 if (fromMsg) {
548 mBroadcastsScheduled = false;
549 }
550
551 // First, deliver any non-serialized broadcasts right away.
552 while (mParallelBroadcasts.size() > 0) {
553 r = mParallelBroadcasts.remove(0);
554 r.dispatchTime = SystemClock.uptimeMillis();
555 r.dispatchClockTime = System.currentTimeMillis();
556 final int N = r.receivers.size();
557 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing parallel broadcast ["
558 + mQueueName + "] " + r);
559 for (int i=0; i<N; i++) {
560 Object target = r.receivers.get(i);
561 if (DEBUG_BROADCAST) Slog.v(TAG,
562 "Delivering non-ordered on [" + mQueueName + "] to registered "
563 + target + ": " + r);
564 deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false);
565 }
566 addBroadcastToHistoryLocked(r);
567 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Done with parallel broadcast ["
568 + mQueueName + "] " + r);
569 }
570
571 // Now take care of the next serialized one...
572
573 // If we are waiting for a process to come up to handle the next
574 // broadcast, then do nothing at this point. Just in case, we
575 // check that the process we're waiting for still exists.
576 if (mPendingBroadcast != null) {
577 if (DEBUG_BROADCAST_LIGHT) {
578 Slog.v(TAG, "processNextBroadcast ["
579 + mQueueName + "]: waiting for "
580 + mPendingBroadcast.curApp);
581 }
582
583 boolean isDead;
584 synchronized (mService.mPidsSelfLocked) {
Dianne Hackborn9357b112013-10-03 18:27:48 -0700585 ProcessRecord proc = mService.mPidsSelfLocked.get(mPendingBroadcast.curApp.pid);
586 isDead = proc == null || proc.crashing;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800587 }
588 if (!isDead) {
589 // It's still alive, so keep waiting
590 return;
591 } else {
592 Slog.w(TAG, "pending app ["
593 + mQueueName + "]" + mPendingBroadcast.curApp
594 + " died before responding to broadcast");
595 mPendingBroadcast.state = BroadcastRecord.IDLE;
596 mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex;
597 mPendingBroadcast = null;
598 }
599 }
600
601 boolean looped = false;
602
603 do {
604 if (mOrderedBroadcasts.size() == 0) {
605 // No more broadcasts pending, so all done!
606 mService.scheduleAppGcsLocked();
607 if (looped) {
608 // If we had finished the last ordered broadcast, then
609 // make sure all processes have correct oom and sched
610 // adjustments.
611 mService.updateOomAdjLocked();
612 }
613 return;
614 }
615 r = mOrderedBroadcasts.get(0);
616 boolean forceReceive = false;
617
618 // Ensure that even if something goes awry with the timeout
619 // detection, we catch "hung" broadcasts here, discard them,
620 // and continue to make progress.
621 //
622 // This is only done if the system is ready so that PRE_BOOT_COMPLETED
623 // receivers don't get executed with timeouts. They're intended for
624 // one time heavy lifting after system upgrades and can take
625 // significant amounts of time.
626 int numReceivers = (r.receivers != null) ? r.receivers.size() : 0;
627 if (mService.mProcessesReady && r.dispatchTime > 0) {
628 long now = SystemClock.uptimeMillis();
629 if ((numReceivers > 0) &&
630 (now > r.dispatchTime + (2*mTimeoutPeriod*numReceivers))) {
631 Slog.w(TAG, "Hung broadcast ["
632 + mQueueName + "] discarded after timeout failure:"
633 + " now=" + now
634 + " dispatchTime=" + r.dispatchTime
635 + " startTime=" + r.receiverTime
636 + " intent=" + r.intent
637 + " numReceivers=" + numReceivers
638 + " nextReceiver=" + r.nextReceiver
639 + " state=" + r.state);
640 broadcastTimeoutLocked(false); // forcibly finish this broadcast
641 forceReceive = true;
642 r.state = BroadcastRecord.IDLE;
643 }
644 }
645
646 if (r.state != BroadcastRecord.IDLE) {
647 if (DEBUG_BROADCAST) Slog.d(TAG,
648 "processNextBroadcast("
649 + mQueueName + ") called when not idle (state="
650 + r.state + ")");
651 return;
652 }
653
654 if (r.receivers == null || r.nextReceiver >= numReceivers
655 || r.resultAbort || forceReceive) {
656 // No more receivers for this broadcast! Send the final
657 // result if requested...
658 if (r.resultTo != null) {
659 try {
660 if (DEBUG_BROADCAST) {
661 int seq = r.intent.getIntExtra("seq", -1);
662 Slog.i(TAG, "Finishing broadcast ["
663 + mQueueName + "] " + r.intent.getAction()
664 + " seq=" + seq + " app=" + r.callerApp);
665 }
666 performReceiveLocked(r.callerApp, r.resultTo,
667 new Intent(r.intent), r.resultCode,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700668 r.resultData, r.resultExtras, false, false, r.userId);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800669 // Set this to null so that the reference
Dianne Hackborn9357b112013-10-03 18:27:48 -0700670 // (local and remote) isn't kept in the mBroadcastHistory.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800671 r.resultTo = null;
672 } catch (RemoteException e) {
Craig Mautner38c1a5f2014-07-21 15:36:37 +0000673 r.resultTo = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800674 Slog.w(TAG, "Failure ["
675 + mQueueName + "] sending broadcast result of "
676 + r.intent, e);
677 }
678 }
679
680 if (DEBUG_BROADCAST) Slog.v(TAG, "Cancelling BROADCAST_TIMEOUT_MSG");
681 cancelBroadcastTimeoutLocked();
682
683 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Finished with ordered broadcast "
684 + r);
685
686 // ... and on to the next...
687 addBroadcastToHistoryLocked(r);
688 mOrderedBroadcasts.remove(0);
689 r = null;
690 looped = true;
691 continue;
692 }
693 } while (r == null);
694
695 // Get the next receiver...
696 int recIdx = r.nextReceiver++;
697
698 // Keep track of when this receiver started, and make sure there
699 // is a timeout message pending to kill it if need be.
700 r.receiverTime = SystemClock.uptimeMillis();
701 if (recIdx == 0) {
702 r.dispatchTime = r.receiverTime;
703 r.dispatchClockTime = System.currentTimeMillis();
704 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing ordered broadcast ["
705 + mQueueName + "] " + r);
706 }
707 if (! mPendingBroadcastTimeoutMessage) {
708 long timeoutTime = r.receiverTime + mTimeoutPeriod;
709 if (DEBUG_BROADCAST) Slog.v(TAG,
710 "Submitting BROADCAST_TIMEOUT_MSG ["
711 + mQueueName + "] for " + r + " at " + timeoutTime);
712 setBroadcastTimeoutLocked(timeoutTime);
713 }
714
715 Object nextReceiver = r.receivers.get(recIdx);
716 if (nextReceiver instanceof BroadcastFilter) {
717 // Simple case: this is a registered receiver who gets
718 // a direct call.
719 BroadcastFilter filter = (BroadcastFilter)nextReceiver;
720 if (DEBUG_BROADCAST) Slog.v(TAG,
721 "Delivering ordered ["
722 + mQueueName + "] to registered "
723 + filter + ": " + r);
724 deliverToRegisteredReceiverLocked(r, filter, r.ordered);
725 if (r.receiver == null || !r.ordered) {
726 // The receiver has already finished, so schedule to
727 // process the next one.
728 if (DEBUG_BROADCAST) Slog.v(TAG, "Quick finishing ["
729 + mQueueName + "]: ordered="
730 + r.ordered + " receiver=" + r.receiver);
731 r.state = BroadcastRecord.IDLE;
732 scheduleBroadcastsLocked();
733 }
734 return;
735 }
736
737 // Hard case: need to instantiate the receiver, possibly
738 // starting its application process to host it.
739
740 ResolveInfo info =
741 (ResolveInfo)nextReceiver;
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700742 ComponentName component = new ComponentName(
743 info.activityInfo.applicationInfo.packageName,
744 info.activityInfo.name);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800745
746 boolean skip = false;
747 int perm = mService.checkComponentPermission(info.activityInfo.permission,
748 r.callingPid, r.callingUid, info.activityInfo.applicationInfo.uid,
749 info.activityInfo.exported);
750 if (perm != PackageManager.PERMISSION_GRANTED) {
751 if (!info.activityInfo.exported) {
752 Slog.w(TAG, "Permission Denial: broadcasting "
753 + r.intent.toString()
754 + " from " + r.callerPackage + " (pid=" + r.callingPid
755 + ", uid=" + r.callingUid + ")"
756 + " is not exported from uid " + info.activityInfo.applicationInfo.uid
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700757 + " due to receiver " + component.flattenToShortString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800758 } else {
759 Slog.w(TAG, "Permission Denial: broadcasting "
760 + r.intent.toString()
761 + " from " + r.callerPackage + " (pid=" + r.callingPid
762 + ", uid=" + r.callingUid + ")"
763 + " requires " + info.activityInfo.permission
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700764 + " due to receiver " + component.flattenToShortString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800765 }
766 skip = true;
767 }
768 if (info.activityInfo.applicationInfo.uid != Process.SYSTEM_UID &&
769 r.requiredPermission != null) {
770 try {
771 perm = AppGlobals.getPackageManager().
772 checkPermission(r.requiredPermission,
773 info.activityInfo.applicationInfo.packageName);
774 } catch (RemoteException e) {
775 perm = PackageManager.PERMISSION_DENIED;
776 }
777 if (perm != PackageManager.PERMISSION_GRANTED) {
778 Slog.w(TAG, "Permission Denial: receiving "
779 + r.intent + " to "
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700780 + component.flattenToShortString()
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800781 + " requires " + r.requiredPermission
782 + " due to sender " + r.callerPackage
783 + " (uid " + r.callingUid + ")");
784 skip = true;
785 }
786 }
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800787 if (r.appOp != AppOpsManager.OP_NONE) {
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700788 int mode = mService.mAppOpsService.noteOperation(r.appOp,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800789 info.activityInfo.applicationInfo.uid, info.activityInfo.packageName);
790 if (mode != AppOpsManager.MODE_ALLOWED) {
791 if (DEBUG_BROADCAST) Slog.v(TAG,
792 "App op " + r.appOp + " not allowed for broadcast to uid "
793 + info.activityInfo.applicationInfo.uid + " pkg "
794 + info.activityInfo.packageName);
795 skip = true;
796 }
797 }
Ben Gruver49660c72013-08-06 19:54:08 -0700798 if (!skip) {
799 skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,
800 r.callingPid, r.resolvedType, info.activityInfo.applicationInfo.uid);
801 }
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700802 boolean isSingleton = false;
803 try {
804 isSingleton = mService.isSingleton(info.activityInfo.processName,
805 info.activityInfo.applicationInfo,
806 info.activityInfo.name, info.activityInfo.flags);
807 } catch (SecurityException e) {
808 Slog.w(TAG, e.getMessage());
809 skip = true;
810 }
811 if ((info.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) {
812 if (ActivityManager.checkUidPermission(
813 android.Manifest.permission.INTERACT_ACROSS_USERS,
814 info.activityInfo.applicationInfo.uid)
815 != PackageManager.PERMISSION_GRANTED) {
816 Slog.w(TAG, "Permission Denial: Receiver " + component.flattenToShortString()
817 + " requests FLAG_SINGLE_USER, but app does not hold "
818 + android.Manifest.permission.INTERACT_ACROSS_USERS);
819 skip = true;
820 }
821 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800822 if (r.curApp != null && r.curApp.crashing) {
823 // If the target process is crashing, just skip it.
Dianne Hackborn9357b112013-10-03 18:27:48 -0700824 Slog.w(TAG, "Skipping deliver ordered [" + mQueueName + "] " + r
825 + " to " + r.curApp + ": process crashing");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800826 skip = true;
827 }
Christopher Tateba629da2013-11-13 17:42:28 -0800828 if (!skip) {
829 boolean isAvailable = false;
830 try {
831 isAvailable = AppGlobals.getPackageManager().isPackageAvailable(
832 info.activityInfo.packageName,
833 UserHandle.getUserId(info.activityInfo.applicationInfo.uid));
834 } catch (Exception e) {
835 // all such failures mean we skip this receiver
836 Slog.w(TAG, "Exception getting recipient info for "
837 + info.activityInfo.packageName, e);
838 }
839 if (!isAvailable) {
840 if (DEBUG_BROADCAST) {
841 Slog.v(TAG, "Skipping delivery to " + info.activityInfo.packageName
842 + " / " + info.activityInfo.applicationInfo.uid
843 + " : package no longer available");
844 }
845 skip = true;
846 }
847 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800848
849 if (skip) {
850 if (DEBUG_BROADCAST) Slog.v(TAG,
851 "Skipping delivery of ordered ["
852 + mQueueName + "] " + r + " for whatever reason");
853 r.receiver = null;
854 r.curFilter = null;
855 r.state = BroadcastRecord.IDLE;
856 scheduleBroadcastsLocked();
857 return;
858 }
859
860 r.state = BroadcastRecord.APP_RECEIVE;
861 String targetProcess = info.activityInfo.processName;
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700862 r.curComponent = component;
Amith Yamasani4b9d79c2014-05-21 19:14:21 -0700863 final int receiverUid = info.activityInfo.applicationInfo.uid;
864 // If it's a singleton, it needs to be the same app or a special app
865 if (r.callingUid != Process.SYSTEM_UID && isSingleton
866 && mService.isValidSingletonCall(r.callingUid, receiverUid)) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700867 info.activityInfo = mService.getActivityInfoForUser(info.activityInfo, 0);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800868 }
869 r.curReceiver = info.activityInfo;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700870 if (DEBUG_MU && r.callingUid > UserHandle.PER_USER_RANGE) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800871 Slog.v(TAG_MU, "Updated broadcast record activity info for secondary user, "
872 + info.activityInfo + ", callingUid = " + r.callingUid + ", uid = "
873 + info.activityInfo.applicationInfo.uid);
874 }
875
876 // Broadcast is being executed, its package can't be stopped.
877 try {
878 AppGlobals.getPackageManager().setPackageStoppedState(
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700879 r.curComponent.getPackageName(), false, UserHandle.getUserId(r.callingUid));
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800880 } catch (RemoteException e) {
881 } catch (IllegalArgumentException e) {
882 Slog.w(TAG, "Failed trying to unstop package "
883 + r.curComponent.getPackageName() + ": " + e);
884 }
885
886 // Is this receiver's application already running?
887 ProcessRecord app = mService.getProcessRecordLocked(targetProcess,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700888 info.activityInfo.applicationInfo.uid, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800889 if (app != null && app.thread != null) {
890 try {
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700891 app.addPackage(info.activityInfo.packageName,
892 info.activityInfo.applicationInfo.versionCode, mService.mProcessStats);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800893 processCurBroadcastLocked(r, app);
894 return;
895 } catch (RemoteException e) {
896 Slog.w(TAG, "Exception when sending broadcast to "
897 + r.curComponent, e);
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800898 } catch (RuntimeException e) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700899 Slog.wtf(TAG, "Failed sending broadcast to "
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800900 + r.curComponent + " with " + r.intent, e);
901 // If some unexpected exception happened, just skip
902 // this broadcast. At this point we are not in the call
903 // from a client, so throwing an exception out from here
904 // will crash the entire system instead of just whoever
905 // sent the broadcast.
906 logBroadcastReceiverDiscardLocked(r);
907 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700908 r.resultExtras, r.resultAbort, false);
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800909 scheduleBroadcastsLocked();
910 // We need to reset the state if we failed to start the receiver.
911 r.state = BroadcastRecord.IDLE;
912 return;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800913 }
914
915 // If a dead object exception was thrown -- fall through to
916 // restart the application.
917 }
918
919 // Not running -- get it started, to be executed when the app comes up.
920 if (DEBUG_BROADCAST) Slog.v(TAG,
921 "Need to start app ["
922 + mQueueName + "] " + targetProcess + " for broadcast " + r);
923 if ((r.curApp=mService.startProcessLocked(targetProcess,
924 info.activityInfo.applicationInfo, true,
925 r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND,
926 "broadcast", r.curComponent,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700927 (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0, false, false))
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800928 == null) {
929 // Ah, this recipient is unavailable. Finish it if necessary,
930 // and mark the broadcast record as ready for the next.
931 Slog.w(TAG, "Unable to launch app "
932 + info.activityInfo.applicationInfo.packageName + "/"
933 + info.activityInfo.applicationInfo.uid + " for broadcast "
934 + r.intent + ": process is bad");
935 logBroadcastReceiverDiscardLocked(r);
936 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700937 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800938 scheduleBroadcastsLocked();
939 r.state = BroadcastRecord.IDLE;
940 return;
941 }
942
943 mPendingBroadcast = r;
944 mPendingBroadcastRecvIndex = recIdx;
945 }
946 }
947
948 final void setBroadcastTimeoutLocked(long timeoutTime) {
949 if (! mPendingBroadcastTimeoutMessage) {
950 Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG, this);
951 mHandler.sendMessageAtTime(msg, timeoutTime);
952 mPendingBroadcastTimeoutMessage = true;
953 }
954 }
955
956 final void cancelBroadcastTimeoutLocked() {
957 if (mPendingBroadcastTimeoutMessage) {
958 mHandler.removeMessages(BROADCAST_TIMEOUT_MSG, this);
959 mPendingBroadcastTimeoutMessage = false;
960 }
961 }
962
963 final void broadcastTimeoutLocked(boolean fromMsg) {
964 if (fromMsg) {
965 mPendingBroadcastTimeoutMessage = false;
966 }
967
968 if (mOrderedBroadcasts.size() == 0) {
969 return;
970 }
971
972 long now = SystemClock.uptimeMillis();
973 BroadcastRecord r = mOrderedBroadcasts.get(0);
974 if (fromMsg) {
975 if (mService.mDidDexOpt) {
976 // Delay timeouts until dexopt finishes.
977 mService.mDidDexOpt = false;
978 long timeoutTime = SystemClock.uptimeMillis() + mTimeoutPeriod;
979 setBroadcastTimeoutLocked(timeoutTime);
980 return;
981 }
982 if (!mService.mProcessesReady) {
983 // Only process broadcast timeouts if the system is ready. That way
984 // PRE_BOOT_COMPLETED broadcasts can't timeout as they are intended
985 // to do heavy lifting for system up.
986 return;
987 }
988
989 long timeoutTime = r.receiverTime + mTimeoutPeriod;
990 if (timeoutTime > now) {
991 // We can observe premature timeouts because we do not cancel and reset the
992 // broadcast timeout message after each receiver finishes. Instead, we set up
993 // an initial timeout then kick it down the road a little further as needed
994 // when it expires.
995 if (DEBUG_BROADCAST) Slog.v(TAG,
996 "Premature timeout ["
997 + mQueueName + "] @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for "
998 + timeoutTime);
999 setBroadcastTimeoutLocked(timeoutTime);
1000 return;
1001 }
1002 }
1003
Dianne Hackborn6285a322013-09-18 12:09:47 -07001004 BroadcastRecord br = mOrderedBroadcasts.get(0);
1005 if (br.state == BroadcastRecord.WAITING_SERVICES) {
1006 // In this case the broadcast had already finished, but we had decided to wait
1007 // for started services to finish as well before going on. So if we have actually
1008 // waited long enough time timeout the broadcast, let's give up on the whole thing
1009 // and just move on to the next.
1010 Slog.i(ActivityManagerService.TAG, "Waited long enough for: " + (br.curComponent != null
1011 ? br.curComponent.flattenToShortString() : "(null)"));
1012 br.curComponent = null;
1013 br.state = BroadcastRecord.IDLE;
1014 processNextBroadcast(false);
1015 return;
1016 }
1017
1018 Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r. receiver
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001019 + ", started " + (now - r.receiverTime) + "ms ago");
1020 r.receiverTime = now;
1021 r.anrCount++;
1022
1023 // Current receiver has passed its expiration date.
1024 if (r.nextReceiver <= 0) {
1025 Slog.w(TAG, "Timeout on receiver with nextReceiver <= 0");
1026 return;
1027 }
1028
1029 ProcessRecord app = null;
1030 String anrMessage = null;
1031
1032 Object curReceiver = r.receivers.get(r.nextReceiver-1);
1033 Slog.w(TAG, "Receiver during timeout: " + curReceiver);
1034 logBroadcastReceiverDiscardLocked(r);
1035 if (curReceiver instanceof BroadcastFilter) {
1036 BroadcastFilter bf = (BroadcastFilter)curReceiver;
1037 if (bf.receiverList.pid != 0
1038 && bf.receiverList.pid != ActivityManagerService.MY_PID) {
1039 synchronized (mService.mPidsSelfLocked) {
1040 app = mService.mPidsSelfLocked.get(
1041 bf.receiverList.pid);
1042 }
1043 }
1044 } else {
1045 app = r.curApp;
1046 }
1047
1048 if (app != null) {
1049 anrMessage = "Broadcast of " + r.intent.toString();
1050 }
1051
1052 if (mPendingBroadcast == r) {
1053 mPendingBroadcast = null;
1054 }
1055
1056 // Move on to the next receiver.
1057 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -07001058 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001059 scheduleBroadcastsLocked();
1060
1061 if (anrMessage != null) {
1062 // Post the ANR to the handler since we do not want to process ANRs while
1063 // potentially holding our lock.
1064 mHandler.post(new AppNotResponding(app, anrMessage));
1065 }
1066 }
1067
1068 private final void addBroadcastToHistoryLocked(BroadcastRecord r) {
1069 if (r.callingUid < 0) {
1070 // This was from a registerReceiver() call; ignore it.
1071 return;
1072 }
1073 System.arraycopy(mBroadcastHistory, 0, mBroadcastHistory, 1,
1074 MAX_BROADCAST_HISTORY-1);
1075 r.finishTime = SystemClock.uptimeMillis();
1076 mBroadcastHistory[0] = r;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001077 System.arraycopy(mBroadcastSummaryHistory, 0, mBroadcastSummaryHistory, 1,
1078 MAX_BROADCAST_SUMMARY_HISTORY-1);
1079 mBroadcastSummaryHistory[0] = r.intent;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001080 }
1081
1082 final void logBroadcastReceiverDiscardLocked(BroadcastRecord r) {
1083 if (r.nextReceiver > 0) {
1084 Object curReceiver = r.receivers.get(r.nextReceiver-1);
1085 if (curReceiver instanceof BroadcastFilter) {
1086 BroadcastFilter bf = (BroadcastFilter) curReceiver;
1087 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001088 bf.owningUserId, System.identityHashCode(r),
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001089 r.intent.getAction(),
1090 r.nextReceiver - 1,
1091 System.identityHashCode(bf));
1092 } else {
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001093 ResolveInfo ri = (ResolveInfo)curReceiver;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001094 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001095 UserHandle.getUserId(ri.activityInfo.applicationInfo.uid),
1096 System.identityHashCode(r), r.intent.getAction(),
1097 r.nextReceiver - 1, ri.toString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001098 }
1099 } else {
1100 Slog.w(TAG, "Discarding broadcast before first receiver is invoked: "
1101 + r);
1102 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001103 -1, System.identityHashCode(r),
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001104 r.intent.getAction(),
1105 r.nextReceiver,
1106 "NONE");
1107 }
1108 }
1109
1110 final boolean dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args,
1111 int opti, boolean dumpAll, String dumpPackage, boolean needSep) {
1112 if (mParallelBroadcasts.size() > 0 || mOrderedBroadcasts.size() > 0
1113 || mPendingBroadcast != null) {
1114 boolean printed = false;
1115 for (int i=mParallelBroadcasts.size()-1; i>=0; i--) {
1116 BroadcastRecord br = mParallelBroadcasts.get(i);
1117 if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) {
1118 continue;
1119 }
1120 if (!printed) {
1121 if (needSep) {
1122 pw.println();
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001123 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001124 needSep = true;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001125 printed = true;
1126 pw.println(" Active broadcasts [" + mQueueName + "]:");
1127 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001128 pw.println(" Active Broadcast " + mQueueName + " #" + i + ":");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001129 br.dump(pw, " ");
1130 }
1131 printed = false;
1132 needSep = true;
1133 for (int i=mOrderedBroadcasts.size()-1; i>=0; i--) {
1134 BroadcastRecord br = mOrderedBroadcasts.get(i);
1135 if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) {
1136 continue;
1137 }
1138 if (!printed) {
1139 if (needSep) {
1140 pw.println();
1141 }
1142 needSep = true;
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001143 printed = true;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001144 pw.println(" Active ordered broadcasts [" + mQueueName + "]:");
1145 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001146 pw.println(" Active Ordered Broadcast " + mQueueName + " #" + i + ":");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001147 mOrderedBroadcasts.get(i).dump(pw, " ");
1148 }
1149 if (dumpPackage == null || (mPendingBroadcast != null
1150 && dumpPackage.equals(mPendingBroadcast.callerPackage))) {
1151 if (needSep) {
1152 pw.println();
1153 }
1154 pw.println(" Pending broadcast [" + mQueueName + "]:");
1155 if (mPendingBroadcast != null) {
1156 mPendingBroadcast.dump(pw, " ");
1157 } else {
1158 pw.println(" (null)");
1159 }
1160 needSep = true;
1161 }
1162 }
1163
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001164 int i;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001165 boolean printed = false;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001166 for (i=0; i<MAX_BROADCAST_HISTORY; i++) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001167 BroadcastRecord r = mBroadcastHistory[i];
1168 if (r == null) {
1169 break;
1170 }
1171 if (dumpPackage != null && !dumpPackage.equals(r.callerPackage)) {
1172 continue;
1173 }
1174 if (!printed) {
1175 if (needSep) {
1176 pw.println();
1177 }
1178 needSep = true;
1179 pw.println(" Historical broadcasts [" + mQueueName + "]:");
1180 printed = true;
1181 }
1182 if (dumpAll) {
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001183 pw.print(" Historical Broadcast " + mQueueName + " #");
1184 pw.print(i); pw.println(":");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001185 r.dump(pw, " ");
1186 } else {
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001187 pw.print(" #"); pw.print(i); pw.print(": "); pw.println(r);
1188 pw.print(" ");
1189 pw.println(r.intent.toShortString(false, true, true, false));
Dianne Hackborna40cfeb2013-03-25 17:49:36 -07001190 if (r.targetComp != null && r.targetComp != r.intent.getComponent()) {
1191 pw.print(" targetComp: "); pw.println(r.targetComp.toShortString());
1192 }
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001193 Bundle bundle = r.intent.getExtras();
1194 if (bundle != null) {
1195 pw.print(" extras: "); pw.println(bundle.toString());
1196 }
1197 }
1198 }
1199
1200 if (dumpPackage == null) {
1201 if (dumpAll) {
1202 i = 0;
1203 printed = false;
1204 }
1205 for (; i<MAX_BROADCAST_SUMMARY_HISTORY; i++) {
1206 Intent intent = mBroadcastSummaryHistory[i];
1207 if (intent == null) {
1208 break;
1209 }
1210 if (!printed) {
1211 if (needSep) {
1212 pw.println();
1213 }
1214 needSep = true;
1215 pw.println(" Historical broadcasts summary [" + mQueueName + "]:");
1216 printed = true;
1217 }
1218 if (!dumpAll && i >= 50) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001219 pw.println(" ...");
1220 break;
1221 }
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001222 pw.print(" #"); pw.print(i); pw.print(": ");
1223 pw.println(intent.toShortString(false, true, true, false));
1224 Bundle bundle = intent.getExtras();
1225 if (bundle != null) {
1226 pw.print(" extras: "); pw.println(bundle.toString());
1227 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001228 }
1229 }
1230
1231 return needSep;
1232 }
1233}