blob: 70733efa68bd2117b496ceb9b2bdd4be91236d80 [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
Ng Zhi An1eb7d382018-08-24 13:27:54 -070019import static com.android.server.am.ActivityManagerDebugConfig.*;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080020
Dianne Hackborn7d19e022012-08-07 19:12:33 -070021import android.app.ActivityManager;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080022import android.app.AppGlobals;
Dianne Hackbornf51f6122013-02-04 18:23:34 -080023import android.app.AppOpsManager;
Dianne Hackborna750a632015-06-16 17:18:23 -070024import android.app.BroadcastOptions;
Svet Ganov9c165d72015-12-01 19:52:26 -080025import android.app.PendingIntent;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080026import android.content.ComponentName;
Christopher Tate2f558d22019-01-17 16:58:31 -080027import android.content.ContentResolver;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080028import android.content.IIntentReceiver;
Svet Ganov9c165d72015-12-01 19:52:26 -080029import android.content.IIntentSender;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080030import android.content.Intent;
Svet Ganov9c165d72015-12-01 19:52:26 -080031import android.content.IntentSender;
Dianne Hackborn7d19e022012-08-07 19:12:33 -070032import android.content.pm.ActivityInfo;
Ng Zhi An1eb7d382018-08-24 13:27:54 -070033import android.content.pm.IPackageManager;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080034import android.content.pm.PackageManager;
Ng Zhi An1eb7d382018-08-24 13:27:54 -070035import android.content.pm.PermissionInfo;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080036import android.content.pm.ResolveInfo;
37import android.os.Bundle;
38import android.os.Handler;
39import android.os.IBinder;
Jeff Brown6f357d32014-01-15 20:40:55 -080040import android.os.Looper;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080041import android.os.Message;
42import android.os.Process;
43import android.os.RemoteException;
44import android.os.SystemClock;
Ng Zhi An1eb7d382018-08-24 13:27:54 -070045import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070046import android.os.UserHandle;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080047import android.util.EventLog;
48import android.util.Slog;
Christopher Tate2f558d22019-01-17 16:58:31 -080049import android.util.SparseIntArray;
Ng Zhi An7ff7fdb2019-01-16 15:35:51 -080050import android.util.StatsLog;
Dianne Hackborn865907d2015-10-21 17:12:53 -070051import android.util.TimeUtils;
Yi Jin129fc6c2017-09-28 15:48:38 -070052import android.util.proto.ProtoOutputStream;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080053
Ng Zhi An1eb7d382018-08-24 13:27:54 -070054import java.io.FileDescriptor;
55import java.io.PrintWriter;
56import java.text.SimpleDateFormat;
57import java.util.ArrayList;
58import java.util.Date;
59import java.util.Set;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080060
Dianne Hackborn40c8db52012-02-10 18:59:48 -080061/**
62 * BROADCASTS
63 *
Ng Zhi An458e0d32018-11-16 14:43:06 -080064 * We keep three broadcast queues and associated bookkeeping, one for those at
65 * foreground priority, and one for normal (background-priority) broadcasts, and one to
66 * offload special broadcasts that we know take a long time, such as BOOT_COMPLETED.
Dianne Hackborn40c8db52012-02-10 18:59:48 -080067 */
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070068public final class BroadcastQueue {
Wale Ogunwalebfac4682015-04-08 14:33:21 -070069 private static final String TAG = "BroadcastQueue";
Wale Ogunwaled57969f2014-11-15 19:37:29 -080070 private static final String TAG_MU = TAG + POSTFIX_MU;
71 private static final String TAG_BROADCAST = TAG + POSTFIX_BROADCAST;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080072
Dianne Hackborn4c51de42013-10-16 23:34:35 -070073 static final int MAX_BROADCAST_HISTORY = ActivityManager.isLowRamDeviceStatic() ? 10 : 50;
Dianne Hackborn6285a322013-09-18 12:09:47 -070074 static final int MAX_BROADCAST_SUMMARY_HISTORY
Dianne Hackborn4c51de42013-10-16 23:34:35 -070075 = ActivityManager.isLowRamDeviceStatic() ? 25 : 300;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080076
77 final ActivityManagerService mService;
78
79 /**
Christopher Tate2f558d22019-01-17 16:58:31 -080080 * Behavioral parameters such as timeouts and deferral policy, tracking Settings
81 * for runtime configurability
82 */
83 final BroadcastConstants mConstants;
84
85 /**
Dianne Hackborn40c8db52012-02-10 18:59:48 -080086 * Recognizable moniker for this queue
87 */
88 final String mQueueName;
89
90 /**
Dianne Hackborn6285a322013-09-18 12:09:47 -070091 * If true, we can delay broadcasts while waiting services to finish in the previous
92 * receiver's process.
93 */
94 final boolean mDelayBehindServices;
95
96 /**
Dianne Hackborn40c8db52012-02-10 18:59:48 -080097 * Lists of all active broadcasts that are to be executed immediately
98 * (without waiting for another broadcast to finish). Currently this only
99 * contains broadcasts to registered receivers, to avoid spinning up
100 * a bunch of processes to execute IntentReceiver components. Background-
101 * and foreground-priority broadcasts are queued separately.
102 */
Wale Ogunwale540e1232015-05-01 15:35:39 -0700103 final ArrayList<BroadcastRecord> mParallelBroadcasts = new ArrayList<>();
Dianne Hackborn6285a322013-09-18 12:09:47 -0700104
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800105 /**
Christopher Tate2f558d22019-01-17 16:58:31 -0800106 * Tracking of the ordered broadcast queue, including deferral policy and alarm
107 * prioritization.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800108 */
Christopher Tate2f558d22019-01-17 16:58:31 -0800109 final BroadcastDispatcher mDispatcher;
110
111 /**
112 * Refcounting for completion callbacks of split/deferred broadcasts. The key
113 * is an opaque integer token assigned lazily when a broadcast is first split
114 * into multiple BroadcastRecord objects.
115 */
116 final SparseIntArray mSplitRefcounts = new SparseIntArray();
117 private int mNextToken = 0;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800118
119 /**
Christopher Tatef278f122015-04-22 13:12:01 -0700120 * Historical data of past broadcasts, for debugging. This is a ring buffer
121 * whose last element is at mHistoryNext.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800122 */
Dianne Hackborn6285a322013-09-18 12:09:47 -0700123 final BroadcastRecord[] mBroadcastHistory = new BroadcastRecord[MAX_BROADCAST_HISTORY];
Christopher Tatef278f122015-04-22 13:12:01 -0700124 int mHistoryNext = 0;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800125
126 /**
Christopher Tatef278f122015-04-22 13:12:01 -0700127 * Summary of historical data of past broadcasts, for debugging. This is a
128 * ring buffer whose last element is at mSummaryHistoryNext.
Dianne Hackbornc0bd7472012-10-09 14:00:30 -0700129 */
Dianne Hackborn6285a322013-09-18 12:09:47 -0700130 final Intent[] mBroadcastSummaryHistory = new Intent[MAX_BROADCAST_SUMMARY_HISTORY];
Christopher Tatef278f122015-04-22 13:12:01 -0700131 int mSummaryHistoryNext = 0;
132
133 /**
134 * Various milestone timestamps of entries in the mBroadcastSummaryHistory ring
135 * buffer, also tracked via the mSummaryHistoryNext index. These are all in wall
136 * clock time, not elapsed.
137 */
138 final long[] mSummaryHistoryEnqueueTime = new long[MAX_BROADCAST_SUMMARY_HISTORY];
139 final long[] mSummaryHistoryDispatchTime = new long[MAX_BROADCAST_SUMMARY_HISTORY];
140 final long[] mSummaryHistoryFinishTime = new long[MAX_BROADCAST_SUMMARY_HISTORY];
Dianne Hackbornc0bd7472012-10-09 14:00:30 -0700141
142 /**
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800143 * Set when we current have a BROADCAST_INTENT_MSG in flight.
144 */
145 boolean mBroadcastsScheduled = false;
146
147 /**
148 * True if we have a pending unexpired BROADCAST_TIMEOUT_MSG posted to our handler.
149 */
150 boolean mPendingBroadcastTimeoutMessage;
151
152 /**
153 * Intent broadcasts that we have tried to start, but are
154 * waiting for the application's process to be created. We only
155 * need one per scheduling class (instead of a list) because we always
156 * process broadcasts one at a time, so no others can be started while
157 * waiting for this one.
158 */
159 BroadcastRecord mPendingBroadcast = null;
160
161 /**
162 * The receiver index that is pending, to restart the broadcast if needed.
163 */
164 int mPendingBroadcastRecvIndex;
165
166 static final int BROADCAST_INTENT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG;
167 static final int BROADCAST_TIMEOUT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG + 1;
168
Ng Zhi An7ff7fdb2019-01-16 15:35:51 -0800169 // log latency metrics for ordered broadcasts during BOOT_COMPLETED processing
170 boolean mLogLatencyMetrics = true;
171
Jeff Brown6f357d32014-01-15 20:40:55 -0800172 final BroadcastHandler mHandler;
173
174 private final class BroadcastHandler extends Handler {
175 public BroadcastHandler(Looper looper) {
176 super(looper, null, true);
177 }
178
179 @Override
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800180 public void handleMessage(Message msg) {
181 switch (msg.what) {
182 case BROADCAST_INTENT_MSG: {
183 if (DEBUG_BROADCAST) Slog.v(
Christopher Tate2f558d22019-01-17 16:58:31 -0800184 TAG_BROADCAST, "Received BROADCAST_INTENT_MSG ["
185 + mQueueName + "]");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800186 processNextBroadcast(true);
187 } break;
188 case BROADCAST_TIMEOUT_MSG: {
189 synchronized (mService) {
190 broadcastTimeoutLocked(true);
191 }
192 } break;
193 }
194 }
Svet Ganov9c165d72015-12-01 19:52:26 -0800195 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800196
197 private final class AppNotResponding implements Runnable {
198 private final ProcessRecord mApp;
199 private final String mAnnotation;
200
201 public AppNotResponding(ProcessRecord app, String annotation) {
202 mApp = app;
203 mAnnotation = annotation;
204 }
205
206 @Override
207 public void run() {
Wale Ogunwale51cc98a2018-10-15 10:41:05 -0700208 mApp.appNotResponding(null, null, null, null, false, mAnnotation);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800209 }
210 }
211
Jeff Brown6f357d32014-01-15 20:40:55 -0800212 BroadcastQueue(ActivityManagerService service, Handler handler,
Christopher Tate2f558d22019-01-17 16:58:31 -0800213 String name, BroadcastConstants constants, boolean allowDelayBehindServices) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800214 mService = service;
Jeff Brown6f357d32014-01-15 20:40:55 -0800215 mHandler = new BroadcastHandler(handler.getLooper());
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800216 mQueueName = name;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700217 mDelayBehindServices = allowDelayBehindServices;
Christopher Tate2f558d22019-01-17 16:58:31 -0800218
219 mConstants = constants;
220 mDispatcher = new BroadcastDispatcher(this, mConstants, mHandler, mService);
221 }
222
223 void start(ContentResolver resolver) {
224 mDispatcher.start();
225 mConstants.startObserving(mHandler, resolver);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800226 }
227
Jeff Sharkeyfd658132017-05-03 11:38:01 -0600228 @Override
229 public String toString() {
230 return mQueueName;
231 }
232
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800233 public boolean isPendingBroadcastProcessLocked(int pid) {
234 return mPendingBroadcast != null && mPendingBroadcast.curApp.pid == pid;
235 }
236
237 public void enqueueParallelBroadcastLocked(BroadcastRecord r) {
238 mParallelBroadcasts.add(r);
Carmen Jacksona68e3452017-01-17 14:01:33 -0800239 enqueueBroadcastHelper(r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800240 }
241
242 public void enqueueOrderedBroadcastLocked(BroadcastRecord r) {
Christopher Tate2f558d22019-01-17 16:58:31 -0800243 mDispatcher.enqueueOrderedBroadcastLocked(r);
Carmen Jacksona68e3452017-01-17 14:01:33 -0800244 enqueueBroadcastHelper(r);
245 }
246
247 /**
248 * Don't call this method directly; call enqueueParallelBroadcastLocked or
249 * enqueueOrderedBroadcastLocked.
250 */
251 private void enqueueBroadcastHelper(BroadcastRecord r) {
Jeff Brown9fb3fd12014-09-29 15:32:12 -0700252 r.enqueueClockTime = System.currentTimeMillis();
Carmen Jacksona68e3452017-01-17 14:01:33 -0800253
254 if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
255 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
256 createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_PENDING),
257 System.identityHashCode(r));
258 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800259 }
260
Makoto Onuki3e7d8452017-03-02 15:33:17 -0800261 /**
262 * Find the same intent from queued parallel broadcast, replace with a new one and return
263 * the old one.
264 */
265 public final BroadcastRecord replaceParallelBroadcastLocked(BroadcastRecord r) {
266 return replaceBroadcastLocked(mParallelBroadcasts, r, "PARALLEL");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800267 }
268
Makoto Onuki3e7d8452017-03-02 15:33:17 -0800269 /**
270 * Find the same intent from queued ordered broadcast, replace with a new one and return
271 * the old one.
272 */
273 public final BroadcastRecord replaceOrderedBroadcastLocked(BroadcastRecord r) {
Christopher Tate2f558d22019-01-17 16:58:31 -0800274 return mDispatcher.replaceBroadcastLocked(r, "ORDERED");
Makoto Onuki3e7d8452017-03-02 15:33:17 -0800275 }
276
277 private BroadcastRecord replaceBroadcastLocked(ArrayList<BroadcastRecord> queue,
278 BroadcastRecord r, String typeForLogging) {
Erik Wolsheimer5d34f002016-07-28 13:51:39 -0700279 final Intent intent = r.intent;
Makoto Onuki3e7d8452017-03-02 15:33:17 -0800280 for (int i = queue.size() - 1; i > 0; i--) {
281 final BroadcastRecord old = queue.get(i);
282 if (old.userId == r.userId && intent.filterEquals(old.intent)) {
283 if (DEBUG_BROADCAST) {
284 Slog.v(TAG_BROADCAST, "***** DROPPING "
285 + typeForLogging + " [" + mQueueName + "]: " + intent);
286 }
287 queue.set(i, r);
288 return old;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800289 }
290 }
Makoto Onuki3e7d8452017-03-02 15:33:17 -0800291 return null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800292 }
293
294 private final void processCurBroadcastLocked(BroadcastRecord r,
Amith Yamasani4e820542018-05-09 13:37:20 -0700295 ProcessRecord app, boolean skipOomAdj) throws RemoteException {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800296 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800297 "Process cur broadcast " + r + " for app " + app);
298 if (app.thread == null) {
299 throw new RemoteException();
300 }
Amith Yamasanib5f3b5c2016-05-16 14:23:04 -0700301 if (app.inFullBackup) {
302 skipReceiverLocked(r);
303 return;
304 }
305
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800306 r.receiver = app.thread.asBinder();
307 r.curApp = app;
yangzhenyud509bc92016-08-31 18:26:46 +0800308 app.curReceivers.add(r);
Dianne Hackborna413dc02013-07-12 12:02:55 -0700309 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_RECEIVER);
Amith Yamasani98a00922018-08-21 12:50:30 -0400310 mService.mProcessList.updateLruProcessLocked(app, false, null);
Amith Yamasani4e820542018-05-09 13:37:20 -0700311 if (!skipOomAdj) {
312 mService.updateOomAdjLocked();
313 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800314
315 // Tell the application to launch this receiver.
316 r.intent.setComponent(r.curComponent);
317
318 boolean started = false;
319 try {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800320 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800321 "Delivering to component " + r.curComponent
322 + ": " + r);
Brian Carlstromca82e612016-04-19 23:16:08 -0700323 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
324 PackageManager.NOTIFY_PACKAGE_USE_BROADCAST_RECEIVER);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800325 app.thread.scheduleReceiver(new Intent(r.intent), r.curReceiver,
Wale Ogunwale53783742018-09-16 10:21:51 -0700326 mService.compatibilityInfoForPackage(r.curReceiver.applicationInfo),
Dianne Hackborna413dc02013-07-12 12:02:55 -0700327 r.resultCode, r.resultData, r.resultExtras, r.ordered, r.userId,
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700328 app.getReportedProcState());
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800329 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800330 "Process cur broadcast " + r + " DELIVERED for app " + app);
331 started = true;
332 } finally {
333 if (!started) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800334 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800335 "Process cur broadcast " + r + ": NOT STARTED!");
336 r.receiver = null;
337 r.curApp = null;
yangzhenyud509bc92016-08-31 18:26:46 +0800338 app.curReceivers.remove(r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800339 }
340 }
341 }
342
343 public boolean sendPendingBroadcastsLocked(ProcessRecord app) {
344 boolean didSomething = false;
345 final BroadcastRecord br = mPendingBroadcast;
Sudheer Shankaf6690102017-10-16 10:20:32 -0700346 if (br != null && br.curApp.pid > 0 && br.curApp.pid == app.pid) {
Amith Yamasanid86e14e2016-08-05 15:25:03 -0700347 if (br.curApp != app) {
348 Slog.e(TAG, "App mismatch when sending pending broadcast to "
349 + app.processName + ", intended target is " + br.curApp.processName);
350 return false;
351 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800352 try {
353 mPendingBroadcast = null;
Amith Yamasani4e820542018-05-09 13:37:20 -0700354 processCurBroadcastLocked(br, app, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800355 didSomething = true;
356 } catch (Exception e) {
357 Slog.w(TAG, "Exception in new application when starting receiver "
358 + br.curComponent.flattenToShortString(), e);
359 logBroadcastReceiverDiscardLocked(br);
360 finishReceiverLocked(br, br.resultCode, br.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700361 br.resultExtras, br.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800362 scheduleBroadcastsLocked();
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700363 // We need to reset the state if we failed to start the receiver.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800364 br.state = BroadcastRecord.IDLE;
365 throw new RuntimeException(e.getMessage());
366 }
367 }
368 return didSomething;
369 }
370
371 public void skipPendingBroadcastLocked(int pid) {
372 final BroadcastRecord br = mPendingBroadcast;
373 if (br != null && br.curApp.pid == pid) {
374 br.state = BroadcastRecord.IDLE;
375 br.nextReceiver = mPendingBroadcastRecvIndex;
376 mPendingBroadcast = null;
377 scheduleBroadcastsLocked();
378 }
379 }
380
Christopher Tate2f558d22019-01-17 16:58:31 -0800381 // Skip the current receiver, if any, that is in flight to the given process
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800382 public void skipCurrentReceiverLocked(ProcessRecord app) {
Wale Ogunwale24b243d2015-07-17 07:20:57 -0700383 BroadcastRecord r = null;
Christopher Tate2f558d22019-01-17 16:58:31 -0800384 final BroadcastRecord curActive = mDispatcher.getActiveBroadcastLocked();
385 if (curActive != null && curActive.curApp == app) {
386 // confirmed: the current active broadcast is to the given app
387 r = curActive;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800388 }
Christopher Tate2f558d22019-01-17 16:58:31 -0800389
390 // If the current active broadcast isn't this BUT we're waiting for
391 // mPendingBroadcast to spin up the target app, that's what we use.
riddle_hsu01eb7fa2015-03-04 17:27:05 +0800392 if (r == null && mPendingBroadcast != null && mPendingBroadcast.curApp == app) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800393 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800394 "[" + mQueueName + "] skip & discard pending app " + r);
riddle_hsu01eb7fa2015-03-04 17:27:05 +0800395 r = mPendingBroadcast;
396 }
397
398 if (r != null) {
Amith Yamasanib5f3b5c2016-05-16 14:23:04 -0700399 skipReceiverLocked(r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800400 }
401 }
402
Amith Yamasanib5f3b5c2016-05-16 14:23:04 -0700403 private void skipReceiverLocked(BroadcastRecord r) {
404 logBroadcastReceiverDiscardLocked(r);
405 finishReceiverLocked(r, r.resultCode, r.resultData,
406 r.resultExtras, r.resultAbort, false);
407 scheduleBroadcastsLocked();
408 }
409
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800410 public void scheduleBroadcastsLocked() {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800411 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Schedule broadcasts ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800412 + mQueueName + "]: current="
413 + mBroadcastsScheduled);
414
415 if (mBroadcastsScheduled) {
416 return;
417 }
418 mHandler.sendMessage(mHandler.obtainMessage(BROADCAST_INTENT_MSG, this));
419 mBroadcastsScheduled = true;
420 }
421
422 public BroadcastRecord getMatchingOrderedReceiver(IBinder receiver) {
Christopher Tate2f558d22019-01-17 16:58:31 -0800423 BroadcastRecord br = mDispatcher.getActiveBroadcastLocked();
424 if (br != null && br.receiver == receiver) {
425 return br;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800426 }
427 return null;
428 }
429
Christopher Tate2f558d22019-01-17 16:58:31 -0800430 // > 0 only, no worry about "eventual" recycling
431 private int nextSplitTokenLocked() {
432 int next = mNextToken + 1;
433 if (next <= 0) {
434 next = 1;
435 }
436 mNextToken = next;
437 return next;
438 }
439
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800440 public boolean finishReceiverLocked(BroadcastRecord r, int resultCode,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700441 String resultData, Bundle resultExtras, boolean resultAbort, boolean waitForServices) {
442 final int state = r.state;
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700443 final ActivityInfo receiver = r.curReceiver;
Ng Zhi An1eb7d382018-08-24 13:27:54 -0700444 final long finishTime = SystemClock.uptimeMillis();
Christopher Tate2f558d22019-01-17 16:58:31 -0800445 final long elapsed = finishTime - r.receiverTime;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800446 r.state = BroadcastRecord.IDLE;
447 if (state == BroadcastRecord.IDLE) {
Christopher Tate41c56912019-03-01 18:56:58 -0800448 Slog.w(TAG_BROADCAST, "finishReceiver [" + mQueueName + "] called but state is IDLE");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800449 }
Christopher Tate200029a2019-01-29 16:37:44 -0800450 if (r.allowBackgroundActivityStarts && r.curApp != null) {
Michal Karpinski4b8aef22019-02-21 14:56:51 +0000451 if (elapsed > mConstants.ALLOW_BG_ACTIVITY_START_TIMEOUT) {
452 // if the receiver has run for more than allowed bg activity start timeout,
453 // just remove the token for this process now and we're done
454 r.curApp.removeAllowBackgroundActivityStartsToken(r);
455 } else {
456 // the receiver had run for less than allowed bg activity start timeout,
457 // so allow the process to still start activities from bg for some more time
458 String msgToken = (r.curApp.toShortString() + r.toString()).intern();
459 // first, if there exists a past scheduled request to remove this token, drop
460 // that request - we don't want the token to be swept from under our feet...
461 mHandler.removeCallbacksAndMessages(msgToken);
462 // ...then schedule the removal of the token after the extended timeout
463 mHandler.postAtTime(() -> {
464 if (r.curApp != null) {
465 r.curApp.removeAllowBackgroundActivityStartsToken(r);
466 }
467 }, msgToken, (r.receiverTime + mConstants.ALLOW_BG_ACTIVITY_START_TIMEOUT));
468 }
469 }
Christopher Tatec7aa3292018-08-31 19:13:03 -0700470 // If we're abandoning this broadcast before any receivers were actually spun up,
471 // nextReceiver is zero; in which case time-to-process bookkeeping doesn't apply.
472 if (r.nextReceiver > 0) {
Christopher Tate2f558d22019-01-17 16:58:31 -0800473 r.duration[r.nextReceiver - 1] = elapsed;
Christopher Tatec7aa3292018-08-31 19:13:03 -0700474 }
Christopher Tate2f558d22019-01-17 16:58:31 -0800475
476 // if this receiver was slow, impose deferral policy on the app. This will kick in
477 // when processNextBroadcastLocked() next finds this uid as a receiver identity.
Christopher Tate1b72ca32019-01-30 16:25:53 -0800478 if (!r.timeoutExempt) {
479 if (mConstants.SLOW_TIME > 0 && elapsed > mConstants.SLOW_TIME) {
480 if (DEBUG_BROADCAST_DEFERRAL) {
Christopher Tate41c56912019-03-01 18:56:58 -0800481 Slog.i(TAG_BROADCAST, "Broadcast receiver " + (r.nextReceiver - 1)
482 + " was slow: " + receiver + " br=" + r);
Christopher Tate1b72ca32019-01-30 16:25:53 -0800483 }
484 if (r.curApp != null) {
485 mDispatcher.startDeferring(r.curApp.uid);
486 } else {
Christopher Tate41c56912019-03-01 18:56:58 -0800487 Slog.d(TAG_BROADCAST, "finish receiver curApp is null? " + r);
Christopher Tate1b72ca32019-01-30 16:25:53 -0800488 }
Christopher Tate2f558d22019-01-17 16:58:31 -0800489 }
Christopher Tate1b72ca32019-01-30 16:25:53 -0800490 } else {
491 if (DEBUG_BROADCAST_DEFERRAL) {
492 Slog.i(TAG_BROADCAST, "Finished broadcast " + r.intent.getAction()
493 + " is exempt from deferral policy");
Christopher Tate2f558d22019-01-17 16:58:31 -0800494 }
495 }
496
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800497 r.receiver = null;
498 r.intent.setComponent(null);
yangzhenyud509bc92016-08-31 18:26:46 +0800499 if (r.curApp != null && r.curApp.curReceivers.contains(r)) {
500 r.curApp.curReceivers.remove(r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800501 }
502 if (r.curFilter != null) {
503 r.curFilter.receiverList.curBroadcast = null;
504 }
505 r.curFilter = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800506 r.curReceiver = null;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700507 r.curApp = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800508 mPendingBroadcast = null;
509
510 r.resultCode = resultCode;
511 r.resultData = resultData;
512 r.resultExtras = resultExtras;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700513 if (resultAbort && (r.intent.getFlags()&Intent.FLAG_RECEIVER_NO_ABORT) == 0) {
514 r.resultAbort = resultAbort;
515 } else {
516 r.resultAbort = false;
517 }
518
Christopher Tate2f558d22019-01-17 16:58:31 -0800519 // If we want to wait behind services *AND* we're finishing the head/
520 // active broadcast on its queue
Dianne Hackborn6285a322013-09-18 12:09:47 -0700521 if (waitForServices && r.curComponent != null && r.queue.mDelayBehindServices
Christopher Tate2f558d22019-01-17 16:58:31 -0800522 && r.queue.mDispatcher.getActiveBroadcastLocked() == r) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700523 ActivityInfo nextReceiver;
524 if (r.nextReceiver < r.receivers.size()) {
525 Object obj = r.receivers.get(r.nextReceiver);
526 nextReceiver = (obj instanceof ActivityInfo) ? (ActivityInfo)obj : null;
527 } else {
528 nextReceiver = null;
529 }
530 // Don't do this if the next receive is in the same process as the current one.
531 if (receiver == null || nextReceiver == null
532 || receiver.applicationInfo.uid != nextReceiver.applicationInfo.uid
533 || !receiver.processName.equals(nextReceiver.processName)) {
534 // In this case, we are ready to process the next receiver for the current broadcast,
535 // but are on a queue that would like to wait for services to finish before moving
536 // on. If there are background services currently starting, then we will go into a
537 // special state where we hold off on continuing this broadcast until they are done.
Dianne Hackbornad51be92016-08-16 16:27:36 -0700538 if (mService.mServices.hasBackgroundServicesLocked(r.userId)) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800539 Slog.i(TAG, "Delay finish: " + r.curComponent.flattenToShortString());
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700540 r.state = BroadcastRecord.WAITING_SERVICES;
541 return false;
542 }
Dianne Hackborn6285a322013-09-18 12:09:47 -0700543 }
544 }
545
546 r.curComponent = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800547
548 // We will process the next receiver right now if this is finishing
549 // an app receiver (which is always asynchronous) or after we have
550 // come back from calling a receiver.
551 return state == BroadcastRecord.APP_RECEIVE
552 || state == BroadcastRecord.CALL_DONE_RECEIVE;
553 }
554
Dianne Hackborn6285a322013-09-18 12:09:47 -0700555 public void backgroundServicesFinishedLocked(int userId) {
Christopher Tate2f558d22019-01-17 16:58:31 -0800556 BroadcastRecord br = mDispatcher.getActiveBroadcastLocked();
557 if (br != null) {
Dianne Hackborn6285a322013-09-18 12:09:47 -0700558 if (br.userId == userId && br.state == BroadcastRecord.WAITING_SERVICES) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800559 Slog.i(TAG, "Resuming delayed broadcast");
Dianne Hackborn6285a322013-09-18 12:09:47 -0700560 br.curComponent = null;
561 br.state = BroadcastRecord.IDLE;
562 processNextBroadcast(false);
563 }
564 }
565 }
566
Dianne Hackbornea05cd52016-06-20 11:22:40 -0700567 void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800568 Intent intent, int resultCode, String data, Bundle extras,
Michal Karpinski4b8aef22019-02-21 14:56:51 +0000569 boolean ordered, boolean sticky, int sendingUser)
Michal Karpinski9cbb20b2019-02-05 17:31:50 +0000570 throws RemoteException {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800571 // Send the intent to the receiver asynchronously using one-way binder calls.
Craig Mautner38c1a5f2014-07-21 15:36:37 +0000572 if (app != null) {
573 if (app.thread != null) {
574 // If we have an app thread, do the call through that so it is
575 // correctly ordered with other one-way calls.
Joe Onorato5869d1c2016-04-20 15:38:07 -0700576 try {
577 app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode,
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700578 data, extras, ordered, sticky, sendingUser, app.getReportedProcState());
Joe Onorato5869d1c2016-04-20 15:38:07 -0700579 // TODO: Uncomment this when (b/28322359) is fixed and we aren't getting
580 // DeadObjectException when the process isn't actually dead.
581 //} catch (DeadObjectException ex) {
582 // Failed to call into the process. It's dying so just let it die and move on.
583 // throw ex;
584 } catch (RemoteException ex) {
585 // Failed to call into the process. It's either dying or wedged. Kill it gently.
586 synchronized (mService) {
587 Slog.w(TAG, "Can't deliver broadcast to " + app.processName
588 + " (pid " + app.pid + "). Crashing it.");
589 app.scheduleCrash("can't deliver broadcast");
590 }
591 throw ex;
592 }
Craig Mautner38c1a5f2014-07-21 15:36:37 +0000593 } else {
594 // Application has died. Receiver doesn't exist.
595 throw new RemoteException("app.thread must not be null");
596 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800597 } else {
Dianne Hackborn20e80982012-08-31 19:00:44 -0700598 receiver.performReceive(intent, resultCode, data, extras, ordered,
599 sticky, sendingUser);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800600 }
601 }
602
Svet Ganov99b60432015-06-27 13:15:22 -0700603 private void deliverToRegisteredReceiverLocked(BroadcastRecord r,
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800604 BroadcastFilter filter, boolean ordered, int index) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800605 boolean skip = false;
Dianne Hackborn769b2e72018-12-05 08:51:20 -0800606 if (!mService.validateAssociationAllowedLocked(r.callerPackage, r.callingUid,
607 filter.packageName, filter.owningUid)) {
608 Slog.w(TAG, "Association not allowed: broadcasting "
609 + r.intent.toString()
610 + " from " + r.callerPackage + " (pid=" + r.callingPid
611 + ", uid=" + r.callingUid + ") to " + filter.packageName + " through "
612 + filter);
613 skip = true;
614 }
615 if (!skip && !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,
616 r.callingPid, r.resolvedType, filter.receiverList.uid)) {
617 Slog.w(TAG, "Firewall blocked: broadcasting "
618 + r.intent.toString()
619 + " from " + r.callerPackage + " (pid=" + r.callingPid
620 + ", uid=" + r.callingUid + ") to " + filter.packageName + " through "
621 + filter);
622 skip = true;
623 }
Amith Yamasani8bf06ed2012-08-27 19:30:30 -0700624 if (filter.requiredPermission != null) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800625 int perm = mService.checkComponentPermission(filter.requiredPermission,
626 r.callingPid, r.callingUid, -1, true);
627 if (perm != PackageManager.PERMISSION_GRANTED) {
628 Slog.w(TAG, "Permission Denial: broadcasting "
629 + r.intent.toString()
630 + " from " + r.callerPackage + " (pid="
631 + r.callingPid + ", uid=" + r.callingUid + ")"
632 + " requires " + filter.requiredPermission
633 + " due to registered receiver " + filter);
634 skip = true;
Svet Ganov99b60432015-06-27 13:15:22 -0700635 } else {
636 final int opCode = AppOpsManager.permissionToOpCode(filter.requiredPermission);
637 if (opCode != AppOpsManager.OP_NONE
638 && mService.mAppOpsService.noteOperation(opCode, r.callingUid,
639 r.callerPackage) != AppOpsManager.MODE_ALLOWED) {
640 Slog.w(TAG, "Appop Denial: broadcasting "
641 + r.intent.toString()
642 + " from " + r.callerPackage + " (pid="
643 + r.callingPid + ", uid=" + r.callingUid + ")"
644 + " requires appop " + AppOpsManager.permissionToOp(
645 filter.requiredPermission)
646 + " due to registered receiver " + filter);
647 skip = true;
648 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800649 }
650 }
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700651 if (!skip && r.requiredPermissions != null && r.requiredPermissions.length > 0) {
652 for (int i = 0; i < r.requiredPermissions.length; i++) {
653 String requiredPermission = r.requiredPermissions[i];
654 int perm = mService.checkComponentPermission(requiredPermission,
655 filter.receiverList.pid, filter.receiverList.uid, -1, true);
656 if (perm != PackageManager.PERMISSION_GRANTED) {
657 Slog.w(TAG, "Permission Denial: receiving "
658 + r.intent.toString()
659 + " to " + filter.receiverList.app
660 + " (pid=" + filter.receiverList.pid
661 + ", uid=" + filter.receiverList.uid + ")"
662 + " requires " + requiredPermission
663 + " due to sender " + r.callerPackage
664 + " (uid " + r.callingUid + ")");
665 skip = true;
666 break;
667 }
668 int appOp = AppOpsManager.permissionToOpCode(requiredPermission);
Svetoslavfb9ec502015-09-01 14:45:18 -0700669 if (appOp != AppOpsManager.OP_NONE && appOp != r.appOp
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700670 && mService.mAppOpsService.noteOperation(appOp,
671 filter.receiverList.uid, filter.packageName)
672 != AppOpsManager.MODE_ALLOWED) {
673 Slog.w(TAG, "Appop Denial: receiving "
674 + r.intent.toString()
675 + " to " + filter.receiverList.app
676 + " (pid=" + filter.receiverList.pid
677 + ", uid=" + filter.receiverList.uid + ")"
678 + " requires appop " + AppOpsManager.permissionToOp(
679 requiredPermission)
680 + " due to sender " + r.callerPackage
681 + " (uid " + r.callingUid + ")");
682 skip = true;
683 break;
684 }
685 }
686 }
687 if (!skip && (r.requiredPermissions == null || r.requiredPermissions.length == 0)) {
688 int perm = mService.checkComponentPermission(null,
Fyodor Kupolove37520b2015-07-14 22:29:21 +0000689 filter.receiverList.pid, filter.receiverList.uid, -1, true);
690 if (perm != PackageManager.PERMISSION_GRANTED) {
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700691 Slog.w(TAG, "Permission Denial: security check failed when receiving "
Fyodor Kupolove37520b2015-07-14 22:29:21 +0000692 + r.intent.toString()
693 + " to " + filter.receiverList.app
694 + " (pid=" + filter.receiverList.pid
695 + ", uid=" + filter.receiverList.uid + ")"
Fyodor Kupolove37520b2015-07-14 22:29:21 +0000696 + " due to sender " + r.callerPackage
697 + " (uid " + r.callingUid + ")");
698 skip = true;
699 }
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700700 }
701 if (!skip && r.appOp != AppOpsManager.OP_NONE
702 && mService.mAppOpsService.noteOperation(r.appOp,
703 filter.receiverList.uid, filter.packageName)
704 != AppOpsManager.MODE_ALLOWED) {
705 Slog.w(TAG, "Appop Denial: receiving "
706 + r.intent.toString()
707 + " to " + filter.receiverList.app
708 + " (pid=" + filter.receiverList.pid
709 + ", uid=" + filter.receiverList.uid + ")"
710 + " requires appop " + AppOpsManager.opToName(r.appOp)
711 + " due to sender " + r.callerPackage
712 + " (uid " + r.callingUid + ")");
713 skip = true;
Fyodor Kupolovb4e72832015-07-13 19:19:25 -0700714 }
Svet Ganov99b60432015-06-27 13:15:22 -0700715
Dianne Hackborn443d35a2017-06-16 17:59:35 -0700716 if (!skip && (filter.receiverList.app == null || filter.receiverList.app.killed
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700717 || filter.receiverList.app.isCrashing())) {
Dianne Hackborn9357b112013-10-03 18:27:48 -0700718 Slog.w(TAG, "Skipping deliver [" + mQueueName + "] " + r
Dianne Hackborn443d35a2017-06-16 17:59:35 -0700719 + " to " + filter.receiverList + ": process gone or crashing");
Dianne Hackborn9357b112013-10-03 18:27:48 -0700720 skip = true;
721 }
722
Chad Brubakerb7e34d52017-02-22 12:36:06 -0800723 // Ensure that broadcasts are only sent to other Instant Apps if they are marked as
724 // visible to Instant Apps.
725 final boolean visibleToInstantApps =
726 (r.intent.getFlags() & Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS) != 0;
727
728 if (!skip && !visibleToInstantApps && filter.instantApp
729 && filter.receiverList.uid != r.callingUid) {
730 Slog.w(TAG, "Instant App Denial: receiving "
731 + r.intent.toString()
732 + " to " + filter.receiverList.app
733 + " (pid=" + filter.receiverList.pid
734 + ", uid=" + filter.receiverList.uid + ")"
735 + " due to sender " + r.callerPackage
736 + " (uid " + r.callingUid + ")"
737 + " not specifying FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS");
738 skip = true;
739 }
740
Chad Brubaker816c83b2017-03-02 10:27:59 -0800741 if (!skip && !filter.visibleToInstantApp && r.callerInstantApp
742 && filter.receiverList.uid != r.callingUid) {
743 Slog.w(TAG, "Instant App Denial: receiving "
744 + r.intent.toString()
745 + " to " + filter.receiverList.app
746 + " (pid=" + filter.receiverList.pid
747 + ", uid=" + filter.receiverList.uid + ")"
748 + " requires receiver be visible to instant apps"
749 + " due to sender " + r.callerPackage
750 + " (uid " + r.callingUid + ")");
751 skip = true;
752 }
753
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800754 if (skip) {
755 r.delivery[index] = BroadcastRecord.DELIVERY_SKIPPED;
756 return;
757 }
Svet Ganov9c165d72015-12-01 19:52:26 -0800758
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800759 // If permissions need a review before any of the app components can run, we drop
760 // the broadcast and if the calling app is in the foreground and the broadcast is
761 // explicit we launch the review UI passing it a pending intent to send the skipped
762 // broadcast.
Philip P. Moltmann6c644e62018-07-18 15:41:24 -0700763 if (!requestStartTargetPermissionsReviewIfNeededLocked(r, filter.packageName,
764 filter.owningUserId)) {
765 r.delivery[index] = BroadcastRecord.DELIVERY_SKIPPED;
766 return;
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800767 }
768
769 r.delivery[index] = BroadcastRecord.DELIVERY_DELIVERED;
770
771 // If this is not being sent as an ordered broadcast, then we
772 // don't want to touch the fields that keep track of the current
773 // state of ordered broadcasts.
774 if (ordered) {
775 r.receiver = filter.receiverList.receiver.asBinder();
776 r.curFilter = filter;
777 filter.receiverList.curBroadcast = r;
778 r.state = BroadcastRecord.CALL_IN_RECEIVE;
779 if (filter.receiverList.app != null) {
780 // Bump hosting application to no longer be in background
781 // scheduling class. Note that we can't do that if there
782 // isn't an app... but we can only be in that case for
783 // things that directly call the IActivityManager API, which
784 // are already core system stuff so don't matter for this.
785 r.curApp = filter.receiverList.app;
yangzhenyud509bc92016-08-31 18:26:46 +0800786 filter.receiverList.app.curReceivers.add(r);
Amith Yamasani385c3ad2017-05-04 14:27:11 -0700787 mService.updateOomAdjLocked(r.curApp, true);
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800788 }
789 }
790 try {
791 if (DEBUG_BROADCAST_LIGHT) Slog.i(TAG_BROADCAST,
792 "Delivering to " + filter + " : " + r);
Amith Yamasanib5f3b5c2016-05-16 14:23:04 -0700793 if (filter.receiverList.app != null && filter.receiverList.app.inFullBackup) {
794 // Skip delivery if full backup in progress
795 // If it's an ordered broadcast, we need to continue to the next receiver.
796 if (ordered) {
797 skipReceiverLocked(r);
798 }
799 } else {
Christopher Tate41c56912019-03-01 18:56:58 -0800800 r.receiverTime = SystemClock.uptimeMillis();
Michal Karpinski4b8aef22019-02-21 14:56:51 +0000801 maybeAddAllowBackgroundActivityStartsToken(filter.receiverList.app, r);
Amith Yamasanib5f3b5c2016-05-16 14:23:04 -0700802 performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver,
803 new Intent(r.intent), r.resultCode, r.resultData,
Michal Karpinski4b8aef22019-02-21 14:56:51 +0000804 r.resultExtras, r.ordered, r.initialSticky, r.userId);
Amith Yamasanib5f3b5c2016-05-16 14:23:04 -0700805 }
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800806 if (ordered) {
807 r.state = BroadcastRecord.CALL_DONE_RECEIVE;
808 }
809 } catch (RemoteException e) {
810 Slog.w(TAG, "Failure sending broadcast " + r.intent, e);
811 if (ordered) {
812 r.receiver = null;
813 r.curFilter = null;
814 filter.receiverList.curBroadcast = null;
815 if (filter.receiverList.app != null) {
yangzhenyud509bc92016-08-31 18:26:46 +0800816 filter.receiverList.app.curReceivers.remove(r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800817 }
818 }
819 }
820 }
821
Svet Ganov9c165d72015-12-01 19:52:26 -0800822 private boolean requestStartTargetPermissionsReviewIfNeededLocked(
823 BroadcastRecord receiverRecord, String receivingPackageName,
824 final int receivingUserId) {
825 if (!mService.getPackageManagerInternalLocked().isPermissionsReviewRequired(
826 receivingPackageName, receivingUserId)) {
827 return true;
828 }
829
830 final boolean callerForeground = receiverRecord.callerApp != null
Dianne Hackborna49ad092016-03-03 13:39:10 -0800831 ? receiverRecord.callerApp.setSchedGroup != ProcessList.SCHED_GROUP_BACKGROUND
Svet Ganov9c165d72015-12-01 19:52:26 -0800832 : true;
833
834 // Show a permission review UI only for explicit broadcast from a foreground app
835 if (callerForeground && receiverRecord.intent.getComponent() != null) {
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700836 IIntentSender target = mService.mPendingIntentController.getIntentSender(
Svet Ganov9c165d72015-12-01 19:52:26 -0800837 ActivityManager.INTENT_SENDER_BROADCAST, receiverRecord.callerPackage,
838 receiverRecord.callingUid, receiverRecord.userId, null, null, 0,
839 new Intent[]{receiverRecord.intent},
840 new String[]{receiverRecord.intent.resolveType(mService.mContext
841 .getContentResolver())},
842 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
843 | PendingIntent.FLAG_IMMUTABLE, null);
844
845 final Intent intent = new Intent(Intent.ACTION_REVIEW_PERMISSIONS);
846 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
Philip P. Moltmann751e46ca2019-01-24 13:56:47 -0800847 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK
Svet Ganov9c165d72015-12-01 19:52:26 -0800848 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
849 intent.putExtra(Intent.EXTRA_PACKAGE_NAME, receivingPackageName);
850 intent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));
851
852 if (DEBUG_PERMISSIONS_REVIEW) {
853 Slog.i(TAG, "u" + receivingUserId + " Launching permission review for package "
854 + receivingPackageName);
855 }
856
857 mHandler.post(new Runnable() {
858 @Override
859 public void run() {
860 mService.mContext.startActivityAsUser(intent, new UserHandle(receivingUserId));
861 }
862 });
863 } else {
864 Slog.w(TAG, "u" + receivingUserId + " Receiving a broadcast in package"
865 + receivingPackageName + " requires a permissions review");
866 }
867
868 return false;
869 }
870
Dianne Hackbornfd854ee2015-07-13 18:00:37 -0700871 final void scheduleTempWhitelistLocked(int uid, long duration, BroadcastRecord r) {
Dianne Hackborna750a632015-06-16 17:18:23 -0700872 if (duration > Integer.MAX_VALUE) {
873 duration = Integer.MAX_VALUE;
874 }
875 // XXX ideally we should pause the broadcast until everything behind this is done,
876 // or else we will likely start dispatching the broadcast before we have opened
877 // access to the app (there is a lot of asynchronicity behind this). It is probably
878 // not that big a deal, however, because the main purpose here is to allow apps
879 // to hold wake locks, and they will be able to acquire their wake lock immediately
880 // it just won't be enabled until we get through this work.
Dianne Hackbornfd854ee2015-07-13 18:00:37 -0700881 StringBuilder b = new StringBuilder();
882 b.append("broadcast:");
883 UserHandle.formatUid(b, r.callingUid);
884 b.append(":");
885 if (r.intent.getAction() != null) {
886 b.append(r.intent.getAction());
887 } else if (r.intent.getComponent() != null) {
Dianne Hackborne4d1a2e2017-04-14 17:57:33 -0700888 r.intent.getComponent().appendShortString(b);
Dianne Hackbornfd854ee2015-07-13 18:00:37 -0700889 } else if (r.intent.getData() != null) {
890 b.append(r.intent.getData());
891 }
Dianne Hackborne4d1a2e2017-04-14 17:57:33 -0700892 mService.tempWhitelistUidLocked(uid, duration, b.toString());
Dianne Hackborna750a632015-06-16 17:18:23 -0700893 }
894
Dianne Hackbornb8633f32017-04-11 17:38:42 -0700895 /**
896 * Return true if all given permissions are signature-only perms.
897 */
898 final boolean isSignaturePerm(String[] perms) {
899 if (perms == null) {
900 return false;
901 }
902 IPackageManager pm = AppGlobals.getPackageManager();
903 for (int i = perms.length-1; i >= 0; i--) {
904 try {
Svetoslav Ganovadb8c522017-07-28 05:46:53 +0000905 PermissionInfo pi = pm.getPermissionInfo(perms[i], "android", 0);
Christopher Tatecfa39d12019-02-08 18:20:07 -0800906 if (pi == null) {
907 // a required permission that no package has actually
908 // defined cannot be signature-required.
909 return false;
910 }
Dianne Hackbornb8633f32017-04-11 17:38:42 -0700911 if ((pi.protectionLevel & (PermissionInfo.PROTECTION_MASK_BASE
912 | PermissionInfo.PROTECTION_FLAG_PRIVILEGED))
913 != PermissionInfo.PROTECTION_SIGNATURE) {
914 // If this a signature permission and NOT allowed for privileged apps, it
915 // is okay... otherwise, nope!
916 return false;
917 }
918 } catch (RemoteException e) {
919 return false;
920 }
921 }
922 return true;
923 }
924
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800925 final void processNextBroadcast(boolean fromMsg) {
Amith Yamasani4e820542018-05-09 13:37:20 -0700926 synchronized (mService) {
927 processNextBroadcastLocked(fromMsg, false);
928 }
929 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800930
Amith Yamasani4e820542018-05-09 13:37:20 -0700931 final void processNextBroadcastLocked(boolean fromMsg, boolean skipOomAdj) {
932 BroadcastRecord r;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800933
Amith Yamasani4e820542018-05-09 13:37:20 -0700934 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "processNextBroadcast ["
935 + mQueueName + "]: "
Christopher Tateb486f412019-02-12 18:05:17 -0800936 + mParallelBroadcasts.size() + " parallel broadcasts; "
937 + mDispatcher.describeStateLocked());
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800938
Amith Yamasani4e820542018-05-09 13:37:20 -0700939 mService.updateCpuStats();
940
941 if (fromMsg) {
942 mBroadcastsScheduled = false;
943 }
944
945 // First, deliver any non-serialized broadcasts right away.
946 while (mParallelBroadcasts.size() > 0) {
947 r = mParallelBroadcasts.remove(0);
948 r.dispatchTime = SystemClock.uptimeMillis();
949 r.dispatchClockTime = System.currentTimeMillis();
950
951 if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
952 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER,
953 createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_PENDING),
954 System.identityHashCode(r));
955 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
956 createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_DELIVERED),
957 System.identityHashCode(r));
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800958 }
959
Amith Yamasani4e820542018-05-09 13:37:20 -0700960 final int N = r.receivers.size();
961 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Processing parallel broadcast ["
962 + mQueueName + "] " + r);
963 for (int i=0; i<N; i++) {
964 Object target = r.receivers.get(i);
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800965 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Amith Yamasani4e820542018-05-09 13:37:20 -0700966 "Delivering non-ordered on [" + mQueueName + "] to registered "
967 + target + ": " + r);
968 deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false, i);
969 }
970 addBroadcastToHistoryLocked(r);
971 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Done with parallel broadcast ["
972 + mQueueName + "] " + r);
973 }
974
975 // Now take care of the next serialized one...
976
977 // If we are waiting for a process to come up to handle the next
978 // broadcast, then do nothing at this point. Just in case, we
979 // check that the process we're waiting for still exists.
980 if (mPendingBroadcast != null) {
981 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST,
982 "processNextBroadcast [" + mQueueName + "]: waiting for "
983 + mPendingBroadcast.curApp);
984
985 boolean isDead;
986 if (mPendingBroadcast.curApp.pid > 0) {
987 synchronized (mService.mPidsSelfLocked) {
988 ProcessRecord proc = mService.mPidsSelfLocked.get(
989 mPendingBroadcast.curApp.pid);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700990 isDead = proc == null || proc.isCrashing();
Amith Yamasani4e820542018-05-09 13:37:20 -0700991 }
992 } else {
Amith Yamasani98a00922018-08-21 12:50:30 -0400993 final ProcessRecord proc = mService.mProcessList.mProcessNames.get(
Amith Yamasani4e820542018-05-09 13:37:20 -0700994 mPendingBroadcast.curApp.processName, mPendingBroadcast.curApp.uid);
995 isDead = proc == null || !proc.pendingStart;
996 }
997 if (!isDead) {
998 // It's still alive, so keep waiting
999 return;
1000 } else {
1001 Slog.w(TAG, "pending app ["
1002 + mQueueName + "]" + mPendingBroadcast.curApp
1003 + " died before responding to broadcast");
1004 mPendingBroadcast.state = BroadcastRecord.IDLE;
1005 mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex;
1006 mPendingBroadcast = null;
1007 }
1008 }
1009
1010 boolean looped = false;
1011
1012 do {
Christopher Tate2f558d22019-01-17 16:58:31 -08001013 final long now = SystemClock.uptimeMillis();
1014 r = mDispatcher.getNextBroadcastLocked(now);
1015
1016 if (r == null) {
1017 // No more broadcasts are deliverable right now, so all done!
1018 mDispatcher.scheduleDeferralCheckLocked(false);
Amith Yamasani4e820542018-05-09 13:37:20 -07001019 mService.scheduleAppGcsLocked();
1020 if (looped) {
1021 // If we had finished the last ordered broadcast, then
1022 // make sure all processes have correct oom and sched
1023 // adjustments.
1024 mService.updateOomAdjLocked();
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001025 }
Ng Zhi An7ff7fdb2019-01-16 15:35:51 -08001026
1027 // when we have no more ordered broadcast on this queue, stop logging
1028 if (mService.mUserController.mBootCompleted && mLogLatencyMetrics) {
1029 mLogLatencyMetrics = false;
1030 }
1031
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001032 return;
1033 }
Christopher Tate2f558d22019-01-17 16:58:31 -08001034
Amith Yamasani4e820542018-05-09 13:37:20 -07001035 boolean forceReceive = false;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001036
Amith Yamasani4e820542018-05-09 13:37:20 -07001037 // Ensure that even if something goes awry with the timeout
1038 // detection, we catch "hung" broadcasts here, discard them,
1039 // and continue to make progress.
1040 //
Christopher Tate1b72ca32019-01-30 16:25:53 -08001041 // This is only done if the system is ready so that early-stage receivers
1042 // don't get executed with timeouts; and of course other timeout-
1043 // exempt broadcasts are ignored.
Amith Yamasani4e820542018-05-09 13:37:20 -07001044 int numReceivers = (r.receivers != null) ? r.receivers.size() : 0;
Christopher Tate1b72ca32019-01-30 16:25:53 -08001045 if (mService.mProcessesReady && !r.timeoutExempt && r.dispatchTime > 0) {
Amith Yamasani4e820542018-05-09 13:37:20 -07001046 if ((numReceivers > 0) &&
Christopher Tate2f558d22019-01-17 16:58:31 -08001047 (now > r.dispatchTime + (2 * mConstants.TIMEOUT * numReceivers))) {
Amith Yamasani4e820542018-05-09 13:37:20 -07001048 Slog.w(TAG, "Hung broadcast ["
1049 + mQueueName + "] discarded after timeout failure:"
1050 + " now=" + now
1051 + " dispatchTime=" + r.dispatchTime
1052 + " startTime=" + r.receiverTime
1053 + " intent=" + r.intent
1054 + " numReceivers=" + numReceivers
1055 + " nextReceiver=" + r.nextReceiver
1056 + " state=" + r.state);
1057 broadcastTimeoutLocked(false); // forcibly finish this broadcast
1058 forceReceive = true;
1059 r.state = BroadcastRecord.IDLE;
1060 }
1061 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001062
Amith Yamasani4e820542018-05-09 13:37:20 -07001063 if (r.state != BroadcastRecord.IDLE) {
1064 if (DEBUG_BROADCAST) Slog.d(TAG_BROADCAST,
1065 "processNextBroadcast("
1066 + mQueueName + ") called when not idle (state="
1067 + r.state + ")");
1068 return;
1069 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001070
Christopher Tate2f558d22019-01-17 16:58:31 -08001071 // Is the current broadcast is done for any reason?
Amith Yamasani4e820542018-05-09 13:37:20 -07001072 if (r.receivers == null || r.nextReceiver >= numReceivers
1073 || r.resultAbort || forceReceive) {
Christopher Tate2f558d22019-01-17 16:58:31 -08001074 // Send the final result if requested
Amith Yamasani4e820542018-05-09 13:37:20 -07001075 if (r.resultTo != null) {
Christopher Tate2f558d22019-01-17 16:58:31 -08001076 boolean sendResult = true;
Amith Yamasani4e820542018-05-09 13:37:20 -07001077
Christopher Tate2f558d22019-01-17 16:58:31 -08001078 // if this was part of a split/deferral complex, update the refcount and only
1079 // send the completion when we clear all of them
1080 if (r.splitToken != 0) {
1081 int newCount = mSplitRefcounts.get(r.splitToken) - 1;
1082 if (newCount == 0) {
1083 // done! clear out this record's bookkeeping and deliver
1084 if (DEBUG_BROADCAST_DEFERRAL) {
Christopher Tate41c56912019-03-01 18:56:58 -08001085 Slog.i(TAG_BROADCAST,
1086 "Sending broadcast completion for split token "
1087 + r.splitToken + " : " + r.intent.getAction());
Christopher Tate2f558d22019-01-17 16:58:31 -08001088 }
1089 mSplitRefcounts.delete(r.splitToken);
1090 } else {
1091 // still have some split broadcast records in flight; update refcount
1092 // and hold off on the callback
1093 if (DEBUG_BROADCAST_DEFERRAL) {
Christopher Tate41c56912019-03-01 18:56:58 -08001094 Slog.i(TAG_BROADCAST,
1095 "Result refcount now " + newCount + " for split token "
1096 + r.splitToken + " : " + r.intent.getAction()
1097 + " - not sending completion yet");
Christopher Tate2f558d22019-01-17 16:58:31 -08001098 }
1099 sendResult = false;
1100 mSplitRefcounts.put(r.splitToken, newCount);
1101 }
1102 }
1103 if (sendResult) {
1104 try {
1105 if (DEBUG_BROADCAST) {
1106 Slog.i(TAG_BROADCAST, "Finishing broadcast [" + mQueueName + "] "
1107 + r.intent.getAction() + " app=" + r.callerApp);
1108 }
1109 performReceiveLocked(r.callerApp, r.resultTo,
1110 new Intent(r.intent), r.resultCode,
Michal Karpinski4b8aef22019-02-21 14:56:51 +00001111 r.resultData, r.resultExtras, false, false, r.userId);
Christopher Tate2f558d22019-01-17 16:58:31 -08001112 // Set this to null so that the reference
1113 // (local and remote) isn't kept in the mBroadcastHistory.
1114 r.resultTo = null;
1115 } catch (RemoteException e) {
1116 r.resultTo = null;
1117 Slog.w(TAG, "Failure ["
1118 + mQueueName + "] sending broadcast result of "
1119 + r.intent, e);
1120 }
Amith Yamasani4e820542018-05-09 13:37:20 -07001121 }
1122 }
1123
1124 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Cancelling BROADCAST_TIMEOUT_MSG");
1125 cancelBroadcastTimeoutLocked();
1126
1127 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST,
1128 "Finished with ordered broadcast " + r);
1129
1130 // ... and on to the next...
1131 addBroadcastToHistoryLocked(r);
1132 if (r.intent.getComponent() == null && r.intent.getPackage() == null
1133 && (r.intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) {
1134 // This was an implicit broadcast... let's record it for posterity.
1135 mService.addBroadcastStatLocked(r.intent.getAction(), r.callerPackage,
1136 r.manifestCount, r.manifestSkipCount, r.finishTime-r.dispatchTime);
1137 }
Christopher Tate2f558d22019-01-17 16:58:31 -08001138 mDispatcher.retireBroadcastLocked(r);
Amith Yamasani4e820542018-05-09 13:37:20 -07001139 r = null;
1140 looped = true;
1141 continue;
1142 }
Christopher Tate2f558d22019-01-17 16:58:31 -08001143
1144 // Check whether the next receiver is under deferral policy, and handle that
1145 // accordingly. If the current broadcast was already part of deferred-delivery
1146 // tracking, we know that it must now be deliverable as-is without re-deferral.
1147 if (!r.deferred) {
1148 final int receiverUid = r.getReceiverUid(r.receivers.get(r.nextReceiver));
1149 if (mDispatcher.isDeferringLocked(receiverUid)) {
1150 if (DEBUG_BROADCAST_DEFERRAL) {
1151 Slog.i(TAG_BROADCAST, "Next receiver in " + r + " uid " + receiverUid
1152 + " at " + r.nextReceiver + " is under deferral");
1153 }
1154 // If this is the only (remaining) receiver in the broadcast, "splitting"
1155 // doesn't make sense -- just defer it as-is and retire it as the
1156 // currently active outgoing broadcast.
1157 BroadcastRecord defer;
1158 if (r.nextReceiver + 1 == numReceivers) {
1159 if (DEBUG_BROADCAST_DEFERRAL) {
Christopher Tate41c56912019-03-01 18:56:58 -08001160 Slog.i(TAG_BROADCAST, "Sole receiver of " + r
Christopher Tate2f558d22019-01-17 16:58:31 -08001161 + " is under deferral; setting aside and proceeding");
1162 }
1163 defer = r;
1164 mDispatcher.retireBroadcastLocked(r);
1165 } else {
1166 // Nontrivial case; split out 'uid's receivers to a new broadcast record
1167 // and defer that, then loop and pick up continuing delivery of the current
1168 // record (now absent those receivers).
1169
1170 // The split operation is guaranteed to match at least at 'nextReceiver'
1171 defer = r.splitRecipientsLocked(receiverUid, r.nextReceiver);
1172 if (DEBUG_BROADCAST_DEFERRAL) {
1173 Slog.i(TAG_BROADCAST, "Post split:");
1174 Slog.i(TAG_BROADCAST, "Original broadcast receivers:");
1175 for (int i = 0; i < r.receivers.size(); i++) {
1176 Slog.i(TAG_BROADCAST, " " + r.receivers.get(i));
1177 }
1178 Slog.i(TAG_BROADCAST, "Split receivers:");
1179 for (int i = 0; i < defer.receivers.size(); i++) {
1180 Slog.i(TAG_BROADCAST, " " + defer.receivers.get(i));
1181 }
1182 }
1183 // Track completion refcount as well if relevant
1184 if (r.resultTo != null) {
1185 int token = r.splitToken;
1186 if (token == 0) {
1187 // first split of this record; refcount for 'r' and 'deferred'
1188 r.splitToken = defer.splitToken = nextSplitTokenLocked();
1189 mSplitRefcounts.put(r.splitToken, 2);
Christopher Tate41c56912019-03-01 18:56:58 -08001190 if (DEBUG_BROADCAST_DEFERRAL) {
1191 Slog.i(TAG_BROADCAST,
1192 "Broadcast needs split refcount; using new token "
1193 + r.splitToken);
1194 }
Christopher Tate2f558d22019-01-17 16:58:31 -08001195 } else {
1196 // new split from an already-refcounted situation; increment count
1197 final int curCount = mSplitRefcounts.get(token);
1198 if (DEBUG_BROADCAST_DEFERRAL) {
1199 if (curCount == 0) {
Christopher Tate41c56912019-03-01 18:56:58 -08001200 Slog.wtf(TAG_BROADCAST,
1201 "Split refcount is zero with token for " + r);
Christopher Tate2f558d22019-01-17 16:58:31 -08001202 }
1203 }
1204 mSplitRefcounts.put(token, curCount + 1);
Christopher Tate41c56912019-03-01 18:56:58 -08001205 if (DEBUG_BROADCAST_DEFERRAL) {
1206 Slog.i(TAG_BROADCAST, "New split count for token " + token
1207 + " is " + (curCount + 1));
1208 }
Christopher Tate2f558d22019-01-17 16:58:31 -08001209 }
1210 }
1211 }
1212 mDispatcher.addDeferredBroadcast(receiverUid, defer);
1213 r = null;
1214 looped = true;
1215 continue;
1216 }
1217 }
Amith Yamasani4e820542018-05-09 13:37:20 -07001218 } while (r == null);
1219
1220 // Get the next receiver...
1221 int recIdx = r.nextReceiver++;
1222
1223 // Keep track of when this receiver started, and make sure there
1224 // is a timeout message pending to kill it if need be.
1225 r.receiverTime = SystemClock.uptimeMillis();
1226 if (recIdx == 0) {
1227 r.dispatchTime = r.receiverTime;
1228 r.dispatchClockTime = System.currentTimeMillis();
Ng Zhi An7ff7fdb2019-01-16 15:35:51 -08001229
1230 if (mLogLatencyMetrics) {
1231 StatsLog.write(
1232 StatsLog.BROADCAST_DISPATCH_LATENCY_REPORTED,
1233 r.dispatchClockTime - r.enqueueClockTime);
1234 }
1235
Amith Yamasani4e820542018-05-09 13:37:20 -07001236 if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
1237 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1238 createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_PENDING),
1239 System.identityHashCode(r));
1240 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1241 createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_DELIVERED),
1242 System.identityHashCode(r));
1243 }
1244 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Processing ordered broadcast ["
1245 + mQueueName + "] " + r);
1246 }
1247 if (! mPendingBroadcastTimeoutMessage) {
Christopher Tate2f558d22019-01-17 16:58:31 -08001248 long timeoutTime = r.receiverTime + mConstants.TIMEOUT;
Amith Yamasani4e820542018-05-09 13:37:20 -07001249 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
1250 "Submitting BROADCAST_TIMEOUT_MSG ["
1251 + mQueueName + "] for " + r + " at " + timeoutTime);
1252 setBroadcastTimeoutLocked(timeoutTime);
1253 }
1254
1255 final BroadcastOptions brOptions = r.options;
1256 final Object nextReceiver = r.receivers.get(recIdx);
1257
1258 if (nextReceiver instanceof BroadcastFilter) {
1259 // Simple case: this is a registered receiver who gets
1260 // a direct call.
1261 BroadcastFilter filter = (BroadcastFilter)nextReceiver;
1262 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
1263 "Delivering ordered ["
1264 + mQueueName + "] to registered "
1265 + filter + ": " + r);
1266 deliverToRegisteredReceiverLocked(r, filter, r.ordered, recIdx);
1267 if (r.receiver == null || !r.ordered) {
1268 // The receiver has already finished, so schedule to
1269 // process the next one.
1270 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Quick finishing ["
1271 + mQueueName + "]: ordered="
1272 + r.ordered + " receiver=" + r.receiver);
1273 r.state = BroadcastRecord.IDLE;
1274 scheduleBroadcastsLocked();
1275 } else {
Michal Karpinski4b8aef22019-02-21 14:56:51 +00001276 if (filter.receiverList != null) {
1277 maybeAddAllowBackgroundActivityStartsToken(filter.receiverList.app, r);
1278 }
Amith Yamasani4e820542018-05-09 13:37:20 -07001279 if (brOptions != null && brOptions.getTemporaryAppWhitelistDuration() > 0) {
1280 scheduleTempWhitelistLocked(filter.owningUid,
1281 brOptions.getTemporaryAppWhitelistDuration(), r);
1282 }
1283 }
1284 return;
1285 }
1286
1287 // Hard case: need to instantiate the receiver, possibly
1288 // starting its application process to host it.
1289
1290 ResolveInfo info =
1291 (ResolveInfo)nextReceiver;
1292 ComponentName component = new ComponentName(
1293 info.activityInfo.applicationInfo.packageName,
1294 info.activityInfo.name);
1295
1296 boolean skip = false;
1297 if (brOptions != null &&
1298 (info.activityInfo.applicationInfo.targetSdkVersion
1299 < brOptions.getMinManifestReceiverApiLevel() ||
1300 info.activityInfo.applicationInfo.targetSdkVersion
1301 > brOptions.getMaxManifestReceiverApiLevel())) {
1302 skip = true;
1303 }
Dianne Hackborn769b2e72018-12-05 08:51:20 -08001304 if (!skip && !mService.validateAssociationAllowedLocked(r.callerPackage, r.callingUid,
1305 component.getPackageName(), info.activityInfo.applicationInfo.uid)) {
1306 Slog.w(TAG, "Association not allowed: broadcasting "
1307 + r.intent.toString()
1308 + " from " + r.callerPackage + " (pid=" + r.callingPid
1309 + ", uid=" + r.callingUid + ") to " + component.flattenToShortString());
1310 skip = true;
1311 }
1312 if (!skip) {
1313 skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,
1314 r.callingPid, r.resolvedType, info.activityInfo.applicationInfo.uid);
1315 if (skip) {
1316 Slog.w(TAG, "Firewall blocked: broadcasting "
1317 + r.intent.toString()
1318 + " from " + r.callerPackage + " (pid=" + r.callingPid
1319 + ", uid=" + r.callingUid + ") to " + component.flattenToShortString());
1320 }
1321 }
Amith Yamasani4e820542018-05-09 13:37:20 -07001322 int perm = mService.checkComponentPermission(info.activityInfo.permission,
1323 r.callingPid, r.callingUid, info.activityInfo.applicationInfo.uid,
1324 info.activityInfo.exported);
1325 if (!skip && perm != PackageManager.PERMISSION_GRANTED) {
1326 if (!info.activityInfo.exported) {
1327 Slog.w(TAG, "Permission Denial: broadcasting "
1328 + r.intent.toString()
1329 + " from " + r.callerPackage + " (pid=" + r.callingPid
1330 + ", uid=" + r.callingUid + ")"
1331 + " is not exported from uid " + info.activityInfo.applicationInfo.uid
1332 + " due to receiver " + component.flattenToShortString());
1333 } else {
1334 Slog.w(TAG, "Permission Denial: broadcasting "
1335 + r.intent.toString()
1336 + " from " + r.callerPackage + " (pid=" + r.callingPid
1337 + ", uid=" + r.callingUid + ")"
1338 + " requires " + info.activityInfo.permission
1339 + " due to receiver " + component.flattenToShortString());
1340 }
1341 skip = true;
1342 } else if (!skip && info.activityInfo.permission != null) {
1343 final int opCode = AppOpsManager.permissionToOpCode(info.activityInfo.permission);
1344 if (opCode != AppOpsManager.OP_NONE
1345 && mService.mAppOpsService.noteOperation(opCode, r.callingUid,
1346 r.callerPackage) != AppOpsManager.MODE_ALLOWED) {
1347 Slog.w(TAG, "Appop Denial: broadcasting "
1348 + r.intent.toString()
1349 + " from " + r.callerPackage + " (pid="
1350 + r.callingPid + ", uid=" + r.callingUid + ")"
1351 + " requires appop " + AppOpsManager.permissionToOp(
1352 info.activityInfo.permission)
1353 + " due to registered receiver "
1354 + component.flattenToShortString());
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001355 skip = true;
1356 }
Amith Yamasani4e820542018-05-09 13:37:20 -07001357 }
1358 if (!skip && info.activityInfo.applicationInfo.uid != Process.SYSTEM_UID &&
1359 r.requiredPermissions != null && r.requiredPermissions.length > 0) {
1360 for (int i = 0; i < r.requiredPermissions.length; i++) {
1361 String requiredPermission = r.requiredPermissions[i];
1362 try {
1363 perm = AppGlobals.getPackageManager().
1364 checkPermission(requiredPermission,
1365 info.activityInfo.applicationInfo.packageName,
1366 UserHandle
1367 .getUserId(info.activityInfo.applicationInfo.uid));
1368 } catch (RemoteException e) {
1369 perm = PackageManager.PERMISSION_DENIED;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001370 }
Amith Yamasani4e820542018-05-09 13:37:20 -07001371 if (perm != PackageManager.PERMISSION_GRANTED) {
1372 Slog.w(TAG, "Permission Denial: receiving "
1373 + r.intent + " to "
1374 + component.flattenToShortString()
1375 + " requires " + requiredPermission
1376 + " due to sender " + r.callerPackage
1377 + " (uid " + r.callingUid + ")");
1378 skip = true;
1379 break;
1380 }
1381 int appOp = AppOpsManager.permissionToOpCode(requiredPermission);
1382 if (appOp != AppOpsManager.OP_NONE && appOp != r.appOp
1383 && mService.mAppOpsService.noteOperation(appOp,
1384 info.activityInfo.applicationInfo.uid, info.activityInfo.packageName)
1385 != AppOpsManager.MODE_ALLOWED) {
1386 Slog.w(TAG, "Appop Denial: receiving "
1387 + r.intent + " to "
1388 + component.flattenToShortString()
Svet Ganov99b60432015-06-27 13:15:22 -07001389 + " requires appop " + AppOpsManager.permissionToOp(
Amith Yamasani4e820542018-05-09 13:37:20 -07001390 requiredPermission)
1391 + " due to sender " + r.callerPackage
1392 + " (uid " + r.callingUid + ")");
1393 skip = true;
1394 break;
1395 }
1396 }
1397 }
1398 if (!skip && r.appOp != AppOpsManager.OP_NONE
1399 && mService.mAppOpsService.noteOperation(r.appOp,
1400 info.activityInfo.applicationInfo.uid, info.activityInfo.packageName)
1401 != AppOpsManager.MODE_ALLOWED) {
1402 Slog.w(TAG, "Appop Denial: receiving "
1403 + r.intent + " to "
1404 + component.flattenToShortString()
1405 + " requires appop " + AppOpsManager.opToName(r.appOp)
1406 + " due to sender " + r.callerPackage
1407 + " (uid " + r.callingUid + ")");
1408 skip = true;
1409 }
Amith Yamasani4e820542018-05-09 13:37:20 -07001410 boolean isSingleton = false;
1411 try {
1412 isSingleton = mService.isSingleton(info.activityInfo.processName,
1413 info.activityInfo.applicationInfo,
1414 info.activityInfo.name, info.activityInfo.flags);
1415 } catch (SecurityException e) {
1416 Slog.w(TAG, e.getMessage());
1417 skip = true;
1418 }
1419 if ((info.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) {
1420 if (ActivityManager.checkUidPermission(
1421 android.Manifest.permission.INTERACT_ACROSS_USERS,
1422 info.activityInfo.applicationInfo.uid)
1423 != PackageManager.PERMISSION_GRANTED) {
1424 Slog.w(TAG, "Permission Denial: Receiver " + component.flattenToShortString()
1425 + " requests FLAG_SINGLE_USER, but app does not hold "
1426 + android.Manifest.permission.INTERACT_ACROSS_USERS);
1427 skip = true;
1428 }
1429 }
1430 if (!skip && info.activityInfo.applicationInfo.isInstantApp()
1431 && r.callingUid != info.activityInfo.applicationInfo.uid) {
1432 Slog.w(TAG, "Instant App Denial: receiving "
1433 + r.intent
1434 + " to " + component.flattenToShortString()
1435 + " due to sender " + r.callerPackage
1436 + " (uid " + r.callingUid + ")"
1437 + " Instant Apps do not support manifest receivers");
1438 skip = true;
1439 }
1440 if (!skip && r.callerInstantApp
1441 && (info.activityInfo.flags & ActivityInfo.FLAG_VISIBLE_TO_INSTANT_APP) == 0
1442 && r.callingUid != info.activityInfo.applicationInfo.uid) {
1443 Slog.w(TAG, "Instant App Denial: receiving "
1444 + r.intent
1445 + " to " + component.flattenToShortString()
1446 + " requires receiver have visibleToInstantApps set"
1447 + " due to sender " + r.callerPackage
1448 + " (uid " + r.callingUid + ")");
1449 skip = true;
1450 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001451 if (r.curApp != null && r.curApp.isCrashing()) {
Amith Yamasani4e820542018-05-09 13:37:20 -07001452 // If the target process is crashing, just skip it.
1453 Slog.w(TAG, "Skipping deliver ordered [" + mQueueName + "] " + r
1454 + " to " + r.curApp + ": process crashing");
1455 skip = true;
1456 }
1457 if (!skip) {
1458 boolean isAvailable = false;
1459 try {
1460 isAvailable = AppGlobals.getPackageManager().isPackageAvailable(
1461 info.activityInfo.packageName,
1462 UserHandle.getUserId(info.activityInfo.applicationInfo.uid));
1463 } catch (Exception e) {
1464 // all such failures mean we skip this receiver
1465 Slog.w(TAG, "Exception getting recipient info for "
1466 + info.activityInfo.packageName, e);
1467 }
1468 if (!isAvailable) {
1469 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
1470 "Skipping delivery to " + info.activityInfo.packageName + " / "
1471 + info.activityInfo.applicationInfo.uid
1472 + " : package no longer available");
1473 skip = true;
1474 }
1475 }
1476
1477 // If permissions need a review before any of the app components can run, we drop
1478 // the broadcast and if the calling app is in the foreground and the broadcast is
1479 // explicit we launch the review UI passing it a pending intent to send the skipped
1480 // broadcast.
Philip P. Moltmann6c644e62018-07-18 15:41:24 -07001481 if (!skip) {
Amith Yamasani4e820542018-05-09 13:37:20 -07001482 if (!requestStartTargetPermissionsReviewIfNeededLocked(r,
1483 info.activityInfo.packageName, UserHandle.getUserId(
1484 info.activityInfo.applicationInfo.uid))) {
1485 skip = true;
1486 }
1487 }
1488
1489 // This is safe to do even if we are skipping the broadcast, and we need
1490 // this information now to evaluate whether it is going to be allowed to run.
1491 final int receiverUid = info.activityInfo.applicationInfo.uid;
1492 // If it's a singleton, it needs to be the same app or a special app
1493 if (r.callingUid != Process.SYSTEM_UID && isSingleton
1494 && mService.isValidSingletonCall(r.callingUid, receiverUid)) {
1495 info.activityInfo = mService.getActivityInfoForUser(info.activityInfo, 0);
1496 }
1497 String targetProcess = info.activityInfo.processName;
1498 ProcessRecord app = mService.getProcessRecordLocked(targetProcess,
1499 info.activityInfo.applicationInfo.uid, false);
1500
1501 if (!skip) {
1502 final int allowed = mService.getAppStartModeLocked(
1503 info.activityInfo.applicationInfo.uid, info.activityInfo.packageName,
1504 info.activityInfo.applicationInfo.targetSdkVersion, -1, true, false, false);
1505 if (allowed != ActivityManager.APP_START_MODE_NORMAL) {
1506 // We won't allow this receiver to be launched if the app has been
1507 // completely disabled from launches, or it was not explicitly sent
1508 // to it and the app is in a state that should not receive it
1509 // (depending on how getAppStartModeLocked has determined that).
1510 if (allowed == ActivityManager.APP_START_MODE_DISABLED) {
1511 Slog.w(TAG, "Background execution disabled: receiving "
1512 + r.intent + " to "
1513 + component.flattenToShortString());
1514 skip = true;
1515 } else if (((r.intent.getFlags()&Intent.FLAG_RECEIVER_EXCLUDE_BACKGROUND) != 0)
1516 || (r.intent.getComponent() == null
1517 && r.intent.getPackage() == null
1518 && ((r.intent.getFlags()
1519 & Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND) == 0)
1520 && !isSignaturePerm(r.requiredPermissions))) {
1521 mService.addBackgroundCheckViolationLocked(r.intent.getAction(),
1522 component.getPackageName());
1523 Slog.w(TAG, "Background execution not allowed: receiving "
1524 + r.intent + " to "
Svet Ganov99b60432015-06-27 13:15:22 -07001525 + component.flattenToShortString());
1526 skip = true;
1527 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001528 }
Amith Yamasani4e820542018-05-09 13:37:20 -07001529 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001530
Amith Yamasani4e820542018-05-09 13:37:20 -07001531 if (!skip && !Intent.ACTION_SHUTDOWN.equals(r.intent.getAction())
1532 && !mService.mUserController
1533 .isUserRunning(UserHandle.getUserId(info.activityInfo.applicationInfo.uid),
1534 0 /* flags */)) {
1535 skip = true;
1536 Slog.w(TAG,
1537 "Skipping delivery to " + info.activityInfo.packageName + " / "
1538 + info.activityInfo.applicationInfo.uid + " : user is not running");
1539 }
Svet Ganov9c165d72015-12-01 19:52:26 -08001540
Amith Yamasani4e820542018-05-09 13:37:20 -07001541 if (skip) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001542 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Amith Yamasani4e820542018-05-09 13:37:20 -07001543 "Skipping delivery of ordered [" + mQueueName + "] "
Christopher Tate41c56912019-03-01 18:56:58 -08001544 + r + " for reason described above");
Amith Yamasani4e820542018-05-09 13:37:20 -07001545 r.delivery[recIdx] = BroadcastRecord.DELIVERY_SKIPPED;
1546 r.receiver = null;
1547 r.curFilter = null;
1548 r.state = BroadcastRecord.IDLE;
1549 r.manifestSkipCount++;
1550 scheduleBroadcastsLocked();
1551 return;
1552 }
1553 r.manifestCount++;
1554
1555 r.delivery[recIdx] = BroadcastRecord.DELIVERY_DELIVERED;
1556 r.state = BroadcastRecord.APP_RECEIVE;
1557 r.curComponent = component;
1558 r.curReceiver = info.activityInfo;
1559 if (DEBUG_MU && r.callingUid > UserHandle.PER_USER_RANGE) {
1560 Slog.v(TAG_MU, "Updated broadcast record activity info for secondary user, "
1561 + info.activityInfo + ", callingUid = " + r.callingUid + ", uid = "
1562 + receiverUid);
1563 }
1564
1565 if (brOptions != null && brOptions.getTemporaryAppWhitelistDuration() > 0) {
1566 scheduleTempWhitelistLocked(receiverUid,
1567 brOptions.getTemporaryAppWhitelistDuration(), r);
1568 }
1569
1570 // Broadcast is being executed, its package can't be stopped.
1571 try {
1572 AppGlobals.getPackageManager().setPackageStoppedState(
Christopher Tatea3a455d2018-12-07 17:22:46 -08001573 r.curComponent.getPackageName(), false, r.userId);
Amith Yamasani4e820542018-05-09 13:37:20 -07001574 } catch (RemoteException e) {
1575 } catch (IllegalArgumentException e) {
1576 Slog.w(TAG, "Failed trying to unstop package "
1577 + r.curComponent.getPackageName() + ": " + e);
1578 }
1579
1580 // Is this receiver's application already running?
1581 if (app != null && app.thread != null && !app.killed) {
1582 try {
1583 app.addPackage(info.activityInfo.packageName,
1584 info.activityInfo.applicationInfo.versionCode, mService.mProcessStats);
Michal Karpinski4b8aef22019-02-21 14:56:51 +00001585 maybeAddAllowBackgroundActivityStartsToken(app, r);
Amith Yamasani4e820542018-05-09 13:37:20 -07001586 processCurBroadcastLocked(r, app, skipOomAdj);
1587 return;
1588 } catch (RemoteException e) {
1589 Slog.w(TAG, "Exception when sending broadcast to "
1590 + r.curComponent, e);
1591 } catch (RuntimeException e) {
1592 Slog.wtf(TAG, "Failed sending broadcast to "
1593 + r.curComponent + " with " + r.intent, e);
1594 // If some unexpected exception happened, just skip
1595 // this broadcast. At this point we are not in the call
1596 // from a client, so throwing an exception out from here
1597 // will crash the entire system instead of just whoever
1598 // sent the broadcast.
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001599 logBroadcastReceiverDiscardLocked(r);
1600 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -07001601 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001602 scheduleBroadcastsLocked();
Amith Yamasani4e820542018-05-09 13:37:20 -07001603 // We need to reset the state if we failed to start the receiver.
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001604 r.state = BroadcastRecord.IDLE;
1605 return;
1606 }
1607
Amith Yamasani4e820542018-05-09 13:37:20 -07001608 // If a dead object exception was thrown -- fall through to
1609 // restart the application.
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001610 }
Amith Yamasani4e820542018-05-09 13:37:20 -07001611
1612 // Not running -- get it started, to be executed when the app comes up.
1613 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
1614 "Need to start app ["
1615 + mQueueName + "] " + targetProcess + " for broadcast " + r);
1616 if ((r.curApp=mService.startProcessLocked(targetProcess,
1617 info.activityInfo.applicationInfo, true,
1618 r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND,
1619 "broadcast", r.curComponent,
1620 (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0, false, false))
1621 == null) {
1622 // Ah, this recipient is unavailable. Finish it if necessary,
1623 // and mark the broadcast record as ready for the next.
1624 Slog.w(TAG, "Unable to launch app "
1625 + info.activityInfo.applicationInfo.packageName + "/"
1626 + receiverUid + " for broadcast "
1627 + r.intent + ": process is bad");
1628 logBroadcastReceiverDiscardLocked(r);
1629 finishReceiverLocked(r, r.resultCode, r.resultData,
1630 r.resultExtras, r.resultAbort, false);
1631 scheduleBroadcastsLocked();
1632 r.state = BroadcastRecord.IDLE;
1633 return;
1634 }
1635
Michal Karpinski4b8aef22019-02-21 14:56:51 +00001636 maybeAddAllowBackgroundActivityStartsToken(r.curApp, r);
Amith Yamasani4e820542018-05-09 13:37:20 -07001637 mPendingBroadcast = r;
1638 mPendingBroadcastRecvIndex = recIdx;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001639 }
1640
Michal Karpinski4b8aef22019-02-21 14:56:51 +00001641 private void maybeAddAllowBackgroundActivityStartsToken(ProcessRecord proc, BroadcastRecord r) {
1642 if (r == null || proc == null || !r.allowBackgroundActivityStarts) {
1643 return;
1644 }
1645 String msgToken = (proc.toShortString() + r.toString()).intern();
1646 // first, if there exists a past scheduled request to remove this token, drop
1647 // that request - we don't want the token to be swept from under our feet...
1648 mHandler.removeCallbacksAndMessages(msgToken);
1649 // ...then add the token
1650 proc.addAllowBackgroundActivityStartsToken(r);
1651 }
1652
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001653 final void setBroadcastTimeoutLocked(long timeoutTime) {
1654 if (! mPendingBroadcastTimeoutMessage) {
1655 Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG, this);
1656 mHandler.sendMessageAtTime(msg, timeoutTime);
1657 mPendingBroadcastTimeoutMessage = true;
1658 }
1659 }
1660
1661 final void cancelBroadcastTimeoutLocked() {
1662 if (mPendingBroadcastTimeoutMessage) {
1663 mHandler.removeMessages(BROADCAST_TIMEOUT_MSG, this);
1664 mPendingBroadcastTimeoutMessage = false;
1665 }
1666 }
1667
1668 final void broadcastTimeoutLocked(boolean fromMsg) {
1669 if (fromMsg) {
1670 mPendingBroadcastTimeoutMessage = false;
1671 }
1672
Christopher Tate2f558d22019-01-17 16:58:31 -08001673 if (mDispatcher.isEmpty() || mDispatcher.getActiveBroadcastLocked() == null) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001674 return;
1675 }
1676
1677 long now = SystemClock.uptimeMillis();
Christopher Tate2f558d22019-01-17 16:58:31 -08001678 BroadcastRecord r = mDispatcher.getActiveBroadcastLocked();
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001679 if (fromMsg) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001680 if (!mService.mProcessesReady) {
Christopher Tate1b72ca32019-01-30 16:25:53 -08001681 // Only process broadcast timeouts if the system is ready; some early
1682 // broadcasts do heavy work setting up system facilities
1683 return;
1684 }
1685
1686 // If the broadcast is generally exempt from timeout tracking, we're done
1687 if (r.timeoutExempt) {
1688 if (DEBUG_BROADCAST) {
1689 Slog.i(TAG_BROADCAST, "Broadcast timeout but it's exempt: "
1690 + r.intent.getAction());
1691 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001692 return;
1693 }
1694
Christopher Tate2f558d22019-01-17 16:58:31 -08001695 long timeoutTime = r.receiverTime + mConstants.TIMEOUT;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001696 if (timeoutTime > now) {
1697 // We can observe premature timeouts because we do not cancel and reset the
1698 // broadcast timeout message after each receiver finishes. Instead, we set up
1699 // an initial timeout then kick it down the road a little further as needed
1700 // when it expires.
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001701 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001702 "Premature timeout ["
1703 + mQueueName + "] @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for "
1704 + timeoutTime);
1705 setBroadcastTimeoutLocked(timeoutTime);
1706 return;
1707 }
1708 }
1709
Christopher Tate2f558d22019-01-17 16:58:31 -08001710 if (r.state == BroadcastRecord.WAITING_SERVICES) {
Dianne Hackborn6285a322013-09-18 12:09:47 -07001711 // In this case the broadcast had already finished, but we had decided to wait
1712 // for started services to finish as well before going on. So if we have actually
1713 // waited long enough time timeout the broadcast, let's give up on the whole thing
1714 // and just move on to the next.
Christopher Tate2f558d22019-01-17 16:58:31 -08001715 Slog.i(TAG, "Waited long enough for: " + (r.curComponent != null
1716 ? r.curComponent.flattenToShortString() : "(null)"));
1717 r.curComponent = null;
1718 r.state = BroadcastRecord.IDLE;
Dianne Hackborn6285a322013-09-18 12:09:47 -07001719 processNextBroadcast(false);
1720 return;
1721 }
1722
Christopher Tatef2370162018-03-16 16:26:23 -07001723 // If the receiver app is being debugged we quietly ignore unresponsiveness, just
1724 // tidying up and moving on to the next broadcast without crashing or ANRing this
1725 // app just because it's stopped at a breakpoint.
Wale Ogunwale906f9c62018-07-23 11:23:44 -07001726 final boolean debugging = (r.curApp != null && r.curApp.isDebugging());
Christopher Tatef2370162018-03-16 16:26:23 -07001727
1728 Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r.receiver
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001729 + ", started " + (now - r.receiverTime) + "ms ago");
1730 r.receiverTime = now;
Christopher Tatef2370162018-03-16 16:26:23 -07001731 if (!debugging) {
1732 r.anrCount++;
1733 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001734
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001735 ProcessRecord app = null;
1736 String anrMessage = null;
1737
Christopher Tatea6b2c882017-09-01 11:41:39 -07001738 Object curReceiver;
1739 if (r.nextReceiver > 0) {
1740 curReceiver = r.receivers.get(r.nextReceiver-1);
1741 r.delivery[r.nextReceiver-1] = BroadcastRecord.DELIVERY_TIMEOUT;
1742 } else {
1743 curReceiver = r.curReceiver;
1744 }
1745 Slog.w(TAG, "Receiver during timeout of " + r + " : " + curReceiver);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001746 logBroadcastReceiverDiscardLocked(r);
Christopher Tatea6b2c882017-09-01 11:41:39 -07001747 if (curReceiver != null && curReceiver instanceof BroadcastFilter) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001748 BroadcastFilter bf = (BroadcastFilter)curReceiver;
1749 if (bf.receiverList.pid != 0
1750 && bf.receiverList.pid != ActivityManagerService.MY_PID) {
1751 synchronized (mService.mPidsSelfLocked) {
1752 app = mService.mPidsSelfLocked.get(
1753 bf.receiverList.pid);
1754 }
1755 }
1756 } else {
1757 app = r.curApp;
1758 }
1759
1760 if (app != null) {
1761 anrMessage = "Broadcast of " + r.intent.toString();
1762 }
1763
1764 if (mPendingBroadcast == r) {
1765 mPendingBroadcast = null;
1766 }
1767
1768 // Move on to the next receiver.
1769 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -07001770 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001771 scheduleBroadcastsLocked();
1772
Christopher Tatef2370162018-03-16 16:26:23 -07001773 if (!debugging && anrMessage != null) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001774 // Post the ANR to the handler since we do not want to process ANRs while
1775 // potentially holding our lock.
1776 mHandler.post(new AppNotResponding(app, anrMessage));
1777 }
1778 }
1779
Christopher Tatef278f122015-04-22 13:12:01 -07001780 private final int ringAdvance(int x, final int increment, final int ringSize) {
1781 x += increment;
1782 if (x < 0) return (ringSize - 1);
1783 else if (x >= ringSize) return 0;
1784 else return x;
1785 }
1786
Makoto Onuki97f82f22017-05-31 16:20:21 -07001787 private final void addBroadcastToHistoryLocked(BroadcastRecord original) {
1788 if (original.callingUid < 0) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001789 // This was from a registerReceiver() call; ignore it.
1790 return;
1791 }
Makoto Onuki97f82f22017-05-31 16:20:21 -07001792 original.finishTime = SystemClock.uptimeMillis();
Christopher Tatef278f122015-04-22 13:12:01 -07001793
Carmen Jacksona68e3452017-01-17 14:01:33 -08001794 if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
1795 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER,
Makoto Onuki97f82f22017-05-31 16:20:21 -07001796 createBroadcastTraceTitle(original, BroadcastRecord.DELIVERY_DELIVERED),
1797 System.identityHashCode(original));
Carmen Jacksona68e3452017-01-17 14:01:33 -08001798 }
1799
Makoto Onuki97f82f22017-05-31 16:20:21 -07001800 // Note sometimes (only for sticky broadcasts?) we reuse BroadcastRecords,
1801 // So don't change the incoming record directly.
1802 final BroadcastRecord historyRecord = original.maybeStripForHistory();
1803
1804 mBroadcastHistory[mHistoryNext] = historyRecord;
Christopher Tatef278f122015-04-22 13:12:01 -07001805 mHistoryNext = ringAdvance(mHistoryNext, 1, MAX_BROADCAST_HISTORY);
1806
Makoto Onuki97f82f22017-05-31 16:20:21 -07001807 mBroadcastSummaryHistory[mSummaryHistoryNext] = historyRecord.intent;
1808 mSummaryHistoryEnqueueTime[mSummaryHistoryNext] = historyRecord.enqueueClockTime;
1809 mSummaryHistoryDispatchTime[mSummaryHistoryNext] = historyRecord.dispatchClockTime;
Christopher Tatef278f122015-04-22 13:12:01 -07001810 mSummaryHistoryFinishTime[mSummaryHistoryNext] = System.currentTimeMillis();
1811 mSummaryHistoryNext = ringAdvance(mSummaryHistoryNext, 1, MAX_BROADCAST_SUMMARY_HISTORY);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001812 }
1813
Wale Ogunwaleca1c1252015-05-15 12:49:13 -07001814 boolean cleanupDisabledPackageReceiversLocked(
1815 String packageName, Set<String> filterByClasses, int userId, boolean doit) {
1816 boolean didSomething = false;
1817 for (int i = mParallelBroadcasts.size() - 1; i >= 0; i--) {
1818 didSomething |= mParallelBroadcasts.get(i).cleanupDisabledPackageReceiversLocked(
1819 packageName, filterByClasses, userId, doit);
1820 if (!doit && didSomething) {
1821 return true;
1822 }
1823 }
1824
Christopher Tate2f558d22019-01-17 16:58:31 -08001825 didSomething |= mDispatcher.cleanupDisabledPackageReceiversLocked(packageName,
1826 filterByClasses, userId, doit);
Wale Ogunwaleca1c1252015-05-15 12:49:13 -07001827
1828 return didSomething;
1829 }
1830
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001831 final void logBroadcastReceiverDiscardLocked(BroadcastRecord r) {
Wale Ogunwalea22c6322015-06-03 09:59:45 -07001832 final int logIndex = r.nextReceiver - 1;
1833 if (logIndex >= 0 && logIndex < r.receivers.size()) {
1834 Object curReceiver = r.receivers.get(logIndex);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001835 if (curReceiver instanceof BroadcastFilter) {
1836 BroadcastFilter bf = (BroadcastFilter) curReceiver;
1837 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001838 bf.owningUserId, System.identityHashCode(r),
Wale Ogunwalea22c6322015-06-03 09:59:45 -07001839 r.intent.getAction(), logIndex, System.identityHashCode(bf));
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001840 } else {
Wale Ogunwalea22c6322015-06-03 09:59:45 -07001841 ResolveInfo ri = (ResolveInfo) curReceiver;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001842 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001843 UserHandle.getUserId(ri.activityInfo.applicationInfo.uid),
Wale Ogunwalea22c6322015-06-03 09:59:45 -07001844 System.identityHashCode(r), r.intent.getAction(), logIndex, ri.toString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001845 }
1846 } else {
Wale Ogunwalea22c6322015-06-03 09:59:45 -07001847 if (logIndex < 0) Slog.w(TAG,
1848 "Discarding broadcast before first receiver is invoked: " + r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001849 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001850 -1, System.identityHashCode(r),
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001851 r.intent.getAction(),
1852 r.nextReceiver,
1853 "NONE");
1854 }
1855 }
1856
Carmen Jacksona68e3452017-01-17 14:01:33 -08001857 private String createBroadcastTraceTitle(BroadcastRecord record, int state) {
1858 return String.format("Broadcast %s from %s (%s) %s",
1859 state == BroadcastRecord.DELIVERY_PENDING ? "in queue" : "dispatched",
1860 record.callerPackage == null ? "" : record.callerPackage,
1861 record.callerApp == null ? "process unknown" : record.callerApp.toShortString(),
1862 record.intent == null ? "" : record.intent.getAction());
1863 }
1864
Christopher Tateb486f412019-02-12 18:05:17 -08001865 boolean isIdle() {
Christopher Tate2f558d22019-01-17 16:58:31 -08001866 return mParallelBroadcasts.isEmpty() && mDispatcher.isEmpty()
Jeff Sharkeyfd658132017-05-03 11:38:01 -06001867 && (mPendingBroadcast == null);
1868 }
1869
Christopher Tateb486f412019-02-12 18:05:17 -08001870 // Used by wait-for-broadcast-idle : fast-forward all current deferrals to
1871 // be immediately deliverable.
1872 void cancelDeferrals() {
Christopher Tate01c3ae12019-02-27 16:51:10 -08001873 synchronized (mService) {
1874 mDispatcher.cancelDeferralsLocked();
1875 scheduleBroadcastsLocked();
1876 }
Christopher Tateb486f412019-02-12 18:05:17 -08001877 }
1878
1879 String describeState() {
1880 synchronized (mService) {
1881 return mParallelBroadcasts.size() + " parallel; "
1882 + mDispatcher.describeStateLocked();
1883 }
1884 }
1885
Yi Jin129fc6c2017-09-28 15:48:38 -07001886 void writeToProto(ProtoOutputStream proto, long fieldId) {
1887 long token = proto.start(fieldId);
1888 proto.write(BroadcastQueueProto.QUEUE_NAME, mQueueName);
1889 int N;
1890 N = mParallelBroadcasts.size();
1891 for (int i = N - 1; i >= 0; i--) {
1892 mParallelBroadcasts.get(i).writeToProto(proto, BroadcastQueueProto.PARALLEL_BROADCASTS);
1893 }
Christopher Tate2f558d22019-01-17 16:58:31 -08001894 mDispatcher.writeToProto(proto, BroadcastQueueProto.ORDERED_BROADCASTS);
Yi Jin129fc6c2017-09-28 15:48:38 -07001895 if (mPendingBroadcast != null) {
1896 mPendingBroadcast.writeToProto(proto, BroadcastQueueProto.PENDING_BROADCAST);
1897 }
1898
1899 int lastIndex = mHistoryNext;
1900 int ringIndex = lastIndex;
1901 do {
1902 // increasing index = more recent entry, and we want to print the most
1903 // recent first and work backwards, so we roll through the ring backwards.
1904 ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_HISTORY);
1905 BroadcastRecord r = mBroadcastHistory[ringIndex];
1906 if (r != null) {
1907 r.writeToProto(proto, BroadcastQueueProto.HISTORICAL_BROADCASTS);
1908 }
1909 } while (ringIndex != lastIndex);
1910
1911 lastIndex = ringIndex = mSummaryHistoryNext;
1912 do {
1913 ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_SUMMARY_HISTORY);
1914 Intent intent = mBroadcastSummaryHistory[ringIndex];
1915 if (intent == null) {
1916 continue;
1917 }
1918 long summaryToken = proto.start(BroadcastQueueProto.HISTORICAL_BROADCASTS_SUMMARY);
1919 intent.writeToProto(proto, BroadcastQueueProto.BroadcastSummary.INTENT,
1920 false, true, true, false);
1921 proto.write(BroadcastQueueProto.BroadcastSummary.ENQUEUE_CLOCK_TIME_MS,
1922 mSummaryHistoryEnqueueTime[ringIndex]);
1923 proto.write(BroadcastQueueProto.BroadcastSummary.DISPATCH_CLOCK_TIME_MS,
1924 mSummaryHistoryDispatchTime[ringIndex]);
1925 proto.write(BroadcastQueueProto.BroadcastSummary.FINISH_CLOCK_TIME_MS,
1926 mSummaryHistoryFinishTime[ringIndex]);
1927 proto.end(summaryToken);
1928 } while (ringIndex != lastIndex);
1929 proto.end(token);
1930 }
1931
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001932 final boolean dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args,
1933 int opti, boolean dumpAll, String dumpPackage, boolean needSep) {
Ng Zhi Ana7f50d42018-09-18 10:51:34 -07001934 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Christopher Tate2f558d22019-01-17 16:58:31 -08001935 if (!mParallelBroadcasts.isEmpty() || !mDispatcher.isEmpty()
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001936 || mPendingBroadcast != null) {
1937 boolean printed = false;
Wale Ogunwaleca1c1252015-05-15 12:49:13 -07001938 for (int i = mParallelBroadcasts.size() - 1; i >= 0; i--) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001939 BroadcastRecord br = mParallelBroadcasts.get(i);
1940 if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) {
1941 continue;
1942 }
1943 if (!printed) {
1944 if (needSep) {
1945 pw.println();
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001946 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001947 needSep = true;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001948 printed = true;
1949 pw.println(" Active broadcasts [" + mQueueName + "]:");
1950 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001951 pw.println(" Active Broadcast " + mQueueName + " #" + i + ":");
Dianne Hackborn865907d2015-10-21 17:12:53 -07001952 br.dump(pw, " ", sdf);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001953 }
Christopher Tate2f558d22019-01-17 16:58:31 -08001954
1955 mDispatcher.dumpLocked(pw, dumpPackage, mQueueName, sdf);
1956
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001957 if (dumpPackage == null || (mPendingBroadcast != null
1958 && dumpPackage.equals(mPendingBroadcast.callerPackage))) {
Christopher Tate2f558d22019-01-17 16:58:31 -08001959 pw.println();
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001960 pw.println(" Pending broadcast [" + mQueueName + "]:");
1961 if (mPendingBroadcast != null) {
Dianne Hackborn865907d2015-10-21 17:12:53 -07001962 mPendingBroadcast.dump(pw, " ", sdf);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001963 } else {
1964 pw.println(" (null)");
1965 }
1966 needSep = true;
1967 }
1968 }
1969
Christopher Tate2f558d22019-01-17 16:58:31 -08001970 mConstants.dump(pw);
1971
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001972 int i;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001973 boolean printed = false;
Christopher Tatef278f122015-04-22 13:12:01 -07001974
1975 i = -1;
1976 int lastIndex = mHistoryNext;
1977 int ringIndex = lastIndex;
1978 do {
1979 // increasing index = more recent entry, and we want to print the most
1980 // recent first and work backwards, so we roll through the ring backwards.
1981 ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_HISTORY);
1982 BroadcastRecord r = mBroadcastHistory[ringIndex];
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001983 if (r == null) {
Christopher Tatef278f122015-04-22 13:12:01 -07001984 continue;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001985 }
Christopher Tatef278f122015-04-22 13:12:01 -07001986
1987 i++; // genuine record of some sort even if we're filtering it out
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001988 if (dumpPackage != null && !dumpPackage.equals(r.callerPackage)) {
1989 continue;
1990 }
1991 if (!printed) {
1992 if (needSep) {
1993 pw.println();
1994 }
1995 needSep = true;
1996 pw.println(" Historical broadcasts [" + mQueueName + "]:");
1997 printed = true;
1998 }
1999 if (dumpAll) {
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07002000 pw.print(" Historical Broadcast " + mQueueName + " #");
2001 pw.print(i); pw.println(":");
Dianne Hackborn865907d2015-10-21 17:12:53 -07002002 r.dump(pw, " ", sdf);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08002003 } else {
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07002004 pw.print(" #"); pw.print(i); pw.print(": "); pw.println(r);
2005 pw.print(" ");
2006 pw.println(r.intent.toShortString(false, true, true, false));
Dianne Hackborna40cfeb2013-03-25 17:49:36 -07002007 if (r.targetComp != null && r.targetComp != r.intent.getComponent()) {
2008 pw.print(" targetComp: "); pw.println(r.targetComp.toShortString());
2009 }
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07002010 Bundle bundle = r.intent.getExtras();
2011 if (bundle != null) {
2012 pw.print(" extras: "); pw.println(bundle.toString());
2013 }
2014 }
Christopher Tatef278f122015-04-22 13:12:01 -07002015 } while (ringIndex != lastIndex);
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07002016
2017 if (dumpPackage == null) {
Christopher Tatef278f122015-04-22 13:12:01 -07002018 lastIndex = ringIndex = mSummaryHistoryNext;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07002019 if (dumpAll) {
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07002020 printed = false;
Christopher Tatef278f122015-04-22 13:12:01 -07002021 i = -1;
2022 } else {
2023 // roll over the 'i' full dumps that have already been issued
2024 for (int j = i;
2025 j > 0 && ringIndex != lastIndex;) {
2026 ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_SUMMARY_HISTORY);
2027 BroadcastRecord r = mBroadcastHistory[ringIndex];
2028 if (r == null) {
2029 continue;
2030 }
2031 j--;
2032 }
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07002033 }
Christopher Tatef278f122015-04-22 13:12:01 -07002034 // done skipping; dump the remainder of the ring. 'i' is still the ordinal within
2035 // the overall broadcast history.
Christopher Tatef278f122015-04-22 13:12:01 -07002036 do {
2037 ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_SUMMARY_HISTORY);
2038 Intent intent = mBroadcastSummaryHistory[ringIndex];
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07002039 if (intent == null) {
Christopher Tatef278f122015-04-22 13:12:01 -07002040 continue;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07002041 }
2042 if (!printed) {
2043 if (needSep) {
2044 pw.println();
2045 }
2046 needSep = true;
2047 pw.println(" Historical broadcasts summary [" + mQueueName + "]:");
2048 printed = true;
2049 }
2050 if (!dumpAll && i >= 50) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08002051 pw.println(" ...");
2052 break;
2053 }
Christopher Tatef278f122015-04-22 13:12:01 -07002054 i++;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07002055 pw.print(" #"); pw.print(i); pw.print(": ");
2056 pw.println(intent.toShortString(false, true, true, false));
Dianne Hackborn865907d2015-10-21 17:12:53 -07002057 pw.print(" ");
2058 TimeUtils.formatDuration(mSummaryHistoryDispatchTime[ringIndex]
2059 - mSummaryHistoryEnqueueTime[ringIndex], pw);
2060 pw.print(" dispatch ");
2061 TimeUtils.formatDuration(mSummaryHistoryFinishTime[ringIndex]
2062 - mSummaryHistoryDispatchTime[ringIndex], pw);
2063 pw.println(" finish");
2064 pw.print(" enq=");
2065 pw.print(sdf.format(new Date(mSummaryHistoryEnqueueTime[ringIndex])));
2066 pw.print(" disp=");
2067 pw.print(sdf.format(new Date(mSummaryHistoryDispatchTime[ringIndex])));
2068 pw.print(" fin=");
2069 pw.println(sdf.format(new Date(mSummaryHistoryFinishTime[ringIndex])));
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07002070 Bundle bundle = intent.getExtras();
2071 if (bundle != null) {
2072 pw.print(" extras: "); pw.println(bundle.toString());
2073 }
Christopher Tatef278f122015-04-22 13:12:01 -07002074 } while (ringIndex != lastIndex);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08002075 }
2076
2077 return needSep;
2078 }
2079}