blob: d83545491d2d23ad77cf1297f58e8775672fe451 [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
Dianne Hackbornb8633f32017-04-11 17:38:42 -070019import android.content.pm.IPackageManager;
20import android.content.pm.PermissionInfo;
Carmen Jacksona68e3452017-01-17 14:01:33 -080021import android.os.Trace;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080022import java.io.FileDescriptor;
23import java.io.PrintWriter;
Christopher Tatef278f122015-04-22 13:12:01 -070024import java.text.SimpleDateFormat;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080025import java.util.ArrayList;
Christopher Tatef278f122015-04-22 13:12:01 -070026import java.util.Date;
Wale Ogunwaleca1c1252015-05-15 12:49:13 -070027import java.util.Set;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080028
Dianne Hackborn7d19e022012-08-07 19:12:33 -070029import android.app.ActivityManager;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080030import android.app.AppGlobals;
Dianne Hackbornf51f6122013-02-04 18:23:34 -080031import android.app.AppOpsManager;
Dianne Hackborna750a632015-06-16 17:18:23 -070032import android.app.BroadcastOptions;
Svet Ganov9c165d72015-12-01 19:52:26 -080033import android.app.PendingIntent;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080034import android.content.ComponentName;
35import android.content.IIntentReceiver;
Svet Ganov9c165d72015-12-01 19:52:26 -080036import android.content.IIntentSender;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080037import android.content.Intent;
Svet Ganov9c165d72015-12-01 19:52:26 -080038import android.content.IntentSender;
Dianne Hackborn7d19e022012-08-07 19:12:33 -070039import android.content.pm.ActivityInfo;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080040import android.content.pm.PackageManager;
41import android.content.pm.ResolveInfo;
42import android.os.Bundle;
43import android.os.Handler;
44import android.os.IBinder;
Jeff Brown6f357d32014-01-15 20:40:55 -080045import android.os.Looper;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080046import android.os.Message;
47import android.os.Process;
48import android.os.RemoteException;
49import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070050import android.os.UserHandle;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080051import android.util.EventLog;
52import android.util.Slog;
Dianne Hackborn865907d2015-10-21 17:12:53 -070053import android.util.TimeUtils;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080054
Wale Ogunwaled57969f2014-11-15 19:37:29 -080055import static com.android.server.am.ActivityManagerDebugConfig.*;
56
Dianne Hackborn40c8db52012-02-10 18:59:48 -080057/**
58 * BROADCASTS
59 *
60 * We keep two broadcast queues and associated bookkeeping, one for those at
61 * foreground priority, and one for normal (background-priority) broadcasts.
62 */
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070063public final class BroadcastQueue {
Wale Ogunwalebfac4682015-04-08 14:33:21 -070064 private static final String TAG = "BroadcastQueue";
Wale Ogunwaled57969f2014-11-15 19:37:29 -080065 private static final String TAG_MU = TAG + POSTFIX_MU;
66 private static final String TAG_BROADCAST = TAG + POSTFIX_BROADCAST;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080067
Dianne Hackborn4c51de42013-10-16 23:34:35 -070068 static final int MAX_BROADCAST_HISTORY = ActivityManager.isLowRamDeviceStatic() ? 10 : 50;
Dianne Hackborn6285a322013-09-18 12:09:47 -070069 static final int MAX_BROADCAST_SUMMARY_HISTORY
Dianne Hackborn4c51de42013-10-16 23:34:35 -070070 = ActivityManager.isLowRamDeviceStatic() ? 25 : 300;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080071
72 final ActivityManagerService mService;
73
74 /**
75 * Recognizable moniker for this queue
76 */
77 final String mQueueName;
78
79 /**
80 * Timeout period for this queue's broadcasts
81 */
82 final long mTimeoutPeriod;
83
84 /**
Dianne Hackborn6285a322013-09-18 12:09:47 -070085 * If true, we can delay broadcasts while waiting services to finish in the previous
86 * receiver's process.
87 */
88 final boolean mDelayBehindServices;
89
90 /**
Dianne Hackborn40c8db52012-02-10 18:59:48 -080091 * Lists of all active broadcasts that are to be executed immediately
92 * (without waiting for another broadcast to finish). Currently this only
93 * contains broadcasts to registered receivers, to avoid spinning up
94 * a bunch of processes to execute IntentReceiver components. Background-
95 * and foreground-priority broadcasts are queued separately.
96 */
Wale Ogunwale540e1232015-05-01 15:35:39 -070097 final ArrayList<BroadcastRecord> mParallelBroadcasts = new ArrayList<>();
Dianne Hackborn6285a322013-09-18 12:09:47 -070098
Dianne Hackborn40c8db52012-02-10 18:59:48 -080099 /**
100 * List of all active broadcasts that are to be executed one at a time.
101 * The object at the top of the list is the currently activity broadcasts;
102 * those after it are waiting for the top to finish. As with parallel
103 * broadcasts, separate background- and foreground-priority queues are
104 * maintained.
105 */
Wale Ogunwale540e1232015-05-01 15:35:39 -0700106 final ArrayList<BroadcastRecord> mOrderedBroadcasts = new ArrayList<>();
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800107
108 /**
Christopher Tatef278f122015-04-22 13:12:01 -0700109 * Historical data of past broadcasts, for debugging. This is a ring buffer
110 * whose last element is at mHistoryNext.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800111 */
Dianne Hackborn6285a322013-09-18 12:09:47 -0700112 final BroadcastRecord[] mBroadcastHistory = new BroadcastRecord[MAX_BROADCAST_HISTORY];
Christopher Tatef278f122015-04-22 13:12:01 -0700113 int mHistoryNext = 0;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800114
115 /**
Christopher Tatef278f122015-04-22 13:12:01 -0700116 * Summary of historical data of past broadcasts, for debugging. This is a
117 * ring buffer whose last element is at mSummaryHistoryNext.
Dianne Hackbornc0bd7472012-10-09 14:00:30 -0700118 */
Dianne Hackborn6285a322013-09-18 12:09:47 -0700119 final Intent[] mBroadcastSummaryHistory = new Intent[MAX_BROADCAST_SUMMARY_HISTORY];
Christopher Tatef278f122015-04-22 13:12:01 -0700120 int mSummaryHistoryNext = 0;
121
122 /**
123 * Various milestone timestamps of entries in the mBroadcastSummaryHistory ring
124 * buffer, also tracked via the mSummaryHistoryNext index. These are all in wall
125 * clock time, not elapsed.
126 */
127 final long[] mSummaryHistoryEnqueueTime = new long[MAX_BROADCAST_SUMMARY_HISTORY];
128 final long[] mSummaryHistoryDispatchTime = new long[MAX_BROADCAST_SUMMARY_HISTORY];
129 final long[] mSummaryHistoryFinishTime = new long[MAX_BROADCAST_SUMMARY_HISTORY];
Dianne Hackbornc0bd7472012-10-09 14:00:30 -0700130
131 /**
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800132 * Set when we current have a BROADCAST_INTENT_MSG in flight.
133 */
134 boolean mBroadcastsScheduled = false;
135
136 /**
137 * True if we have a pending unexpired BROADCAST_TIMEOUT_MSG posted to our handler.
138 */
139 boolean mPendingBroadcastTimeoutMessage;
140
141 /**
142 * Intent broadcasts that we have tried to start, but are
143 * waiting for the application's process to be created. We only
144 * need one per scheduling class (instead of a list) because we always
145 * process broadcasts one at a time, so no others can be started while
146 * waiting for this one.
147 */
148 BroadcastRecord mPendingBroadcast = null;
149
150 /**
151 * The receiver index that is pending, to restart the broadcast if needed.
152 */
153 int mPendingBroadcastRecvIndex;
154
155 static final int BROADCAST_INTENT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG;
156 static final int BROADCAST_TIMEOUT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG + 1;
157
Jeff Brown6f357d32014-01-15 20:40:55 -0800158 final BroadcastHandler mHandler;
159
160 private final class BroadcastHandler extends Handler {
161 public BroadcastHandler(Looper looper) {
162 super(looper, null, true);
163 }
164
165 @Override
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800166 public void handleMessage(Message msg) {
167 switch (msg.what) {
168 case BROADCAST_INTENT_MSG: {
169 if (DEBUG_BROADCAST) Slog.v(
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800170 TAG_BROADCAST, "Received BROADCAST_INTENT_MSG");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800171 processNextBroadcast(true);
172 } break;
173 case BROADCAST_TIMEOUT_MSG: {
174 synchronized (mService) {
175 broadcastTimeoutLocked(true);
176 }
177 } break;
178 }
179 }
Svet Ganov9c165d72015-12-01 19:52:26 -0800180 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800181
182 private final class AppNotResponding implements Runnable {
183 private final ProcessRecord mApp;
184 private final String mAnnotation;
185
186 public AppNotResponding(ProcessRecord app, String annotation) {
187 mApp = app;
188 mAnnotation = annotation;
189 }
190
191 @Override
192 public void run() {
Adrian Roos20d7df32016-01-12 18:59:43 +0100193 mService.mAppErrors.appNotResponding(mApp, null, null, false, mAnnotation);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800194 }
195 }
196
Jeff Brown6f357d32014-01-15 20:40:55 -0800197 BroadcastQueue(ActivityManagerService service, Handler handler,
198 String name, long timeoutPeriod, boolean allowDelayBehindServices) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800199 mService = service;
Jeff Brown6f357d32014-01-15 20:40:55 -0800200 mHandler = new BroadcastHandler(handler.getLooper());
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800201 mQueueName = name;
202 mTimeoutPeriod = timeoutPeriod;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700203 mDelayBehindServices = allowDelayBehindServices;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800204 }
205
Jeff Sharkeyfd658132017-05-03 11:38:01 -0600206 @Override
207 public String toString() {
208 return mQueueName;
209 }
210
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800211 public boolean isPendingBroadcastProcessLocked(int pid) {
212 return mPendingBroadcast != null && mPendingBroadcast.curApp.pid == pid;
213 }
214
215 public void enqueueParallelBroadcastLocked(BroadcastRecord r) {
216 mParallelBroadcasts.add(r);
Carmen Jacksona68e3452017-01-17 14:01:33 -0800217 enqueueBroadcastHelper(r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800218 }
219
220 public void enqueueOrderedBroadcastLocked(BroadcastRecord r) {
221 mOrderedBroadcasts.add(r);
Carmen Jacksona68e3452017-01-17 14:01:33 -0800222 enqueueBroadcastHelper(r);
223 }
224
225 /**
226 * Don't call this method directly; call enqueueParallelBroadcastLocked or
227 * enqueueOrderedBroadcastLocked.
228 */
229 private void enqueueBroadcastHelper(BroadcastRecord r) {
Jeff Brown9fb3fd12014-09-29 15:32:12 -0700230 r.enqueueClockTime = System.currentTimeMillis();
Carmen Jacksona68e3452017-01-17 14:01:33 -0800231
232 if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
233 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
234 createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_PENDING),
235 System.identityHashCode(r));
236 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800237 }
238
Makoto Onuki3e7d8452017-03-02 15:33:17 -0800239 /**
240 * Find the same intent from queued parallel broadcast, replace with a new one and return
241 * the old one.
242 */
243 public final BroadcastRecord replaceParallelBroadcastLocked(BroadcastRecord r) {
244 return replaceBroadcastLocked(mParallelBroadcasts, r, "PARALLEL");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800245 }
246
Makoto Onuki3e7d8452017-03-02 15:33:17 -0800247 /**
248 * Find the same intent from queued ordered broadcast, replace with a new one and return
249 * the old one.
250 */
251 public final BroadcastRecord replaceOrderedBroadcastLocked(BroadcastRecord r) {
252 return replaceBroadcastLocked(mOrderedBroadcasts, r, "ORDERED");
253 }
254
255 private BroadcastRecord replaceBroadcastLocked(ArrayList<BroadcastRecord> queue,
256 BroadcastRecord r, String typeForLogging) {
Erik Wolsheimer5d34f002016-07-28 13:51:39 -0700257 final Intent intent = r.intent;
Makoto Onuki3e7d8452017-03-02 15:33:17 -0800258 for (int i = queue.size() - 1; i > 0; i--) {
259 final BroadcastRecord old = queue.get(i);
260 if (old.userId == r.userId && intent.filterEquals(old.intent)) {
261 if (DEBUG_BROADCAST) {
262 Slog.v(TAG_BROADCAST, "***** DROPPING "
263 + typeForLogging + " [" + mQueueName + "]: " + intent);
264 }
265 queue.set(i, r);
266 return old;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800267 }
268 }
Makoto Onuki3e7d8452017-03-02 15:33:17 -0800269 return null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800270 }
271
272 private final void processCurBroadcastLocked(BroadcastRecord r,
273 ProcessRecord app) throws RemoteException {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800274 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800275 "Process cur broadcast " + r + " for app " + app);
276 if (app.thread == null) {
277 throw new RemoteException();
278 }
Amith Yamasanib5f3b5c2016-05-16 14:23:04 -0700279 if (app.inFullBackup) {
280 skipReceiverLocked(r);
281 return;
282 }
283
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800284 r.receiver = app.thread.asBinder();
285 r.curApp = app;
yangzhenyud509bc92016-08-31 18:26:46 +0800286 app.curReceivers.add(r);
Dianne Hackborna413dc02013-07-12 12:02:55 -0700287 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_RECEIVER);
Dianne Hackborndb926082013-10-31 16:32:44 -0700288 mService.updateLruProcessLocked(app, false, null);
289 mService.updateOomAdjLocked();
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800290
291 // Tell the application to launch this receiver.
292 r.intent.setComponent(r.curComponent);
293
294 boolean started = false;
295 try {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800296 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800297 "Delivering to component " + r.curComponent
298 + ": " + r);
Brian Carlstromca82e612016-04-19 23:16:08 -0700299 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
300 PackageManager.NOTIFY_PACKAGE_USE_BROADCAST_RECEIVER);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800301 app.thread.scheduleReceiver(new Intent(r.intent), r.curReceiver,
302 mService.compatibilityInfoForPackageLocked(r.curReceiver.applicationInfo),
Dianne Hackborna413dc02013-07-12 12:02:55 -0700303 r.resultCode, r.resultData, r.resultExtras, r.ordered, r.userId,
304 app.repProcState);
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800305 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800306 "Process cur broadcast " + r + " DELIVERED for app " + app);
307 started = true;
308 } finally {
309 if (!started) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800310 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800311 "Process cur broadcast " + r + ": NOT STARTED!");
312 r.receiver = null;
313 r.curApp = null;
yangzhenyud509bc92016-08-31 18:26:46 +0800314 app.curReceivers.remove(r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800315 }
316 }
317 }
318
319 public boolean sendPendingBroadcastsLocked(ProcessRecord app) {
320 boolean didSomething = false;
321 final BroadcastRecord br = mPendingBroadcast;
322 if (br != null && br.curApp.pid == app.pid) {
Amith Yamasanid86e14e2016-08-05 15:25:03 -0700323 if (br.curApp != app) {
324 Slog.e(TAG, "App mismatch when sending pending broadcast to "
325 + app.processName + ", intended target is " + br.curApp.processName);
326 return false;
327 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800328 try {
329 mPendingBroadcast = null;
330 processCurBroadcastLocked(br, app);
331 didSomething = true;
332 } catch (Exception e) {
333 Slog.w(TAG, "Exception in new application when starting receiver "
334 + br.curComponent.flattenToShortString(), e);
335 logBroadcastReceiverDiscardLocked(br);
336 finishReceiverLocked(br, br.resultCode, br.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700337 br.resultExtras, br.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800338 scheduleBroadcastsLocked();
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700339 // We need to reset the state if we failed to start the receiver.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800340 br.state = BroadcastRecord.IDLE;
341 throw new RuntimeException(e.getMessage());
342 }
343 }
344 return didSomething;
345 }
346
347 public void skipPendingBroadcastLocked(int pid) {
348 final BroadcastRecord br = mPendingBroadcast;
349 if (br != null && br.curApp.pid == pid) {
350 br.state = BroadcastRecord.IDLE;
351 br.nextReceiver = mPendingBroadcastRecvIndex;
352 mPendingBroadcast = null;
353 scheduleBroadcastsLocked();
354 }
355 }
356
357 public void skipCurrentReceiverLocked(ProcessRecord app) {
Wale Ogunwale24b243d2015-07-17 07:20:57 -0700358 BroadcastRecord r = null;
359 if (mOrderedBroadcasts.size() > 0) {
360 BroadcastRecord br = mOrderedBroadcasts.get(0);
361 if (br.curApp == app) {
362 r = br;
363 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800364 }
riddle_hsu01eb7fa2015-03-04 17:27:05 +0800365 if (r == null && mPendingBroadcast != null && mPendingBroadcast.curApp == app) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800366 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800367 "[" + mQueueName + "] skip & discard pending app " + r);
riddle_hsu01eb7fa2015-03-04 17:27:05 +0800368 r = mPendingBroadcast;
369 }
370
371 if (r != null) {
Amith Yamasanib5f3b5c2016-05-16 14:23:04 -0700372 skipReceiverLocked(r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800373 }
374 }
375
Amith Yamasanib5f3b5c2016-05-16 14:23:04 -0700376 private void skipReceiverLocked(BroadcastRecord r) {
377 logBroadcastReceiverDiscardLocked(r);
378 finishReceiverLocked(r, r.resultCode, r.resultData,
379 r.resultExtras, r.resultAbort, false);
380 scheduleBroadcastsLocked();
381 }
382
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800383 public void scheduleBroadcastsLocked() {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800384 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Schedule broadcasts ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800385 + mQueueName + "]: current="
386 + mBroadcastsScheduled);
387
388 if (mBroadcastsScheduled) {
389 return;
390 }
391 mHandler.sendMessage(mHandler.obtainMessage(BROADCAST_INTENT_MSG, this));
392 mBroadcastsScheduled = true;
393 }
394
395 public BroadcastRecord getMatchingOrderedReceiver(IBinder receiver) {
396 if (mOrderedBroadcasts.size() > 0) {
397 final BroadcastRecord r = mOrderedBroadcasts.get(0);
398 if (r != null && r.receiver == receiver) {
399 return r;
400 }
401 }
402 return null;
403 }
404
405 public boolean finishReceiverLocked(BroadcastRecord r, int resultCode,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700406 String resultData, Bundle resultExtras, boolean resultAbort, boolean waitForServices) {
407 final int state = r.state;
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700408 final ActivityInfo receiver = r.curReceiver;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800409 r.state = BroadcastRecord.IDLE;
410 if (state == BroadcastRecord.IDLE) {
Dianne Hackborn6285a322013-09-18 12:09:47 -0700411 Slog.w(TAG, "finishReceiver [" + mQueueName + "] called but state is IDLE");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800412 }
413 r.receiver = null;
414 r.intent.setComponent(null);
yangzhenyud509bc92016-08-31 18:26:46 +0800415 if (r.curApp != null && r.curApp.curReceivers.contains(r)) {
416 r.curApp.curReceivers.remove(r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800417 }
418 if (r.curFilter != null) {
419 r.curFilter.receiverList.curBroadcast = null;
420 }
421 r.curFilter = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800422 r.curReceiver = null;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700423 r.curApp = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800424 mPendingBroadcast = null;
425
426 r.resultCode = resultCode;
427 r.resultData = resultData;
428 r.resultExtras = resultExtras;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700429 if (resultAbort && (r.intent.getFlags()&Intent.FLAG_RECEIVER_NO_ABORT) == 0) {
430 r.resultAbort = resultAbort;
431 } else {
432 r.resultAbort = false;
433 }
434
435 if (waitForServices && r.curComponent != null && r.queue.mDelayBehindServices
436 && r.queue.mOrderedBroadcasts.size() > 0
437 && r.queue.mOrderedBroadcasts.get(0) == r) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700438 ActivityInfo nextReceiver;
439 if (r.nextReceiver < r.receivers.size()) {
440 Object obj = r.receivers.get(r.nextReceiver);
441 nextReceiver = (obj instanceof ActivityInfo) ? (ActivityInfo)obj : null;
442 } else {
443 nextReceiver = null;
444 }
445 // Don't do this if the next receive is in the same process as the current one.
446 if (receiver == null || nextReceiver == null
447 || receiver.applicationInfo.uid != nextReceiver.applicationInfo.uid
448 || !receiver.processName.equals(nextReceiver.processName)) {
449 // In this case, we are ready to process the next receiver for the current broadcast,
450 // but are on a queue that would like to wait for services to finish before moving
451 // on. If there are background services currently starting, then we will go into a
452 // special state where we hold off on continuing this broadcast until they are done.
Dianne Hackbornad51be92016-08-16 16:27:36 -0700453 if (mService.mServices.hasBackgroundServicesLocked(r.userId)) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800454 Slog.i(TAG, "Delay finish: " + r.curComponent.flattenToShortString());
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700455 r.state = BroadcastRecord.WAITING_SERVICES;
456 return false;
457 }
Dianne Hackborn6285a322013-09-18 12:09:47 -0700458 }
459 }
460
461 r.curComponent = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800462
463 // We will process the next receiver right now if this is finishing
464 // an app receiver (which is always asynchronous) or after we have
465 // come back from calling a receiver.
466 return state == BroadcastRecord.APP_RECEIVE
467 || state == BroadcastRecord.CALL_DONE_RECEIVE;
468 }
469
Dianne Hackborn6285a322013-09-18 12:09:47 -0700470 public void backgroundServicesFinishedLocked(int userId) {
471 if (mOrderedBroadcasts.size() > 0) {
472 BroadcastRecord br = mOrderedBroadcasts.get(0);
473 if (br.userId == userId && br.state == BroadcastRecord.WAITING_SERVICES) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800474 Slog.i(TAG, "Resuming delayed broadcast");
Dianne Hackborn6285a322013-09-18 12:09:47 -0700475 br.curComponent = null;
476 br.state = BroadcastRecord.IDLE;
477 processNextBroadcast(false);
478 }
479 }
480 }
481
Dianne Hackbornea05cd52016-06-20 11:22:40 -0700482 void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800483 Intent intent, int resultCode, String data, Bundle extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700484 boolean ordered, boolean sticky, int sendingUser) throws RemoteException {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800485 // Send the intent to the receiver asynchronously using one-way binder calls.
Craig Mautner38c1a5f2014-07-21 15:36:37 +0000486 if (app != null) {
487 if (app.thread != null) {
488 // If we have an app thread, do the call through that so it is
489 // correctly ordered with other one-way calls.
Joe Onorato5869d1c2016-04-20 15:38:07 -0700490 try {
491 app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode,
492 data, extras, ordered, sticky, sendingUser, app.repProcState);
493 // TODO: Uncomment this when (b/28322359) is fixed and we aren't getting
494 // DeadObjectException when the process isn't actually dead.
495 //} catch (DeadObjectException ex) {
496 // Failed to call into the process. It's dying so just let it die and move on.
497 // throw ex;
498 } catch (RemoteException ex) {
499 // Failed to call into the process. It's either dying or wedged. Kill it gently.
500 synchronized (mService) {
501 Slog.w(TAG, "Can't deliver broadcast to " + app.processName
502 + " (pid " + app.pid + "). Crashing it.");
503 app.scheduleCrash("can't deliver broadcast");
504 }
505 throw ex;
506 }
Craig Mautner38c1a5f2014-07-21 15:36:37 +0000507 } else {
508 // Application has died. Receiver doesn't exist.
509 throw new RemoteException("app.thread must not be null");
510 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800511 } else {
Dianne Hackborn20e80982012-08-31 19:00:44 -0700512 receiver.performReceive(intent, resultCode, data, extras, ordered,
513 sticky, sendingUser);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800514 }
515 }
516
Svet Ganov99b60432015-06-27 13:15:22 -0700517 private void deliverToRegisteredReceiverLocked(BroadcastRecord r,
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800518 BroadcastFilter filter, boolean ordered, int index) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800519 boolean skip = false;
Amith Yamasani8bf06ed2012-08-27 19:30:30 -0700520 if (filter.requiredPermission != null) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800521 int perm = mService.checkComponentPermission(filter.requiredPermission,
522 r.callingPid, r.callingUid, -1, true);
523 if (perm != PackageManager.PERMISSION_GRANTED) {
524 Slog.w(TAG, "Permission Denial: broadcasting "
525 + r.intent.toString()
526 + " from " + r.callerPackage + " (pid="
527 + r.callingPid + ", uid=" + r.callingUid + ")"
528 + " requires " + filter.requiredPermission
529 + " due to registered receiver " + filter);
530 skip = true;
Svet Ganov99b60432015-06-27 13:15:22 -0700531 } else {
532 final int opCode = AppOpsManager.permissionToOpCode(filter.requiredPermission);
533 if (opCode != AppOpsManager.OP_NONE
534 && mService.mAppOpsService.noteOperation(opCode, r.callingUid,
535 r.callerPackage) != AppOpsManager.MODE_ALLOWED) {
536 Slog.w(TAG, "Appop Denial: broadcasting "
537 + r.intent.toString()
538 + " from " + r.callerPackage + " (pid="
539 + r.callingPid + ", uid=" + r.callingUid + ")"
540 + " requires appop " + AppOpsManager.permissionToOp(
541 filter.requiredPermission)
542 + " due to registered receiver " + filter);
543 skip = true;
544 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800545 }
546 }
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700547 if (!skip && r.requiredPermissions != null && r.requiredPermissions.length > 0) {
548 for (int i = 0; i < r.requiredPermissions.length; i++) {
549 String requiredPermission = r.requiredPermissions[i];
550 int perm = mService.checkComponentPermission(requiredPermission,
551 filter.receiverList.pid, filter.receiverList.uid, -1, true);
552 if (perm != PackageManager.PERMISSION_GRANTED) {
553 Slog.w(TAG, "Permission Denial: receiving "
554 + r.intent.toString()
555 + " to " + filter.receiverList.app
556 + " (pid=" + filter.receiverList.pid
557 + ", uid=" + filter.receiverList.uid + ")"
558 + " requires " + requiredPermission
559 + " due to sender " + r.callerPackage
560 + " (uid " + r.callingUid + ")");
561 skip = true;
562 break;
563 }
564 int appOp = AppOpsManager.permissionToOpCode(requiredPermission);
Svetoslavfb9ec502015-09-01 14:45:18 -0700565 if (appOp != AppOpsManager.OP_NONE && appOp != r.appOp
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700566 && mService.mAppOpsService.noteOperation(appOp,
567 filter.receiverList.uid, filter.packageName)
568 != AppOpsManager.MODE_ALLOWED) {
569 Slog.w(TAG, "Appop Denial: receiving "
570 + r.intent.toString()
571 + " to " + filter.receiverList.app
572 + " (pid=" + filter.receiverList.pid
573 + ", uid=" + filter.receiverList.uid + ")"
574 + " requires appop " + AppOpsManager.permissionToOp(
575 requiredPermission)
576 + " due to sender " + r.callerPackage
577 + " (uid " + r.callingUid + ")");
578 skip = true;
579 break;
580 }
581 }
582 }
583 if (!skip && (r.requiredPermissions == null || r.requiredPermissions.length == 0)) {
584 int perm = mService.checkComponentPermission(null,
Fyodor Kupolove37520b2015-07-14 22:29:21 +0000585 filter.receiverList.pid, filter.receiverList.uid, -1, true);
586 if (perm != PackageManager.PERMISSION_GRANTED) {
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700587 Slog.w(TAG, "Permission Denial: security check failed when receiving "
Fyodor Kupolove37520b2015-07-14 22:29:21 +0000588 + r.intent.toString()
589 + " to " + filter.receiverList.app
590 + " (pid=" + filter.receiverList.pid
591 + ", uid=" + filter.receiverList.uid + ")"
Fyodor Kupolove37520b2015-07-14 22:29:21 +0000592 + " due to sender " + r.callerPackage
593 + " (uid " + r.callingUid + ")");
594 skip = true;
595 }
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700596 }
597 if (!skip && r.appOp != AppOpsManager.OP_NONE
598 && mService.mAppOpsService.noteOperation(r.appOp,
599 filter.receiverList.uid, filter.packageName)
600 != AppOpsManager.MODE_ALLOWED) {
601 Slog.w(TAG, "Appop Denial: receiving "
602 + r.intent.toString()
603 + " to " + filter.receiverList.app
604 + " (pid=" + filter.receiverList.pid
605 + ", uid=" + filter.receiverList.uid + ")"
606 + " requires appop " + AppOpsManager.opToName(r.appOp)
607 + " due to sender " + r.callerPackage
608 + " (uid " + r.callingUid + ")");
609 skip = true;
Fyodor Kupolovb4e72832015-07-13 19:19:25 -0700610 }
Svet Ganov99b60432015-06-27 13:15:22 -0700611
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700612 if (!mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,
613 r.callingPid, r.resolvedType, filter.receiverList.uid)) {
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800614 skip = true;
Ben Gruver49660c72013-08-06 19:54:08 -0700615 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800616
Dianne Hackborn443d35a2017-06-16 17:59:35 -0700617 if (!skip && (filter.receiverList.app == null || filter.receiverList.app.killed
618 || filter.receiverList.app.crashing)) {
Dianne Hackborn9357b112013-10-03 18:27:48 -0700619 Slog.w(TAG, "Skipping deliver [" + mQueueName + "] " + r
Dianne Hackborn443d35a2017-06-16 17:59:35 -0700620 + " to " + filter.receiverList + ": process gone or crashing");
Dianne Hackborn9357b112013-10-03 18:27:48 -0700621 skip = true;
622 }
623
Chad Brubakerb7e34d52017-02-22 12:36:06 -0800624 // Ensure that broadcasts are only sent to other Instant Apps if they are marked as
625 // visible to Instant Apps.
626 final boolean visibleToInstantApps =
627 (r.intent.getFlags() & Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS) != 0;
628
629 if (!skip && !visibleToInstantApps && filter.instantApp
630 && filter.receiverList.uid != r.callingUid) {
631 Slog.w(TAG, "Instant App Denial: receiving "
632 + r.intent.toString()
633 + " to " + filter.receiverList.app
634 + " (pid=" + filter.receiverList.pid
635 + ", uid=" + filter.receiverList.uid + ")"
636 + " due to sender " + r.callerPackage
637 + " (uid " + r.callingUid + ")"
638 + " not specifying FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS");
639 skip = true;
640 }
641
Chad Brubaker816c83b2017-03-02 10:27:59 -0800642 if (!skip && !filter.visibleToInstantApp && r.callerInstantApp
643 && filter.receiverList.uid != r.callingUid) {
644 Slog.w(TAG, "Instant App Denial: receiving "
645 + r.intent.toString()
646 + " to " + filter.receiverList.app
647 + " (pid=" + filter.receiverList.pid
648 + ", uid=" + filter.receiverList.uid + ")"
649 + " requires receiver be visible to instant apps"
650 + " due to sender " + r.callerPackage
651 + " (uid " + r.callingUid + ")");
652 skip = true;
653 }
654
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800655 if (skip) {
656 r.delivery[index] = BroadcastRecord.DELIVERY_SKIPPED;
657 return;
658 }
Svet Ganov9c165d72015-12-01 19:52:26 -0800659
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800660 // If permissions need a review before any of the app components can run, we drop
661 // the broadcast and if the calling app is in the foreground and the broadcast is
662 // explicit we launch the review UI passing it a pending intent to send the skipped
663 // broadcast.
Svet Ganov77df6f32016-08-17 11:46:34 -0700664 if (mService.mPermissionReviewRequired) {
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800665 if (!requestStartTargetPermissionsReviewIfNeededLocked(r, filter.packageName,
666 filter.owningUserId)) {
667 r.delivery[index] = BroadcastRecord.DELIVERY_SKIPPED;
668 return;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800669 }
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800670 }
671
672 r.delivery[index] = BroadcastRecord.DELIVERY_DELIVERED;
673
674 // If this is not being sent as an ordered broadcast, then we
675 // don't want to touch the fields that keep track of the current
676 // state of ordered broadcasts.
677 if (ordered) {
678 r.receiver = filter.receiverList.receiver.asBinder();
679 r.curFilter = filter;
680 filter.receiverList.curBroadcast = r;
681 r.state = BroadcastRecord.CALL_IN_RECEIVE;
682 if (filter.receiverList.app != null) {
683 // Bump hosting application to no longer be in background
684 // scheduling class. Note that we can't do that if there
685 // isn't an app... but we can only be in that case for
686 // things that directly call the IActivityManager API, which
687 // are already core system stuff so don't matter for this.
688 r.curApp = filter.receiverList.app;
yangzhenyud509bc92016-08-31 18:26:46 +0800689 filter.receiverList.app.curReceivers.add(r);
Amith Yamasani385c3ad2017-05-04 14:27:11 -0700690 mService.updateOomAdjLocked(r.curApp, true);
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800691 }
692 }
693 try {
694 if (DEBUG_BROADCAST_LIGHT) Slog.i(TAG_BROADCAST,
695 "Delivering to " + filter + " : " + r);
Amith Yamasanib5f3b5c2016-05-16 14:23:04 -0700696 if (filter.receiverList.app != null && filter.receiverList.app.inFullBackup) {
697 // Skip delivery if full backup in progress
698 // If it's an ordered broadcast, we need to continue to the next receiver.
699 if (ordered) {
700 skipReceiverLocked(r);
701 }
702 } else {
703 performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver,
704 new Intent(r.intent), r.resultCode, r.resultData,
705 r.resultExtras, r.ordered, r.initialSticky, r.userId);
706 }
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800707 if (ordered) {
708 r.state = BroadcastRecord.CALL_DONE_RECEIVE;
709 }
710 } catch (RemoteException e) {
711 Slog.w(TAG, "Failure sending broadcast " + r.intent, e);
712 if (ordered) {
713 r.receiver = null;
714 r.curFilter = null;
715 filter.receiverList.curBroadcast = null;
716 if (filter.receiverList.app != null) {
yangzhenyud509bc92016-08-31 18:26:46 +0800717 filter.receiverList.app.curReceivers.remove(r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800718 }
719 }
720 }
721 }
722
Svet Ganov9c165d72015-12-01 19:52:26 -0800723 private boolean requestStartTargetPermissionsReviewIfNeededLocked(
724 BroadcastRecord receiverRecord, String receivingPackageName,
725 final int receivingUserId) {
726 if (!mService.getPackageManagerInternalLocked().isPermissionsReviewRequired(
727 receivingPackageName, receivingUserId)) {
728 return true;
729 }
730
731 final boolean callerForeground = receiverRecord.callerApp != null
Dianne Hackborna49ad092016-03-03 13:39:10 -0800732 ? receiverRecord.callerApp.setSchedGroup != ProcessList.SCHED_GROUP_BACKGROUND
Svet Ganov9c165d72015-12-01 19:52:26 -0800733 : true;
734
735 // Show a permission review UI only for explicit broadcast from a foreground app
736 if (callerForeground && receiverRecord.intent.getComponent() != null) {
737 IIntentSender target = mService.getIntentSenderLocked(
738 ActivityManager.INTENT_SENDER_BROADCAST, receiverRecord.callerPackage,
739 receiverRecord.callingUid, receiverRecord.userId, null, null, 0,
740 new Intent[]{receiverRecord.intent},
741 new String[]{receiverRecord.intent.resolveType(mService.mContext
742 .getContentResolver())},
743 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
744 | PendingIntent.FLAG_IMMUTABLE, null);
745
746 final Intent intent = new Intent(Intent.ACTION_REVIEW_PERMISSIONS);
747 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
748 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
749 intent.putExtra(Intent.EXTRA_PACKAGE_NAME, receivingPackageName);
750 intent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));
751
752 if (DEBUG_PERMISSIONS_REVIEW) {
753 Slog.i(TAG, "u" + receivingUserId + " Launching permission review for package "
754 + receivingPackageName);
755 }
756
757 mHandler.post(new Runnable() {
758 @Override
759 public void run() {
760 mService.mContext.startActivityAsUser(intent, new UserHandle(receivingUserId));
761 }
762 });
763 } else {
764 Slog.w(TAG, "u" + receivingUserId + " Receiving a broadcast in package"
765 + receivingPackageName + " requires a permissions review");
766 }
767
768 return false;
769 }
770
Dianne Hackbornfd854ee2015-07-13 18:00:37 -0700771 final void scheduleTempWhitelistLocked(int uid, long duration, BroadcastRecord r) {
Dianne Hackborna750a632015-06-16 17:18:23 -0700772 if (duration > Integer.MAX_VALUE) {
773 duration = Integer.MAX_VALUE;
774 }
775 // XXX ideally we should pause the broadcast until everything behind this is done,
776 // or else we will likely start dispatching the broadcast before we have opened
777 // access to the app (there is a lot of asynchronicity behind this). It is probably
778 // not that big a deal, however, because the main purpose here is to allow apps
779 // to hold wake locks, and they will be able to acquire their wake lock immediately
780 // it just won't be enabled until we get through this work.
Dianne Hackbornfd854ee2015-07-13 18:00:37 -0700781 StringBuilder b = new StringBuilder();
782 b.append("broadcast:");
783 UserHandle.formatUid(b, r.callingUid);
784 b.append(":");
785 if (r.intent.getAction() != null) {
786 b.append(r.intent.getAction());
787 } else if (r.intent.getComponent() != null) {
Dianne Hackborne4d1a2e2017-04-14 17:57:33 -0700788 r.intent.getComponent().appendShortString(b);
Dianne Hackbornfd854ee2015-07-13 18:00:37 -0700789 } else if (r.intent.getData() != null) {
790 b.append(r.intent.getData());
791 }
Dianne Hackborne4d1a2e2017-04-14 17:57:33 -0700792 mService.tempWhitelistUidLocked(uid, duration, b.toString());
Dianne Hackborna750a632015-06-16 17:18:23 -0700793 }
794
Dianne Hackbornb8633f32017-04-11 17:38:42 -0700795 /**
796 * Return true if all given permissions are signature-only perms.
797 */
798 final boolean isSignaturePerm(String[] perms) {
799 if (perms == null) {
800 return false;
801 }
802 IPackageManager pm = AppGlobals.getPackageManager();
803 for (int i = perms.length-1; i >= 0; i--) {
804 try {
Svetoslav Ganovadb8c522017-07-28 05:46:53 +0000805 PermissionInfo pi = pm.getPermissionInfo(perms[i], "android", 0);
Dianne Hackbornb8633f32017-04-11 17:38:42 -0700806 if ((pi.protectionLevel & (PermissionInfo.PROTECTION_MASK_BASE
807 | PermissionInfo.PROTECTION_FLAG_PRIVILEGED))
808 != PermissionInfo.PROTECTION_SIGNATURE) {
809 // If this a signature permission and NOT allowed for privileged apps, it
810 // is okay... otherwise, nope!
811 return false;
812 }
813 } catch (RemoteException e) {
814 return false;
815 }
816 }
817 return true;
818 }
819
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800820 final void processNextBroadcast(boolean fromMsg) {
821 synchronized(mService) {
822 BroadcastRecord r;
823
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800824 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "processNextBroadcast ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800825 + mQueueName + "]: "
Carmen Jacksona68e3452017-01-17 14:01:33 -0800826 + mParallelBroadcasts.size() + " parallel broadcasts, "
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800827 + mOrderedBroadcasts.size() + " ordered broadcasts");
828
829 mService.updateCpuStats();
830
831 if (fromMsg) {
832 mBroadcastsScheduled = false;
833 }
834
835 // First, deliver any non-serialized broadcasts right away.
836 while (mParallelBroadcasts.size() > 0) {
837 r = mParallelBroadcasts.remove(0);
838 r.dispatchTime = SystemClock.uptimeMillis();
839 r.dispatchClockTime = System.currentTimeMillis();
Carmen Jacksona68e3452017-01-17 14:01:33 -0800840
841 if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
842 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER,
843 createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_PENDING),
844 System.identityHashCode(r));
845 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
846 createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_DELIVERED),
847 System.identityHashCode(r));
848 }
849
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800850 final int N = r.receivers.size();
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800851 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Processing parallel broadcast ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800852 + mQueueName + "] " + r);
853 for (int i=0; i<N; i++) {
854 Object target = r.receivers.get(i);
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800855 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800856 "Delivering non-ordered on [" + mQueueName + "] to registered "
857 + target + ": " + r);
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800858 deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false, i);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800859 }
860 addBroadcastToHistoryLocked(r);
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800861 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Done with parallel broadcast ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800862 + mQueueName + "] " + r);
863 }
864
865 // Now take care of the next serialized one...
866
867 // If we are waiting for a process to come up to handle the next
868 // broadcast, then do nothing at this point. Just in case, we
869 // check that the process we're waiting for still exists.
870 if (mPendingBroadcast != null) {
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -0700871 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST,
872 "processNextBroadcast [" + mQueueName + "]: waiting for "
873 + mPendingBroadcast.curApp);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800874
875 boolean isDead;
876 synchronized (mService.mPidsSelfLocked) {
Dianne Hackborn9357b112013-10-03 18:27:48 -0700877 ProcessRecord proc = mService.mPidsSelfLocked.get(mPendingBroadcast.curApp.pid);
878 isDead = proc == null || proc.crashing;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800879 }
880 if (!isDead) {
881 // It's still alive, so keep waiting
882 return;
883 } else {
884 Slog.w(TAG, "pending app ["
885 + mQueueName + "]" + mPendingBroadcast.curApp
886 + " died before responding to broadcast");
887 mPendingBroadcast.state = BroadcastRecord.IDLE;
888 mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex;
889 mPendingBroadcast = null;
890 }
891 }
892
893 boolean looped = false;
894
895 do {
896 if (mOrderedBroadcasts.size() == 0) {
897 // No more broadcasts pending, so all done!
898 mService.scheduleAppGcsLocked();
899 if (looped) {
900 // If we had finished the last ordered broadcast, then
901 // make sure all processes have correct oom and sched
902 // adjustments.
903 mService.updateOomAdjLocked();
904 }
905 return;
906 }
907 r = mOrderedBroadcasts.get(0);
908 boolean forceReceive = false;
909
910 // Ensure that even if something goes awry with the timeout
911 // detection, we catch "hung" broadcasts here, discard them,
912 // and continue to make progress.
913 //
914 // This is only done if the system is ready so that PRE_BOOT_COMPLETED
915 // receivers don't get executed with timeouts. They're intended for
916 // one time heavy lifting after system upgrades and can take
917 // significant amounts of time.
918 int numReceivers = (r.receivers != null) ? r.receivers.size() : 0;
919 if (mService.mProcessesReady && r.dispatchTime > 0) {
920 long now = SystemClock.uptimeMillis();
921 if ((numReceivers > 0) &&
922 (now > r.dispatchTime + (2*mTimeoutPeriod*numReceivers))) {
923 Slog.w(TAG, "Hung broadcast ["
924 + mQueueName + "] discarded after timeout failure:"
925 + " now=" + now
926 + " dispatchTime=" + r.dispatchTime
927 + " startTime=" + r.receiverTime
928 + " intent=" + r.intent
929 + " numReceivers=" + numReceivers
930 + " nextReceiver=" + r.nextReceiver
931 + " state=" + r.state);
932 broadcastTimeoutLocked(false); // forcibly finish this broadcast
933 forceReceive = true;
934 r.state = BroadcastRecord.IDLE;
935 }
936 }
937
938 if (r.state != BroadcastRecord.IDLE) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800939 if (DEBUG_BROADCAST) Slog.d(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800940 "processNextBroadcast("
941 + mQueueName + ") called when not idle (state="
942 + r.state + ")");
943 return;
944 }
945
946 if (r.receivers == null || r.nextReceiver >= numReceivers
947 || r.resultAbort || forceReceive) {
948 // No more receivers for this broadcast! Send the final
949 // result if requested...
950 if (r.resultTo != null) {
951 try {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800952 if (DEBUG_BROADCAST) Slog.i(TAG_BROADCAST,
Todd Kennedyd2f15112015-01-21 15:25:56 -0800953 "Finishing broadcast [" + mQueueName + "] "
954 + r.intent.getAction() + " app=" + r.callerApp);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800955 performReceiveLocked(r.callerApp, r.resultTo,
956 new Intent(r.intent), r.resultCode,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700957 r.resultData, r.resultExtras, false, false, r.userId);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800958 // Set this to null so that the reference
Dianne Hackborn9357b112013-10-03 18:27:48 -0700959 // (local and remote) isn't kept in the mBroadcastHistory.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800960 r.resultTo = null;
961 } catch (RemoteException e) {
Craig Mautner38c1a5f2014-07-21 15:36:37 +0000962 r.resultTo = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800963 Slog.w(TAG, "Failure ["
964 + mQueueName + "] sending broadcast result of "
965 + r.intent, e);
Joe Onorato5869d1c2016-04-20 15:38:07 -0700966
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800967 }
968 }
969
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800970 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Cancelling BROADCAST_TIMEOUT_MSG");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800971 cancelBroadcastTimeoutLocked();
972
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -0700973 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST,
974 "Finished with ordered broadcast " + r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800975
976 // ... and on to the next...
977 addBroadcastToHistoryLocked(r);
Dianne Hackbornbc02a392016-06-02 17:15:08 -0700978 if (r.intent.getComponent() == null && r.intent.getPackage() == null
979 && (r.intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) {
980 // This was an implicit broadcast... let's record it for posterity.
981 mService.addBroadcastStatLocked(r.intent.getAction(), r.callerPackage,
982 r.manifestCount, r.manifestSkipCount, r.finishTime-r.dispatchTime);
983 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800984 mOrderedBroadcasts.remove(0);
985 r = null;
986 looped = true;
987 continue;
988 }
989 } while (r == null);
990
991 // Get the next receiver...
992 int recIdx = r.nextReceiver++;
993
994 // Keep track of when this receiver started, and make sure there
995 // is a timeout message pending to kill it if need be.
996 r.receiverTime = SystemClock.uptimeMillis();
997 if (recIdx == 0) {
998 r.dispatchTime = r.receiverTime;
999 r.dispatchClockTime = System.currentTimeMillis();
Carmen Jacksona68e3452017-01-17 14:01:33 -08001000 if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
1001 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1002 createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_PENDING),
1003 System.identityHashCode(r));
1004 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1005 createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_DELIVERED),
1006 System.identityHashCode(r));
1007 }
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001008 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Processing ordered broadcast ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001009 + mQueueName + "] " + r);
1010 }
1011 if (! mPendingBroadcastTimeoutMessage) {
1012 long timeoutTime = r.receiverTime + mTimeoutPeriod;
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001013 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001014 "Submitting BROADCAST_TIMEOUT_MSG ["
1015 + mQueueName + "] for " + r + " at " + timeoutTime);
1016 setBroadcastTimeoutLocked(timeoutTime);
1017 }
1018
Dianne Hackborna750a632015-06-16 17:18:23 -07001019 final BroadcastOptions brOptions = r.options;
1020 final Object nextReceiver = r.receivers.get(recIdx);
1021
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001022 if (nextReceiver instanceof BroadcastFilter) {
1023 // Simple case: this is a registered receiver who gets
1024 // a direct call.
1025 BroadcastFilter filter = (BroadcastFilter)nextReceiver;
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001026 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001027 "Delivering ordered ["
1028 + mQueueName + "] to registered "
1029 + filter + ": " + r);
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001030 deliverToRegisteredReceiverLocked(r, filter, r.ordered, recIdx);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001031 if (r.receiver == null || !r.ordered) {
1032 // The receiver has already finished, so schedule to
1033 // process the next one.
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001034 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Quick finishing ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001035 + mQueueName + "]: ordered="
1036 + r.ordered + " receiver=" + r.receiver);
1037 r.state = BroadcastRecord.IDLE;
1038 scheduleBroadcastsLocked();
Dianne Hackborna750a632015-06-16 17:18:23 -07001039 } else {
1040 if (brOptions != null && brOptions.getTemporaryAppWhitelistDuration() > 0) {
1041 scheduleTempWhitelistLocked(filter.owningUid,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001042 brOptions.getTemporaryAppWhitelistDuration(), r);
Dianne Hackborna750a632015-06-16 17:18:23 -07001043 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001044 }
1045 return;
1046 }
1047
1048 // Hard case: need to instantiate the receiver, possibly
1049 // starting its application process to host it.
1050
1051 ResolveInfo info =
1052 (ResolveInfo)nextReceiver;
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001053 ComponentName component = new ComponentName(
1054 info.activityInfo.applicationInfo.packageName,
1055 info.activityInfo.name);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001056
1057 boolean skip = false;
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001058 if (brOptions != null &&
1059 (info.activityInfo.applicationInfo.targetSdkVersion
1060 < brOptions.getMinManifestReceiverApiLevel() ||
1061 info.activityInfo.applicationInfo.targetSdkVersion
1062 > brOptions.getMaxManifestReceiverApiLevel())) {
1063 skip = true;
1064 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001065 int perm = mService.checkComponentPermission(info.activityInfo.permission,
1066 r.callingPid, r.callingUid, info.activityInfo.applicationInfo.uid,
1067 info.activityInfo.exported);
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001068 if (!skip && perm != PackageManager.PERMISSION_GRANTED) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001069 if (!info.activityInfo.exported) {
1070 Slog.w(TAG, "Permission Denial: broadcasting "
1071 + r.intent.toString()
1072 + " from " + r.callerPackage + " (pid=" + r.callingPid
1073 + ", uid=" + r.callingUid + ")"
1074 + " is not exported from uid " + info.activityInfo.applicationInfo.uid
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001075 + " due to receiver " + component.flattenToShortString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001076 } else {
1077 Slog.w(TAG, "Permission Denial: broadcasting "
1078 + r.intent.toString()
1079 + " from " + r.callerPackage + " (pid=" + r.callingPid
1080 + ", uid=" + r.callingUid + ")"
1081 + " requires " + info.activityInfo.permission
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001082 + " due to receiver " + component.flattenToShortString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001083 }
1084 skip = true;
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001085 } else if (!skip && info.activityInfo.permission != null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001086 final int opCode = AppOpsManager.permissionToOpCode(info.activityInfo.permission);
1087 if (opCode != AppOpsManager.OP_NONE
1088 && mService.mAppOpsService.noteOperation(opCode, r.callingUid,
1089 r.callerPackage) != AppOpsManager.MODE_ALLOWED) {
1090 Slog.w(TAG, "Appop Denial: broadcasting "
1091 + r.intent.toString()
1092 + " from " + r.callerPackage + " (pid="
1093 + r.callingPid + ", uid=" + r.callingUid + ")"
1094 + " requires appop " + AppOpsManager.permissionToOp(
1095 info.activityInfo.permission)
1096 + " due to registered receiver "
1097 + component.flattenToShortString());
1098 skip = true;
1099 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001100 }
Svet Ganov99b60432015-06-27 13:15:22 -07001101 if (!skip && info.activityInfo.applicationInfo.uid != Process.SYSTEM_UID &&
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001102 r.requiredPermissions != null && r.requiredPermissions.length > 0) {
1103 for (int i = 0; i < r.requiredPermissions.length; i++) {
1104 String requiredPermission = r.requiredPermissions[i];
1105 try {
1106 perm = AppGlobals.getPackageManager().
1107 checkPermission(requiredPermission,
1108 info.activityInfo.applicationInfo.packageName,
1109 UserHandle
1110 .getUserId(info.activityInfo.applicationInfo.uid));
1111 } catch (RemoteException e) {
1112 perm = PackageManager.PERMISSION_DENIED;
1113 }
1114 if (perm != PackageManager.PERMISSION_GRANTED) {
1115 Slog.w(TAG, "Permission Denial: receiving "
1116 + r.intent + " to "
1117 + component.flattenToShortString()
1118 + " requires " + requiredPermission
1119 + " due to sender " + r.callerPackage
1120 + " (uid " + r.callingUid + ")");
1121 skip = true;
1122 break;
1123 }
1124 int appOp = AppOpsManager.permissionToOpCode(requiredPermission);
1125 if (appOp != AppOpsManager.OP_NONE && appOp != r.appOp
1126 && mService.mAppOpsService.noteOperation(appOp,
Fyodor Kupolove37520b2015-07-14 22:29:21 +00001127 info.activityInfo.applicationInfo.uid, info.activityInfo.packageName)
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001128 != AppOpsManager.MODE_ALLOWED) {
1129 Slog.w(TAG, "Appop Denial: receiving "
1130 + r.intent + " to "
1131 + component.flattenToShortString()
1132 + " requires appop " + AppOpsManager.permissionToOp(
1133 requiredPermission)
1134 + " due to sender " + r.callerPackage
1135 + " (uid " + r.callingUid + ")");
1136 skip = true;
1137 break;
1138 }
1139 }
1140 }
1141 if (!skip && r.appOp != AppOpsManager.OP_NONE
1142 && mService.mAppOpsService.noteOperation(r.appOp,
1143 info.activityInfo.applicationInfo.uid, info.activityInfo.packageName)
1144 != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001145 Slog.w(TAG, "Appop Denial: receiving "
1146 + r.intent + " to "
1147 + component.flattenToShortString()
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001148 + " requires appop " + AppOpsManager.opToName(r.appOp)
Svet Ganov99b60432015-06-27 13:15:22 -07001149 + " due to sender " + r.callerPackage
1150 + " (uid " + r.callingUid + ")");
1151 skip = true;
1152 }
Ben Gruver49660c72013-08-06 19:54:08 -07001153 if (!skip) {
1154 skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,
1155 r.callingPid, r.resolvedType, info.activityInfo.applicationInfo.uid);
1156 }
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001157 boolean isSingleton = false;
1158 try {
1159 isSingleton = mService.isSingleton(info.activityInfo.processName,
1160 info.activityInfo.applicationInfo,
1161 info.activityInfo.name, info.activityInfo.flags);
1162 } catch (SecurityException e) {
1163 Slog.w(TAG, e.getMessage());
1164 skip = true;
1165 }
1166 if ((info.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) {
1167 if (ActivityManager.checkUidPermission(
1168 android.Manifest.permission.INTERACT_ACROSS_USERS,
1169 info.activityInfo.applicationInfo.uid)
1170 != PackageManager.PERMISSION_GRANTED) {
1171 Slog.w(TAG, "Permission Denial: Receiver " + component.flattenToShortString()
1172 + " requests FLAG_SINGLE_USER, but app does not hold "
1173 + android.Manifest.permission.INTERACT_ACROSS_USERS);
1174 skip = true;
1175 }
1176 }
Chad Brubakerb7e34d52017-02-22 12:36:06 -08001177 if (!skip && info.activityInfo.applicationInfo.isInstantApp()
Chad Brubakerb7e34d52017-02-22 12:36:06 -08001178 && r.callingUid != info.activityInfo.applicationInfo.uid) {
1179 Slog.w(TAG, "Instant App Denial: receiving "
1180 + r.intent
1181 + " to " + component.flattenToShortString()
1182 + " due to sender " + r.callerPackage
1183 + " (uid " + r.callingUid + ")"
Chad Brubakerabd2b662017-03-16 11:24:16 -07001184 + " Instant Apps do not support manifest receivers");
Chad Brubakerb7e34d52017-02-22 12:36:06 -08001185 skip = true;
1186 }
Chad Brubaker816c83b2017-03-02 10:27:59 -08001187 if (!skip && r.callerInstantApp
Todd Kennedyc05f5d12017-04-25 11:11:40 -07001188 && (info.activityInfo.flags & ActivityInfo.FLAG_VISIBLE_TO_INSTANT_APP) == 0
Chad Brubaker816c83b2017-03-02 10:27:59 -08001189 && r.callingUid != info.activityInfo.applicationInfo.uid) {
1190 Slog.w(TAG, "Instant App Denial: receiving "
1191 + r.intent
1192 + " to " + component.flattenToShortString()
1193 + " requires receiver have visibleToInstantApps set"
1194 + " due to sender " + r.callerPackage
1195 + " (uid " + r.callingUid + ")");
1196 skip = true;
1197 }
Dianne Hackbornbc02a392016-06-02 17:15:08 -07001198 if (!skip) {
1199 r.manifestCount++;
1200 } else {
1201 r.manifestSkipCount++;
1202 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001203 if (r.curApp != null && r.curApp.crashing) {
1204 // If the target process is crashing, just skip it.
Dianne Hackborn9357b112013-10-03 18:27:48 -07001205 Slog.w(TAG, "Skipping deliver ordered [" + mQueueName + "] " + r
1206 + " to " + r.curApp + ": process crashing");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001207 skip = true;
1208 }
Christopher Tateba629da2013-11-13 17:42:28 -08001209 if (!skip) {
1210 boolean isAvailable = false;
1211 try {
1212 isAvailable = AppGlobals.getPackageManager().isPackageAvailable(
1213 info.activityInfo.packageName,
1214 UserHandle.getUserId(info.activityInfo.applicationInfo.uid));
1215 } catch (Exception e) {
1216 // all such failures mean we skip this receiver
1217 Slog.w(TAG, "Exception getting recipient info for "
1218 + info.activityInfo.packageName, e);
1219 }
1220 if (!isAvailable) {
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -07001221 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
1222 "Skipping delivery to " + info.activityInfo.packageName + " / "
1223 + info.activityInfo.applicationInfo.uid
1224 + " : package no longer available");
Christopher Tateba629da2013-11-13 17:42:28 -08001225 skip = true;
1226 }
1227 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001228
Svet Ganov9c165d72015-12-01 19:52:26 -08001229 // If permissions need a review before any of the app components can run, we drop
1230 // the broadcast and if the calling app is in the foreground and the broadcast is
1231 // explicit we launch the review UI passing it a pending intent to send the skipped
1232 // broadcast.
Svet Ganov77df6f32016-08-17 11:46:34 -07001233 if (mService.mPermissionReviewRequired && !skip) {
Svet Ganov9c165d72015-12-01 19:52:26 -08001234 if (!requestStartTargetPermissionsReviewIfNeededLocked(r,
1235 info.activityInfo.packageName, UserHandle.getUserId(
1236 info.activityInfo.applicationInfo.uid))) {
1237 skip = true;
1238 }
1239 }
1240
Dianne Hackborn76e80092015-12-09 14:15:34 -08001241 // This is safe to do even if we are skipping the broadcast, and we need
1242 // this information now to evaluate whether it is going to be allowed to run.
1243 final int receiverUid = info.activityInfo.applicationInfo.uid;
1244 // If it's a singleton, it needs to be the same app or a special app
1245 if (r.callingUid != Process.SYSTEM_UID && isSingleton
1246 && mService.isValidSingletonCall(r.callingUid, receiverUid)) {
1247 info.activityInfo = mService.getActivityInfoForUser(info.activityInfo, 0);
1248 }
Dianne Hackborn2639c4b2015-12-04 13:11:09 -08001249 String targetProcess = info.activityInfo.processName;
1250 ProcessRecord app = mService.getProcessRecordLocked(targetProcess,
1251 info.activityInfo.applicationInfo.uid, false);
Dianne Hackborn76e80092015-12-09 14:15:34 -08001252
Dianne Hackborn2639c4b2015-12-04 13:11:09 -08001253 if (!skip) {
Dianne Hackbornc3af19a2017-01-20 17:00:44 -08001254 final int allowed = mService.getAppStartModeLocked(
1255 info.activityInfo.applicationInfo.uid, info.activityInfo.packageName,
1256 info.activityInfo.applicationInfo.targetSdkVersion, -1, true, false);
Dianne Hackborn2639c4b2015-12-04 13:11:09 -08001257 if (allowed != ActivityManager.APP_START_MODE_NORMAL) {
1258 // We won't allow this receiver to be launched if the app has been
Dianne Hackborne91f3e72016-03-25 18:48:15 -07001259 // completely disabled from launches, or it was not explicitly sent
1260 // to it and the app is in a state that should not receive it
Dianne Hackbornc3af19a2017-01-20 17:00:44 -08001261 // (depending on how getAppStartModeLocked has determined that).
Dianne Hackborn6ac42ae2015-12-08 17:22:10 -08001262 if (allowed == ActivityManager.APP_START_MODE_DISABLED) {
1263 Slog.w(TAG, "Background execution disabled: receiving "
1264 + r.intent + " to "
1265 + component.flattenToShortString());
1266 skip = true;
Dianne Hackborne91f3e72016-03-25 18:48:15 -07001267 } else if (((r.intent.getFlags()&Intent.FLAG_RECEIVER_EXCLUDE_BACKGROUND) != 0)
Dianne Hackborn2639c4b2015-12-04 13:11:09 -08001268 || (r.intent.getComponent() == null
Dianne Hackborne91f3e72016-03-25 18:48:15 -07001269 && r.intent.getPackage() == null
Dianne Hackborn6ac42ae2015-12-08 17:22:10 -08001270 && ((r.intent.getFlags()
Dianne Hackbornb8633f32017-04-11 17:38:42 -07001271 & Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND) == 0)
1272 && !isSignaturePerm(r.requiredPermissions))) {
Dianne Hackborn7fc46d82017-02-14 15:20:06 -08001273 mService.addBackgroundCheckViolationLocked(r.intent.getAction(),
1274 component.getPackageName());
Dianne Hackborn2639c4b2015-12-04 13:11:09 -08001275 Slog.w(TAG, "Background execution not allowed: receiving "
1276 + r.intent + " to "
1277 + component.flattenToShortString());
1278 skip = true;
1279 }
1280 }
1281 }
1282
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001283 if (skip) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001284 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -07001285 "Skipping delivery of ordered [" + mQueueName + "] "
1286 + r + " for whatever reason");
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001287 r.delivery[recIdx] = BroadcastRecord.DELIVERY_SKIPPED;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001288 r.receiver = null;
1289 r.curFilter = null;
1290 r.state = BroadcastRecord.IDLE;
1291 scheduleBroadcastsLocked();
1292 return;
1293 }
1294
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001295 r.delivery[recIdx] = BroadcastRecord.DELIVERY_DELIVERED;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001296 r.state = BroadcastRecord.APP_RECEIVE;
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001297 r.curComponent = component;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001298 r.curReceiver = info.activityInfo;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001299 if (DEBUG_MU && r.callingUid > UserHandle.PER_USER_RANGE) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001300 Slog.v(TAG_MU, "Updated broadcast record activity info for secondary user, "
1301 + info.activityInfo + ", callingUid = " + r.callingUid + ", uid = "
1302 + info.activityInfo.applicationInfo.uid);
1303 }
1304
Dianne Hackborna750a632015-06-16 17:18:23 -07001305 if (brOptions != null && brOptions.getTemporaryAppWhitelistDuration() > 0) {
1306 scheduleTempWhitelistLocked(receiverUid,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001307 brOptions.getTemporaryAppWhitelistDuration(), r);
Dianne Hackborna750a632015-06-16 17:18:23 -07001308 }
1309
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001310 // Broadcast is being executed, its package can't be stopped.
1311 try {
1312 AppGlobals.getPackageManager().setPackageStoppedState(
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001313 r.curComponent.getPackageName(), false, UserHandle.getUserId(r.callingUid));
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001314 } catch (RemoteException e) {
1315 } catch (IllegalArgumentException e) {
1316 Slog.w(TAG, "Failed trying to unstop package "
1317 + r.curComponent.getPackageName() + ": " + e);
1318 }
1319
1320 // Is this receiver's application already running?
Dianne Hackborn443d35a2017-06-16 17:59:35 -07001321 if (app != null && app.thread != null && !app.killed) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001322 try {
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001323 app.addPackage(info.activityInfo.packageName,
1324 info.activityInfo.applicationInfo.versionCode, mService.mProcessStats);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001325 processCurBroadcastLocked(r, app);
1326 return;
1327 } catch (RemoteException e) {
1328 Slog.w(TAG, "Exception when sending broadcast to "
1329 + r.curComponent, e);
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001330 } catch (RuntimeException e) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07001331 Slog.wtf(TAG, "Failed sending broadcast to "
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001332 + r.curComponent + " with " + r.intent, e);
1333 // If some unexpected exception happened, just skip
1334 // this broadcast. At this point we are not in the call
1335 // from a client, so throwing an exception out from here
1336 // will crash the entire system instead of just whoever
1337 // sent the broadcast.
1338 logBroadcastReceiverDiscardLocked(r);
1339 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -07001340 r.resultExtras, r.resultAbort, false);
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001341 scheduleBroadcastsLocked();
1342 // We need to reset the state if we failed to start the receiver.
1343 r.state = BroadcastRecord.IDLE;
1344 return;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001345 }
1346
1347 // If a dead object exception was thrown -- fall through to
1348 // restart the application.
1349 }
1350
1351 // Not running -- get it started, to be executed when the app comes up.
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001352 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001353 "Need to start app ["
1354 + mQueueName + "] " + targetProcess + " for broadcast " + r);
1355 if ((r.curApp=mService.startProcessLocked(targetProcess,
1356 info.activityInfo.applicationInfo, true,
1357 r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND,
1358 "broadcast", r.curComponent,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001359 (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0, false, false))
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001360 == null) {
1361 // Ah, this recipient is unavailable. Finish it if necessary,
1362 // and mark the broadcast record as ready for the next.
1363 Slog.w(TAG, "Unable to launch app "
1364 + info.activityInfo.applicationInfo.packageName + "/"
1365 + info.activityInfo.applicationInfo.uid + " for broadcast "
1366 + r.intent + ": process is bad");
1367 logBroadcastReceiverDiscardLocked(r);
1368 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -07001369 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001370 scheduleBroadcastsLocked();
1371 r.state = BroadcastRecord.IDLE;
1372 return;
1373 }
1374
1375 mPendingBroadcast = r;
1376 mPendingBroadcastRecvIndex = recIdx;
1377 }
1378 }
1379
1380 final void setBroadcastTimeoutLocked(long timeoutTime) {
1381 if (! mPendingBroadcastTimeoutMessage) {
1382 Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG, this);
1383 mHandler.sendMessageAtTime(msg, timeoutTime);
1384 mPendingBroadcastTimeoutMessage = true;
1385 }
1386 }
1387
1388 final void cancelBroadcastTimeoutLocked() {
1389 if (mPendingBroadcastTimeoutMessage) {
1390 mHandler.removeMessages(BROADCAST_TIMEOUT_MSG, this);
1391 mPendingBroadcastTimeoutMessage = false;
1392 }
1393 }
1394
1395 final void broadcastTimeoutLocked(boolean fromMsg) {
1396 if (fromMsg) {
1397 mPendingBroadcastTimeoutMessage = false;
1398 }
1399
1400 if (mOrderedBroadcasts.size() == 0) {
1401 return;
1402 }
1403
1404 long now = SystemClock.uptimeMillis();
1405 BroadcastRecord r = mOrderedBroadcasts.get(0);
1406 if (fromMsg) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001407 if (!mService.mProcessesReady) {
1408 // Only process broadcast timeouts if the system is ready. That way
1409 // PRE_BOOT_COMPLETED broadcasts can't timeout as they are intended
1410 // to do heavy lifting for system up.
1411 return;
1412 }
1413
1414 long timeoutTime = r.receiverTime + mTimeoutPeriod;
1415 if (timeoutTime > now) {
1416 // We can observe premature timeouts because we do not cancel and reset the
1417 // broadcast timeout message after each receiver finishes. Instead, we set up
1418 // an initial timeout then kick it down the road a little further as needed
1419 // when it expires.
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001420 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001421 "Premature timeout ["
1422 + mQueueName + "] @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for "
1423 + timeoutTime);
1424 setBroadcastTimeoutLocked(timeoutTime);
1425 return;
1426 }
1427 }
1428
Dianne Hackborn6285a322013-09-18 12:09:47 -07001429 BroadcastRecord br = mOrderedBroadcasts.get(0);
1430 if (br.state == BroadcastRecord.WAITING_SERVICES) {
1431 // In this case the broadcast had already finished, but we had decided to wait
1432 // for started services to finish as well before going on. So if we have actually
1433 // waited long enough time timeout the broadcast, let's give up on the whole thing
1434 // and just move on to the next.
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001435 Slog.i(TAG, "Waited long enough for: " + (br.curComponent != null
Dianne Hackborn6285a322013-09-18 12:09:47 -07001436 ? br.curComponent.flattenToShortString() : "(null)"));
1437 br.curComponent = null;
1438 br.state = BroadcastRecord.IDLE;
1439 processNextBroadcast(false);
1440 return;
1441 }
1442
1443 Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r. receiver
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001444 + ", started " + (now - r.receiverTime) + "ms ago");
1445 r.receiverTime = now;
1446 r.anrCount++;
1447
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001448 ProcessRecord app = null;
1449 String anrMessage = null;
1450
Christopher Tatea6b2c882017-09-01 11:41:39 -07001451 Object curReceiver;
1452 if (r.nextReceiver > 0) {
1453 curReceiver = r.receivers.get(r.nextReceiver-1);
1454 r.delivery[r.nextReceiver-1] = BroadcastRecord.DELIVERY_TIMEOUT;
1455 } else {
1456 curReceiver = r.curReceiver;
1457 }
1458 Slog.w(TAG, "Receiver during timeout of " + r + " : " + curReceiver);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001459 logBroadcastReceiverDiscardLocked(r);
Christopher Tatea6b2c882017-09-01 11:41:39 -07001460 if (curReceiver != null && curReceiver instanceof BroadcastFilter) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001461 BroadcastFilter bf = (BroadcastFilter)curReceiver;
1462 if (bf.receiverList.pid != 0
1463 && bf.receiverList.pid != ActivityManagerService.MY_PID) {
1464 synchronized (mService.mPidsSelfLocked) {
1465 app = mService.mPidsSelfLocked.get(
1466 bf.receiverList.pid);
1467 }
1468 }
1469 } else {
1470 app = r.curApp;
1471 }
1472
1473 if (app != null) {
1474 anrMessage = "Broadcast of " + r.intent.toString();
1475 }
1476
1477 if (mPendingBroadcast == r) {
1478 mPendingBroadcast = null;
1479 }
1480
1481 // Move on to the next receiver.
1482 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -07001483 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001484 scheduleBroadcastsLocked();
1485
1486 if (anrMessage != null) {
1487 // Post the ANR to the handler since we do not want to process ANRs while
1488 // potentially holding our lock.
1489 mHandler.post(new AppNotResponding(app, anrMessage));
1490 }
1491 }
1492
Christopher Tatef278f122015-04-22 13:12:01 -07001493 private final int ringAdvance(int x, final int increment, final int ringSize) {
1494 x += increment;
1495 if (x < 0) return (ringSize - 1);
1496 else if (x >= ringSize) return 0;
1497 else return x;
1498 }
1499
Makoto Onuki97f82f22017-05-31 16:20:21 -07001500 private final void addBroadcastToHistoryLocked(BroadcastRecord original) {
1501 if (original.callingUid < 0) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001502 // This was from a registerReceiver() call; ignore it.
1503 return;
1504 }
Makoto Onuki97f82f22017-05-31 16:20:21 -07001505 original.finishTime = SystemClock.uptimeMillis();
Christopher Tatef278f122015-04-22 13:12:01 -07001506
Carmen Jacksona68e3452017-01-17 14:01:33 -08001507 if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
1508 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER,
Makoto Onuki97f82f22017-05-31 16:20:21 -07001509 createBroadcastTraceTitle(original, BroadcastRecord.DELIVERY_DELIVERED),
1510 System.identityHashCode(original));
Carmen Jacksona68e3452017-01-17 14:01:33 -08001511 }
1512
Makoto Onuki97f82f22017-05-31 16:20:21 -07001513 // Note sometimes (only for sticky broadcasts?) we reuse BroadcastRecords,
1514 // So don't change the incoming record directly.
1515 final BroadcastRecord historyRecord = original.maybeStripForHistory();
1516
1517 mBroadcastHistory[mHistoryNext] = historyRecord;
Christopher Tatef278f122015-04-22 13:12:01 -07001518 mHistoryNext = ringAdvance(mHistoryNext, 1, MAX_BROADCAST_HISTORY);
1519
Makoto Onuki97f82f22017-05-31 16:20:21 -07001520 mBroadcastSummaryHistory[mSummaryHistoryNext] = historyRecord.intent;
1521 mSummaryHistoryEnqueueTime[mSummaryHistoryNext] = historyRecord.enqueueClockTime;
1522 mSummaryHistoryDispatchTime[mSummaryHistoryNext] = historyRecord.dispatchClockTime;
Christopher Tatef278f122015-04-22 13:12:01 -07001523 mSummaryHistoryFinishTime[mSummaryHistoryNext] = System.currentTimeMillis();
1524 mSummaryHistoryNext = ringAdvance(mSummaryHistoryNext, 1, MAX_BROADCAST_SUMMARY_HISTORY);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001525 }
1526
Wale Ogunwaleca1c1252015-05-15 12:49:13 -07001527 boolean cleanupDisabledPackageReceiversLocked(
1528 String packageName, Set<String> filterByClasses, int userId, boolean doit) {
1529 boolean didSomething = false;
1530 for (int i = mParallelBroadcasts.size() - 1; i >= 0; i--) {
1531 didSomething |= mParallelBroadcasts.get(i).cleanupDisabledPackageReceiversLocked(
1532 packageName, filterByClasses, userId, doit);
1533 if (!doit && didSomething) {
1534 return true;
1535 }
1536 }
1537
1538 for (int i = mOrderedBroadcasts.size() - 1; i >= 0; i--) {
1539 didSomething |= mOrderedBroadcasts.get(i).cleanupDisabledPackageReceiversLocked(
1540 packageName, filterByClasses, userId, doit);
1541 if (!doit && didSomething) {
1542 return true;
1543 }
1544 }
1545
1546 return didSomething;
1547 }
1548
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001549 final void logBroadcastReceiverDiscardLocked(BroadcastRecord r) {
Wale Ogunwalea22c6322015-06-03 09:59:45 -07001550 final int logIndex = r.nextReceiver - 1;
1551 if (logIndex >= 0 && logIndex < r.receivers.size()) {
1552 Object curReceiver = r.receivers.get(logIndex);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001553 if (curReceiver instanceof BroadcastFilter) {
1554 BroadcastFilter bf = (BroadcastFilter) curReceiver;
1555 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001556 bf.owningUserId, System.identityHashCode(r),
Wale Ogunwalea22c6322015-06-03 09:59:45 -07001557 r.intent.getAction(), logIndex, System.identityHashCode(bf));
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001558 } else {
Wale Ogunwalea22c6322015-06-03 09:59:45 -07001559 ResolveInfo ri = (ResolveInfo) curReceiver;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001560 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001561 UserHandle.getUserId(ri.activityInfo.applicationInfo.uid),
Wale Ogunwalea22c6322015-06-03 09:59:45 -07001562 System.identityHashCode(r), r.intent.getAction(), logIndex, ri.toString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001563 }
1564 } else {
Wale Ogunwalea22c6322015-06-03 09:59:45 -07001565 if (logIndex < 0) Slog.w(TAG,
1566 "Discarding broadcast before first receiver is invoked: " + r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001567 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001568 -1, System.identityHashCode(r),
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001569 r.intent.getAction(),
1570 r.nextReceiver,
1571 "NONE");
1572 }
1573 }
1574
Carmen Jacksona68e3452017-01-17 14:01:33 -08001575 private String createBroadcastTraceTitle(BroadcastRecord record, int state) {
1576 return String.format("Broadcast %s from %s (%s) %s",
1577 state == BroadcastRecord.DELIVERY_PENDING ? "in queue" : "dispatched",
1578 record.callerPackage == null ? "" : record.callerPackage,
1579 record.callerApp == null ? "process unknown" : record.callerApp.toShortString(),
1580 record.intent == null ? "" : record.intent.getAction());
1581 }
1582
Jeff Sharkeyfd658132017-05-03 11:38:01 -06001583 final boolean isIdle() {
1584 return mParallelBroadcasts.isEmpty() && mOrderedBroadcasts.isEmpty()
1585 && (mPendingBroadcast == null);
1586 }
1587
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001588 final boolean dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args,
1589 int opti, boolean dumpAll, String dumpPackage, boolean needSep) {
Dianne Hackborn865907d2015-10-21 17:12:53 -07001590 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001591 if (mParallelBroadcasts.size() > 0 || mOrderedBroadcasts.size() > 0
1592 || mPendingBroadcast != null) {
1593 boolean printed = false;
Wale Ogunwaleca1c1252015-05-15 12:49:13 -07001594 for (int i = mParallelBroadcasts.size() - 1; i >= 0; i--) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001595 BroadcastRecord br = mParallelBroadcasts.get(i);
1596 if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) {
1597 continue;
1598 }
1599 if (!printed) {
1600 if (needSep) {
1601 pw.println();
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001602 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001603 needSep = true;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001604 printed = true;
1605 pw.println(" Active broadcasts [" + mQueueName + "]:");
1606 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001607 pw.println(" Active Broadcast " + mQueueName + " #" + i + ":");
Dianne Hackborn865907d2015-10-21 17:12:53 -07001608 br.dump(pw, " ", sdf);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001609 }
1610 printed = false;
1611 needSep = true;
Wale Ogunwaleca1c1252015-05-15 12:49:13 -07001612 for (int i = mOrderedBroadcasts.size() - 1; i >= 0; i--) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001613 BroadcastRecord br = mOrderedBroadcasts.get(i);
1614 if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) {
1615 continue;
1616 }
1617 if (!printed) {
1618 if (needSep) {
1619 pw.println();
1620 }
1621 needSep = true;
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001622 printed = true;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001623 pw.println(" Active ordered broadcasts [" + mQueueName + "]:");
1624 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001625 pw.println(" Active Ordered Broadcast " + mQueueName + " #" + i + ":");
Dianne Hackborn865907d2015-10-21 17:12:53 -07001626 mOrderedBroadcasts.get(i).dump(pw, " ", sdf);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001627 }
1628 if (dumpPackage == null || (mPendingBroadcast != null
1629 && dumpPackage.equals(mPendingBroadcast.callerPackage))) {
1630 if (needSep) {
1631 pw.println();
1632 }
1633 pw.println(" Pending broadcast [" + mQueueName + "]:");
1634 if (mPendingBroadcast != null) {
Dianne Hackborn865907d2015-10-21 17:12:53 -07001635 mPendingBroadcast.dump(pw, " ", sdf);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001636 } else {
1637 pw.println(" (null)");
1638 }
1639 needSep = true;
1640 }
1641 }
1642
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001643 int i;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001644 boolean printed = false;
Christopher Tatef278f122015-04-22 13:12:01 -07001645
1646 i = -1;
1647 int lastIndex = mHistoryNext;
1648 int ringIndex = lastIndex;
1649 do {
1650 // increasing index = more recent entry, and we want to print the most
1651 // recent first and work backwards, so we roll through the ring backwards.
1652 ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_HISTORY);
1653 BroadcastRecord r = mBroadcastHistory[ringIndex];
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001654 if (r == null) {
Christopher Tatef278f122015-04-22 13:12:01 -07001655 continue;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001656 }
Christopher Tatef278f122015-04-22 13:12:01 -07001657
1658 i++; // genuine record of some sort even if we're filtering it out
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001659 if (dumpPackage != null && !dumpPackage.equals(r.callerPackage)) {
1660 continue;
1661 }
1662 if (!printed) {
1663 if (needSep) {
1664 pw.println();
1665 }
1666 needSep = true;
1667 pw.println(" Historical broadcasts [" + mQueueName + "]:");
1668 printed = true;
1669 }
1670 if (dumpAll) {
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001671 pw.print(" Historical Broadcast " + mQueueName + " #");
1672 pw.print(i); pw.println(":");
Dianne Hackborn865907d2015-10-21 17:12:53 -07001673 r.dump(pw, " ", sdf);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001674 } else {
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001675 pw.print(" #"); pw.print(i); pw.print(": "); pw.println(r);
1676 pw.print(" ");
1677 pw.println(r.intent.toShortString(false, true, true, false));
Dianne Hackborna40cfeb2013-03-25 17:49:36 -07001678 if (r.targetComp != null && r.targetComp != r.intent.getComponent()) {
1679 pw.print(" targetComp: "); pw.println(r.targetComp.toShortString());
1680 }
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001681 Bundle bundle = r.intent.getExtras();
1682 if (bundle != null) {
1683 pw.print(" extras: "); pw.println(bundle.toString());
1684 }
1685 }
Christopher Tatef278f122015-04-22 13:12:01 -07001686 } while (ringIndex != lastIndex);
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001687
1688 if (dumpPackage == null) {
Christopher Tatef278f122015-04-22 13:12:01 -07001689 lastIndex = ringIndex = mSummaryHistoryNext;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001690 if (dumpAll) {
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001691 printed = false;
Christopher Tatef278f122015-04-22 13:12:01 -07001692 i = -1;
1693 } else {
1694 // roll over the 'i' full dumps that have already been issued
1695 for (int j = i;
1696 j > 0 && ringIndex != lastIndex;) {
1697 ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_SUMMARY_HISTORY);
1698 BroadcastRecord r = mBroadcastHistory[ringIndex];
1699 if (r == null) {
1700 continue;
1701 }
1702 j--;
1703 }
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001704 }
Christopher Tatef278f122015-04-22 13:12:01 -07001705 // done skipping; dump the remainder of the ring. 'i' is still the ordinal within
1706 // the overall broadcast history.
Christopher Tatef278f122015-04-22 13:12:01 -07001707 do {
1708 ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_SUMMARY_HISTORY);
1709 Intent intent = mBroadcastSummaryHistory[ringIndex];
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001710 if (intent == null) {
Christopher Tatef278f122015-04-22 13:12:01 -07001711 continue;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001712 }
1713 if (!printed) {
1714 if (needSep) {
1715 pw.println();
1716 }
1717 needSep = true;
1718 pw.println(" Historical broadcasts summary [" + mQueueName + "]:");
1719 printed = true;
1720 }
1721 if (!dumpAll && i >= 50) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001722 pw.println(" ...");
1723 break;
1724 }
Christopher Tatef278f122015-04-22 13:12:01 -07001725 i++;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001726 pw.print(" #"); pw.print(i); pw.print(": ");
1727 pw.println(intent.toShortString(false, true, true, false));
Dianne Hackborn865907d2015-10-21 17:12:53 -07001728 pw.print(" ");
1729 TimeUtils.formatDuration(mSummaryHistoryDispatchTime[ringIndex]
1730 - mSummaryHistoryEnqueueTime[ringIndex], pw);
1731 pw.print(" dispatch ");
1732 TimeUtils.formatDuration(mSummaryHistoryFinishTime[ringIndex]
1733 - mSummaryHistoryDispatchTime[ringIndex], pw);
1734 pw.println(" finish");
1735 pw.print(" enq=");
1736 pw.print(sdf.format(new Date(mSummaryHistoryEnqueueTime[ringIndex])));
1737 pw.print(" disp=");
1738 pw.print(sdf.format(new Date(mSummaryHistoryDispatchTime[ringIndex])));
1739 pw.print(" fin=");
1740 pw.println(sdf.format(new Date(mSummaryHistoryFinishTime[ringIndex])));
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001741 Bundle bundle = intent.getExtras();
1742 if (bundle != null) {
1743 pw.print(" extras: "); pw.println(bundle.toString());
1744 }
Christopher Tatef278f122015-04-22 13:12:01 -07001745 } while (ringIndex != lastIndex);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001746 }
1747
1748 return needSep;
1749 }
1750}