blob: aef9e5c38e6f8db08570050eee3a0c7885f2c010 [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;
Christopher Tateba629da2013-11-13 17:42:28 -080030import android.content.pm.PackageInfo;
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;
299 if (r != null) {
300 // 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);
355 if (r.curApp != null) {
356 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.
427 if (app != null && app.thread != null) {
428 // If we have an app thread, do the call through that so it is
429 // correctly ordered with other one-way calls.
430 app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700431 data, extras, ordered, sticky, sendingUser, app.repProcState);
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 Hackborna413dc02013-07-12 12:02:55 -0700507 mService.updateOomAdjLocked(r.curApp, true);
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) {
673 Slog.w(TAG, "Failure ["
674 + mQueueName + "] sending broadcast result of "
675 + r.intent, e);
676 }
677 }
678
679 if (DEBUG_BROADCAST) Slog.v(TAG, "Cancelling BROADCAST_TIMEOUT_MSG");
680 cancelBroadcastTimeoutLocked();
681
682 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Finished with ordered broadcast "
683 + r);
684
685 // ... and on to the next...
686 addBroadcastToHistoryLocked(r);
687 mOrderedBroadcasts.remove(0);
688 r = null;
689 looped = true;
690 continue;
691 }
692 } while (r == null);
693
694 // Get the next receiver...
695 int recIdx = r.nextReceiver++;
696
697 // Keep track of when this receiver started, and make sure there
698 // is a timeout message pending to kill it if need be.
699 r.receiverTime = SystemClock.uptimeMillis();
700 if (recIdx == 0) {
701 r.dispatchTime = r.receiverTime;
702 r.dispatchClockTime = System.currentTimeMillis();
703 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing ordered broadcast ["
704 + mQueueName + "] " + r);
705 }
706 if (! mPendingBroadcastTimeoutMessage) {
707 long timeoutTime = r.receiverTime + mTimeoutPeriod;
708 if (DEBUG_BROADCAST) Slog.v(TAG,
709 "Submitting BROADCAST_TIMEOUT_MSG ["
710 + mQueueName + "] for " + r + " at " + timeoutTime);
711 setBroadcastTimeoutLocked(timeoutTime);
712 }
713
714 Object nextReceiver = r.receivers.get(recIdx);
715 if (nextReceiver instanceof BroadcastFilter) {
716 // Simple case: this is a registered receiver who gets
717 // a direct call.
718 BroadcastFilter filter = (BroadcastFilter)nextReceiver;
719 if (DEBUG_BROADCAST) Slog.v(TAG,
720 "Delivering ordered ["
721 + mQueueName + "] to registered "
722 + filter + ": " + r);
723 deliverToRegisteredReceiverLocked(r, filter, r.ordered);
724 if (r.receiver == null || !r.ordered) {
725 // The receiver has already finished, so schedule to
726 // process the next one.
727 if (DEBUG_BROADCAST) Slog.v(TAG, "Quick finishing ["
728 + mQueueName + "]: ordered="
729 + r.ordered + " receiver=" + r.receiver);
730 r.state = BroadcastRecord.IDLE;
731 scheduleBroadcastsLocked();
732 }
733 return;
734 }
735
736 // Hard case: need to instantiate the receiver, possibly
737 // starting its application process to host it.
738
739 ResolveInfo info =
740 (ResolveInfo)nextReceiver;
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700741 ComponentName component = new ComponentName(
742 info.activityInfo.applicationInfo.packageName,
743 info.activityInfo.name);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800744
745 boolean skip = false;
746 int perm = mService.checkComponentPermission(info.activityInfo.permission,
747 r.callingPid, r.callingUid, info.activityInfo.applicationInfo.uid,
748 info.activityInfo.exported);
749 if (perm != PackageManager.PERMISSION_GRANTED) {
750 if (!info.activityInfo.exported) {
751 Slog.w(TAG, "Permission Denial: broadcasting "
752 + r.intent.toString()
753 + " from " + r.callerPackage + " (pid=" + r.callingPid
754 + ", uid=" + r.callingUid + ")"
755 + " is not exported from uid " + info.activityInfo.applicationInfo.uid
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700756 + " due to receiver " + component.flattenToShortString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800757 } else {
758 Slog.w(TAG, "Permission Denial: broadcasting "
759 + r.intent.toString()
760 + " from " + r.callerPackage + " (pid=" + r.callingPid
761 + ", uid=" + r.callingUid + ")"
762 + " requires " + info.activityInfo.permission
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700763 + " due to receiver " + component.flattenToShortString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800764 }
765 skip = true;
766 }
767 if (info.activityInfo.applicationInfo.uid != Process.SYSTEM_UID &&
768 r.requiredPermission != null) {
769 try {
770 perm = AppGlobals.getPackageManager().
771 checkPermission(r.requiredPermission,
772 info.activityInfo.applicationInfo.packageName);
773 } catch (RemoteException e) {
774 perm = PackageManager.PERMISSION_DENIED;
775 }
776 if (perm != PackageManager.PERMISSION_GRANTED) {
777 Slog.w(TAG, "Permission Denial: receiving "
778 + r.intent + " to "
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700779 + component.flattenToShortString()
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800780 + " requires " + r.requiredPermission
781 + " due to sender " + r.callerPackage
782 + " (uid " + r.callingUid + ")");
783 skip = true;
784 }
785 }
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800786 if (r.appOp != AppOpsManager.OP_NONE) {
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700787 int mode = mService.mAppOpsService.noteOperation(r.appOp,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800788 info.activityInfo.applicationInfo.uid, info.activityInfo.packageName);
789 if (mode != AppOpsManager.MODE_ALLOWED) {
790 if (DEBUG_BROADCAST) Slog.v(TAG,
791 "App op " + r.appOp + " not allowed for broadcast to uid "
792 + info.activityInfo.applicationInfo.uid + " pkg "
793 + info.activityInfo.packageName);
794 skip = true;
795 }
796 }
Ben Gruver49660c72013-08-06 19:54:08 -0700797 if (!skip) {
798 skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,
799 r.callingPid, r.resolvedType, info.activityInfo.applicationInfo.uid);
800 }
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700801 boolean isSingleton = false;
802 try {
803 isSingleton = mService.isSingleton(info.activityInfo.processName,
804 info.activityInfo.applicationInfo,
805 info.activityInfo.name, info.activityInfo.flags);
806 } catch (SecurityException e) {
807 Slog.w(TAG, e.getMessage());
808 skip = true;
809 }
810 if ((info.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) {
811 if (ActivityManager.checkUidPermission(
812 android.Manifest.permission.INTERACT_ACROSS_USERS,
813 info.activityInfo.applicationInfo.uid)
814 != PackageManager.PERMISSION_GRANTED) {
815 Slog.w(TAG, "Permission Denial: Receiver " + component.flattenToShortString()
816 + " requests FLAG_SINGLE_USER, but app does not hold "
817 + android.Manifest.permission.INTERACT_ACROSS_USERS);
818 skip = true;
819 }
820 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800821 if (r.curApp != null && r.curApp.crashing) {
822 // If the target process is crashing, just skip it.
Dianne Hackborn9357b112013-10-03 18:27:48 -0700823 Slog.w(TAG, "Skipping deliver ordered [" + mQueueName + "] " + r
824 + " to " + r.curApp + ": process crashing");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800825 skip = true;
826 }
Christopher Tateba629da2013-11-13 17:42:28 -0800827 if (!skip) {
828 boolean isAvailable = false;
829 try {
830 isAvailable = AppGlobals.getPackageManager().isPackageAvailable(
831 info.activityInfo.packageName,
832 UserHandle.getUserId(info.activityInfo.applicationInfo.uid));
833 } catch (Exception e) {
834 // all such failures mean we skip this receiver
835 Slog.w(TAG, "Exception getting recipient info for "
836 + info.activityInfo.packageName, e);
837 }
838 if (!isAvailable) {
839 if (DEBUG_BROADCAST) {
840 Slog.v(TAG, "Skipping delivery to " + info.activityInfo.packageName
841 + " / " + info.activityInfo.applicationInfo.uid
842 + " : package no longer available");
843 }
844 skip = true;
845 }
846 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800847
848 if (skip) {
849 if (DEBUG_BROADCAST) Slog.v(TAG,
850 "Skipping delivery of ordered ["
851 + mQueueName + "] " + r + " for whatever reason");
852 r.receiver = null;
853 r.curFilter = null;
854 r.state = BroadcastRecord.IDLE;
855 scheduleBroadcastsLocked();
856 return;
857 }
858
859 r.state = BroadcastRecord.APP_RECEIVE;
860 String targetProcess = info.activityInfo.processName;
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700861 r.curComponent = component;
862 if (r.callingUid != Process.SYSTEM_UID && isSingleton) {
863 info.activityInfo = mService.getActivityInfoForUser(info.activityInfo, 0);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800864 }
865 r.curReceiver = info.activityInfo;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700866 if (DEBUG_MU && r.callingUid > UserHandle.PER_USER_RANGE) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800867 Slog.v(TAG_MU, "Updated broadcast record activity info for secondary user, "
868 + info.activityInfo + ", callingUid = " + r.callingUid + ", uid = "
869 + info.activityInfo.applicationInfo.uid);
870 }
871
872 // Broadcast is being executed, its package can't be stopped.
873 try {
874 AppGlobals.getPackageManager().setPackageStoppedState(
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700875 r.curComponent.getPackageName(), false, UserHandle.getUserId(r.callingUid));
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800876 } catch (RemoteException e) {
877 } catch (IllegalArgumentException e) {
878 Slog.w(TAG, "Failed trying to unstop package "
879 + r.curComponent.getPackageName() + ": " + e);
880 }
881
882 // Is this receiver's application already running?
883 ProcessRecord app = mService.getProcessRecordLocked(targetProcess,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700884 info.activityInfo.applicationInfo.uid, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800885 if (app != null && app.thread != null) {
886 try {
Dianne Hackbornd2932242013-08-05 18:18:42 -0700887 app.addPackage(info.activityInfo.packageName, mService.mProcessStats);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800888 processCurBroadcastLocked(r, app);
889 return;
890 } catch (RemoteException e) {
891 Slog.w(TAG, "Exception when sending broadcast to "
892 + r.curComponent, e);
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800893 } catch (RuntimeException e) {
894 Log.wtf(TAG, "Failed sending broadcast to "
895 + r.curComponent + " with " + r.intent, e);
896 // If some unexpected exception happened, just skip
897 // this broadcast. At this point we are not in the call
898 // from a client, so throwing an exception out from here
899 // will crash the entire system instead of just whoever
900 // sent the broadcast.
901 logBroadcastReceiverDiscardLocked(r);
902 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700903 r.resultExtras, r.resultAbort, false);
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800904 scheduleBroadcastsLocked();
905 // We need to reset the state if we failed to start the receiver.
906 r.state = BroadcastRecord.IDLE;
907 return;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800908 }
909
910 // If a dead object exception was thrown -- fall through to
911 // restart the application.
912 }
913
914 // Not running -- get it started, to be executed when the app comes up.
915 if (DEBUG_BROADCAST) Slog.v(TAG,
916 "Need to start app ["
917 + mQueueName + "] " + targetProcess + " for broadcast " + r);
918 if ((r.curApp=mService.startProcessLocked(targetProcess,
919 info.activityInfo.applicationInfo, true,
920 r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND,
921 "broadcast", r.curComponent,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700922 (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0, false, false))
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800923 == null) {
924 // Ah, this recipient is unavailable. Finish it if necessary,
925 // and mark the broadcast record as ready for the next.
926 Slog.w(TAG, "Unable to launch app "
927 + info.activityInfo.applicationInfo.packageName + "/"
928 + info.activityInfo.applicationInfo.uid + " for broadcast "
929 + r.intent + ": process is bad");
930 logBroadcastReceiverDiscardLocked(r);
931 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700932 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800933 scheduleBroadcastsLocked();
934 r.state = BroadcastRecord.IDLE;
935 return;
936 }
937
938 mPendingBroadcast = r;
939 mPendingBroadcastRecvIndex = recIdx;
940 }
941 }
942
943 final void setBroadcastTimeoutLocked(long timeoutTime) {
944 if (! mPendingBroadcastTimeoutMessage) {
945 Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG, this);
946 mHandler.sendMessageAtTime(msg, timeoutTime);
947 mPendingBroadcastTimeoutMessage = true;
948 }
949 }
950
951 final void cancelBroadcastTimeoutLocked() {
952 if (mPendingBroadcastTimeoutMessage) {
953 mHandler.removeMessages(BROADCAST_TIMEOUT_MSG, this);
954 mPendingBroadcastTimeoutMessage = false;
955 }
956 }
957
958 final void broadcastTimeoutLocked(boolean fromMsg) {
959 if (fromMsg) {
960 mPendingBroadcastTimeoutMessage = false;
961 }
962
963 if (mOrderedBroadcasts.size() == 0) {
964 return;
965 }
966
967 long now = SystemClock.uptimeMillis();
968 BroadcastRecord r = mOrderedBroadcasts.get(0);
969 if (fromMsg) {
970 if (mService.mDidDexOpt) {
971 // Delay timeouts until dexopt finishes.
972 mService.mDidDexOpt = false;
973 long timeoutTime = SystemClock.uptimeMillis() + mTimeoutPeriod;
974 setBroadcastTimeoutLocked(timeoutTime);
975 return;
976 }
977 if (!mService.mProcessesReady) {
978 // Only process broadcast timeouts if the system is ready. That way
979 // PRE_BOOT_COMPLETED broadcasts can't timeout as they are intended
980 // to do heavy lifting for system up.
981 return;
982 }
983
984 long timeoutTime = r.receiverTime + mTimeoutPeriod;
985 if (timeoutTime > now) {
986 // We can observe premature timeouts because we do not cancel and reset the
987 // broadcast timeout message after each receiver finishes. Instead, we set up
988 // an initial timeout then kick it down the road a little further as needed
989 // when it expires.
990 if (DEBUG_BROADCAST) Slog.v(TAG,
991 "Premature timeout ["
992 + mQueueName + "] @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for "
993 + timeoutTime);
994 setBroadcastTimeoutLocked(timeoutTime);
995 return;
996 }
997 }
998
Dianne Hackborn6285a322013-09-18 12:09:47 -0700999 BroadcastRecord br = mOrderedBroadcasts.get(0);
1000 if (br.state == BroadcastRecord.WAITING_SERVICES) {
1001 // In this case the broadcast had already finished, but we had decided to wait
1002 // for started services to finish as well before going on. So if we have actually
1003 // waited long enough time timeout the broadcast, let's give up on the whole thing
1004 // and just move on to the next.
1005 Slog.i(ActivityManagerService.TAG, "Waited long enough for: " + (br.curComponent != null
1006 ? br.curComponent.flattenToShortString() : "(null)"));
1007 br.curComponent = null;
1008 br.state = BroadcastRecord.IDLE;
1009 processNextBroadcast(false);
1010 return;
1011 }
1012
1013 Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r. receiver
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001014 + ", started " + (now - r.receiverTime) + "ms ago");
1015 r.receiverTime = now;
1016 r.anrCount++;
1017
1018 // Current receiver has passed its expiration date.
1019 if (r.nextReceiver <= 0) {
1020 Slog.w(TAG, "Timeout on receiver with nextReceiver <= 0");
1021 return;
1022 }
1023
1024 ProcessRecord app = null;
1025 String anrMessage = null;
1026
1027 Object curReceiver = r.receivers.get(r.nextReceiver-1);
1028 Slog.w(TAG, "Receiver during timeout: " + curReceiver);
1029 logBroadcastReceiverDiscardLocked(r);
1030 if (curReceiver instanceof BroadcastFilter) {
1031 BroadcastFilter bf = (BroadcastFilter)curReceiver;
1032 if (bf.receiverList.pid != 0
1033 && bf.receiverList.pid != ActivityManagerService.MY_PID) {
1034 synchronized (mService.mPidsSelfLocked) {
1035 app = mService.mPidsSelfLocked.get(
1036 bf.receiverList.pid);
1037 }
1038 }
1039 } else {
1040 app = r.curApp;
1041 }
1042
1043 if (app != null) {
1044 anrMessage = "Broadcast of " + r.intent.toString();
1045 }
1046
1047 if (mPendingBroadcast == r) {
1048 mPendingBroadcast = null;
1049 }
1050
1051 // Move on to the next receiver.
1052 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -07001053 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001054 scheduleBroadcastsLocked();
1055
1056 if (anrMessage != null) {
1057 // Post the ANR to the handler since we do not want to process ANRs while
1058 // potentially holding our lock.
1059 mHandler.post(new AppNotResponding(app, anrMessage));
1060 }
1061 }
1062
1063 private final void addBroadcastToHistoryLocked(BroadcastRecord r) {
1064 if (r.callingUid < 0) {
1065 // This was from a registerReceiver() call; ignore it.
1066 return;
1067 }
1068 System.arraycopy(mBroadcastHistory, 0, mBroadcastHistory, 1,
1069 MAX_BROADCAST_HISTORY-1);
1070 r.finishTime = SystemClock.uptimeMillis();
1071 mBroadcastHistory[0] = r;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001072 System.arraycopy(mBroadcastSummaryHistory, 0, mBroadcastSummaryHistory, 1,
1073 MAX_BROADCAST_SUMMARY_HISTORY-1);
1074 mBroadcastSummaryHistory[0] = r.intent;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001075 }
1076
1077 final void logBroadcastReceiverDiscardLocked(BroadcastRecord r) {
1078 if (r.nextReceiver > 0) {
1079 Object curReceiver = r.receivers.get(r.nextReceiver-1);
1080 if (curReceiver instanceof BroadcastFilter) {
1081 BroadcastFilter bf = (BroadcastFilter) curReceiver;
1082 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001083 bf.owningUserId, System.identityHashCode(r),
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001084 r.intent.getAction(),
1085 r.nextReceiver - 1,
1086 System.identityHashCode(bf));
1087 } else {
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001088 ResolveInfo ri = (ResolveInfo)curReceiver;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001089 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001090 UserHandle.getUserId(ri.activityInfo.applicationInfo.uid),
1091 System.identityHashCode(r), r.intent.getAction(),
1092 r.nextReceiver - 1, ri.toString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001093 }
1094 } else {
1095 Slog.w(TAG, "Discarding broadcast before first receiver is invoked: "
1096 + r);
1097 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001098 -1, System.identityHashCode(r),
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001099 r.intent.getAction(),
1100 r.nextReceiver,
1101 "NONE");
1102 }
1103 }
1104
1105 final boolean dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args,
1106 int opti, boolean dumpAll, String dumpPackage, boolean needSep) {
1107 if (mParallelBroadcasts.size() > 0 || mOrderedBroadcasts.size() > 0
1108 || mPendingBroadcast != null) {
1109 boolean printed = false;
1110 for (int i=mParallelBroadcasts.size()-1; i>=0; i--) {
1111 BroadcastRecord br = mParallelBroadcasts.get(i);
1112 if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) {
1113 continue;
1114 }
1115 if (!printed) {
1116 if (needSep) {
1117 pw.println();
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001118 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001119 needSep = true;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001120 printed = true;
1121 pw.println(" Active broadcasts [" + mQueueName + "]:");
1122 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001123 pw.println(" Active Broadcast " + mQueueName + " #" + i + ":");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001124 br.dump(pw, " ");
1125 }
1126 printed = false;
1127 needSep = true;
1128 for (int i=mOrderedBroadcasts.size()-1; i>=0; i--) {
1129 BroadcastRecord br = mOrderedBroadcasts.get(i);
1130 if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) {
1131 continue;
1132 }
1133 if (!printed) {
1134 if (needSep) {
1135 pw.println();
1136 }
1137 needSep = true;
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001138 printed = true;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001139 pw.println(" Active ordered broadcasts [" + mQueueName + "]:");
1140 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001141 pw.println(" Active Ordered Broadcast " + mQueueName + " #" + i + ":");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001142 mOrderedBroadcasts.get(i).dump(pw, " ");
1143 }
1144 if (dumpPackage == null || (mPendingBroadcast != null
1145 && dumpPackage.equals(mPendingBroadcast.callerPackage))) {
1146 if (needSep) {
1147 pw.println();
1148 }
1149 pw.println(" Pending broadcast [" + mQueueName + "]:");
1150 if (mPendingBroadcast != null) {
1151 mPendingBroadcast.dump(pw, " ");
1152 } else {
1153 pw.println(" (null)");
1154 }
1155 needSep = true;
1156 }
1157 }
1158
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001159 int i;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001160 boolean printed = false;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001161 for (i=0; i<MAX_BROADCAST_HISTORY; i++) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001162 BroadcastRecord r = mBroadcastHistory[i];
1163 if (r == null) {
1164 break;
1165 }
1166 if (dumpPackage != null && !dumpPackage.equals(r.callerPackage)) {
1167 continue;
1168 }
1169 if (!printed) {
1170 if (needSep) {
1171 pw.println();
1172 }
1173 needSep = true;
1174 pw.println(" Historical broadcasts [" + mQueueName + "]:");
1175 printed = true;
1176 }
1177 if (dumpAll) {
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001178 pw.print(" Historical Broadcast " + mQueueName + " #");
1179 pw.print(i); pw.println(":");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001180 r.dump(pw, " ");
1181 } else {
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001182 pw.print(" #"); pw.print(i); pw.print(": "); pw.println(r);
1183 pw.print(" ");
1184 pw.println(r.intent.toShortString(false, true, true, false));
Dianne Hackborna40cfeb2013-03-25 17:49:36 -07001185 if (r.targetComp != null && r.targetComp != r.intent.getComponent()) {
1186 pw.print(" targetComp: "); pw.println(r.targetComp.toShortString());
1187 }
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001188 Bundle bundle = r.intent.getExtras();
1189 if (bundle != null) {
1190 pw.print(" extras: "); pw.println(bundle.toString());
1191 }
1192 }
1193 }
1194
1195 if (dumpPackage == null) {
1196 if (dumpAll) {
1197 i = 0;
1198 printed = false;
1199 }
1200 for (; i<MAX_BROADCAST_SUMMARY_HISTORY; i++) {
1201 Intent intent = mBroadcastSummaryHistory[i];
1202 if (intent == null) {
1203 break;
1204 }
1205 if (!printed) {
1206 if (needSep) {
1207 pw.println();
1208 }
1209 needSep = true;
1210 pw.println(" Historical broadcasts summary [" + mQueueName + "]:");
1211 printed = true;
1212 }
1213 if (!dumpAll && i >= 50) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001214 pw.println(" ...");
1215 break;
1216 }
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001217 pw.print(" #"); pw.print(i); pw.print(": ");
1218 pw.println(intent.toShortString(false, true, true, false));
1219 Bundle bundle = intent.getExtras();
1220 if (bundle != null) {
1221 pw.print(" extras: "); pw.println(bundle.toString());
1222 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001223 }
1224 }
1225
1226 return needSep;
1227 }
1228}