blob: 5b5ebef46e1fbe07cc7750beb2fdc43a8df2313b [file] [log] [blame]
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
19import java.io.FileDescriptor;
20import java.io.PrintWriter;
21import java.util.ArrayList;
22
Dianne Hackborn7d19e022012-08-07 19:12:33 -070023import android.app.ActivityManager;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080024import android.app.AppGlobals;
Dianne Hackbornf51f6122013-02-04 18:23:34 -080025import android.app.AppOpsManager;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080026import android.content.ComponentName;
27import android.content.IIntentReceiver;
28import android.content.Intent;
Dianne Hackborn7d19e022012-08-07 19:12:33 -070029import android.content.pm.ActivityInfo;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080030import android.content.pm.PackageManager;
31import android.content.pm.ResolveInfo;
32import android.os.Bundle;
33import android.os.Handler;
34import android.os.IBinder;
Jeff Brown6f357d32014-01-15 20:40:55 -080035import android.os.Looper;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080036import android.os.Message;
37import android.os.Process;
38import android.os.RemoteException;
39import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070040import android.os.UserHandle;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080041import android.util.EventLog;
42import android.util.Slog;
43
Wale Ogunwaled57969f2014-11-15 19:37:29 -080044import static com.android.server.am.ActivityManagerDebugConfig.*;
45
Dianne Hackborn40c8db52012-02-10 18:59:48 -080046/**
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 {
Wale Ogunwalebfac4682015-04-08 14:33:21 -070053 private static final String TAG = "BroadcastQueue";
Wale Ogunwaled57969f2014-11-15 19:37:29 -080054 private static final String TAG_MU = TAG + POSTFIX_MU;
55 private static final String TAG_BROADCAST = TAG + POSTFIX_BROADCAST;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080056
Dianne Hackborn4c51de42013-10-16 23:34:35 -070057 static final int MAX_BROADCAST_HISTORY = ActivityManager.isLowRamDeviceStatic() ? 10 : 50;
Dianne Hackborn6285a322013-09-18 12:09:47 -070058 static final int MAX_BROADCAST_SUMMARY_HISTORY
Dianne Hackborn4c51de42013-10-16 23:34:35 -070059 = ActivityManager.isLowRamDeviceStatic() ? 25 : 300;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080060
61 final ActivityManagerService mService;
62
63 /**
64 * Recognizable moniker for this queue
65 */
66 final String mQueueName;
67
68 /**
69 * Timeout period for this queue's broadcasts
70 */
71 final long mTimeoutPeriod;
72
73 /**
Dianne Hackborn6285a322013-09-18 12:09:47 -070074 * If true, we can delay broadcasts while waiting services to finish in the previous
75 * receiver's process.
76 */
77 final boolean mDelayBehindServices;
78
79 /**
Dianne Hackborn40c8db52012-02-10 18:59:48 -080080 * Lists of all active broadcasts that are to be executed immediately
81 * (without waiting for another broadcast to finish). Currently this only
82 * contains broadcasts to registered receivers, to avoid spinning up
83 * a bunch of processes to execute IntentReceiver components. Background-
84 * and foreground-priority broadcasts are queued separately.
85 */
Dianne Hackborn6285a322013-09-18 12:09:47 -070086 final ArrayList<BroadcastRecord> mParallelBroadcasts = new ArrayList<BroadcastRecord>();
87
Dianne Hackborn40c8db52012-02-10 18:59:48 -080088 /**
89 * List of all active broadcasts that are to be executed one at a time.
90 * The object at the top of the list is the currently activity broadcasts;
91 * those after it are waiting for the top to finish. As with parallel
92 * broadcasts, separate background- and foreground-priority queues are
93 * maintained.
94 */
Dianne Hackborn6285a322013-09-18 12:09:47 -070095 final ArrayList<BroadcastRecord> mOrderedBroadcasts = new ArrayList<BroadcastRecord>();
Dianne Hackborn40c8db52012-02-10 18:59:48 -080096
97 /**
98 * Historical data of past broadcasts, for debugging.
99 */
Dianne Hackborn6285a322013-09-18 12:09:47 -0700100 final BroadcastRecord[] mBroadcastHistory = new BroadcastRecord[MAX_BROADCAST_HISTORY];
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800101
102 /**
Dianne Hackbornc0bd7472012-10-09 14:00:30 -0700103 * Summary of historical data of past broadcasts, for debugging.
104 */
Dianne Hackborn6285a322013-09-18 12:09:47 -0700105 final Intent[] mBroadcastSummaryHistory = new Intent[MAX_BROADCAST_SUMMARY_HISTORY];
Dianne Hackbornc0bd7472012-10-09 14:00:30 -0700106
107 /**
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800108 * Set when we current have a BROADCAST_INTENT_MSG in flight.
109 */
110 boolean mBroadcastsScheduled = false;
111
112 /**
113 * True if we have a pending unexpired BROADCAST_TIMEOUT_MSG posted to our handler.
114 */
115 boolean mPendingBroadcastTimeoutMessage;
116
117 /**
118 * Intent broadcasts that we have tried to start, but are
119 * waiting for the application's process to be created. We only
120 * need one per scheduling class (instead of a list) because we always
121 * process broadcasts one at a time, so no others can be started while
122 * waiting for this one.
123 */
124 BroadcastRecord mPendingBroadcast = null;
125
126 /**
127 * The receiver index that is pending, to restart the broadcast if needed.
128 */
129 int mPendingBroadcastRecvIndex;
130
131 static final int BROADCAST_INTENT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG;
132 static final int BROADCAST_TIMEOUT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG + 1;
133
Jeff Brown6f357d32014-01-15 20:40:55 -0800134 final BroadcastHandler mHandler;
135
136 private final class BroadcastHandler extends Handler {
137 public BroadcastHandler(Looper looper) {
138 super(looper, null, true);
139 }
140
141 @Override
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800142 public void handleMessage(Message msg) {
143 switch (msg.what) {
144 case BROADCAST_INTENT_MSG: {
145 if (DEBUG_BROADCAST) Slog.v(
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800146 TAG_BROADCAST, "Received BROADCAST_INTENT_MSG");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800147 processNextBroadcast(true);
148 } break;
149 case BROADCAST_TIMEOUT_MSG: {
150 synchronized (mService) {
151 broadcastTimeoutLocked(true);
152 }
153 } break;
154 }
155 }
156 };
157
158 private final class AppNotResponding implements Runnable {
159 private final ProcessRecord mApp;
160 private final String mAnnotation;
161
162 public AppNotResponding(ProcessRecord app, String annotation) {
163 mApp = app;
164 mAnnotation = annotation;
165 }
166
167 @Override
168 public void run() {
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700169 mService.appNotResponding(mApp, null, null, false, mAnnotation);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800170 }
171 }
172
Jeff Brown6f357d32014-01-15 20:40:55 -0800173 BroadcastQueue(ActivityManagerService service, Handler handler,
174 String name, long timeoutPeriod, boolean allowDelayBehindServices) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800175 mService = service;
Jeff Brown6f357d32014-01-15 20:40:55 -0800176 mHandler = new BroadcastHandler(handler.getLooper());
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800177 mQueueName = name;
178 mTimeoutPeriod = timeoutPeriod;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700179 mDelayBehindServices = allowDelayBehindServices;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800180 }
181
182 public boolean isPendingBroadcastProcessLocked(int pid) {
183 return mPendingBroadcast != null && mPendingBroadcast.curApp.pid == pid;
184 }
185
186 public void enqueueParallelBroadcastLocked(BroadcastRecord r) {
187 mParallelBroadcasts.add(r);
Jeff Brown9fb3fd12014-09-29 15:32:12 -0700188 r.enqueueClockTime = System.currentTimeMillis();
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800189 }
190
191 public void enqueueOrderedBroadcastLocked(BroadcastRecord r) {
192 mOrderedBroadcasts.add(r);
Jeff Brown9fb3fd12014-09-29 15:32:12 -0700193 r.enqueueClockTime = System.currentTimeMillis();
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800194 }
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)) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800199 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800200 "***** 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)) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800212 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800213 "***** 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 {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800224 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800225 "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 {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800241 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800242 "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);
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800249 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800250 "Process cur broadcast " + r + " DELIVERED for app " + app);
251 started = true;
252 } finally {
253 if (!started) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800254 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800255 "Process cur broadcast " + r + ": NOT STARTED!");
256 r.receiver = null;
257 r.curApp = null;
258 app.curReceiver = null;
259 }
260 }
261 }
262
263 public boolean sendPendingBroadcastsLocked(ProcessRecord app) {
264 boolean didSomething = false;
265 final BroadcastRecord br = mPendingBroadcast;
266 if (br != null && br.curApp.pid == app.pid) {
267 try {
268 mPendingBroadcast = null;
269 processCurBroadcastLocked(br, app);
270 didSomething = true;
271 } catch (Exception e) {
272 Slog.w(TAG, "Exception in new application when starting receiver "
273 + br.curComponent.flattenToShortString(), e);
274 logBroadcastReceiverDiscardLocked(br);
275 finishReceiverLocked(br, br.resultCode, br.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700276 br.resultExtras, br.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800277 scheduleBroadcastsLocked();
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700278 // We need to reset the state if we failed to start the receiver.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800279 br.state = BroadcastRecord.IDLE;
280 throw new RuntimeException(e.getMessage());
281 }
282 }
283 return didSomething;
284 }
285
286 public void skipPendingBroadcastLocked(int pid) {
287 final BroadcastRecord br = mPendingBroadcast;
288 if (br != null && br.curApp.pid == pid) {
289 br.state = BroadcastRecord.IDLE;
290 br.nextReceiver = mPendingBroadcastRecvIndex;
291 mPendingBroadcast = null;
292 scheduleBroadcastsLocked();
293 }
294 }
295
296 public void skipCurrentReceiverLocked(ProcessRecord app) {
297 boolean reschedule = false;
298 BroadcastRecord r = app.curReceiver;
Kenji Sugimoto4472fa972014-07-17 14:50:41 +0900299 if (r != null && r.queue == this) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800300 // The current broadcast is waiting for this app's receiver
301 // to be finished. Looks like that's not going to happen, so
302 // let the broadcast continue.
303 logBroadcastReceiverDiscardLocked(r);
304 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700305 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800306 reschedule = true;
307 }
riddle_hsu01eb7fa2015-03-04 17:27:05 +0800308 if (r == null && mPendingBroadcast != null && mPendingBroadcast.curApp == app) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800309 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800310 "[" + mQueueName + "] skip & discard pending app " + r);
riddle_hsu01eb7fa2015-03-04 17:27:05 +0800311 r = mPendingBroadcast;
312 }
313
314 if (r != null) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800315 logBroadcastReceiverDiscardLocked(r);
316 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700317 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800318 scheduleBroadcastsLocked();
319 }
320 }
321
322 public void scheduleBroadcastsLocked() {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800323 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Schedule broadcasts ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800324 + mQueueName + "]: current="
325 + mBroadcastsScheduled);
326
327 if (mBroadcastsScheduled) {
328 return;
329 }
330 mHandler.sendMessage(mHandler.obtainMessage(BROADCAST_INTENT_MSG, this));
331 mBroadcastsScheduled = true;
332 }
333
334 public BroadcastRecord getMatchingOrderedReceiver(IBinder receiver) {
335 if (mOrderedBroadcasts.size() > 0) {
336 final BroadcastRecord r = mOrderedBroadcasts.get(0);
337 if (r != null && r.receiver == receiver) {
338 return r;
339 }
340 }
341 return null;
342 }
343
344 public boolean finishReceiverLocked(BroadcastRecord r, int resultCode,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700345 String resultData, Bundle resultExtras, boolean resultAbort, boolean waitForServices) {
346 final int state = r.state;
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700347 final ActivityInfo receiver = r.curReceiver;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800348 r.state = BroadcastRecord.IDLE;
349 if (state == BroadcastRecord.IDLE) {
Dianne Hackborn6285a322013-09-18 12:09:47 -0700350 Slog.w(TAG, "finishReceiver [" + mQueueName + "] called but state is IDLE");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800351 }
352 r.receiver = null;
353 r.intent.setComponent(null);
Guobin Zhang04d0bb62014-03-07 17:47:10 +0800354 if (r.curApp != null && r.curApp.curReceiver == r) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800355 r.curApp.curReceiver = null;
356 }
357 if (r.curFilter != null) {
358 r.curFilter.receiverList.curBroadcast = null;
359 }
360 r.curFilter = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800361 r.curReceiver = null;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700362 r.curApp = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800363 mPendingBroadcast = null;
364
365 r.resultCode = resultCode;
366 r.resultData = resultData;
367 r.resultExtras = resultExtras;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700368 if (resultAbort && (r.intent.getFlags()&Intent.FLAG_RECEIVER_NO_ABORT) == 0) {
369 r.resultAbort = resultAbort;
370 } else {
371 r.resultAbort = false;
372 }
373
374 if (waitForServices && r.curComponent != null && r.queue.mDelayBehindServices
375 && r.queue.mOrderedBroadcasts.size() > 0
376 && r.queue.mOrderedBroadcasts.get(0) == r) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700377 ActivityInfo nextReceiver;
378 if (r.nextReceiver < r.receivers.size()) {
379 Object obj = r.receivers.get(r.nextReceiver);
380 nextReceiver = (obj instanceof ActivityInfo) ? (ActivityInfo)obj : null;
381 } else {
382 nextReceiver = null;
383 }
384 // Don't do this if the next receive is in the same process as the current one.
385 if (receiver == null || nextReceiver == null
386 || receiver.applicationInfo.uid != nextReceiver.applicationInfo.uid
387 || !receiver.processName.equals(nextReceiver.processName)) {
388 // In this case, we are ready to process the next receiver for the current broadcast,
389 // but are on a queue that would like to wait for services to finish before moving
390 // on. If there are background services currently starting, then we will go into a
391 // special state where we hold off on continuing this broadcast until they are done.
392 if (mService.mServices.hasBackgroundServices(r.userId)) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800393 Slog.i(TAG, "Delay finish: " + r.curComponent.flattenToShortString());
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700394 r.state = BroadcastRecord.WAITING_SERVICES;
395 return false;
396 }
Dianne Hackborn6285a322013-09-18 12:09:47 -0700397 }
398 }
399
400 r.curComponent = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800401
402 // We will process the next receiver right now if this is finishing
403 // an app receiver (which is always asynchronous) or after we have
404 // come back from calling a receiver.
405 return state == BroadcastRecord.APP_RECEIVE
406 || state == BroadcastRecord.CALL_DONE_RECEIVE;
407 }
408
Dianne Hackborn6285a322013-09-18 12:09:47 -0700409 public void backgroundServicesFinishedLocked(int userId) {
410 if (mOrderedBroadcasts.size() > 0) {
411 BroadcastRecord br = mOrderedBroadcasts.get(0);
412 if (br.userId == userId && br.state == BroadcastRecord.WAITING_SERVICES) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800413 Slog.i(TAG, "Resuming delayed broadcast");
Dianne Hackborn6285a322013-09-18 12:09:47 -0700414 br.curComponent = null;
415 br.state = BroadcastRecord.IDLE;
416 processNextBroadcast(false);
417 }
418 }
419 }
420
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800421 private static void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver,
422 Intent intent, int resultCode, String data, Bundle extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700423 boolean ordered, boolean sticky, int sendingUser) throws RemoteException {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800424 // Send the intent to the receiver asynchronously using one-way binder calls.
Craig Mautner38c1a5f2014-07-21 15:36:37 +0000425 if (app != null) {
426 if (app.thread != null) {
427 // If we have an app thread, do the call through that so it is
428 // correctly ordered with other one-way calls.
429 app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode,
430 data, extras, ordered, sticky, sendingUser, app.repProcState);
431 } else {
432 // Application has died. Receiver doesn't exist.
433 throw new RemoteException("app.thread must not be null");
434 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800435 } else {
Dianne Hackborn20e80982012-08-31 19:00:44 -0700436 receiver.performReceive(intent, resultCode, data, extras, ordered,
437 sticky, sendingUser);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800438 }
439 }
440
441 private final void deliverToRegisteredReceiverLocked(BroadcastRecord r,
442 BroadcastFilter filter, boolean ordered) {
443 boolean skip = false;
Amith Yamasani8bf06ed2012-08-27 19:30:30 -0700444 if (filter.requiredPermission != null) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800445 int perm = mService.checkComponentPermission(filter.requiredPermission,
446 r.callingPid, r.callingUid, -1, true);
447 if (perm != PackageManager.PERMISSION_GRANTED) {
448 Slog.w(TAG, "Permission Denial: broadcasting "
449 + r.intent.toString()
450 + " from " + r.callerPackage + " (pid="
451 + r.callingPid + ", uid=" + r.callingUid + ")"
452 + " requires " + filter.requiredPermission
453 + " due to registered receiver " + filter);
454 skip = true;
455 }
456 }
Dianne Hackbornb4163a62012-08-02 18:31:26 -0700457 if (!skip && r.requiredPermission != null) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800458 int perm = mService.checkComponentPermission(r.requiredPermission,
459 filter.receiverList.pid, filter.receiverList.uid, -1, true);
460 if (perm != PackageManager.PERMISSION_GRANTED) {
461 Slog.w(TAG, "Permission Denial: receiving "
462 + r.intent.toString()
463 + " to " + filter.receiverList.app
464 + " (pid=" + filter.receiverList.pid
465 + ", uid=" + filter.receiverList.uid + ")"
466 + " requires " + r.requiredPermission
467 + " due to sender " + r.callerPackage
468 + " (uid " + r.callingUid + ")");
469 skip = true;
470 }
471 }
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800472 if (r.appOp != AppOpsManager.OP_NONE) {
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700473 int mode = mService.mAppOpsService.noteOperation(r.appOp,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800474 filter.receiverList.uid, filter.packageName);
475 if (mode != AppOpsManager.MODE_ALLOWED) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800476 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800477 "App op " + r.appOp + " not allowed for broadcast to uid "
478 + filter.receiverList.uid + " pkg " + filter.packageName);
479 skip = true;
480 }
481 }
Ben Gruver49660c72013-08-06 19:54:08 -0700482 if (!skip) {
483 skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,
484 r.callingPid, r.resolvedType, filter.receiverList.uid);
485 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800486
Dianne Hackborn9357b112013-10-03 18:27:48 -0700487 if (filter.receiverList.app == null || filter.receiverList.app.crashing) {
488 Slog.w(TAG, "Skipping deliver [" + mQueueName + "] " + r
489 + " to " + filter.receiverList + ": process crashing");
490 skip = true;
491 }
492
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800493 if (!skip) {
494 // If this is not being sent as an ordered broadcast, then we
495 // don't want to touch the fields that keep track of the current
496 // state of ordered broadcasts.
497 if (ordered) {
498 r.receiver = filter.receiverList.receiver.asBinder();
499 r.curFilter = filter;
500 filter.receiverList.curBroadcast = r;
501 r.state = BroadcastRecord.CALL_IN_RECEIVE;
502 if (filter.receiverList.app != null) {
503 // Bump hosting application to no longer be in background
504 // scheduling class. Note that we can't do that if there
505 // isn't an app... but we can only be in that case for
506 // things that directly call the IActivityManager API, which
507 // are already core system stuff so don't matter for this.
508 r.curApp = filter.receiverList.app;
509 filter.receiverList.app.curReceiver = r;
Dianne Hackborn684bf342014-04-29 17:56:57 -0700510 mService.updateOomAdjLocked(r.curApp);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800511 }
512 }
513 try {
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -0700514 if (DEBUG_BROADCAST_LIGHT) Slog.i(TAG_BROADCAST,
515 "Delivering to " + filter + " : " + r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800516 performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver,
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -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
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800540 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "processNextBroadcast ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800541 + 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();
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800557 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Processing parallel broadcast ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800558 + mQueueName + "] " + r);
559 for (int i=0; i<N; i++) {
560 Object target = r.receivers.get(i);
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800561 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800562 "Delivering non-ordered on [" + mQueueName + "] to registered "
563 + target + ": " + r);
564 deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false);
565 }
566 addBroadcastToHistoryLocked(r);
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800567 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Done with parallel broadcast ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800568 + 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) {
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -0700577 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST,
578 "processNextBroadcast [" + mQueueName + "]: waiting for "
579 + mPendingBroadcast.curApp);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800580
581 boolean isDead;
582 synchronized (mService.mPidsSelfLocked) {
Dianne Hackborn9357b112013-10-03 18:27:48 -0700583 ProcessRecord proc = mService.mPidsSelfLocked.get(mPendingBroadcast.curApp.pid);
584 isDead = proc == null || proc.crashing;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800585 }
586 if (!isDead) {
587 // It's still alive, so keep waiting
588 return;
589 } else {
590 Slog.w(TAG, "pending app ["
591 + mQueueName + "]" + mPendingBroadcast.curApp
592 + " died before responding to broadcast");
593 mPendingBroadcast.state = BroadcastRecord.IDLE;
594 mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex;
595 mPendingBroadcast = null;
596 }
597 }
598
599 boolean looped = false;
600
601 do {
602 if (mOrderedBroadcasts.size() == 0) {
603 // No more broadcasts pending, so all done!
604 mService.scheduleAppGcsLocked();
605 if (looped) {
606 // If we had finished the last ordered broadcast, then
607 // make sure all processes have correct oom and sched
608 // adjustments.
609 mService.updateOomAdjLocked();
610 }
611 return;
612 }
613 r = mOrderedBroadcasts.get(0);
614 boolean forceReceive = false;
615
616 // Ensure that even if something goes awry with the timeout
617 // detection, we catch "hung" broadcasts here, discard them,
618 // and continue to make progress.
619 //
620 // This is only done if the system is ready so that PRE_BOOT_COMPLETED
621 // receivers don't get executed with timeouts. They're intended for
622 // one time heavy lifting after system upgrades and can take
623 // significant amounts of time.
624 int numReceivers = (r.receivers != null) ? r.receivers.size() : 0;
625 if (mService.mProcessesReady && r.dispatchTime > 0) {
626 long now = SystemClock.uptimeMillis();
627 if ((numReceivers > 0) &&
628 (now > r.dispatchTime + (2*mTimeoutPeriod*numReceivers))) {
629 Slog.w(TAG, "Hung broadcast ["
630 + mQueueName + "] discarded after timeout failure:"
631 + " now=" + now
632 + " dispatchTime=" + r.dispatchTime
633 + " startTime=" + r.receiverTime
634 + " intent=" + r.intent
635 + " numReceivers=" + numReceivers
636 + " nextReceiver=" + r.nextReceiver
637 + " state=" + r.state);
638 broadcastTimeoutLocked(false); // forcibly finish this broadcast
639 forceReceive = true;
640 r.state = BroadcastRecord.IDLE;
641 }
642 }
643
644 if (r.state != BroadcastRecord.IDLE) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800645 if (DEBUG_BROADCAST) Slog.d(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800646 "processNextBroadcast("
647 + mQueueName + ") called when not idle (state="
648 + r.state + ")");
649 return;
650 }
651
652 if (r.receivers == null || r.nextReceiver >= numReceivers
653 || r.resultAbort || forceReceive) {
654 // No more receivers for this broadcast! Send the final
655 // result if requested...
656 if (r.resultTo != null) {
657 try {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800658 if (DEBUG_BROADCAST) Slog.i(TAG_BROADCAST,
Todd Kennedyd2f15112015-01-21 15:25:56 -0800659 "Finishing broadcast [" + mQueueName + "] "
660 + r.intent.getAction() + " app=" + r.callerApp);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800661 performReceiveLocked(r.callerApp, r.resultTo,
662 new Intent(r.intent), r.resultCode,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700663 r.resultData, r.resultExtras, false, false, r.userId);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800664 // Set this to null so that the reference
Dianne Hackborn9357b112013-10-03 18:27:48 -0700665 // (local and remote) isn't kept in the mBroadcastHistory.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800666 r.resultTo = null;
667 } catch (RemoteException e) {
Craig Mautner38c1a5f2014-07-21 15:36:37 +0000668 r.resultTo = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800669 Slog.w(TAG, "Failure ["
670 + mQueueName + "] sending broadcast result of "
671 + r.intent, e);
672 }
673 }
674
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800675 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Cancelling BROADCAST_TIMEOUT_MSG");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800676 cancelBroadcastTimeoutLocked();
677
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -0700678 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST,
679 "Finished with ordered broadcast " + r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800680
681 // ... and on to the next...
682 addBroadcastToHistoryLocked(r);
683 mOrderedBroadcasts.remove(0);
684 r = null;
685 looped = true;
686 continue;
687 }
688 } while (r == null);
689
690 // Get the next receiver...
691 int recIdx = r.nextReceiver++;
692
693 // Keep track of when this receiver started, and make sure there
694 // is a timeout message pending to kill it if need be.
695 r.receiverTime = SystemClock.uptimeMillis();
696 if (recIdx == 0) {
697 r.dispatchTime = r.receiverTime;
698 r.dispatchClockTime = System.currentTimeMillis();
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800699 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Processing ordered broadcast ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800700 + mQueueName + "] " + r);
701 }
702 if (! mPendingBroadcastTimeoutMessage) {
703 long timeoutTime = r.receiverTime + mTimeoutPeriod;
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800704 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800705 "Submitting BROADCAST_TIMEOUT_MSG ["
706 + mQueueName + "] for " + r + " at " + timeoutTime);
707 setBroadcastTimeoutLocked(timeoutTime);
708 }
709
710 Object nextReceiver = r.receivers.get(recIdx);
711 if (nextReceiver instanceof BroadcastFilter) {
712 // Simple case: this is a registered receiver who gets
713 // a direct call.
714 BroadcastFilter filter = (BroadcastFilter)nextReceiver;
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800715 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800716 "Delivering ordered ["
717 + mQueueName + "] to registered "
718 + filter + ": " + r);
719 deliverToRegisteredReceiverLocked(r, filter, r.ordered);
720 if (r.receiver == null || !r.ordered) {
721 // The receiver has already finished, so schedule to
722 // process the next one.
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800723 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Quick finishing ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800724 + mQueueName + "]: ordered="
725 + r.ordered + " receiver=" + r.receiver);
726 r.state = BroadcastRecord.IDLE;
727 scheduleBroadcastsLocked();
728 }
729 return;
730 }
731
732 // Hard case: need to instantiate the receiver, possibly
733 // starting its application process to host it.
734
735 ResolveInfo info =
736 (ResolveInfo)nextReceiver;
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700737 ComponentName component = new ComponentName(
738 info.activityInfo.applicationInfo.packageName,
739 info.activityInfo.name);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800740
741 boolean skip = false;
742 int perm = mService.checkComponentPermission(info.activityInfo.permission,
743 r.callingPid, r.callingUid, info.activityInfo.applicationInfo.uid,
744 info.activityInfo.exported);
745 if (perm != PackageManager.PERMISSION_GRANTED) {
746 if (!info.activityInfo.exported) {
747 Slog.w(TAG, "Permission Denial: broadcasting "
748 + r.intent.toString()
749 + " from " + r.callerPackage + " (pid=" + r.callingPid
750 + ", uid=" + r.callingUid + ")"
751 + " is not exported from uid " + info.activityInfo.applicationInfo.uid
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700752 + " due to receiver " + component.flattenToShortString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800753 } else {
754 Slog.w(TAG, "Permission Denial: broadcasting "
755 + r.intent.toString()
756 + " from " + r.callerPackage + " (pid=" + r.callingPid
757 + ", uid=" + r.callingUid + ")"
758 + " requires " + info.activityInfo.permission
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700759 + " due to receiver " + component.flattenToShortString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800760 }
761 skip = true;
762 }
763 if (info.activityInfo.applicationInfo.uid != Process.SYSTEM_UID &&
764 r.requiredPermission != null) {
765 try {
766 perm = AppGlobals.getPackageManager().
767 checkPermission(r.requiredPermission,
Svetoslavc6d1c342015-02-26 14:44:43 -0800768 info.activityInfo.applicationInfo.packageName, r.userId);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800769 } catch (RemoteException e) {
770 perm = PackageManager.PERMISSION_DENIED;
771 }
772 if (perm != PackageManager.PERMISSION_GRANTED) {
773 Slog.w(TAG, "Permission Denial: receiving "
774 + r.intent + " to "
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700775 + component.flattenToShortString()
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800776 + " requires " + r.requiredPermission
777 + " due to sender " + r.callerPackage
778 + " (uid " + r.callingUid + ")");
779 skip = true;
780 }
781 }
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800782 if (r.appOp != AppOpsManager.OP_NONE) {
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700783 int mode = mService.mAppOpsService.noteOperation(r.appOp,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800784 info.activityInfo.applicationInfo.uid, info.activityInfo.packageName);
785 if (mode != AppOpsManager.MODE_ALLOWED) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800786 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800787 "App op " + r.appOp + " not allowed for broadcast to uid "
788 + info.activityInfo.applicationInfo.uid + " pkg "
789 + info.activityInfo.packageName);
790 skip = true;
791 }
792 }
Ben Gruver49660c72013-08-06 19:54:08 -0700793 if (!skip) {
794 skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,
795 r.callingPid, r.resolvedType, info.activityInfo.applicationInfo.uid);
796 }
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700797 boolean isSingleton = false;
798 try {
799 isSingleton = mService.isSingleton(info.activityInfo.processName,
800 info.activityInfo.applicationInfo,
801 info.activityInfo.name, info.activityInfo.flags);
802 } catch (SecurityException e) {
803 Slog.w(TAG, e.getMessage());
804 skip = true;
805 }
806 if ((info.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) {
807 if (ActivityManager.checkUidPermission(
808 android.Manifest.permission.INTERACT_ACROSS_USERS,
809 info.activityInfo.applicationInfo.uid)
810 != PackageManager.PERMISSION_GRANTED) {
811 Slog.w(TAG, "Permission Denial: Receiver " + component.flattenToShortString()
812 + " requests FLAG_SINGLE_USER, but app does not hold "
813 + android.Manifest.permission.INTERACT_ACROSS_USERS);
814 skip = true;
815 }
816 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800817 if (r.curApp != null && r.curApp.crashing) {
818 // If the target process is crashing, just skip it.
Dianne Hackborn9357b112013-10-03 18:27:48 -0700819 Slog.w(TAG, "Skipping deliver ordered [" + mQueueName + "] " + r
820 + " to " + r.curApp + ": process crashing");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800821 skip = true;
822 }
Christopher Tateba629da2013-11-13 17:42:28 -0800823 if (!skip) {
824 boolean isAvailable = false;
825 try {
826 isAvailable = AppGlobals.getPackageManager().isPackageAvailable(
827 info.activityInfo.packageName,
828 UserHandle.getUserId(info.activityInfo.applicationInfo.uid));
829 } catch (Exception e) {
830 // all such failures mean we skip this receiver
831 Slog.w(TAG, "Exception getting recipient info for "
832 + info.activityInfo.packageName, e);
833 }
834 if (!isAvailable) {
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -0700835 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
836 "Skipping delivery to " + info.activityInfo.packageName + " / "
837 + info.activityInfo.applicationInfo.uid
838 + " : package no longer available");
Christopher Tateba629da2013-11-13 17:42:28 -0800839 skip = true;
840 }
841 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800842
843 if (skip) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800844 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -0700845 "Skipping delivery of ordered [" + mQueueName + "] "
846 + r + " for whatever reason");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800847 r.receiver = null;
848 r.curFilter = null;
849 r.state = BroadcastRecord.IDLE;
850 scheduleBroadcastsLocked();
851 return;
852 }
853
854 r.state = BroadcastRecord.APP_RECEIVE;
855 String targetProcess = info.activityInfo.processName;
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700856 r.curComponent = component;
Amith Yamasani4b9d79c2014-05-21 19:14:21 -0700857 final int receiverUid = info.activityInfo.applicationInfo.uid;
858 // If it's a singleton, it needs to be the same app or a special app
859 if (r.callingUid != Process.SYSTEM_UID && isSingleton
860 && mService.isValidSingletonCall(r.callingUid, receiverUid)) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700861 info.activityInfo = mService.getActivityInfoForUser(info.activityInfo, 0);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800862 }
863 r.curReceiver = info.activityInfo;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700864 if (DEBUG_MU && r.callingUid > UserHandle.PER_USER_RANGE) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800865 Slog.v(TAG_MU, "Updated broadcast record activity info for secondary user, "
866 + info.activityInfo + ", callingUid = " + r.callingUid + ", uid = "
867 + info.activityInfo.applicationInfo.uid);
868 }
869
870 // Broadcast is being executed, its package can't be stopped.
871 try {
872 AppGlobals.getPackageManager().setPackageStoppedState(
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700873 r.curComponent.getPackageName(), false, UserHandle.getUserId(r.callingUid));
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800874 } catch (RemoteException e) {
875 } catch (IllegalArgumentException e) {
876 Slog.w(TAG, "Failed trying to unstop package "
877 + r.curComponent.getPackageName() + ": " + e);
878 }
879
880 // Is this receiver's application already running?
881 ProcessRecord app = mService.getProcessRecordLocked(targetProcess,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700882 info.activityInfo.applicationInfo.uid, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800883 if (app != null && app.thread != null) {
884 try {
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700885 app.addPackage(info.activityInfo.packageName,
886 info.activityInfo.applicationInfo.versionCode, mService.mProcessStats);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800887 processCurBroadcastLocked(r, app);
888 return;
889 } catch (RemoteException e) {
890 Slog.w(TAG, "Exception when sending broadcast to "
891 + r.curComponent, e);
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800892 } catch (RuntimeException e) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700893 Slog.wtf(TAG, "Failed sending broadcast to "
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800894 + r.curComponent + " with " + r.intent, e);
895 // If some unexpected exception happened, just skip
896 // this broadcast. At this point we are not in the call
897 // from a client, so throwing an exception out from here
898 // will crash the entire system instead of just whoever
899 // sent the broadcast.
900 logBroadcastReceiverDiscardLocked(r);
901 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700902 r.resultExtras, r.resultAbort, false);
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800903 scheduleBroadcastsLocked();
904 // We need to reset the state if we failed to start the receiver.
905 r.state = BroadcastRecord.IDLE;
906 return;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800907 }
908
909 // If a dead object exception was thrown -- fall through to
910 // restart the application.
911 }
912
913 // Not running -- get it started, to be executed when the app comes up.
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800914 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800915 "Need to start app ["
916 + mQueueName + "] " + targetProcess + " for broadcast " + r);
917 if ((r.curApp=mService.startProcessLocked(targetProcess,
918 info.activityInfo.applicationInfo, true,
919 r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND,
920 "broadcast", r.curComponent,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700921 (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0, false, false))
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800922 == null) {
923 // Ah, this recipient is unavailable. Finish it if necessary,
924 // and mark the broadcast record as ready for the next.
925 Slog.w(TAG, "Unable to launch app "
926 + info.activityInfo.applicationInfo.packageName + "/"
927 + info.activityInfo.applicationInfo.uid + " for broadcast "
928 + r.intent + ": process is bad");
929 logBroadcastReceiverDiscardLocked(r);
930 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700931 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800932 scheduleBroadcastsLocked();
933 r.state = BroadcastRecord.IDLE;
934 return;
935 }
936
937 mPendingBroadcast = r;
938 mPendingBroadcastRecvIndex = recIdx;
939 }
940 }
941
942 final void setBroadcastTimeoutLocked(long timeoutTime) {
943 if (! mPendingBroadcastTimeoutMessage) {
944 Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG, this);
945 mHandler.sendMessageAtTime(msg, timeoutTime);
946 mPendingBroadcastTimeoutMessage = true;
947 }
948 }
949
950 final void cancelBroadcastTimeoutLocked() {
951 if (mPendingBroadcastTimeoutMessage) {
952 mHandler.removeMessages(BROADCAST_TIMEOUT_MSG, this);
953 mPendingBroadcastTimeoutMessage = false;
954 }
955 }
956
957 final void broadcastTimeoutLocked(boolean fromMsg) {
958 if (fromMsg) {
959 mPendingBroadcastTimeoutMessage = false;
960 }
961
962 if (mOrderedBroadcasts.size() == 0) {
963 return;
964 }
965
966 long now = SystemClock.uptimeMillis();
967 BroadcastRecord r = mOrderedBroadcasts.get(0);
968 if (fromMsg) {
969 if (mService.mDidDexOpt) {
970 // Delay timeouts until dexopt finishes.
971 mService.mDidDexOpt = false;
972 long timeoutTime = SystemClock.uptimeMillis() + mTimeoutPeriod;
973 setBroadcastTimeoutLocked(timeoutTime);
974 return;
975 }
976 if (!mService.mProcessesReady) {
977 // Only process broadcast timeouts if the system is ready. That way
978 // PRE_BOOT_COMPLETED broadcasts can't timeout as they are intended
979 // to do heavy lifting for system up.
980 return;
981 }
982
983 long timeoutTime = r.receiverTime + mTimeoutPeriod;
984 if (timeoutTime > now) {
985 // We can observe premature timeouts because we do not cancel and reset the
986 // broadcast timeout message after each receiver finishes. Instead, we set up
987 // an initial timeout then kick it down the road a little further as needed
988 // when it expires.
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800989 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800990 "Premature timeout ["
991 + mQueueName + "] @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for "
992 + timeoutTime);
993 setBroadcastTimeoutLocked(timeoutTime);
994 return;
995 }
996 }
997
Dianne Hackborn6285a322013-09-18 12:09:47 -0700998 BroadcastRecord br = mOrderedBroadcasts.get(0);
999 if (br.state == BroadcastRecord.WAITING_SERVICES) {
1000 // In this case the broadcast had already finished, but we had decided to wait
1001 // for started services to finish as well before going on. So if we have actually
1002 // waited long enough time timeout the broadcast, let's give up on the whole thing
1003 // and just move on to the next.
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001004 Slog.i(TAG, "Waited long enough for: " + (br.curComponent != null
Dianne Hackborn6285a322013-09-18 12:09:47 -07001005 ? br.curComponent.flattenToShortString() : "(null)"));
1006 br.curComponent = null;
1007 br.state = BroadcastRecord.IDLE;
1008 processNextBroadcast(false);
1009 return;
1010 }
1011
1012 Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r. receiver
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001013 + ", started " + (now - r.receiverTime) + "ms ago");
1014 r.receiverTime = now;
1015 r.anrCount++;
1016
1017 // Current receiver has passed its expiration date.
1018 if (r.nextReceiver <= 0) {
1019 Slog.w(TAG, "Timeout on receiver with nextReceiver <= 0");
1020 return;
1021 }
1022
1023 ProcessRecord app = null;
1024 String anrMessage = null;
1025
1026 Object curReceiver = r.receivers.get(r.nextReceiver-1);
1027 Slog.w(TAG, "Receiver during timeout: " + curReceiver);
1028 logBroadcastReceiverDiscardLocked(r);
1029 if (curReceiver instanceof BroadcastFilter) {
1030 BroadcastFilter bf = (BroadcastFilter)curReceiver;
1031 if (bf.receiverList.pid != 0
1032 && bf.receiverList.pid != ActivityManagerService.MY_PID) {
1033 synchronized (mService.mPidsSelfLocked) {
1034 app = mService.mPidsSelfLocked.get(
1035 bf.receiverList.pid);
1036 }
1037 }
1038 } else {
1039 app = r.curApp;
1040 }
1041
1042 if (app != null) {
1043 anrMessage = "Broadcast of " + r.intent.toString();
1044 }
1045
1046 if (mPendingBroadcast == r) {
1047 mPendingBroadcast = null;
1048 }
1049
1050 // Move on to the next receiver.
1051 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -07001052 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001053 scheduleBroadcastsLocked();
1054
1055 if (anrMessage != null) {
1056 // Post the ANR to the handler since we do not want to process ANRs while
1057 // potentially holding our lock.
1058 mHandler.post(new AppNotResponding(app, anrMessage));
1059 }
1060 }
1061
1062 private final void addBroadcastToHistoryLocked(BroadcastRecord r) {
1063 if (r.callingUid < 0) {
1064 // This was from a registerReceiver() call; ignore it.
1065 return;
1066 }
1067 System.arraycopy(mBroadcastHistory, 0, mBroadcastHistory, 1,
1068 MAX_BROADCAST_HISTORY-1);
1069 r.finishTime = SystemClock.uptimeMillis();
1070 mBroadcastHistory[0] = r;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001071 System.arraycopy(mBroadcastSummaryHistory, 0, mBroadcastSummaryHistory, 1,
1072 MAX_BROADCAST_SUMMARY_HISTORY-1);
1073 mBroadcastSummaryHistory[0] = r.intent;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001074 }
1075
1076 final void logBroadcastReceiverDiscardLocked(BroadcastRecord r) {
1077 if (r.nextReceiver > 0) {
1078 Object curReceiver = r.receivers.get(r.nextReceiver-1);
1079 if (curReceiver instanceof BroadcastFilter) {
1080 BroadcastFilter bf = (BroadcastFilter) curReceiver;
1081 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001082 bf.owningUserId, System.identityHashCode(r),
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001083 r.intent.getAction(),
1084 r.nextReceiver - 1,
1085 System.identityHashCode(bf));
1086 } else {
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001087 ResolveInfo ri = (ResolveInfo)curReceiver;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001088 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001089 UserHandle.getUserId(ri.activityInfo.applicationInfo.uid),
1090 System.identityHashCode(r), r.intent.getAction(),
1091 r.nextReceiver - 1, ri.toString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001092 }
1093 } else {
1094 Slog.w(TAG, "Discarding broadcast before first receiver is invoked: "
1095 + r);
1096 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001097 -1, System.identityHashCode(r),
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001098 r.intent.getAction(),
1099 r.nextReceiver,
1100 "NONE");
1101 }
1102 }
1103
1104 final boolean dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args,
1105 int opti, boolean dumpAll, String dumpPackage, boolean needSep) {
1106 if (mParallelBroadcasts.size() > 0 || mOrderedBroadcasts.size() > 0
1107 || mPendingBroadcast != null) {
1108 boolean printed = false;
1109 for (int i=mParallelBroadcasts.size()-1; i>=0; i--) {
1110 BroadcastRecord br = mParallelBroadcasts.get(i);
1111 if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) {
1112 continue;
1113 }
1114 if (!printed) {
1115 if (needSep) {
1116 pw.println();
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001117 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001118 needSep = true;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001119 printed = true;
1120 pw.println(" Active broadcasts [" + mQueueName + "]:");
1121 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001122 pw.println(" Active Broadcast " + mQueueName + " #" + i + ":");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001123 br.dump(pw, " ");
1124 }
1125 printed = false;
1126 needSep = true;
1127 for (int i=mOrderedBroadcasts.size()-1; i>=0; i--) {
1128 BroadcastRecord br = mOrderedBroadcasts.get(i);
1129 if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) {
1130 continue;
1131 }
1132 if (!printed) {
1133 if (needSep) {
1134 pw.println();
1135 }
1136 needSep = true;
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001137 printed = true;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001138 pw.println(" Active ordered broadcasts [" + mQueueName + "]:");
1139 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001140 pw.println(" Active Ordered Broadcast " + mQueueName + " #" + i + ":");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001141 mOrderedBroadcasts.get(i).dump(pw, " ");
1142 }
1143 if (dumpPackage == null || (mPendingBroadcast != null
1144 && dumpPackage.equals(mPendingBroadcast.callerPackage))) {
1145 if (needSep) {
1146 pw.println();
1147 }
1148 pw.println(" Pending broadcast [" + mQueueName + "]:");
1149 if (mPendingBroadcast != null) {
1150 mPendingBroadcast.dump(pw, " ");
1151 } else {
1152 pw.println(" (null)");
1153 }
1154 needSep = true;
1155 }
1156 }
1157
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001158 int i;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001159 boolean printed = false;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001160 for (i=0; i<MAX_BROADCAST_HISTORY; i++) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001161 BroadcastRecord r = mBroadcastHistory[i];
1162 if (r == null) {
1163 break;
1164 }
1165 if (dumpPackage != null && !dumpPackage.equals(r.callerPackage)) {
1166 continue;
1167 }
1168 if (!printed) {
1169 if (needSep) {
1170 pw.println();
1171 }
1172 needSep = true;
1173 pw.println(" Historical broadcasts [" + mQueueName + "]:");
1174 printed = true;
1175 }
1176 if (dumpAll) {
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001177 pw.print(" Historical Broadcast " + mQueueName + " #");
1178 pw.print(i); pw.println(":");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001179 r.dump(pw, " ");
1180 } else {
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001181 pw.print(" #"); pw.print(i); pw.print(": "); pw.println(r);
1182 pw.print(" ");
1183 pw.println(r.intent.toShortString(false, true, true, false));
Dianne Hackborna40cfeb2013-03-25 17:49:36 -07001184 if (r.targetComp != null && r.targetComp != r.intent.getComponent()) {
1185 pw.print(" targetComp: "); pw.println(r.targetComp.toShortString());
1186 }
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001187 Bundle bundle = r.intent.getExtras();
1188 if (bundle != null) {
1189 pw.print(" extras: "); pw.println(bundle.toString());
1190 }
1191 }
1192 }
1193
1194 if (dumpPackage == null) {
1195 if (dumpAll) {
1196 i = 0;
1197 printed = false;
1198 }
1199 for (; i<MAX_BROADCAST_SUMMARY_HISTORY; i++) {
1200 Intent intent = mBroadcastSummaryHistory[i];
1201 if (intent == null) {
1202 break;
1203 }
1204 if (!printed) {
1205 if (needSep) {
1206 pw.println();
1207 }
1208 needSep = true;
1209 pw.println(" Historical broadcasts summary [" + mQueueName + "]:");
1210 printed = true;
1211 }
1212 if (!dumpAll && i >= 50) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001213 pw.println(" ...");
1214 break;
1215 }
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001216 pw.print(" #"); pw.print(i); pw.print(": ");
1217 pw.println(intent.toShortString(false, true, true, false));
1218 Bundle bundle = intent.getExtras();
1219 if (bundle != null) {
1220 pw.print(" extras: "); pw.println(bundle.toString());
1221 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001222 }
1223 }
1224
1225 return needSep;
1226 }
1227}