blob: e89ef5724dcdecb1291b7d99e2ead8bb5a2e9024 [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;
Christopher Tatef278f122015-04-22 13:12:01 -070021import java.text.SimpleDateFormat;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080022import java.util.ArrayList;
Christopher Tatef278f122015-04-22 13:12:01 -070023import java.util.Date;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080024
Dianne Hackborn7d19e022012-08-07 19:12:33 -070025import android.app.ActivityManager;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080026import android.app.AppGlobals;
Dianne Hackbornf51f6122013-02-04 18:23:34 -080027import android.app.AppOpsManager;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080028import android.content.ComponentName;
29import android.content.IIntentReceiver;
30import android.content.Intent;
Dianne Hackborn7d19e022012-08-07 19:12:33 -070031import android.content.pm.ActivityInfo;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080032import android.content.pm.PackageManager;
33import android.content.pm.ResolveInfo;
34import android.os.Bundle;
35import android.os.Handler;
36import android.os.IBinder;
Jeff Brown6f357d32014-01-15 20:40:55 -080037import android.os.Looper;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080038import android.os.Message;
39import android.os.Process;
40import android.os.RemoteException;
41import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070042import android.os.UserHandle;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080043import android.util.EventLog;
44import android.util.Slog;
45
Wale Ogunwaled57969f2014-11-15 19:37:29 -080046import static com.android.server.am.ActivityManagerDebugConfig.*;
47
Dianne Hackborn40c8db52012-02-10 18:59:48 -080048/**
49 * BROADCASTS
50 *
51 * We keep two broadcast queues and associated bookkeeping, one for those at
52 * foreground priority, and one for normal (background-priority) broadcasts.
53 */
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070054public final class BroadcastQueue {
Wale Ogunwalebfac4682015-04-08 14:33:21 -070055 private static final String TAG = "BroadcastQueue";
Wale Ogunwaled57969f2014-11-15 19:37:29 -080056 private static final String TAG_MU = TAG + POSTFIX_MU;
57 private static final String TAG_BROADCAST = TAG + POSTFIX_BROADCAST;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080058
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 */
Wale Ogunwale540e1232015-05-01 15:35:39 -070088 final ArrayList<BroadcastRecord> mParallelBroadcasts = new ArrayList<>();
Dianne Hackborn6285a322013-09-18 12:09:47 -070089
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 */
Wale Ogunwale540e1232015-05-01 15:35:39 -070097 final ArrayList<BroadcastRecord> mOrderedBroadcasts = new ArrayList<>();
Dianne Hackborn40c8db52012-02-10 18:59:48 -080098
99 /**
Christopher Tatef278f122015-04-22 13:12:01 -0700100 * Historical data of past broadcasts, for debugging. This is a ring buffer
101 * whose last element is at mHistoryNext.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800102 */
Dianne Hackborn6285a322013-09-18 12:09:47 -0700103 final BroadcastRecord[] mBroadcastHistory = new BroadcastRecord[MAX_BROADCAST_HISTORY];
Christopher Tatef278f122015-04-22 13:12:01 -0700104 int mHistoryNext = 0;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800105
106 /**
Christopher Tatef278f122015-04-22 13:12:01 -0700107 * Summary of historical data of past broadcasts, for debugging. This is a
108 * ring buffer whose last element is at mSummaryHistoryNext.
Dianne Hackbornc0bd7472012-10-09 14:00:30 -0700109 */
Dianne Hackborn6285a322013-09-18 12:09:47 -0700110 final Intent[] mBroadcastSummaryHistory = new Intent[MAX_BROADCAST_SUMMARY_HISTORY];
Christopher Tatef278f122015-04-22 13:12:01 -0700111 int mSummaryHistoryNext = 0;
112
113 /**
114 * Various milestone timestamps of entries in the mBroadcastSummaryHistory ring
115 * buffer, also tracked via the mSummaryHistoryNext index. These are all in wall
116 * clock time, not elapsed.
117 */
118 final long[] mSummaryHistoryEnqueueTime = new long[MAX_BROADCAST_SUMMARY_HISTORY];
119 final long[] mSummaryHistoryDispatchTime = new long[MAX_BROADCAST_SUMMARY_HISTORY];
120 final long[] mSummaryHistoryFinishTime = new long[MAX_BROADCAST_SUMMARY_HISTORY];
Dianne Hackbornc0bd7472012-10-09 14:00:30 -0700121
122 /**
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800123 * Set when we current have a BROADCAST_INTENT_MSG in flight.
124 */
125 boolean mBroadcastsScheduled = false;
126
127 /**
128 * True if we have a pending unexpired BROADCAST_TIMEOUT_MSG posted to our handler.
129 */
130 boolean mPendingBroadcastTimeoutMessage;
131
132 /**
133 * Intent broadcasts that we have tried to start, but are
134 * waiting for the application's process to be created. We only
135 * need one per scheduling class (instead of a list) because we always
136 * process broadcasts one at a time, so no others can be started while
137 * waiting for this one.
138 */
139 BroadcastRecord mPendingBroadcast = null;
140
141 /**
142 * The receiver index that is pending, to restart the broadcast if needed.
143 */
144 int mPendingBroadcastRecvIndex;
145
146 static final int BROADCAST_INTENT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG;
147 static final int BROADCAST_TIMEOUT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG + 1;
148
Jeff Brown6f357d32014-01-15 20:40:55 -0800149 final BroadcastHandler mHandler;
150
151 private final class BroadcastHandler extends Handler {
152 public BroadcastHandler(Looper looper) {
153 super(looper, null, true);
154 }
155
156 @Override
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800157 public void handleMessage(Message msg) {
158 switch (msg.what) {
159 case BROADCAST_INTENT_MSG: {
160 if (DEBUG_BROADCAST) Slog.v(
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800161 TAG_BROADCAST, "Received BROADCAST_INTENT_MSG");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800162 processNextBroadcast(true);
163 } break;
164 case BROADCAST_TIMEOUT_MSG: {
165 synchronized (mService) {
166 broadcastTimeoutLocked(true);
167 }
168 } break;
169 }
170 }
171 };
172
173 private final class AppNotResponding implements Runnable {
174 private final ProcessRecord mApp;
175 private final String mAnnotation;
176
177 public AppNotResponding(ProcessRecord app, String annotation) {
178 mApp = app;
179 mAnnotation = annotation;
180 }
181
182 @Override
183 public void run() {
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700184 mService.appNotResponding(mApp, null, null, false, mAnnotation);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800185 }
186 }
187
Jeff Brown6f357d32014-01-15 20:40:55 -0800188 BroadcastQueue(ActivityManagerService service, Handler handler,
189 String name, long timeoutPeriod, boolean allowDelayBehindServices) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800190 mService = service;
Jeff Brown6f357d32014-01-15 20:40:55 -0800191 mHandler = new BroadcastHandler(handler.getLooper());
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800192 mQueueName = name;
193 mTimeoutPeriod = timeoutPeriod;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700194 mDelayBehindServices = allowDelayBehindServices;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800195 }
196
197 public boolean isPendingBroadcastProcessLocked(int pid) {
198 return mPendingBroadcast != null && mPendingBroadcast.curApp.pid == pid;
199 }
200
201 public void enqueueParallelBroadcastLocked(BroadcastRecord r) {
202 mParallelBroadcasts.add(r);
Jeff Brown9fb3fd12014-09-29 15:32:12 -0700203 r.enqueueClockTime = System.currentTimeMillis();
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800204 }
205
206 public void enqueueOrderedBroadcastLocked(BroadcastRecord r) {
207 mOrderedBroadcasts.add(r);
Jeff Brown9fb3fd12014-09-29 15:32:12 -0700208 r.enqueueClockTime = System.currentTimeMillis();
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800209 }
210
211 public final boolean replaceParallelBroadcastLocked(BroadcastRecord r) {
212 for (int i=mParallelBroadcasts.size()-1; i>=0; i--) {
213 if (r.intent.filterEquals(mParallelBroadcasts.get(i).intent)) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800214 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800215 "***** DROPPING PARALLEL ["
216 + mQueueName + "]: " + r.intent);
217 mParallelBroadcasts.set(i, r);
218 return true;
219 }
220 }
221 return false;
222 }
223
224 public final boolean replaceOrderedBroadcastLocked(BroadcastRecord r) {
225 for (int i=mOrderedBroadcasts.size()-1; i>0; i--) {
226 if (r.intent.filterEquals(mOrderedBroadcasts.get(i).intent)) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800227 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800228 "***** DROPPING ORDERED ["
229 + mQueueName + "]: " + r.intent);
230 mOrderedBroadcasts.set(i, r);
231 return true;
232 }
233 }
234 return false;
235 }
236
237 private final void processCurBroadcastLocked(BroadcastRecord r,
238 ProcessRecord app) throws RemoteException {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800239 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800240 "Process cur broadcast " + r + " for app " + app);
241 if (app.thread == null) {
242 throw new RemoteException();
243 }
244 r.receiver = app.thread.asBinder();
245 r.curApp = app;
246 app.curReceiver = r;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700247 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_RECEIVER);
Dianne Hackborndb926082013-10-31 16:32:44 -0700248 mService.updateLruProcessLocked(app, false, null);
249 mService.updateOomAdjLocked();
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800250
251 // Tell the application to launch this receiver.
252 r.intent.setComponent(r.curComponent);
253
254 boolean started = false;
255 try {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800256 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800257 "Delivering to component " + r.curComponent
258 + ": " + r);
259 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
260 app.thread.scheduleReceiver(new Intent(r.intent), r.curReceiver,
261 mService.compatibilityInfoForPackageLocked(r.curReceiver.applicationInfo),
Dianne Hackborna413dc02013-07-12 12:02:55 -0700262 r.resultCode, r.resultData, r.resultExtras, r.ordered, r.userId,
263 app.repProcState);
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800264 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800265 "Process cur broadcast " + r + " DELIVERED for app " + app);
266 started = true;
267 } finally {
268 if (!started) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800269 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800270 "Process cur broadcast " + r + ": NOT STARTED!");
271 r.receiver = null;
272 r.curApp = null;
273 app.curReceiver = null;
274 }
275 }
276 }
277
278 public boolean sendPendingBroadcastsLocked(ProcessRecord app) {
279 boolean didSomething = false;
280 final BroadcastRecord br = mPendingBroadcast;
281 if (br != null && br.curApp.pid == app.pid) {
282 try {
283 mPendingBroadcast = null;
284 processCurBroadcastLocked(br, app);
285 didSomething = true;
286 } catch (Exception e) {
287 Slog.w(TAG, "Exception in new application when starting receiver "
288 + br.curComponent.flattenToShortString(), e);
289 logBroadcastReceiverDiscardLocked(br);
290 finishReceiverLocked(br, br.resultCode, br.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700291 br.resultExtras, br.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800292 scheduleBroadcastsLocked();
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700293 // We need to reset the state if we failed to start the receiver.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800294 br.state = BroadcastRecord.IDLE;
295 throw new RuntimeException(e.getMessage());
296 }
297 }
298 return didSomething;
299 }
300
301 public void skipPendingBroadcastLocked(int pid) {
302 final BroadcastRecord br = mPendingBroadcast;
303 if (br != null && br.curApp.pid == pid) {
304 br.state = BroadcastRecord.IDLE;
305 br.nextReceiver = mPendingBroadcastRecvIndex;
306 mPendingBroadcast = null;
307 scheduleBroadcastsLocked();
308 }
309 }
310
311 public void skipCurrentReceiverLocked(ProcessRecord app) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800312 BroadcastRecord r = app.curReceiver;
Kenji Sugimoto4472fa972014-07-17 14:50:41 +0900313 if (r != null && r.queue == this) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800314 // The current broadcast is waiting for this app's receiver
315 // to be finished. Looks like that's not going to happen, so
316 // let the broadcast continue.
317 logBroadcastReceiverDiscardLocked(r);
318 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700319 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800320 }
riddle_hsu01eb7fa2015-03-04 17:27:05 +0800321 if (r == null && mPendingBroadcast != null && mPendingBroadcast.curApp == app) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800322 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800323 "[" + mQueueName + "] skip & discard pending app " + r);
riddle_hsu01eb7fa2015-03-04 17:27:05 +0800324 r = mPendingBroadcast;
325 }
326
327 if (r != null) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800328 logBroadcastReceiverDiscardLocked(r);
329 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700330 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800331 scheduleBroadcastsLocked();
332 }
333 }
334
335 public void scheduleBroadcastsLocked() {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800336 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Schedule broadcasts ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800337 + mQueueName + "]: current="
338 + mBroadcastsScheduled);
339
340 if (mBroadcastsScheduled) {
341 return;
342 }
343 mHandler.sendMessage(mHandler.obtainMessage(BROADCAST_INTENT_MSG, this));
344 mBroadcastsScheduled = true;
345 }
346
347 public BroadcastRecord getMatchingOrderedReceiver(IBinder receiver) {
348 if (mOrderedBroadcasts.size() > 0) {
349 final BroadcastRecord r = mOrderedBroadcasts.get(0);
350 if (r != null && r.receiver == receiver) {
351 return r;
352 }
353 }
354 return null;
355 }
356
357 public boolean finishReceiverLocked(BroadcastRecord r, int resultCode,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700358 String resultData, Bundle resultExtras, boolean resultAbort, boolean waitForServices) {
359 final int state = r.state;
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700360 final ActivityInfo receiver = r.curReceiver;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800361 r.state = BroadcastRecord.IDLE;
362 if (state == BroadcastRecord.IDLE) {
Dianne Hackborn6285a322013-09-18 12:09:47 -0700363 Slog.w(TAG, "finishReceiver [" + mQueueName + "] called but state is IDLE");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800364 }
365 r.receiver = null;
366 r.intent.setComponent(null);
Guobin Zhang04d0bb62014-03-07 17:47:10 +0800367 if (r.curApp != null && r.curApp.curReceiver == r) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800368 r.curApp.curReceiver = null;
369 }
370 if (r.curFilter != null) {
371 r.curFilter.receiverList.curBroadcast = null;
372 }
373 r.curFilter = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800374 r.curReceiver = null;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700375 r.curApp = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800376 mPendingBroadcast = null;
377
378 r.resultCode = resultCode;
379 r.resultData = resultData;
380 r.resultExtras = resultExtras;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700381 if (resultAbort && (r.intent.getFlags()&Intent.FLAG_RECEIVER_NO_ABORT) == 0) {
382 r.resultAbort = resultAbort;
383 } else {
384 r.resultAbort = false;
385 }
386
387 if (waitForServices && r.curComponent != null && r.queue.mDelayBehindServices
388 && r.queue.mOrderedBroadcasts.size() > 0
389 && r.queue.mOrderedBroadcasts.get(0) == r) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700390 ActivityInfo nextReceiver;
391 if (r.nextReceiver < r.receivers.size()) {
392 Object obj = r.receivers.get(r.nextReceiver);
393 nextReceiver = (obj instanceof ActivityInfo) ? (ActivityInfo)obj : null;
394 } else {
395 nextReceiver = null;
396 }
397 // Don't do this if the next receive is in the same process as the current one.
398 if (receiver == null || nextReceiver == null
399 || receiver.applicationInfo.uid != nextReceiver.applicationInfo.uid
400 || !receiver.processName.equals(nextReceiver.processName)) {
401 // In this case, we are ready to process the next receiver for the current broadcast,
402 // but are on a queue that would like to wait for services to finish before moving
403 // on. If there are background services currently starting, then we will go into a
404 // special state where we hold off on continuing this broadcast until they are done.
405 if (mService.mServices.hasBackgroundServices(r.userId)) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800406 Slog.i(TAG, "Delay finish: " + r.curComponent.flattenToShortString());
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700407 r.state = BroadcastRecord.WAITING_SERVICES;
408 return false;
409 }
Dianne Hackborn6285a322013-09-18 12:09:47 -0700410 }
411 }
412
413 r.curComponent = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800414
415 // We will process the next receiver right now if this is finishing
416 // an app receiver (which is always asynchronous) or after we have
417 // come back from calling a receiver.
418 return state == BroadcastRecord.APP_RECEIVE
419 || state == BroadcastRecord.CALL_DONE_RECEIVE;
420 }
421
Dianne Hackborn6285a322013-09-18 12:09:47 -0700422 public void backgroundServicesFinishedLocked(int userId) {
423 if (mOrderedBroadcasts.size() > 0) {
424 BroadcastRecord br = mOrderedBroadcasts.get(0);
425 if (br.userId == userId && br.state == BroadcastRecord.WAITING_SERVICES) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800426 Slog.i(TAG, "Resuming delayed broadcast");
Dianne Hackborn6285a322013-09-18 12:09:47 -0700427 br.curComponent = null;
428 br.state = BroadcastRecord.IDLE;
429 processNextBroadcast(false);
430 }
431 }
432 }
433
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800434 private static void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver,
435 Intent intent, int resultCode, String data, Bundle extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700436 boolean ordered, boolean sticky, int sendingUser) throws RemoteException {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800437 // Send the intent to the receiver asynchronously using one-way binder calls.
Craig Mautner38c1a5f2014-07-21 15:36:37 +0000438 if (app != null) {
439 if (app.thread != null) {
440 // If we have an app thread, do the call through that so it is
441 // correctly ordered with other one-way calls.
442 app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode,
443 data, extras, ordered, sticky, sendingUser, app.repProcState);
444 } else {
445 // Application has died. Receiver doesn't exist.
446 throw new RemoteException("app.thread must not be null");
447 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800448 } else {
Dianne Hackborn20e80982012-08-31 19:00:44 -0700449 receiver.performReceive(intent, resultCode, data, extras, ordered,
450 sticky, sendingUser);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800451 }
452 }
453
454 private final void deliverToRegisteredReceiverLocked(BroadcastRecord r,
455 BroadcastFilter filter, boolean ordered) {
456 boolean skip = false;
Amith Yamasani8bf06ed2012-08-27 19:30:30 -0700457 if (filter.requiredPermission != null) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800458 int perm = mService.checkComponentPermission(filter.requiredPermission,
459 r.callingPid, r.callingUid, -1, true);
460 if (perm != PackageManager.PERMISSION_GRANTED) {
461 Slog.w(TAG, "Permission Denial: broadcasting "
462 + r.intent.toString()
463 + " from " + r.callerPackage + " (pid="
464 + r.callingPid + ", uid=" + r.callingUid + ")"
465 + " requires " + filter.requiredPermission
466 + " due to registered receiver " + filter);
467 skip = true;
468 }
469 }
Dianne Hackbornb4163a62012-08-02 18:31:26 -0700470 if (!skip && r.requiredPermission != null) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800471 int perm = mService.checkComponentPermission(r.requiredPermission,
472 filter.receiverList.pid, filter.receiverList.uid, -1, true);
473 if (perm != PackageManager.PERMISSION_GRANTED) {
474 Slog.w(TAG, "Permission Denial: receiving "
475 + r.intent.toString()
476 + " to " + filter.receiverList.app
477 + " (pid=" + filter.receiverList.pid
478 + ", uid=" + filter.receiverList.uid + ")"
479 + " requires " + r.requiredPermission
480 + " due to sender " + r.callerPackage
481 + " (uid " + r.callingUid + ")");
482 skip = true;
483 }
484 }
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800485 if (r.appOp != AppOpsManager.OP_NONE) {
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700486 int mode = mService.mAppOpsService.noteOperation(r.appOp,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800487 filter.receiverList.uid, filter.packageName);
488 if (mode != AppOpsManager.MODE_ALLOWED) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800489 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800490 "App op " + r.appOp + " not allowed for broadcast to uid "
491 + filter.receiverList.uid + " pkg " + filter.packageName);
492 skip = true;
493 }
494 }
Ben Gruver49660c72013-08-06 19:54:08 -0700495 if (!skip) {
496 skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,
497 r.callingPid, r.resolvedType, filter.receiverList.uid);
498 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800499
Dianne Hackborn9357b112013-10-03 18:27:48 -0700500 if (filter.receiverList.app == null || filter.receiverList.app.crashing) {
501 Slog.w(TAG, "Skipping deliver [" + mQueueName + "] " + r
502 + " to " + filter.receiverList + ": process crashing");
503 skip = true;
504 }
505
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800506 if (!skip) {
507 // If this is not being sent as an ordered broadcast, then we
508 // don't want to touch the fields that keep track of the current
509 // state of ordered broadcasts.
510 if (ordered) {
511 r.receiver = filter.receiverList.receiver.asBinder();
512 r.curFilter = filter;
513 filter.receiverList.curBroadcast = r;
514 r.state = BroadcastRecord.CALL_IN_RECEIVE;
515 if (filter.receiverList.app != null) {
516 // Bump hosting application to no longer be in background
517 // scheduling class. Note that we can't do that if there
518 // isn't an app... but we can only be in that case for
519 // things that directly call the IActivityManager API, which
520 // are already core system stuff so don't matter for this.
521 r.curApp = filter.receiverList.app;
522 filter.receiverList.app.curReceiver = r;
Dianne Hackborn684bf342014-04-29 17:56:57 -0700523 mService.updateOomAdjLocked(r.curApp);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800524 }
525 }
526 try {
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -0700527 if (DEBUG_BROADCAST_LIGHT) Slog.i(TAG_BROADCAST,
528 "Delivering to " + filter + " : " + r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800529 performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver,
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -0700530 new Intent(r.intent), r.resultCode, r.resultData,
531 r.resultExtras, r.ordered, r.initialSticky, r.userId);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800532 if (ordered) {
533 r.state = BroadcastRecord.CALL_DONE_RECEIVE;
534 }
535 } catch (RemoteException e) {
536 Slog.w(TAG, "Failure sending broadcast " + r.intent, e);
537 if (ordered) {
538 r.receiver = null;
539 r.curFilter = null;
540 filter.receiverList.curBroadcast = null;
541 if (filter.receiverList.app != null) {
542 filter.receiverList.app.curReceiver = null;
543 }
544 }
545 }
546 }
547 }
548
549 final void processNextBroadcast(boolean fromMsg) {
550 synchronized(mService) {
551 BroadcastRecord r;
552
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800553 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "processNextBroadcast ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800554 + mQueueName + "]: "
555 + mParallelBroadcasts.size() + " broadcasts, "
556 + mOrderedBroadcasts.size() + " ordered broadcasts");
557
558 mService.updateCpuStats();
559
560 if (fromMsg) {
561 mBroadcastsScheduled = false;
562 }
563
564 // First, deliver any non-serialized broadcasts right away.
565 while (mParallelBroadcasts.size() > 0) {
566 r = mParallelBroadcasts.remove(0);
567 r.dispatchTime = SystemClock.uptimeMillis();
568 r.dispatchClockTime = System.currentTimeMillis();
569 final int N = r.receivers.size();
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800570 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Processing parallel broadcast ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800571 + mQueueName + "] " + r);
572 for (int i=0; i<N; i++) {
573 Object target = r.receivers.get(i);
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800574 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800575 "Delivering non-ordered on [" + mQueueName + "] to registered "
576 + target + ": " + r);
577 deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false);
578 }
579 addBroadcastToHistoryLocked(r);
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800580 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Done with parallel broadcast ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800581 + mQueueName + "] " + r);
582 }
583
584 // Now take care of the next serialized one...
585
586 // If we are waiting for a process to come up to handle the next
587 // broadcast, then do nothing at this point. Just in case, we
588 // check that the process we're waiting for still exists.
589 if (mPendingBroadcast != null) {
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -0700590 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST,
591 "processNextBroadcast [" + mQueueName + "]: waiting for "
592 + mPendingBroadcast.curApp);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800593
594 boolean isDead;
595 synchronized (mService.mPidsSelfLocked) {
Dianne Hackborn9357b112013-10-03 18:27:48 -0700596 ProcessRecord proc = mService.mPidsSelfLocked.get(mPendingBroadcast.curApp.pid);
597 isDead = proc == null || proc.crashing;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800598 }
599 if (!isDead) {
600 // It's still alive, so keep waiting
601 return;
602 } else {
603 Slog.w(TAG, "pending app ["
604 + mQueueName + "]" + mPendingBroadcast.curApp
605 + " died before responding to broadcast");
606 mPendingBroadcast.state = BroadcastRecord.IDLE;
607 mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex;
608 mPendingBroadcast = null;
609 }
610 }
611
612 boolean looped = false;
613
614 do {
615 if (mOrderedBroadcasts.size() == 0) {
616 // No more broadcasts pending, so all done!
617 mService.scheduleAppGcsLocked();
618 if (looped) {
619 // If we had finished the last ordered broadcast, then
620 // make sure all processes have correct oom and sched
621 // adjustments.
622 mService.updateOomAdjLocked();
623 }
624 return;
625 }
626 r = mOrderedBroadcasts.get(0);
627 boolean forceReceive = false;
628
629 // Ensure that even if something goes awry with the timeout
630 // detection, we catch "hung" broadcasts here, discard them,
631 // and continue to make progress.
632 //
633 // This is only done if the system is ready so that PRE_BOOT_COMPLETED
634 // receivers don't get executed with timeouts. They're intended for
635 // one time heavy lifting after system upgrades and can take
636 // significant amounts of time.
637 int numReceivers = (r.receivers != null) ? r.receivers.size() : 0;
638 if (mService.mProcessesReady && r.dispatchTime > 0) {
639 long now = SystemClock.uptimeMillis();
640 if ((numReceivers > 0) &&
641 (now > r.dispatchTime + (2*mTimeoutPeriod*numReceivers))) {
642 Slog.w(TAG, "Hung broadcast ["
643 + mQueueName + "] discarded after timeout failure:"
644 + " now=" + now
645 + " dispatchTime=" + r.dispatchTime
646 + " startTime=" + r.receiverTime
647 + " intent=" + r.intent
648 + " numReceivers=" + numReceivers
649 + " nextReceiver=" + r.nextReceiver
650 + " state=" + r.state);
651 broadcastTimeoutLocked(false); // forcibly finish this broadcast
652 forceReceive = true;
653 r.state = BroadcastRecord.IDLE;
654 }
655 }
656
657 if (r.state != BroadcastRecord.IDLE) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800658 if (DEBUG_BROADCAST) Slog.d(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800659 "processNextBroadcast("
660 + mQueueName + ") called when not idle (state="
661 + r.state + ")");
662 return;
663 }
664
665 if (r.receivers == null || r.nextReceiver >= numReceivers
666 || r.resultAbort || forceReceive) {
667 // No more receivers for this broadcast! Send the final
668 // result if requested...
669 if (r.resultTo != null) {
670 try {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800671 if (DEBUG_BROADCAST) Slog.i(TAG_BROADCAST,
Todd Kennedyd2f15112015-01-21 15:25:56 -0800672 "Finishing broadcast [" + mQueueName + "] "
673 + r.intent.getAction() + " app=" + r.callerApp);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800674 performReceiveLocked(r.callerApp, r.resultTo,
675 new Intent(r.intent), r.resultCode,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700676 r.resultData, r.resultExtras, false, false, r.userId);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800677 // Set this to null so that the reference
Dianne Hackborn9357b112013-10-03 18:27:48 -0700678 // (local and remote) isn't kept in the mBroadcastHistory.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800679 r.resultTo = null;
680 } catch (RemoteException e) {
Craig Mautner38c1a5f2014-07-21 15:36:37 +0000681 r.resultTo = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800682 Slog.w(TAG, "Failure ["
683 + mQueueName + "] sending broadcast result of "
684 + r.intent, e);
685 }
686 }
687
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800688 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Cancelling BROADCAST_TIMEOUT_MSG");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800689 cancelBroadcastTimeoutLocked();
690
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -0700691 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST,
692 "Finished with ordered broadcast " + r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800693
694 // ... and on to the next...
695 addBroadcastToHistoryLocked(r);
696 mOrderedBroadcasts.remove(0);
697 r = null;
698 looped = true;
699 continue;
700 }
701 } while (r == null);
702
703 // Get the next receiver...
704 int recIdx = r.nextReceiver++;
705
706 // Keep track of when this receiver started, and make sure there
707 // is a timeout message pending to kill it if need be.
708 r.receiverTime = SystemClock.uptimeMillis();
709 if (recIdx == 0) {
710 r.dispatchTime = r.receiverTime;
711 r.dispatchClockTime = System.currentTimeMillis();
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800712 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Processing ordered broadcast ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800713 + mQueueName + "] " + r);
714 }
715 if (! mPendingBroadcastTimeoutMessage) {
716 long timeoutTime = r.receiverTime + mTimeoutPeriod;
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800717 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800718 "Submitting BROADCAST_TIMEOUT_MSG ["
719 + mQueueName + "] for " + r + " at " + timeoutTime);
720 setBroadcastTimeoutLocked(timeoutTime);
721 }
722
723 Object nextReceiver = r.receivers.get(recIdx);
724 if (nextReceiver instanceof BroadcastFilter) {
725 // Simple case: this is a registered receiver who gets
726 // a direct call.
727 BroadcastFilter filter = (BroadcastFilter)nextReceiver;
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800728 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800729 "Delivering ordered ["
730 + mQueueName + "] to registered "
731 + filter + ": " + r);
732 deliverToRegisteredReceiverLocked(r, filter, r.ordered);
733 if (r.receiver == null || !r.ordered) {
734 // The receiver has already finished, so schedule to
735 // process the next one.
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800736 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Quick finishing ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800737 + mQueueName + "]: ordered="
738 + r.ordered + " receiver=" + r.receiver);
739 r.state = BroadcastRecord.IDLE;
740 scheduleBroadcastsLocked();
741 }
742 return;
743 }
744
745 // Hard case: need to instantiate the receiver, possibly
746 // starting its application process to host it.
747
748 ResolveInfo info =
749 (ResolveInfo)nextReceiver;
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700750 ComponentName component = new ComponentName(
751 info.activityInfo.applicationInfo.packageName,
752 info.activityInfo.name);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800753
754 boolean skip = false;
755 int perm = mService.checkComponentPermission(info.activityInfo.permission,
756 r.callingPid, r.callingUid, info.activityInfo.applicationInfo.uid,
757 info.activityInfo.exported);
758 if (perm != PackageManager.PERMISSION_GRANTED) {
759 if (!info.activityInfo.exported) {
760 Slog.w(TAG, "Permission Denial: broadcasting "
761 + r.intent.toString()
762 + " from " + r.callerPackage + " (pid=" + r.callingPid
763 + ", uid=" + r.callingUid + ")"
764 + " is not exported from uid " + info.activityInfo.applicationInfo.uid
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700765 + " due to receiver " + component.flattenToShortString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800766 } else {
767 Slog.w(TAG, "Permission Denial: broadcasting "
768 + r.intent.toString()
769 + " from " + r.callerPackage + " (pid=" + r.callingPid
770 + ", uid=" + r.callingUid + ")"
771 + " requires " + info.activityInfo.permission
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700772 + " due to receiver " + component.flattenToShortString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800773 }
774 skip = true;
775 }
776 if (info.activityInfo.applicationInfo.uid != Process.SYSTEM_UID &&
777 r.requiredPermission != null) {
778 try {
779 perm = AppGlobals.getPackageManager().
780 checkPermission(r.requiredPermission,
Svet Ganov4f2dcfd2015-04-22 02:14:47 -0700781 info.activityInfo.applicationInfo.packageName,
782 UserHandle.getUserId(info.activityInfo.applicationInfo.uid));
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800783 } catch (RemoteException e) {
784 perm = PackageManager.PERMISSION_DENIED;
785 }
786 if (perm != PackageManager.PERMISSION_GRANTED) {
787 Slog.w(TAG, "Permission Denial: receiving "
788 + r.intent + " to "
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700789 + component.flattenToShortString()
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800790 + " requires " + r.requiredPermission
791 + " due to sender " + r.callerPackage
792 + " (uid " + r.callingUid + ")");
793 skip = true;
794 }
795 }
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800796 if (r.appOp != AppOpsManager.OP_NONE) {
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700797 int mode = mService.mAppOpsService.noteOperation(r.appOp,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800798 info.activityInfo.applicationInfo.uid, info.activityInfo.packageName);
799 if (mode != AppOpsManager.MODE_ALLOWED) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800800 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800801 "App op " + r.appOp + " not allowed for broadcast to uid "
802 + info.activityInfo.applicationInfo.uid + " pkg "
803 + info.activityInfo.packageName);
804 skip = true;
805 }
806 }
Ben Gruver49660c72013-08-06 19:54:08 -0700807 if (!skip) {
808 skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,
809 r.callingPid, r.resolvedType, info.activityInfo.applicationInfo.uid);
810 }
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700811 boolean isSingleton = false;
812 try {
813 isSingleton = mService.isSingleton(info.activityInfo.processName,
814 info.activityInfo.applicationInfo,
815 info.activityInfo.name, info.activityInfo.flags);
816 } catch (SecurityException e) {
817 Slog.w(TAG, e.getMessage());
818 skip = true;
819 }
820 if ((info.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) {
821 if (ActivityManager.checkUidPermission(
822 android.Manifest.permission.INTERACT_ACROSS_USERS,
823 info.activityInfo.applicationInfo.uid)
824 != PackageManager.PERMISSION_GRANTED) {
825 Slog.w(TAG, "Permission Denial: Receiver " + component.flattenToShortString()
826 + " requests FLAG_SINGLE_USER, but app does not hold "
827 + android.Manifest.permission.INTERACT_ACROSS_USERS);
828 skip = true;
829 }
830 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800831 if (r.curApp != null && r.curApp.crashing) {
832 // If the target process is crashing, just skip it.
Dianne Hackborn9357b112013-10-03 18:27:48 -0700833 Slog.w(TAG, "Skipping deliver ordered [" + mQueueName + "] " + r
834 + " to " + r.curApp + ": process crashing");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800835 skip = true;
836 }
Christopher Tateba629da2013-11-13 17:42:28 -0800837 if (!skip) {
838 boolean isAvailable = false;
839 try {
840 isAvailable = AppGlobals.getPackageManager().isPackageAvailable(
841 info.activityInfo.packageName,
842 UserHandle.getUserId(info.activityInfo.applicationInfo.uid));
843 } catch (Exception e) {
844 // all such failures mean we skip this receiver
845 Slog.w(TAG, "Exception getting recipient info for "
846 + info.activityInfo.packageName, e);
847 }
848 if (!isAvailable) {
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -0700849 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
850 "Skipping delivery to " + info.activityInfo.packageName + " / "
851 + info.activityInfo.applicationInfo.uid
852 + " : package no longer available");
Christopher Tateba629da2013-11-13 17:42:28 -0800853 skip = true;
854 }
855 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800856
857 if (skip) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800858 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -0700859 "Skipping delivery of ordered [" + mQueueName + "] "
860 + r + " for whatever reason");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800861 r.receiver = null;
862 r.curFilter = null;
863 r.state = BroadcastRecord.IDLE;
864 scheduleBroadcastsLocked();
865 return;
866 }
867
868 r.state = BroadcastRecord.APP_RECEIVE;
869 String targetProcess = info.activityInfo.processName;
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700870 r.curComponent = component;
Amith Yamasani4b9d79c2014-05-21 19:14:21 -0700871 final int receiverUid = info.activityInfo.applicationInfo.uid;
872 // If it's a singleton, it needs to be the same app or a special app
873 if (r.callingUid != Process.SYSTEM_UID && isSingleton
874 && mService.isValidSingletonCall(r.callingUid, receiverUid)) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700875 info.activityInfo = mService.getActivityInfoForUser(info.activityInfo, 0);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800876 }
877 r.curReceiver = info.activityInfo;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700878 if (DEBUG_MU && r.callingUid > UserHandle.PER_USER_RANGE) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800879 Slog.v(TAG_MU, "Updated broadcast record activity info for secondary user, "
880 + info.activityInfo + ", callingUid = " + r.callingUid + ", uid = "
881 + info.activityInfo.applicationInfo.uid);
882 }
883
884 // Broadcast is being executed, its package can't be stopped.
885 try {
886 AppGlobals.getPackageManager().setPackageStoppedState(
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700887 r.curComponent.getPackageName(), false, UserHandle.getUserId(r.callingUid));
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800888 } catch (RemoteException e) {
889 } catch (IllegalArgumentException e) {
890 Slog.w(TAG, "Failed trying to unstop package "
891 + r.curComponent.getPackageName() + ": " + e);
892 }
893
894 // Is this receiver's application already running?
895 ProcessRecord app = mService.getProcessRecordLocked(targetProcess,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700896 info.activityInfo.applicationInfo.uid, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800897 if (app != null && app.thread != null) {
898 try {
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700899 app.addPackage(info.activityInfo.packageName,
900 info.activityInfo.applicationInfo.versionCode, mService.mProcessStats);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800901 processCurBroadcastLocked(r, app);
902 return;
903 } catch (RemoteException e) {
904 Slog.w(TAG, "Exception when sending broadcast to "
905 + r.curComponent, e);
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800906 } catch (RuntimeException e) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700907 Slog.wtf(TAG, "Failed sending broadcast to "
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800908 + r.curComponent + " with " + r.intent, e);
909 // If some unexpected exception happened, just skip
910 // this broadcast. At this point we are not in the call
911 // from a client, so throwing an exception out from here
912 // will crash the entire system instead of just whoever
913 // sent the broadcast.
914 logBroadcastReceiverDiscardLocked(r);
915 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700916 r.resultExtras, r.resultAbort, false);
Dianne Hackborn6c5406a2012-11-29 16:18:01 -0800917 scheduleBroadcastsLocked();
918 // We need to reset the state if we failed to start the receiver.
919 r.state = BroadcastRecord.IDLE;
920 return;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800921 }
922
923 // If a dead object exception was thrown -- fall through to
924 // restart the application.
925 }
926
927 // Not running -- get it started, to be executed when the app comes up.
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800928 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800929 "Need to start app ["
930 + mQueueName + "] " + targetProcess + " for broadcast " + r);
931 if ((r.curApp=mService.startProcessLocked(targetProcess,
932 info.activityInfo.applicationInfo, true,
933 r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND,
934 "broadcast", r.curComponent,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700935 (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0, false, false))
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800936 == null) {
937 // Ah, this recipient is unavailable. Finish it if necessary,
938 // and mark the broadcast record as ready for the next.
939 Slog.w(TAG, "Unable to launch app "
940 + info.activityInfo.applicationInfo.packageName + "/"
941 + info.activityInfo.applicationInfo.uid + " for broadcast "
942 + r.intent + ": process is bad");
943 logBroadcastReceiverDiscardLocked(r);
944 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700945 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800946 scheduleBroadcastsLocked();
947 r.state = BroadcastRecord.IDLE;
948 return;
949 }
950
951 mPendingBroadcast = r;
952 mPendingBroadcastRecvIndex = recIdx;
953 }
954 }
955
956 final void setBroadcastTimeoutLocked(long timeoutTime) {
957 if (! mPendingBroadcastTimeoutMessage) {
958 Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG, this);
959 mHandler.sendMessageAtTime(msg, timeoutTime);
960 mPendingBroadcastTimeoutMessage = true;
961 }
962 }
963
964 final void cancelBroadcastTimeoutLocked() {
965 if (mPendingBroadcastTimeoutMessage) {
966 mHandler.removeMessages(BROADCAST_TIMEOUT_MSG, this);
967 mPendingBroadcastTimeoutMessage = false;
968 }
969 }
970
971 final void broadcastTimeoutLocked(boolean fromMsg) {
972 if (fromMsg) {
973 mPendingBroadcastTimeoutMessage = false;
974 }
975
976 if (mOrderedBroadcasts.size() == 0) {
977 return;
978 }
979
980 long now = SystemClock.uptimeMillis();
981 BroadcastRecord r = mOrderedBroadcasts.get(0);
982 if (fromMsg) {
983 if (mService.mDidDexOpt) {
984 // Delay timeouts until dexopt finishes.
985 mService.mDidDexOpt = false;
986 long timeoutTime = SystemClock.uptimeMillis() + mTimeoutPeriod;
987 setBroadcastTimeoutLocked(timeoutTime);
988 return;
989 }
990 if (!mService.mProcessesReady) {
991 // Only process broadcast timeouts if the system is ready. That way
992 // PRE_BOOT_COMPLETED broadcasts can't timeout as they are intended
993 // to do heavy lifting for system up.
994 return;
995 }
996
997 long timeoutTime = r.receiverTime + mTimeoutPeriod;
998 if (timeoutTime > now) {
999 // We can observe premature timeouts because we do not cancel and reset the
1000 // broadcast timeout message after each receiver finishes. Instead, we set up
1001 // an initial timeout then kick it down the road a little further as needed
1002 // when it expires.
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001003 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001004 "Premature timeout ["
1005 + mQueueName + "] @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for "
1006 + timeoutTime);
1007 setBroadcastTimeoutLocked(timeoutTime);
1008 return;
1009 }
1010 }
1011
Dianne Hackborn6285a322013-09-18 12:09:47 -07001012 BroadcastRecord br = mOrderedBroadcasts.get(0);
1013 if (br.state == BroadcastRecord.WAITING_SERVICES) {
1014 // In this case the broadcast had already finished, but we had decided to wait
1015 // for started services to finish as well before going on. So if we have actually
1016 // waited long enough time timeout the broadcast, let's give up on the whole thing
1017 // and just move on to the next.
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001018 Slog.i(TAG, "Waited long enough for: " + (br.curComponent != null
Dianne Hackborn6285a322013-09-18 12:09:47 -07001019 ? br.curComponent.flattenToShortString() : "(null)"));
1020 br.curComponent = null;
1021 br.state = BroadcastRecord.IDLE;
1022 processNextBroadcast(false);
1023 return;
1024 }
1025
1026 Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r. receiver
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001027 + ", started " + (now - r.receiverTime) + "ms ago");
1028 r.receiverTime = now;
1029 r.anrCount++;
1030
1031 // Current receiver has passed its expiration date.
1032 if (r.nextReceiver <= 0) {
1033 Slog.w(TAG, "Timeout on receiver with nextReceiver <= 0");
1034 return;
1035 }
1036
1037 ProcessRecord app = null;
1038 String anrMessage = null;
1039
1040 Object curReceiver = r.receivers.get(r.nextReceiver-1);
1041 Slog.w(TAG, "Receiver during timeout: " + curReceiver);
1042 logBroadcastReceiverDiscardLocked(r);
1043 if (curReceiver instanceof BroadcastFilter) {
1044 BroadcastFilter bf = (BroadcastFilter)curReceiver;
1045 if (bf.receiverList.pid != 0
1046 && bf.receiverList.pid != ActivityManagerService.MY_PID) {
1047 synchronized (mService.mPidsSelfLocked) {
1048 app = mService.mPidsSelfLocked.get(
1049 bf.receiverList.pid);
1050 }
1051 }
1052 } else {
1053 app = r.curApp;
1054 }
1055
1056 if (app != null) {
1057 anrMessage = "Broadcast of " + r.intent.toString();
1058 }
1059
1060 if (mPendingBroadcast == r) {
1061 mPendingBroadcast = null;
1062 }
1063
1064 // Move on to the next receiver.
1065 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -07001066 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001067 scheduleBroadcastsLocked();
1068
1069 if (anrMessage != null) {
1070 // Post the ANR to the handler since we do not want to process ANRs while
1071 // potentially holding our lock.
1072 mHandler.post(new AppNotResponding(app, anrMessage));
1073 }
1074 }
1075
Christopher Tatef278f122015-04-22 13:12:01 -07001076 private final int ringAdvance(int x, final int increment, final int ringSize) {
1077 x += increment;
1078 if (x < 0) return (ringSize - 1);
1079 else if (x >= ringSize) return 0;
1080 else return x;
1081 }
1082
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001083 private final void addBroadcastToHistoryLocked(BroadcastRecord r) {
1084 if (r.callingUid < 0) {
1085 // This was from a registerReceiver() call; ignore it.
1086 return;
1087 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001088 r.finishTime = SystemClock.uptimeMillis();
Christopher Tatef278f122015-04-22 13:12:01 -07001089
1090 mBroadcastHistory[mHistoryNext] = r;
1091 mHistoryNext = ringAdvance(mHistoryNext, 1, MAX_BROADCAST_HISTORY);
1092
1093 mBroadcastSummaryHistory[mSummaryHistoryNext] = r.intent;
1094 mSummaryHistoryEnqueueTime[mSummaryHistoryNext] = r.enqueueClockTime;
1095 mSummaryHistoryDispatchTime[mSummaryHistoryNext] = r.dispatchClockTime;
1096 mSummaryHistoryFinishTime[mSummaryHistoryNext] = System.currentTimeMillis();
1097 mSummaryHistoryNext = ringAdvance(mSummaryHistoryNext, 1, MAX_BROADCAST_SUMMARY_HISTORY);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001098 }
1099
1100 final void logBroadcastReceiverDiscardLocked(BroadcastRecord r) {
1101 if (r.nextReceiver > 0) {
1102 Object curReceiver = r.receivers.get(r.nextReceiver-1);
1103 if (curReceiver instanceof BroadcastFilter) {
1104 BroadcastFilter bf = (BroadcastFilter) curReceiver;
1105 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001106 bf.owningUserId, System.identityHashCode(r),
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001107 r.intent.getAction(),
1108 r.nextReceiver - 1,
1109 System.identityHashCode(bf));
1110 } else {
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001111 ResolveInfo ri = (ResolveInfo)curReceiver;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001112 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001113 UserHandle.getUserId(ri.activityInfo.applicationInfo.uid),
1114 System.identityHashCode(r), r.intent.getAction(),
1115 r.nextReceiver - 1, ri.toString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001116 }
1117 } else {
1118 Slog.w(TAG, "Discarding broadcast before first receiver is invoked: "
1119 + r);
1120 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001121 -1, System.identityHashCode(r),
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001122 r.intent.getAction(),
1123 r.nextReceiver,
1124 "NONE");
1125 }
1126 }
1127
1128 final boolean dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args,
1129 int opti, boolean dumpAll, String dumpPackage, boolean needSep) {
1130 if (mParallelBroadcasts.size() > 0 || mOrderedBroadcasts.size() > 0
1131 || mPendingBroadcast != null) {
1132 boolean printed = false;
1133 for (int i=mParallelBroadcasts.size()-1; i>=0; i--) {
1134 BroadcastRecord br = mParallelBroadcasts.get(i);
1135 if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) {
1136 continue;
1137 }
1138 if (!printed) {
1139 if (needSep) {
1140 pw.println();
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001141 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001142 needSep = true;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001143 printed = true;
1144 pw.println(" Active broadcasts [" + mQueueName + "]:");
1145 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001146 pw.println(" Active Broadcast " + mQueueName + " #" + i + ":");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001147 br.dump(pw, " ");
1148 }
1149 printed = false;
1150 needSep = true;
1151 for (int i=mOrderedBroadcasts.size()-1; i>=0; i--) {
1152 BroadcastRecord br = mOrderedBroadcasts.get(i);
1153 if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) {
1154 continue;
1155 }
1156 if (!printed) {
1157 if (needSep) {
1158 pw.println();
1159 }
1160 needSep = true;
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001161 printed = true;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001162 pw.println(" Active ordered broadcasts [" + mQueueName + "]:");
1163 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001164 pw.println(" Active Ordered Broadcast " + mQueueName + " #" + i + ":");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001165 mOrderedBroadcasts.get(i).dump(pw, " ");
1166 }
1167 if (dumpPackage == null || (mPendingBroadcast != null
1168 && dumpPackage.equals(mPendingBroadcast.callerPackage))) {
1169 if (needSep) {
1170 pw.println();
1171 }
1172 pw.println(" Pending broadcast [" + mQueueName + "]:");
1173 if (mPendingBroadcast != null) {
1174 mPendingBroadcast.dump(pw, " ");
1175 } else {
1176 pw.println(" (null)");
1177 }
1178 needSep = true;
1179 }
1180 }
1181
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001182 int i;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001183 boolean printed = false;
Christopher Tatef278f122015-04-22 13:12:01 -07001184
1185 i = -1;
1186 int lastIndex = mHistoryNext;
1187 int ringIndex = lastIndex;
1188 do {
1189 // increasing index = more recent entry, and we want to print the most
1190 // recent first and work backwards, so we roll through the ring backwards.
1191 ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_HISTORY);
1192 BroadcastRecord r = mBroadcastHistory[ringIndex];
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001193 if (r == null) {
Christopher Tatef278f122015-04-22 13:12:01 -07001194 continue;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001195 }
Christopher Tatef278f122015-04-22 13:12:01 -07001196
1197 i++; // genuine record of some sort even if we're filtering it out
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001198 if (dumpPackage != null && !dumpPackage.equals(r.callerPackage)) {
1199 continue;
1200 }
1201 if (!printed) {
1202 if (needSep) {
1203 pw.println();
1204 }
1205 needSep = true;
1206 pw.println(" Historical broadcasts [" + mQueueName + "]:");
1207 printed = true;
1208 }
1209 if (dumpAll) {
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001210 pw.print(" Historical Broadcast " + mQueueName + " #");
1211 pw.print(i); pw.println(":");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001212 r.dump(pw, " ");
1213 } else {
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001214 pw.print(" #"); pw.print(i); pw.print(": "); pw.println(r);
1215 pw.print(" ");
1216 pw.println(r.intent.toShortString(false, true, true, false));
Dianne Hackborna40cfeb2013-03-25 17:49:36 -07001217 if (r.targetComp != null && r.targetComp != r.intent.getComponent()) {
1218 pw.print(" targetComp: "); pw.println(r.targetComp.toShortString());
1219 }
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001220 Bundle bundle = r.intent.getExtras();
1221 if (bundle != null) {
1222 pw.print(" extras: "); pw.println(bundle.toString());
1223 }
1224 }
Christopher Tatef278f122015-04-22 13:12:01 -07001225 } while (ringIndex != lastIndex);
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001226
1227 if (dumpPackage == null) {
Christopher Tatef278f122015-04-22 13:12:01 -07001228 lastIndex = ringIndex = mSummaryHistoryNext;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001229 if (dumpAll) {
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001230 printed = false;
Christopher Tatef278f122015-04-22 13:12:01 -07001231 i = -1;
1232 } else {
1233 // roll over the 'i' full dumps that have already been issued
1234 for (int j = i;
1235 j > 0 && ringIndex != lastIndex;) {
1236 ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_SUMMARY_HISTORY);
1237 BroadcastRecord r = mBroadcastHistory[ringIndex];
1238 if (r == null) {
1239 continue;
1240 }
1241 j--;
1242 }
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001243 }
Christopher Tatef278f122015-04-22 13:12:01 -07001244 // done skipping; dump the remainder of the ring. 'i' is still the ordinal within
1245 // the overall broadcast history.
1246 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
1247 do {
1248 ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_SUMMARY_HISTORY);
1249 Intent intent = mBroadcastSummaryHistory[ringIndex];
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001250 if (intent == null) {
Christopher Tatef278f122015-04-22 13:12:01 -07001251 continue;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001252 }
1253 if (!printed) {
1254 if (needSep) {
1255 pw.println();
1256 }
1257 needSep = true;
1258 pw.println(" Historical broadcasts summary [" + mQueueName + "]:");
1259 printed = true;
1260 }
1261 if (!dumpAll && i >= 50) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001262 pw.println(" ...");
1263 break;
1264 }
Christopher Tatef278f122015-04-22 13:12:01 -07001265 i++;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001266 pw.print(" #"); pw.print(i); pw.print(": ");
1267 pw.println(intent.toShortString(false, true, true, false));
Christopher Tatef278f122015-04-22 13:12:01 -07001268 pw.print(" enq="); pw.print(sdf.format(new Date(mSummaryHistoryEnqueueTime[ringIndex])));
1269 pw.print(" disp="); pw.print(sdf.format(new Date(mSummaryHistoryDispatchTime[ringIndex])));
1270 pw.print(" fin="); pw.println(sdf.format(new Date(mSummaryHistoryFinishTime[ringIndex])));
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001271 Bundle bundle = intent.getExtras();
1272 if (bundle != null) {
1273 pw.print(" extras: "); pw.println(bundle.toString());
1274 }
Christopher Tatef278f122015-04-22 13:12:01 -07001275 } while (ringIndex != lastIndex);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001276 }
1277
1278 return needSep;
1279 }
1280}