blob: 75b5929b0c655fd5a69023126845f7cf5ebd895b [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
Carmen Jacksona68e3452017-01-17 14:01:33 -080019import android.os.Trace;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080020import java.io.FileDescriptor;
21import java.io.PrintWriter;
Christopher Tatef278f122015-04-22 13:12:01 -070022import java.text.SimpleDateFormat;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080023import java.util.ArrayList;
Christopher Tatef278f122015-04-22 13:12:01 -070024import java.util.Date;
Wale Ogunwaleca1c1252015-05-15 12:49:13 -070025import java.util.Set;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080026
Dianne Hackborn7d19e022012-08-07 19:12:33 -070027import android.app.ActivityManager;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080028import android.app.AppGlobals;
Dianne Hackbornf51f6122013-02-04 18:23:34 -080029import android.app.AppOpsManager;
Dianne Hackborna750a632015-06-16 17:18:23 -070030import android.app.BroadcastOptions;
Svet Ganov9c165d72015-12-01 19:52:26 -080031import android.app.PendingIntent;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080032import android.content.ComponentName;
33import android.content.IIntentReceiver;
Svet Ganov9c165d72015-12-01 19:52:26 -080034import android.content.IIntentSender;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080035import android.content.Intent;
Svet Ganov9c165d72015-12-01 19:52:26 -080036import android.content.IntentSender;
Dianne Hackborn7d19e022012-08-07 19:12:33 -070037import android.content.pm.ActivityInfo;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080038import android.content.pm.PackageManager;
39import android.content.pm.ResolveInfo;
40import android.os.Bundle;
41import android.os.Handler;
42import android.os.IBinder;
Jeff Brown6f357d32014-01-15 20:40:55 -080043import android.os.Looper;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080044import android.os.Message;
45import android.os.Process;
46import android.os.RemoteException;
47import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070048import android.os.UserHandle;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080049import android.util.EventLog;
50import android.util.Slog;
Dianne Hackborn865907d2015-10-21 17:12:53 -070051import android.util.TimeUtils;
Dianne Hackborna750a632015-06-16 17:18:23 -070052import com.android.server.DeviceIdleController;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080053
Wale Ogunwaled57969f2014-11-15 19:37:29 -080054import static com.android.server.am.ActivityManagerDebugConfig.*;
55
Dianne Hackborn40c8db52012-02-10 18:59:48 -080056/**
57 * BROADCASTS
58 *
59 * We keep two broadcast queues and associated bookkeeping, one for those at
60 * foreground priority, and one for normal (background-priority) broadcasts.
61 */
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070062public final class BroadcastQueue {
Wale Ogunwalebfac4682015-04-08 14:33:21 -070063 private static final String TAG = "BroadcastQueue";
Wale Ogunwaled57969f2014-11-15 19:37:29 -080064 private static final String TAG_MU = TAG + POSTFIX_MU;
65 private static final String TAG_BROADCAST = TAG + POSTFIX_BROADCAST;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080066
Dianne Hackborn4c51de42013-10-16 23:34:35 -070067 static final int MAX_BROADCAST_HISTORY = ActivityManager.isLowRamDeviceStatic() ? 10 : 50;
Dianne Hackborn6285a322013-09-18 12:09:47 -070068 static final int MAX_BROADCAST_SUMMARY_HISTORY
Dianne Hackborn4c51de42013-10-16 23:34:35 -070069 = ActivityManager.isLowRamDeviceStatic() ? 25 : 300;
Dianne Hackborn40c8db52012-02-10 18:59:48 -080070
71 final ActivityManagerService mService;
72
73 /**
74 * Recognizable moniker for this queue
75 */
76 final String mQueueName;
77
78 /**
79 * Timeout period for this queue's broadcasts
80 */
81 final long mTimeoutPeriod;
82
83 /**
Dianne Hackborn6285a322013-09-18 12:09:47 -070084 * If true, we can delay broadcasts while waiting services to finish in the previous
85 * receiver's process.
86 */
87 final boolean mDelayBehindServices;
88
89 /**
Dianne Hackborn40c8db52012-02-10 18:59:48 -080090 * Lists of all active broadcasts that are to be executed immediately
91 * (without waiting for another broadcast to finish). Currently this only
92 * contains broadcasts to registered receivers, to avoid spinning up
93 * a bunch of processes to execute IntentReceiver components. Background-
94 * and foreground-priority broadcasts are queued separately.
95 */
Wale Ogunwale540e1232015-05-01 15:35:39 -070096 final ArrayList<BroadcastRecord> mParallelBroadcasts = new ArrayList<>();
Dianne Hackborn6285a322013-09-18 12:09:47 -070097
Dianne Hackborn40c8db52012-02-10 18:59:48 -080098 /**
99 * List of all active broadcasts that are to be executed one at a time.
100 * The object at the top of the list is the currently activity broadcasts;
101 * those after it are waiting for the top to finish. As with parallel
102 * broadcasts, separate background- and foreground-priority queues are
103 * maintained.
104 */
Wale Ogunwale540e1232015-05-01 15:35:39 -0700105 final ArrayList<BroadcastRecord> mOrderedBroadcasts = new ArrayList<>();
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800106
107 /**
Christopher Tatef278f122015-04-22 13:12:01 -0700108 * Historical data of past broadcasts, for debugging. This is a ring buffer
109 * whose last element is at mHistoryNext.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800110 */
Dianne Hackborn6285a322013-09-18 12:09:47 -0700111 final BroadcastRecord[] mBroadcastHistory = new BroadcastRecord[MAX_BROADCAST_HISTORY];
Christopher Tatef278f122015-04-22 13:12:01 -0700112 int mHistoryNext = 0;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800113
114 /**
Christopher Tatef278f122015-04-22 13:12:01 -0700115 * Summary of historical data of past broadcasts, for debugging. This is a
116 * ring buffer whose last element is at mSummaryHistoryNext.
Dianne Hackbornc0bd7472012-10-09 14:00:30 -0700117 */
Dianne Hackborn6285a322013-09-18 12:09:47 -0700118 final Intent[] mBroadcastSummaryHistory = new Intent[MAX_BROADCAST_SUMMARY_HISTORY];
Christopher Tatef278f122015-04-22 13:12:01 -0700119 int mSummaryHistoryNext = 0;
120
121 /**
122 * Various milestone timestamps of entries in the mBroadcastSummaryHistory ring
123 * buffer, also tracked via the mSummaryHistoryNext index. These are all in wall
124 * clock time, not elapsed.
125 */
126 final long[] mSummaryHistoryEnqueueTime = new long[MAX_BROADCAST_SUMMARY_HISTORY];
127 final long[] mSummaryHistoryDispatchTime = new long[MAX_BROADCAST_SUMMARY_HISTORY];
128 final long[] mSummaryHistoryFinishTime = new long[MAX_BROADCAST_SUMMARY_HISTORY];
Dianne Hackbornc0bd7472012-10-09 14:00:30 -0700129
130 /**
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800131 * Set when we current have a BROADCAST_INTENT_MSG in flight.
132 */
133 boolean mBroadcastsScheduled = false;
134
135 /**
136 * True if we have a pending unexpired BROADCAST_TIMEOUT_MSG posted to our handler.
137 */
138 boolean mPendingBroadcastTimeoutMessage;
139
140 /**
141 * Intent broadcasts that we have tried to start, but are
142 * waiting for the application's process to be created. We only
143 * need one per scheduling class (instead of a list) because we always
144 * process broadcasts one at a time, so no others can be started while
145 * waiting for this one.
146 */
147 BroadcastRecord mPendingBroadcast = null;
148
149 /**
150 * The receiver index that is pending, to restart the broadcast if needed.
151 */
152 int mPendingBroadcastRecvIndex;
153
154 static final int BROADCAST_INTENT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG;
155 static final int BROADCAST_TIMEOUT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG + 1;
Dianne Hackborna750a632015-06-16 17:18:23 -0700156 static final int SCHEDULE_TEMP_WHITELIST_MSG
157 = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG + 2;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800158
Jeff Brown6f357d32014-01-15 20:40:55 -0800159 final BroadcastHandler mHandler;
160
161 private final class BroadcastHandler extends Handler {
162 public BroadcastHandler(Looper looper) {
163 super(looper, null, true);
164 }
165
166 @Override
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800167 public void handleMessage(Message msg) {
168 switch (msg.what) {
169 case BROADCAST_INTENT_MSG: {
170 if (DEBUG_BROADCAST) Slog.v(
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800171 TAG_BROADCAST, "Received BROADCAST_INTENT_MSG");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800172 processNextBroadcast(true);
173 } break;
174 case BROADCAST_TIMEOUT_MSG: {
175 synchronized (mService) {
176 broadcastTimeoutLocked(true);
177 }
178 } break;
Dianne Hackborna750a632015-06-16 17:18:23 -0700179 case SCHEDULE_TEMP_WHITELIST_MSG: {
180 DeviceIdleController.LocalService dic = mService.mLocalDeviceIdleController;
181 if (dic != null) {
182 dic.addPowerSaveTempWhitelistAppDirect(UserHandle.getAppId(msg.arg1),
Dianne Hackbornfd854ee2015-07-13 18:00:37 -0700183 msg.arg2, true, (String)msg.obj);
Dianne Hackborna750a632015-06-16 17:18:23 -0700184 }
185 } break;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800186 }
187 }
Svet Ganov9c165d72015-12-01 19:52:26 -0800188 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800189
190 private final class AppNotResponding implements Runnable {
191 private final ProcessRecord mApp;
192 private final String mAnnotation;
193
194 public AppNotResponding(ProcessRecord app, String annotation) {
195 mApp = app;
196 mAnnotation = annotation;
197 }
198
199 @Override
200 public void run() {
Adrian Roos20d7df32016-01-12 18:59:43 +0100201 mService.mAppErrors.appNotResponding(mApp, null, null, false, mAnnotation);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800202 }
203 }
204
Jeff Brown6f357d32014-01-15 20:40:55 -0800205 BroadcastQueue(ActivityManagerService service, Handler handler,
206 String name, long timeoutPeriod, boolean allowDelayBehindServices) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800207 mService = service;
Jeff Brown6f357d32014-01-15 20:40:55 -0800208 mHandler = new BroadcastHandler(handler.getLooper());
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800209 mQueueName = name;
210 mTimeoutPeriod = timeoutPeriod;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700211 mDelayBehindServices = allowDelayBehindServices;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800212 }
213
214 public boolean isPendingBroadcastProcessLocked(int pid) {
215 return mPendingBroadcast != null && mPendingBroadcast.curApp.pid == pid;
216 }
217
218 public void enqueueParallelBroadcastLocked(BroadcastRecord r) {
219 mParallelBroadcasts.add(r);
Carmen Jacksona68e3452017-01-17 14:01:33 -0800220 enqueueBroadcastHelper(r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800221 }
222
223 public void enqueueOrderedBroadcastLocked(BroadcastRecord r) {
224 mOrderedBroadcasts.add(r);
Carmen Jacksona68e3452017-01-17 14:01:33 -0800225 enqueueBroadcastHelper(r);
226 }
227
228 /**
229 * Don't call this method directly; call enqueueParallelBroadcastLocked or
230 * enqueueOrderedBroadcastLocked.
231 */
232 private void enqueueBroadcastHelper(BroadcastRecord r) {
Jeff Brown9fb3fd12014-09-29 15:32:12 -0700233 r.enqueueClockTime = System.currentTimeMillis();
Carmen Jacksona68e3452017-01-17 14:01:33 -0800234
235 if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
236 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
237 createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_PENDING),
238 System.identityHashCode(r));
239 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800240 }
241
Makoto Onuki3e7d8452017-03-02 15:33:17 -0800242 /**
243 * Find the same intent from queued parallel broadcast, replace with a new one and return
244 * the old one.
245 */
246 public final BroadcastRecord replaceParallelBroadcastLocked(BroadcastRecord r) {
247 return replaceBroadcastLocked(mParallelBroadcasts, r, "PARALLEL");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800248 }
249
Makoto Onuki3e7d8452017-03-02 15:33:17 -0800250 /**
251 * Find the same intent from queued ordered broadcast, replace with a new one and return
252 * the old one.
253 */
254 public final BroadcastRecord replaceOrderedBroadcastLocked(BroadcastRecord r) {
255 return replaceBroadcastLocked(mOrderedBroadcasts, r, "ORDERED");
256 }
257
258 private BroadcastRecord replaceBroadcastLocked(ArrayList<BroadcastRecord> queue,
259 BroadcastRecord r, String typeForLogging) {
Erik Wolsheimer5d34f002016-07-28 13:51:39 -0700260 final Intent intent = r.intent;
Makoto Onuki3e7d8452017-03-02 15:33:17 -0800261 for (int i = queue.size() - 1; i > 0; i--) {
262 final BroadcastRecord old = queue.get(i);
263 if (old.userId == r.userId && intent.filterEquals(old.intent)) {
264 if (DEBUG_BROADCAST) {
265 Slog.v(TAG_BROADCAST, "***** DROPPING "
266 + typeForLogging + " [" + mQueueName + "]: " + intent);
267 }
268 queue.set(i, r);
269 return old;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800270 }
271 }
Makoto Onuki3e7d8452017-03-02 15:33:17 -0800272 return null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800273 }
274
275 private final void processCurBroadcastLocked(BroadcastRecord r,
276 ProcessRecord app) throws RemoteException {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800277 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800278 "Process cur broadcast " + r + " for app " + app);
279 if (app.thread == null) {
280 throw new RemoteException();
281 }
Amith Yamasanib5f3b5c2016-05-16 14:23:04 -0700282 if (app.inFullBackup) {
283 skipReceiverLocked(r);
284 return;
285 }
286
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800287 r.receiver = app.thread.asBinder();
288 r.curApp = app;
yangzhenyud509bc92016-08-31 18:26:46 +0800289 app.curReceivers.add(r);
Dianne Hackborna413dc02013-07-12 12:02:55 -0700290 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_RECEIVER);
Dianne Hackborndb926082013-10-31 16:32:44 -0700291 mService.updateLruProcessLocked(app, false, null);
292 mService.updateOomAdjLocked();
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800293
294 // Tell the application to launch this receiver.
295 r.intent.setComponent(r.curComponent);
296
297 boolean started = false;
298 try {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800299 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800300 "Delivering to component " + r.curComponent
301 + ": " + r);
Brian Carlstromca82e612016-04-19 23:16:08 -0700302 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
303 PackageManager.NOTIFY_PACKAGE_USE_BROADCAST_RECEIVER);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800304 app.thread.scheduleReceiver(new Intent(r.intent), r.curReceiver,
305 mService.compatibilityInfoForPackageLocked(r.curReceiver.applicationInfo),
Dianne Hackborna413dc02013-07-12 12:02:55 -0700306 r.resultCode, r.resultData, r.resultExtras, r.ordered, r.userId,
307 app.repProcState);
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800308 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800309 "Process cur broadcast " + r + " DELIVERED for app " + app);
310 started = true;
311 } finally {
312 if (!started) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800313 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800314 "Process cur broadcast " + r + ": NOT STARTED!");
315 r.receiver = null;
316 r.curApp = null;
yangzhenyud509bc92016-08-31 18:26:46 +0800317 app.curReceivers.remove(r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800318 }
319 }
320 }
321
322 public boolean sendPendingBroadcastsLocked(ProcessRecord app) {
323 boolean didSomething = false;
324 final BroadcastRecord br = mPendingBroadcast;
325 if (br != null && br.curApp.pid == app.pid) {
Amith Yamasanid86e14e2016-08-05 15:25:03 -0700326 if (br.curApp != app) {
327 Slog.e(TAG, "App mismatch when sending pending broadcast to "
328 + app.processName + ", intended target is " + br.curApp.processName);
329 return false;
330 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800331 try {
332 mPendingBroadcast = null;
333 processCurBroadcastLocked(br, app);
334 didSomething = true;
335 } catch (Exception e) {
336 Slog.w(TAG, "Exception in new application when starting receiver "
337 + br.curComponent.flattenToShortString(), e);
338 logBroadcastReceiverDiscardLocked(br);
339 finishReceiverLocked(br, br.resultCode, br.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700340 br.resultExtras, br.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800341 scheduleBroadcastsLocked();
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700342 // We need to reset the state if we failed to start the receiver.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800343 br.state = BroadcastRecord.IDLE;
344 throw new RuntimeException(e.getMessage());
345 }
346 }
347 return didSomething;
348 }
349
350 public void skipPendingBroadcastLocked(int pid) {
351 final BroadcastRecord br = mPendingBroadcast;
352 if (br != null && br.curApp.pid == pid) {
353 br.state = BroadcastRecord.IDLE;
354 br.nextReceiver = mPendingBroadcastRecvIndex;
355 mPendingBroadcast = null;
356 scheduleBroadcastsLocked();
357 }
358 }
359
360 public void skipCurrentReceiverLocked(ProcessRecord app) {
Wale Ogunwale24b243d2015-07-17 07:20:57 -0700361 BroadcastRecord r = null;
362 if (mOrderedBroadcasts.size() > 0) {
363 BroadcastRecord br = mOrderedBroadcasts.get(0);
364 if (br.curApp == app) {
365 r = br;
366 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800367 }
riddle_hsu01eb7fa2015-03-04 17:27:05 +0800368 if (r == null && mPendingBroadcast != null && mPendingBroadcast.curApp == app) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800369 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800370 "[" + mQueueName + "] skip & discard pending app " + r);
riddle_hsu01eb7fa2015-03-04 17:27:05 +0800371 r = mPendingBroadcast;
372 }
373
374 if (r != null) {
Amith Yamasanib5f3b5c2016-05-16 14:23:04 -0700375 skipReceiverLocked(r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800376 }
377 }
378
Amith Yamasanib5f3b5c2016-05-16 14:23:04 -0700379 private void skipReceiverLocked(BroadcastRecord r) {
380 logBroadcastReceiverDiscardLocked(r);
381 finishReceiverLocked(r, r.resultCode, r.resultData,
382 r.resultExtras, r.resultAbort, false);
383 scheduleBroadcastsLocked();
384 }
385
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800386 public void scheduleBroadcastsLocked() {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800387 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Schedule broadcasts ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800388 + mQueueName + "]: current="
389 + mBroadcastsScheduled);
390
391 if (mBroadcastsScheduled) {
392 return;
393 }
394 mHandler.sendMessage(mHandler.obtainMessage(BROADCAST_INTENT_MSG, this));
395 mBroadcastsScheduled = true;
396 }
397
398 public BroadcastRecord getMatchingOrderedReceiver(IBinder receiver) {
399 if (mOrderedBroadcasts.size() > 0) {
400 final BroadcastRecord r = mOrderedBroadcasts.get(0);
401 if (r != null && r.receiver == receiver) {
402 return r;
403 }
404 }
405 return null;
406 }
407
408 public boolean finishReceiverLocked(BroadcastRecord r, int resultCode,
Dianne Hackborn6285a322013-09-18 12:09:47 -0700409 String resultData, Bundle resultExtras, boolean resultAbort, boolean waitForServices) {
410 final int state = r.state;
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700411 final ActivityInfo receiver = r.curReceiver;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800412 r.state = BroadcastRecord.IDLE;
413 if (state == BroadcastRecord.IDLE) {
Dianne Hackborn6285a322013-09-18 12:09:47 -0700414 Slog.w(TAG, "finishReceiver [" + mQueueName + "] called but state is IDLE");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800415 }
416 r.receiver = null;
417 r.intent.setComponent(null);
yangzhenyud509bc92016-08-31 18:26:46 +0800418 if (r.curApp != null && r.curApp.curReceivers.contains(r)) {
419 r.curApp.curReceivers.remove(r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800420 }
421 if (r.curFilter != null) {
422 r.curFilter.receiverList.curBroadcast = null;
423 }
424 r.curFilter = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800425 r.curReceiver = null;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700426 r.curApp = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800427 mPendingBroadcast = null;
428
429 r.resultCode = resultCode;
430 r.resultData = resultData;
431 r.resultExtras = resultExtras;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700432 if (resultAbort && (r.intent.getFlags()&Intent.FLAG_RECEIVER_NO_ABORT) == 0) {
433 r.resultAbort = resultAbort;
434 } else {
435 r.resultAbort = false;
436 }
437
438 if (waitForServices && r.curComponent != null && r.queue.mDelayBehindServices
439 && r.queue.mOrderedBroadcasts.size() > 0
440 && r.queue.mOrderedBroadcasts.get(0) == r) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700441 ActivityInfo nextReceiver;
442 if (r.nextReceiver < r.receivers.size()) {
443 Object obj = r.receivers.get(r.nextReceiver);
444 nextReceiver = (obj instanceof ActivityInfo) ? (ActivityInfo)obj : null;
445 } else {
446 nextReceiver = null;
447 }
448 // Don't do this if the next receive is in the same process as the current one.
449 if (receiver == null || nextReceiver == null
450 || receiver.applicationInfo.uid != nextReceiver.applicationInfo.uid
451 || !receiver.processName.equals(nextReceiver.processName)) {
452 // In this case, we are ready to process the next receiver for the current broadcast,
453 // but are on a queue that would like to wait for services to finish before moving
454 // on. If there are background services currently starting, then we will go into a
455 // special state where we hold off on continuing this broadcast until they are done.
Dianne Hackbornad51be92016-08-16 16:27:36 -0700456 if (mService.mServices.hasBackgroundServicesLocked(r.userId)) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800457 Slog.i(TAG, "Delay finish: " + r.curComponent.flattenToShortString());
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700458 r.state = BroadcastRecord.WAITING_SERVICES;
459 return false;
460 }
Dianne Hackborn6285a322013-09-18 12:09:47 -0700461 }
462 }
463
464 r.curComponent = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800465
466 // We will process the next receiver right now if this is finishing
467 // an app receiver (which is always asynchronous) or after we have
468 // come back from calling a receiver.
469 return state == BroadcastRecord.APP_RECEIVE
470 || state == BroadcastRecord.CALL_DONE_RECEIVE;
471 }
472
Dianne Hackborn6285a322013-09-18 12:09:47 -0700473 public void backgroundServicesFinishedLocked(int userId) {
474 if (mOrderedBroadcasts.size() > 0) {
475 BroadcastRecord br = mOrderedBroadcasts.get(0);
476 if (br.userId == userId && br.state == BroadcastRecord.WAITING_SERVICES) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800477 Slog.i(TAG, "Resuming delayed broadcast");
Dianne Hackborn6285a322013-09-18 12:09:47 -0700478 br.curComponent = null;
479 br.state = BroadcastRecord.IDLE;
480 processNextBroadcast(false);
481 }
482 }
483 }
484
Dianne Hackbornea05cd52016-06-20 11:22:40 -0700485 void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800486 Intent intent, int resultCode, String data, Bundle extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700487 boolean ordered, boolean sticky, int sendingUser) throws RemoteException {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800488 // Send the intent to the receiver asynchronously using one-way binder calls.
Craig Mautner38c1a5f2014-07-21 15:36:37 +0000489 if (app != null) {
490 if (app.thread != null) {
491 // If we have an app thread, do the call through that so it is
492 // correctly ordered with other one-way calls.
Joe Onorato5869d1c2016-04-20 15:38:07 -0700493 try {
494 app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode,
495 data, extras, ordered, sticky, sendingUser, app.repProcState);
496 // TODO: Uncomment this when (b/28322359) is fixed and we aren't getting
497 // DeadObjectException when the process isn't actually dead.
498 //} catch (DeadObjectException ex) {
499 // Failed to call into the process. It's dying so just let it die and move on.
500 // throw ex;
501 } catch (RemoteException ex) {
502 // Failed to call into the process. It's either dying or wedged. Kill it gently.
503 synchronized (mService) {
504 Slog.w(TAG, "Can't deliver broadcast to " + app.processName
505 + " (pid " + app.pid + "). Crashing it.");
506 app.scheduleCrash("can't deliver broadcast");
507 }
508 throw ex;
509 }
Craig Mautner38c1a5f2014-07-21 15:36:37 +0000510 } else {
511 // Application has died. Receiver doesn't exist.
512 throw new RemoteException("app.thread must not be null");
513 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800514 } else {
Dianne Hackborn20e80982012-08-31 19:00:44 -0700515 receiver.performReceive(intent, resultCode, data, extras, ordered,
516 sticky, sendingUser);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800517 }
518 }
519
Svet Ganov99b60432015-06-27 13:15:22 -0700520 private void deliverToRegisteredReceiverLocked(BroadcastRecord r,
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800521 BroadcastFilter filter, boolean ordered, int index) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800522 boolean skip = false;
Amith Yamasani8bf06ed2012-08-27 19:30:30 -0700523 if (filter.requiredPermission != null) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800524 int perm = mService.checkComponentPermission(filter.requiredPermission,
525 r.callingPid, r.callingUid, -1, true);
526 if (perm != PackageManager.PERMISSION_GRANTED) {
527 Slog.w(TAG, "Permission Denial: broadcasting "
528 + r.intent.toString()
529 + " from " + r.callerPackage + " (pid="
530 + r.callingPid + ", uid=" + r.callingUid + ")"
531 + " requires " + filter.requiredPermission
532 + " due to registered receiver " + filter);
533 skip = true;
Svet Ganov99b60432015-06-27 13:15:22 -0700534 } else {
535 final int opCode = AppOpsManager.permissionToOpCode(filter.requiredPermission);
536 if (opCode != AppOpsManager.OP_NONE
537 && mService.mAppOpsService.noteOperation(opCode, r.callingUid,
538 r.callerPackage) != AppOpsManager.MODE_ALLOWED) {
539 Slog.w(TAG, "Appop Denial: broadcasting "
540 + r.intent.toString()
541 + " from " + r.callerPackage + " (pid="
542 + r.callingPid + ", uid=" + r.callingUid + ")"
543 + " requires appop " + AppOpsManager.permissionToOp(
544 filter.requiredPermission)
545 + " due to registered receiver " + filter);
546 skip = true;
547 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800548 }
549 }
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700550 if (!skip && r.requiredPermissions != null && r.requiredPermissions.length > 0) {
551 for (int i = 0; i < r.requiredPermissions.length; i++) {
552 String requiredPermission = r.requiredPermissions[i];
553 int perm = mService.checkComponentPermission(requiredPermission,
554 filter.receiverList.pid, filter.receiverList.uid, -1, true);
555 if (perm != PackageManager.PERMISSION_GRANTED) {
556 Slog.w(TAG, "Permission Denial: receiving "
557 + r.intent.toString()
558 + " to " + filter.receiverList.app
559 + " (pid=" + filter.receiverList.pid
560 + ", uid=" + filter.receiverList.uid + ")"
561 + " requires " + requiredPermission
562 + " due to sender " + r.callerPackage
563 + " (uid " + r.callingUid + ")");
564 skip = true;
565 break;
566 }
567 int appOp = AppOpsManager.permissionToOpCode(requiredPermission);
Svetoslavfb9ec502015-09-01 14:45:18 -0700568 if (appOp != AppOpsManager.OP_NONE && appOp != r.appOp
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700569 && mService.mAppOpsService.noteOperation(appOp,
570 filter.receiverList.uid, filter.packageName)
571 != AppOpsManager.MODE_ALLOWED) {
572 Slog.w(TAG, "Appop Denial: receiving "
573 + r.intent.toString()
574 + " to " + filter.receiverList.app
575 + " (pid=" + filter.receiverList.pid
576 + ", uid=" + filter.receiverList.uid + ")"
577 + " requires appop " + AppOpsManager.permissionToOp(
578 requiredPermission)
579 + " due to sender " + r.callerPackage
580 + " (uid " + r.callingUid + ")");
581 skip = true;
582 break;
583 }
584 }
585 }
586 if (!skip && (r.requiredPermissions == null || r.requiredPermissions.length == 0)) {
587 int perm = mService.checkComponentPermission(null,
Fyodor Kupolove37520b2015-07-14 22:29:21 +0000588 filter.receiverList.pid, filter.receiverList.uid, -1, true);
589 if (perm != PackageManager.PERMISSION_GRANTED) {
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700590 Slog.w(TAG, "Permission Denial: security check failed when receiving "
Fyodor Kupolove37520b2015-07-14 22:29:21 +0000591 + r.intent.toString()
592 + " to " + filter.receiverList.app
593 + " (pid=" + filter.receiverList.pid
594 + ", uid=" + filter.receiverList.uid + ")"
Fyodor Kupolove37520b2015-07-14 22:29:21 +0000595 + " due to sender " + r.callerPackage
596 + " (uid " + r.callingUid + ")");
597 skip = true;
598 }
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700599 }
600 if (!skip && r.appOp != AppOpsManager.OP_NONE
601 && mService.mAppOpsService.noteOperation(r.appOp,
602 filter.receiverList.uid, filter.packageName)
603 != AppOpsManager.MODE_ALLOWED) {
604 Slog.w(TAG, "Appop Denial: receiving "
605 + r.intent.toString()
606 + " to " + filter.receiverList.app
607 + " (pid=" + filter.receiverList.pid
608 + ", uid=" + filter.receiverList.uid + ")"
609 + " requires appop " + AppOpsManager.opToName(r.appOp)
610 + " due to sender " + r.callerPackage
611 + " (uid " + r.callingUid + ")");
612 skip = true;
Fyodor Kupolovb4e72832015-07-13 19:19:25 -0700613 }
Svet Ganov99b60432015-06-27 13:15:22 -0700614
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700615 if (!mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,
616 r.callingPid, r.resolvedType, filter.receiverList.uid)) {
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800617 skip = true;
Ben Gruver49660c72013-08-06 19:54:08 -0700618 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800619
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800620 if (!skip && (filter.receiverList.app == null || filter.receiverList.app.crashing)) {
Dianne Hackborn9357b112013-10-03 18:27:48 -0700621 Slog.w(TAG, "Skipping deliver [" + mQueueName + "] " + r
622 + " to " + filter.receiverList + ": process crashing");
623 skip = true;
624 }
625
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800626 if (skip) {
627 r.delivery[index] = BroadcastRecord.DELIVERY_SKIPPED;
628 return;
629 }
Svet Ganov9c165d72015-12-01 19:52:26 -0800630
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800631 // If permissions need a review before any of the app components can run, we drop
632 // the broadcast and if the calling app is in the foreground and the broadcast is
633 // explicit we launch the review UI passing it a pending intent to send the skipped
634 // broadcast.
Svet Ganov77df6f32016-08-17 11:46:34 -0700635 if (mService.mPermissionReviewRequired) {
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800636 if (!requestStartTargetPermissionsReviewIfNeededLocked(r, filter.packageName,
637 filter.owningUserId)) {
638 r.delivery[index] = BroadcastRecord.DELIVERY_SKIPPED;
639 return;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800640 }
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800641 }
642
643 r.delivery[index] = BroadcastRecord.DELIVERY_DELIVERED;
644
645 // If this is not being sent as an ordered broadcast, then we
646 // don't want to touch the fields that keep track of the current
647 // state of ordered broadcasts.
648 if (ordered) {
649 r.receiver = filter.receiverList.receiver.asBinder();
650 r.curFilter = filter;
651 filter.receiverList.curBroadcast = r;
652 r.state = BroadcastRecord.CALL_IN_RECEIVE;
653 if (filter.receiverList.app != null) {
654 // Bump hosting application to no longer be in background
655 // scheduling class. Note that we can't do that if there
656 // isn't an app... but we can only be in that case for
657 // things that directly call the IActivityManager API, which
658 // are already core system stuff so don't matter for this.
659 r.curApp = filter.receiverList.app;
yangzhenyud509bc92016-08-31 18:26:46 +0800660 filter.receiverList.app.curReceivers.add(r);
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800661 mService.updateOomAdjLocked(r.curApp);
662 }
663 }
664 try {
665 if (DEBUG_BROADCAST_LIGHT) Slog.i(TAG_BROADCAST,
666 "Delivering to " + filter + " : " + r);
Amith Yamasanib5f3b5c2016-05-16 14:23:04 -0700667 if (filter.receiverList.app != null && filter.receiverList.app.inFullBackup) {
668 // Skip delivery if full backup in progress
669 // If it's an ordered broadcast, we need to continue to the next receiver.
670 if (ordered) {
671 skipReceiverLocked(r);
672 }
673 } else {
674 performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver,
675 new Intent(r.intent), r.resultCode, r.resultData,
676 r.resultExtras, r.ordered, r.initialSticky, r.userId);
677 }
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800678 if (ordered) {
679 r.state = BroadcastRecord.CALL_DONE_RECEIVE;
680 }
681 } catch (RemoteException e) {
682 Slog.w(TAG, "Failure sending broadcast " + r.intent, e);
683 if (ordered) {
684 r.receiver = null;
685 r.curFilter = null;
686 filter.receiverList.curBroadcast = null;
687 if (filter.receiverList.app != null) {
yangzhenyud509bc92016-08-31 18:26:46 +0800688 filter.receiverList.app.curReceivers.remove(r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800689 }
690 }
691 }
692 }
693
Svet Ganov9c165d72015-12-01 19:52:26 -0800694 private boolean requestStartTargetPermissionsReviewIfNeededLocked(
695 BroadcastRecord receiverRecord, String receivingPackageName,
696 final int receivingUserId) {
697 if (!mService.getPackageManagerInternalLocked().isPermissionsReviewRequired(
698 receivingPackageName, receivingUserId)) {
699 return true;
700 }
701
702 final boolean callerForeground = receiverRecord.callerApp != null
Dianne Hackborna49ad092016-03-03 13:39:10 -0800703 ? receiverRecord.callerApp.setSchedGroup != ProcessList.SCHED_GROUP_BACKGROUND
Svet Ganov9c165d72015-12-01 19:52:26 -0800704 : true;
705
706 // Show a permission review UI only for explicit broadcast from a foreground app
707 if (callerForeground && receiverRecord.intent.getComponent() != null) {
708 IIntentSender target = mService.getIntentSenderLocked(
709 ActivityManager.INTENT_SENDER_BROADCAST, receiverRecord.callerPackage,
710 receiverRecord.callingUid, receiverRecord.userId, null, null, 0,
711 new Intent[]{receiverRecord.intent},
712 new String[]{receiverRecord.intent.resolveType(mService.mContext
713 .getContentResolver())},
714 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
715 | PendingIntent.FLAG_IMMUTABLE, null);
716
717 final Intent intent = new Intent(Intent.ACTION_REVIEW_PERMISSIONS);
718 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
719 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
720 intent.putExtra(Intent.EXTRA_PACKAGE_NAME, receivingPackageName);
721 intent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));
722
723 if (DEBUG_PERMISSIONS_REVIEW) {
724 Slog.i(TAG, "u" + receivingUserId + " Launching permission review for package "
725 + receivingPackageName);
726 }
727
728 mHandler.post(new Runnable() {
729 @Override
730 public void run() {
731 mService.mContext.startActivityAsUser(intent, new UserHandle(receivingUserId));
732 }
733 });
734 } else {
735 Slog.w(TAG, "u" + receivingUserId + " Receiving a broadcast in package"
736 + receivingPackageName + " requires a permissions review");
737 }
738
739 return false;
740 }
741
Dianne Hackbornfd854ee2015-07-13 18:00:37 -0700742 final void scheduleTempWhitelistLocked(int uid, long duration, BroadcastRecord r) {
Dianne Hackborna750a632015-06-16 17:18:23 -0700743 if (duration > Integer.MAX_VALUE) {
744 duration = Integer.MAX_VALUE;
745 }
746 // XXX ideally we should pause the broadcast until everything behind this is done,
747 // or else we will likely start dispatching the broadcast before we have opened
748 // access to the app (there is a lot of asynchronicity behind this). It is probably
749 // not that big a deal, however, because the main purpose here is to allow apps
750 // to hold wake locks, and they will be able to acquire their wake lock immediately
751 // it just won't be enabled until we get through this work.
Dianne Hackbornfd854ee2015-07-13 18:00:37 -0700752 StringBuilder b = new StringBuilder();
753 b.append("broadcast:");
754 UserHandle.formatUid(b, r.callingUid);
755 b.append(":");
756 if (r.intent.getAction() != null) {
757 b.append(r.intent.getAction());
758 } else if (r.intent.getComponent() != null) {
759 b.append(r.intent.getComponent().flattenToShortString());
760 } else if (r.intent.getData() != null) {
761 b.append(r.intent.getData());
762 }
763 mHandler.obtainMessage(SCHEDULE_TEMP_WHITELIST_MSG, uid, (int)duration, b.toString())
764 .sendToTarget();
Dianne Hackborna750a632015-06-16 17:18:23 -0700765 }
766
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800767 final void processNextBroadcast(boolean fromMsg) {
768 synchronized(mService) {
769 BroadcastRecord r;
770
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800771 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "processNextBroadcast ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800772 + mQueueName + "]: "
Carmen Jacksona68e3452017-01-17 14:01:33 -0800773 + mParallelBroadcasts.size() + " parallel broadcasts, "
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800774 + mOrderedBroadcasts.size() + " ordered broadcasts");
775
776 mService.updateCpuStats();
777
778 if (fromMsg) {
779 mBroadcastsScheduled = false;
780 }
781
782 // First, deliver any non-serialized broadcasts right away.
783 while (mParallelBroadcasts.size() > 0) {
784 r = mParallelBroadcasts.remove(0);
785 r.dispatchTime = SystemClock.uptimeMillis();
786 r.dispatchClockTime = System.currentTimeMillis();
Carmen Jacksona68e3452017-01-17 14:01:33 -0800787
788 if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
789 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER,
790 createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_PENDING),
791 System.identityHashCode(r));
792 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
793 createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_DELIVERED),
794 System.identityHashCode(r));
795 }
796
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800797 final int N = r.receivers.size();
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800798 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Processing parallel broadcast ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800799 + mQueueName + "] " + r);
800 for (int i=0; i<N; i++) {
801 Object target = r.receivers.get(i);
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800802 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800803 "Delivering non-ordered on [" + mQueueName + "] to registered "
804 + target + ": " + r);
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800805 deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false, i);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800806 }
807 addBroadcastToHistoryLocked(r);
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800808 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Done with parallel broadcast ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800809 + mQueueName + "] " + r);
810 }
811
812 // Now take care of the next serialized one...
813
814 // If we are waiting for a process to come up to handle the next
815 // broadcast, then do nothing at this point. Just in case, we
816 // check that the process we're waiting for still exists.
817 if (mPendingBroadcast != null) {
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -0700818 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST,
819 "processNextBroadcast [" + mQueueName + "]: waiting for "
820 + mPendingBroadcast.curApp);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800821
822 boolean isDead;
823 synchronized (mService.mPidsSelfLocked) {
Dianne Hackborn9357b112013-10-03 18:27:48 -0700824 ProcessRecord proc = mService.mPidsSelfLocked.get(mPendingBroadcast.curApp.pid);
825 isDead = proc == null || proc.crashing;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800826 }
827 if (!isDead) {
828 // It's still alive, so keep waiting
829 return;
830 } else {
831 Slog.w(TAG, "pending app ["
832 + mQueueName + "]" + mPendingBroadcast.curApp
833 + " died before responding to broadcast");
834 mPendingBroadcast.state = BroadcastRecord.IDLE;
835 mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex;
836 mPendingBroadcast = null;
837 }
838 }
839
840 boolean looped = false;
841
842 do {
843 if (mOrderedBroadcasts.size() == 0) {
844 // No more broadcasts pending, so all done!
845 mService.scheduleAppGcsLocked();
846 if (looped) {
847 // If we had finished the last ordered broadcast, then
848 // make sure all processes have correct oom and sched
849 // adjustments.
850 mService.updateOomAdjLocked();
851 }
852 return;
853 }
854 r = mOrderedBroadcasts.get(0);
855 boolean forceReceive = false;
856
857 // Ensure that even if something goes awry with the timeout
858 // detection, we catch "hung" broadcasts here, discard them,
859 // and continue to make progress.
860 //
861 // This is only done if the system is ready so that PRE_BOOT_COMPLETED
862 // receivers don't get executed with timeouts. They're intended for
863 // one time heavy lifting after system upgrades and can take
864 // significant amounts of time.
865 int numReceivers = (r.receivers != null) ? r.receivers.size() : 0;
866 if (mService.mProcessesReady && r.dispatchTime > 0) {
867 long now = SystemClock.uptimeMillis();
868 if ((numReceivers > 0) &&
869 (now > r.dispatchTime + (2*mTimeoutPeriod*numReceivers))) {
870 Slog.w(TAG, "Hung broadcast ["
871 + mQueueName + "] discarded after timeout failure:"
872 + " now=" + now
873 + " dispatchTime=" + r.dispatchTime
874 + " startTime=" + r.receiverTime
875 + " intent=" + r.intent
876 + " numReceivers=" + numReceivers
877 + " nextReceiver=" + r.nextReceiver
878 + " state=" + r.state);
879 broadcastTimeoutLocked(false); // forcibly finish this broadcast
880 forceReceive = true;
881 r.state = BroadcastRecord.IDLE;
882 }
883 }
884
885 if (r.state != BroadcastRecord.IDLE) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800886 if (DEBUG_BROADCAST) Slog.d(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800887 "processNextBroadcast("
888 + mQueueName + ") called when not idle (state="
889 + r.state + ")");
890 return;
891 }
892
893 if (r.receivers == null || r.nextReceiver >= numReceivers
894 || r.resultAbort || forceReceive) {
895 // No more receivers for this broadcast! Send the final
896 // result if requested...
897 if (r.resultTo != null) {
898 try {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800899 if (DEBUG_BROADCAST) Slog.i(TAG_BROADCAST,
Todd Kennedyd2f15112015-01-21 15:25:56 -0800900 "Finishing broadcast [" + mQueueName + "] "
901 + r.intent.getAction() + " app=" + r.callerApp);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800902 performReceiveLocked(r.callerApp, r.resultTo,
903 new Intent(r.intent), r.resultCode,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700904 r.resultData, r.resultExtras, false, false, r.userId);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800905 // Set this to null so that the reference
Dianne Hackborn9357b112013-10-03 18:27:48 -0700906 // (local and remote) isn't kept in the mBroadcastHistory.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800907 r.resultTo = null;
908 } catch (RemoteException e) {
Craig Mautner38c1a5f2014-07-21 15:36:37 +0000909 r.resultTo = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800910 Slog.w(TAG, "Failure ["
911 + mQueueName + "] sending broadcast result of "
912 + r.intent, e);
Joe Onorato5869d1c2016-04-20 15:38:07 -0700913
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800914 }
915 }
916
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800917 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Cancelling BROADCAST_TIMEOUT_MSG");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800918 cancelBroadcastTimeoutLocked();
919
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -0700920 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST,
921 "Finished with ordered broadcast " + r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800922
923 // ... and on to the next...
924 addBroadcastToHistoryLocked(r);
Dianne Hackbornbc02a392016-06-02 17:15:08 -0700925 if (r.intent.getComponent() == null && r.intent.getPackage() == null
926 && (r.intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) {
927 // This was an implicit broadcast... let's record it for posterity.
928 mService.addBroadcastStatLocked(r.intent.getAction(), r.callerPackage,
929 r.manifestCount, r.manifestSkipCount, r.finishTime-r.dispatchTime);
930 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800931 mOrderedBroadcasts.remove(0);
932 r = null;
933 looped = true;
934 continue;
935 }
936 } while (r == null);
937
938 // Get the next receiver...
939 int recIdx = r.nextReceiver++;
940
941 // Keep track of when this receiver started, and make sure there
942 // is a timeout message pending to kill it if need be.
943 r.receiverTime = SystemClock.uptimeMillis();
944 if (recIdx == 0) {
945 r.dispatchTime = r.receiverTime;
946 r.dispatchClockTime = System.currentTimeMillis();
Carmen Jacksona68e3452017-01-17 14:01:33 -0800947 if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
948 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER,
949 createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_PENDING),
950 System.identityHashCode(r));
951 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
952 createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_DELIVERED),
953 System.identityHashCode(r));
954 }
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800955 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Processing ordered broadcast ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800956 + mQueueName + "] " + r);
957 }
958 if (! mPendingBroadcastTimeoutMessage) {
959 long timeoutTime = r.receiverTime + mTimeoutPeriod;
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800960 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800961 "Submitting BROADCAST_TIMEOUT_MSG ["
962 + mQueueName + "] for " + r + " at " + timeoutTime);
963 setBroadcastTimeoutLocked(timeoutTime);
964 }
965
Dianne Hackborna750a632015-06-16 17:18:23 -0700966 final BroadcastOptions brOptions = r.options;
967 final Object nextReceiver = r.receivers.get(recIdx);
968
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800969 if (nextReceiver instanceof BroadcastFilter) {
970 // Simple case: this is a registered receiver who gets
971 // a direct call.
972 BroadcastFilter filter = (BroadcastFilter)nextReceiver;
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800973 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800974 "Delivering ordered ["
975 + mQueueName + "] to registered "
976 + filter + ": " + r);
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800977 deliverToRegisteredReceiverLocked(r, filter, r.ordered, recIdx);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800978 if (r.receiver == null || !r.ordered) {
979 // The receiver has already finished, so schedule to
980 // process the next one.
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800981 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Quick finishing ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800982 + mQueueName + "]: ordered="
983 + r.ordered + " receiver=" + r.receiver);
984 r.state = BroadcastRecord.IDLE;
985 scheduleBroadcastsLocked();
Dianne Hackborna750a632015-06-16 17:18:23 -0700986 } else {
987 if (brOptions != null && brOptions.getTemporaryAppWhitelistDuration() > 0) {
988 scheduleTempWhitelistLocked(filter.owningUid,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -0700989 brOptions.getTemporaryAppWhitelistDuration(), r);
Dianne Hackborna750a632015-06-16 17:18:23 -0700990 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800991 }
992 return;
993 }
994
995 // Hard case: need to instantiate the receiver, possibly
996 // starting its application process to host it.
997
998 ResolveInfo info =
999 (ResolveInfo)nextReceiver;
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001000 ComponentName component = new ComponentName(
1001 info.activityInfo.applicationInfo.packageName,
1002 info.activityInfo.name);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001003
1004 boolean skip = false;
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001005 if (brOptions != null &&
1006 (info.activityInfo.applicationInfo.targetSdkVersion
1007 < brOptions.getMinManifestReceiverApiLevel() ||
1008 info.activityInfo.applicationInfo.targetSdkVersion
1009 > brOptions.getMaxManifestReceiverApiLevel())) {
1010 skip = true;
1011 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001012 int perm = mService.checkComponentPermission(info.activityInfo.permission,
1013 r.callingPid, r.callingUid, info.activityInfo.applicationInfo.uid,
1014 info.activityInfo.exported);
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001015 if (!skip && perm != PackageManager.PERMISSION_GRANTED) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001016 if (!info.activityInfo.exported) {
1017 Slog.w(TAG, "Permission Denial: broadcasting "
1018 + r.intent.toString()
1019 + " from " + r.callerPackage + " (pid=" + r.callingPid
1020 + ", uid=" + r.callingUid + ")"
1021 + " is not exported from uid " + info.activityInfo.applicationInfo.uid
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001022 + " due to receiver " + component.flattenToShortString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001023 } else {
1024 Slog.w(TAG, "Permission Denial: broadcasting "
1025 + r.intent.toString()
1026 + " from " + r.callerPackage + " (pid=" + r.callingPid
1027 + ", uid=" + r.callingUid + ")"
1028 + " requires " + info.activityInfo.permission
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001029 + " due to receiver " + component.flattenToShortString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001030 }
1031 skip = true;
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001032 } else if (!skip && info.activityInfo.permission != null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001033 final int opCode = AppOpsManager.permissionToOpCode(info.activityInfo.permission);
1034 if (opCode != AppOpsManager.OP_NONE
1035 && mService.mAppOpsService.noteOperation(opCode, r.callingUid,
1036 r.callerPackage) != AppOpsManager.MODE_ALLOWED) {
1037 Slog.w(TAG, "Appop Denial: broadcasting "
1038 + r.intent.toString()
1039 + " from " + r.callerPackage + " (pid="
1040 + r.callingPid + ", uid=" + r.callingUid + ")"
1041 + " requires appop " + AppOpsManager.permissionToOp(
1042 info.activityInfo.permission)
1043 + " due to registered receiver "
1044 + component.flattenToShortString());
1045 skip = true;
1046 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001047 }
Svet Ganov99b60432015-06-27 13:15:22 -07001048 if (!skip && info.activityInfo.applicationInfo.uid != Process.SYSTEM_UID &&
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001049 r.requiredPermissions != null && r.requiredPermissions.length > 0) {
1050 for (int i = 0; i < r.requiredPermissions.length; i++) {
1051 String requiredPermission = r.requiredPermissions[i];
1052 try {
1053 perm = AppGlobals.getPackageManager().
1054 checkPermission(requiredPermission,
1055 info.activityInfo.applicationInfo.packageName,
1056 UserHandle
1057 .getUserId(info.activityInfo.applicationInfo.uid));
1058 } catch (RemoteException e) {
1059 perm = PackageManager.PERMISSION_DENIED;
1060 }
1061 if (perm != PackageManager.PERMISSION_GRANTED) {
1062 Slog.w(TAG, "Permission Denial: receiving "
1063 + r.intent + " to "
1064 + component.flattenToShortString()
1065 + " requires " + requiredPermission
1066 + " due to sender " + r.callerPackage
1067 + " (uid " + r.callingUid + ")");
1068 skip = true;
1069 break;
1070 }
1071 int appOp = AppOpsManager.permissionToOpCode(requiredPermission);
1072 if (appOp != AppOpsManager.OP_NONE && appOp != r.appOp
1073 && mService.mAppOpsService.noteOperation(appOp,
Fyodor Kupolove37520b2015-07-14 22:29:21 +00001074 info.activityInfo.applicationInfo.uid, info.activityInfo.packageName)
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001075 != AppOpsManager.MODE_ALLOWED) {
1076 Slog.w(TAG, "Appop Denial: receiving "
1077 + r.intent + " to "
1078 + component.flattenToShortString()
1079 + " requires appop " + AppOpsManager.permissionToOp(
1080 requiredPermission)
1081 + " due to sender " + r.callerPackage
1082 + " (uid " + r.callingUid + ")");
1083 skip = true;
1084 break;
1085 }
1086 }
1087 }
1088 if (!skip && r.appOp != AppOpsManager.OP_NONE
1089 && mService.mAppOpsService.noteOperation(r.appOp,
1090 info.activityInfo.applicationInfo.uid, info.activityInfo.packageName)
1091 != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001092 Slog.w(TAG, "Appop Denial: receiving "
1093 + r.intent + " to "
1094 + component.flattenToShortString()
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001095 + " requires appop " + AppOpsManager.opToName(r.appOp)
Svet Ganov99b60432015-06-27 13:15:22 -07001096 + " due to sender " + r.callerPackage
1097 + " (uid " + r.callingUid + ")");
1098 skip = true;
1099 }
Ben Gruver49660c72013-08-06 19:54:08 -07001100 if (!skip) {
1101 skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,
1102 r.callingPid, r.resolvedType, info.activityInfo.applicationInfo.uid);
1103 }
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001104 boolean isSingleton = false;
1105 try {
1106 isSingleton = mService.isSingleton(info.activityInfo.processName,
1107 info.activityInfo.applicationInfo,
1108 info.activityInfo.name, info.activityInfo.flags);
1109 } catch (SecurityException e) {
1110 Slog.w(TAG, e.getMessage());
1111 skip = true;
1112 }
1113 if ((info.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) {
1114 if (ActivityManager.checkUidPermission(
1115 android.Manifest.permission.INTERACT_ACROSS_USERS,
1116 info.activityInfo.applicationInfo.uid)
1117 != PackageManager.PERMISSION_GRANTED) {
1118 Slog.w(TAG, "Permission Denial: Receiver " + component.flattenToShortString()
1119 + " requests FLAG_SINGLE_USER, but app does not hold "
1120 + android.Manifest.permission.INTERACT_ACROSS_USERS);
1121 skip = true;
1122 }
1123 }
Dianne Hackbornbc02a392016-06-02 17:15:08 -07001124 if (!skip) {
1125 r.manifestCount++;
1126 } else {
1127 r.manifestSkipCount++;
1128 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001129 if (r.curApp != null && r.curApp.crashing) {
1130 // If the target process is crashing, just skip it.
Dianne Hackborn9357b112013-10-03 18:27:48 -07001131 Slog.w(TAG, "Skipping deliver ordered [" + mQueueName + "] " + r
1132 + " to " + r.curApp + ": process crashing");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001133 skip = true;
1134 }
Christopher Tateba629da2013-11-13 17:42:28 -08001135 if (!skip) {
1136 boolean isAvailable = false;
1137 try {
1138 isAvailable = AppGlobals.getPackageManager().isPackageAvailable(
1139 info.activityInfo.packageName,
1140 UserHandle.getUserId(info.activityInfo.applicationInfo.uid));
1141 } catch (Exception e) {
1142 // all such failures mean we skip this receiver
1143 Slog.w(TAG, "Exception getting recipient info for "
1144 + info.activityInfo.packageName, e);
1145 }
1146 if (!isAvailable) {
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -07001147 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
1148 "Skipping delivery to " + info.activityInfo.packageName + " / "
1149 + info.activityInfo.applicationInfo.uid
1150 + " : package no longer available");
Christopher Tateba629da2013-11-13 17:42:28 -08001151 skip = true;
1152 }
1153 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001154
Svet Ganov9c165d72015-12-01 19:52:26 -08001155 // If permissions need a review before any of the app components can run, we drop
1156 // the broadcast and if the calling app is in the foreground and the broadcast is
1157 // explicit we launch the review UI passing it a pending intent to send the skipped
1158 // broadcast.
Svet Ganov77df6f32016-08-17 11:46:34 -07001159 if (mService.mPermissionReviewRequired && !skip) {
Svet Ganov9c165d72015-12-01 19:52:26 -08001160 if (!requestStartTargetPermissionsReviewIfNeededLocked(r,
1161 info.activityInfo.packageName, UserHandle.getUserId(
1162 info.activityInfo.applicationInfo.uid))) {
1163 skip = true;
1164 }
1165 }
1166
Dianne Hackborn76e80092015-12-09 14:15:34 -08001167 // This is safe to do even if we are skipping the broadcast, and we need
1168 // this information now to evaluate whether it is going to be allowed to run.
1169 final int receiverUid = info.activityInfo.applicationInfo.uid;
1170 // If it's a singleton, it needs to be the same app or a special app
1171 if (r.callingUid != Process.SYSTEM_UID && isSingleton
1172 && mService.isValidSingletonCall(r.callingUid, receiverUid)) {
1173 info.activityInfo = mService.getActivityInfoForUser(info.activityInfo, 0);
1174 }
Dianne Hackborn2639c4b2015-12-04 13:11:09 -08001175 String targetProcess = info.activityInfo.processName;
1176 ProcessRecord app = mService.getProcessRecordLocked(targetProcess,
1177 info.activityInfo.applicationInfo.uid, false);
Dianne Hackborn76e80092015-12-09 14:15:34 -08001178
Dianne Hackborn2639c4b2015-12-04 13:11:09 -08001179 if (!skip) {
Dianne Hackbornc3af19a2017-01-20 17:00:44 -08001180 final int allowed = mService.getAppStartModeLocked(
1181 info.activityInfo.applicationInfo.uid, info.activityInfo.packageName,
1182 info.activityInfo.applicationInfo.targetSdkVersion, -1, true, false);
Dianne Hackborn2639c4b2015-12-04 13:11:09 -08001183 if (allowed != ActivityManager.APP_START_MODE_NORMAL) {
1184 // We won't allow this receiver to be launched if the app has been
Dianne Hackborne91f3e72016-03-25 18:48:15 -07001185 // completely disabled from launches, or it was not explicitly sent
1186 // to it and the app is in a state that should not receive it
Dianne Hackbornc3af19a2017-01-20 17:00:44 -08001187 // (depending on how getAppStartModeLocked has determined that).
Dianne Hackborn6ac42ae2015-12-08 17:22:10 -08001188 if (allowed == ActivityManager.APP_START_MODE_DISABLED) {
1189 Slog.w(TAG, "Background execution disabled: receiving "
1190 + r.intent + " to "
1191 + component.flattenToShortString());
1192 skip = true;
Dianne Hackborne91f3e72016-03-25 18:48:15 -07001193 } else if (((r.intent.getFlags()&Intent.FLAG_RECEIVER_EXCLUDE_BACKGROUND) != 0)
Dianne Hackborn2639c4b2015-12-04 13:11:09 -08001194 || (r.intent.getComponent() == null
Dianne Hackborne91f3e72016-03-25 18:48:15 -07001195 && r.intent.getPackage() == null
Dianne Hackborn6ac42ae2015-12-08 17:22:10 -08001196 && ((r.intent.getFlags()
1197 & Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND) == 0))) {
Dianne Hackborn2639c4b2015-12-04 13:11:09 -08001198 Slog.w(TAG, "Background execution not allowed: receiving "
1199 + r.intent + " to "
1200 + component.flattenToShortString());
1201 skip = true;
1202 }
1203 }
1204 }
1205
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001206 if (skip) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001207 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -07001208 "Skipping delivery of ordered [" + mQueueName + "] "
1209 + r + " for whatever reason");
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001210 r.delivery[recIdx] = BroadcastRecord.DELIVERY_SKIPPED;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001211 r.receiver = null;
1212 r.curFilter = null;
1213 r.state = BroadcastRecord.IDLE;
1214 scheduleBroadcastsLocked();
1215 return;
1216 }
1217
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001218 r.delivery[recIdx] = BroadcastRecord.DELIVERY_DELIVERED;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001219 r.state = BroadcastRecord.APP_RECEIVE;
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001220 r.curComponent = component;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001221 r.curReceiver = info.activityInfo;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001222 if (DEBUG_MU && r.callingUid > UserHandle.PER_USER_RANGE) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001223 Slog.v(TAG_MU, "Updated broadcast record activity info for secondary user, "
1224 + info.activityInfo + ", callingUid = " + r.callingUid + ", uid = "
1225 + info.activityInfo.applicationInfo.uid);
1226 }
1227
Dianne Hackborna750a632015-06-16 17:18:23 -07001228 if (brOptions != null && brOptions.getTemporaryAppWhitelistDuration() > 0) {
1229 scheduleTempWhitelistLocked(receiverUid,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001230 brOptions.getTemporaryAppWhitelistDuration(), r);
Dianne Hackborna750a632015-06-16 17:18:23 -07001231 }
1232
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001233 // Broadcast is being executed, its package can't be stopped.
1234 try {
1235 AppGlobals.getPackageManager().setPackageStoppedState(
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001236 r.curComponent.getPackageName(), false, UserHandle.getUserId(r.callingUid));
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001237 } catch (RemoteException e) {
1238 } catch (IllegalArgumentException e) {
1239 Slog.w(TAG, "Failed trying to unstop package "
1240 + r.curComponent.getPackageName() + ": " + e);
1241 }
1242
1243 // Is this receiver's application already running?
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001244 if (app != null && app.thread != null) {
1245 try {
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001246 app.addPackage(info.activityInfo.packageName,
1247 info.activityInfo.applicationInfo.versionCode, mService.mProcessStats);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001248 processCurBroadcastLocked(r, app);
1249 return;
1250 } catch (RemoteException e) {
1251 Slog.w(TAG, "Exception when sending broadcast to "
1252 + r.curComponent, e);
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001253 } catch (RuntimeException e) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07001254 Slog.wtf(TAG, "Failed sending broadcast to "
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001255 + r.curComponent + " with " + r.intent, e);
1256 // If some unexpected exception happened, just skip
1257 // this broadcast. At this point we are not in the call
1258 // from a client, so throwing an exception out from here
1259 // will crash the entire system instead of just whoever
1260 // sent the broadcast.
1261 logBroadcastReceiverDiscardLocked(r);
1262 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -07001263 r.resultExtras, r.resultAbort, false);
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001264 scheduleBroadcastsLocked();
1265 // We need to reset the state if we failed to start the receiver.
1266 r.state = BroadcastRecord.IDLE;
1267 return;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001268 }
1269
1270 // If a dead object exception was thrown -- fall through to
1271 // restart the application.
1272 }
1273
1274 // Not running -- get it started, to be executed when the app comes up.
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001275 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001276 "Need to start app ["
1277 + mQueueName + "] " + targetProcess + " for broadcast " + r);
1278 if ((r.curApp=mService.startProcessLocked(targetProcess,
1279 info.activityInfo.applicationInfo, true,
1280 r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND,
1281 "broadcast", r.curComponent,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001282 (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0, false, false))
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001283 == null) {
1284 // Ah, this recipient is unavailable. Finish it if necessary,
1285 // and mark the broadcast record as ready for the next.
1286 Slog.w(TAG, "Unable to launch app "
1287 + info.activityInfo.applicationInfo.packageName + "/"
1288 + info.activityInfo.applicationInfo.uid + " for broadcast "
1289 + r.intent + ": process is bad");
1290 logBroadcastReceiverDiscardLocked(r);
1291 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -07001292 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001293 scheduleBroadcastsLocked();
1294 r.state = BroadcastRecord.IDLE;
1295 return;
1296 }
1297
1298 mPendingBroadcast = r;
1299 mPendingBroadcastRecvIndex = recIdx;
1300 }
1301 }
1302
1303 final void setBroadcastTimeoutLocked(long timeoutTime) {
1304 if (! mPendingBroadcastTimeoutMessage) {
1305 Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG, this);
1306 mHandler.sendMessageAtTime(msg, timeoutTime);
1307 mPendingBroadcastTimeoutMessage = true;
1308 }
1309 }
1310
1311 final void cancelBroadcastTimeoutLocked() {
1312 if (mPendingBroadcastTimeoutMessage) {
1313 mHandler.removeMessages(BROADCAST_TIMEOUT_MSG, this);
1314 mPendingBroadcastTimeoutMessage = false;
1315 }
1316 }
1317
1318 final void broadcastTimeoutLocked(boolean fromMsg) {
1319 if (fromMsg) {
1320 mPendingBroadcastTimeoutMessage = false;
1321 }
1322
1323 if (mOrderedBroadcasts.size() == 0) {
1324 return;
1325 }
1326
1327 long now = SystemClock.uptimeMillis();
1328 BroadcastRecord r = mOrderedBroadcasts.get(0);
1329 if (fromMsg) {
1330 if (mService.mDidDexOpt) {
1331 // Delay timeouts until dexopt finishes.
1332 mService.mDidDexOpt = false;
1333 long timeoutTime = SystemClock.uptimeMillis() + mTimeoutPeriod;
1334 setBroadcastTimeoutLocked(timeoutTime);
1335 return;
1336 }
1337 if (!mService.mProcessesReady) {
1338 // Only process broadcast timeouts if the system is ready. That way
1339 // PRE_BOOT_COMPLETED broadcasts can't timeout as they are intended
1340 // to do heavy lifting for system up.
1341 return;
1342 }
1343
1344 long timeoutTime = r.receiverTime + mTimeoutPeriod;
1345 if (timeoutTime > now) {
1346 // We can observe premature timeouts because we do not cancel and reset the
1347 // broadcast timeout message after each receiver finishes. Instead, we set up
1348 // an initial timeout then kick it down the road a little further as needed
1349 // when it expires.
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001350 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001351 "Premature timeout ["
1352 + mQueueName + "] @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for "
1353 + timeoutTime);
1354 setBroadcastTimeoutLocked(timeoutTime);
1355 return;
1356 }
1357 }
1358
Dianne Hackborn6285a322013-09-18 12:09:47 -07001359 BroadcastRecord br = mOrderedBroadcasts.get(0);
1360 if (br.state == BroadcastRecord.WAITING_SERVICES) {
1361 // In this case the broadcast had already finished, but we had decided to wait
1362 // for started services to finish as well before going on. So if we have actually
1363 // waited long enough time timeout the broadcast, let's give up on the whole thing
1364 // and just move on to the next.
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001365 Slog.i(TAG, "Waited long enough for: " + (br.curComponent != null
Dianne Hackborn6285a322013-09-18 12:09:47 -07001366 ? br.curComponent.flattenToShortString() : "(null)"));
1367 br.curComponent = null;
1368 br.state = BroadcastRecord.IDLE;
1369 processNextBroadcast(false);
1370 return;
1371 }
1372
1373 Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r. receiver
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001374 + ", started " + (now - r.receiverTime) + "ms ago");
1375 r.receiverTime = now;
1376 r.anrCount++;
1377
1378 // Current receiver has passed its expiration date.
1379 if (r.nextReceiver <= 0) {
1380 Slog.w(TAG, "Timeout on receiver with nextReceiver <= 0");
1381 return;
1382 }
1383
1384 ProcessRecord app = null;
1385 String anrMessage = null;
1386
1387 Object curReceiver = r.receivers.get(r.nextReceiver-1);
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001388 r.delivery[r.nextReceiver-1] = BroadcastRecord.DELIVERY_TIMEOUT;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001389 Slog.w(TAG, "Receiver during timeout: " + curReceiver);
1390 logBroadcastReceiverDiscardLocked(r);
1391 if (curReceiver instanceof BroadcastFilter) {
1392 BroadcastFilter bf = (BroadcastFilter)curReceiver;
1393 if (bf.receiverList.pid != 0
1394 && bf.receiverList.pid != ActivityManagerService.MY_PID) {
1395 synchronized (mService.mPidsSelfLocked) {
1396 app = mService.mPidsSelfLocked.get(
1397 bf.receiverList.pid);
1398 }
1399 }
1400 } else {
1401 app = r.curApp;
1402 }
1403
1404 if (app != null) {
1405 anrMessage = "Broadcast of " + r.intent.toString();
1406 }
1407
1408 if (mPendingBroadcast == r) {
1409 mPendingBroadcast = null;
1410 }
1411
1412 // Move on to the next receiver.
1413 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -07001414 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001415 scheduleBroadcastsLocked();
1416
1417 if (anrMessage != null) {
1418 // Post the ANR to the handler since we do not want to process ANRs while
1419 // potentially holding our lock.
1420 mHandler.post(new AppNotResponding(app, anrMessage));
1421 }
1422 }
1423
Christopher Tatef278f122015-04-22 13:12:01 -07001424 private final int ringAdvance(int x, final int increment, final int ringSize) {
1425 x += increment;
1426 if (x < 0) return (ringSize - 1);
1427 else if (x >= ringSize) return 0;
1428 else return x;
1429 }
1430
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001431 private final void addBroadcastToHistoryLocked(BroadcastRecord r) {
1432 if (r.callingUid < 0) {
1433 // This was from a registerReceiver() call; ignore it.
1434 return;
1435 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001436 r.finishTime = SystemClock.uptimeMillis();
Christopher Tatef278f122015-04-22 13:12:01 -07001437
Carmen Jacksona68e3452017-01-17 14:01:33 -08001438 if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
1439 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1440 createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_DELIVERED),
1441 System.identityHashCode(r));
1442 }
1443
Christopher Tatef278f122015-04-22 13:12:01 -07001444 mBroadcastHistory[mHistoryNext] = r;
1445 mHistoryNext = ringAdvance(mHistoryNext, 1, MAX_BROADCAST_HISTORY);
1446
1447 mBroadcastSummaryHistory[mSummaryHistoryNext] = r.intent;
1448 mSummaryHistoryEnqueueTime[mSummaryHistoryNext] = r.enqueueClockTime;
1449 mSummaryHistoryDispatchTime[mSummaryHistoryNext] = r.dispatchClockTime;
1450 mSummaryHistoryFinishTime[mSummaryHistoryNext] = System.currentTimeMillis();
1451 mSummaryHistoryNext = ringAdvance(mSummaryHistoryNext, 1, MAX_BROADCAST_SUMMARY_HISTORY);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001452 }
1453
Wale Ogunwaleca1c1252015-05-15 12:49:13 -07001454 boolean cleanupDisabledPackageReceiversLocked(
1455 String packageName, Set<String> filterByClasses, int userId, boolean doit) {
1456 boolean didSomething = false;
1457 for (int i = mParallelBroadcasts.size() - 1; i >= 0; i--) {
1458 didSomething |= mParallelBroadcasts.get(i).cleanupDisabledPackageReceiversLocked(
1459 packageName, filterByClasses, userId, doit);
1460 if (!doit && didSomething) {
1461 return true;
1462 }
1463 }
1464
1465 for (int i = mOrderedBroadcasts.size() - 1; i >= 0; i--) {
1466 didSomething |= mOrderedBroadcasts.get(i).cleanupDisabledPackageReceiversLocked(
1467 packageName, filterByClasses, userId, doit);
1468 if (!doit && didSomething) {
1469 return true;
1470 }
1471 }
1472
1473 return didSomething;
1474 }
1475
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001476 final void logBroadcastReceiverDiscardLocked(BroadcastRecord r) {
Wale Ogunwalea22c6322015-06-03 09:59:45 -07001477 final int logIndex = r.nextReceiver - 1;
1478 if (logIndex >= 0 && logIndex < r.receivers.size()) {
1479 Object curReceiver = r.receivers.get(logIndex);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001480 if (curReceiver instanceof BroadcastFilter) {
1481 BroadcastFilter bf = (BroadcastFilter) curReceiver;
1482 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001483 bf.owningUserId, System.identityHashCode(r),
Wale Ogunwalea22c6322015-06-03 09:59:45 -07001484 r.intent.getAction(), logIndex, System.identityHashCode(bf));
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001485 } else {
Wale Ogunwalea22c6322015-06-03 09:59:45 -07001486 ResolveInfo ri = (ResolveInfo) curReceiver;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001487 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001488 UserHandle.getUserId(ri.activityInfo.applicationInfo.uid),
Wale Ogunwalea22c6322015-06-03 09:59:45 -07001489 System.identityHashCode(r), r.intent.getAction(), logIndex, ri.toString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001490 }
1491 } else {
Wale Ogunwalea22c6322015-06-03 09:59:45 -07001492 if (logIndex < 0) Slog.w(TAG,
1493 "Discarding broadcast before first receiver is invoked: " + r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001494 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001495 -1, System.identityHashCode(r),
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001496 r.intent.getAction(),
1497 r.nextReceiver,
1498 "NONE");
1499 }
1500 }
1501
Carmen Jacksona68e3452017-01-17 14:01:33 -08001502 private String createBroadcastTraceTitle(BroadcastRecord record, int state) {
1503 return String.format("Broadcast %s from %s (%s) %s",
1504 state == BroadcastRecord.DELIVERY_PENDING ? "in queue" : "dispatched",
1505 record.callerPackage == null ? "" : record.callerPackage,
1506 record.callerApp == null ? "process unknown" : record.callerApp.toShortString(),
1507 record.intent == null ? "" : record.intent.getAction());
1508 }
1509
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001510 final boolean dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args,
1511 int opti, boolean dumpAll, String dumpPackage, boolean needSep) {
Dianne Hackborn865907d2015-10-21 17:12:53 -07001512 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001513 if (mParallelBroadcasts.size() > 0 || mOrderedBroadcasts.size() > 0
1514 || mPendingBroadcast != null) {
1515 boolean printed = false;
Wale Ogunwaleca1c1252015-05-15 12:49:13 -07001516 for (int i = mParallelBroadcasts.size() - 1; i >= 0; i--) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001517 BroadcastRecord br = mParallelBroadcasts.get(i);
1518 if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) {
1519 continue;
1520 }
1521 if (!printed) {
1522 if (needSep) {
1523 pw.println();
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001524 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001525 needSep = true;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001526 printed = true;
1527 pw.println(" Active broadcasts [" + mQueueName + "]:");
1528 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001529 pw.println(" Active Broadcast " + mQueueName + " #" + i + ":");
Dianne Hackborn865907d2015-10-21 17:12:53 -07001530 br.dump(pw, " ", sdf);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001531 }
1532 printed = false;
1533 needSep = true;
Wale Ogunwaleca1c1252015-05-15 12:49:13 -07001534 for (int i = mOrderedBroadcasts.size() - 1; i >= 0; i--) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001535 BroadcastRecord br = mOrderedBroadcasts.get(i);
1536 if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) {
1537 continue;
1538 }
1539 if (!printed) {
1540 if (needSep) {
1541 pw.println();
1542 }
1543 needSep = true;
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001544 printed = true;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001545 pw.println(" Active ordered broadcasts [" + mQueueName + "]:");
1546 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001547 pw.println(" Active Ordered Broadcast " + mQueueName + " #" + i + ":");
Dianne Hackborn865907d2015-10-21 17:12:53 -07001548 mOrderedBroadcasts.get(i).dump(pw, " ", sdf);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001549 }
1550 if (dumpPackage == null || (mPendingBroadcast != null
1551 && dumpPackage.equals(mPendingBroadcast.callerPackage))) {
1552 if (needSep) {
1553 pw.println();
1554 }
1555 pw.println(" Pending broadcast [" + mQueueName + "]:");
1556 if (mPendingBroadcast != null) {
Dianne Hackborn865907d2015-10-21 17:12:53 -07001557 mPendingBroadcast.dump(pw, " ", sdf);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001558 } else {
1559 pw.println(" (null)");
1560 }
1561 needSep = true;
1562 }
1563 }
1564
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001565 int i;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001566 boolean printed = false;
Christopher Tatef278f122015-04-22 13:12:01 -07001567
1568 i = -1;
1569 int lastIndex = mHistoryNext;
1570 int ringIndex = lastIndex;
1571 do {
1572 // increasing index = more recent entry, and we want to print the most
1573 // recent first and work backwards, so we roll through the ring backwards.
1574 ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_HISTORY);
1575 BroadcastRecord r = mBroadcastHistory[ringIndex];
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001576 if (r == null) {
Christopher Tatef278f122015-04-22 13:12:01 -07001577 continue;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001578 }
Christopher Tatef278f122015-04-22 13:12:01 -07001579
1580 i++; // genuine record of some sort even if we're filtering it out
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001581 if (dumpPackage != null && !dumpPackage.equals(r.callerPackage)) {
1582 continue;
1583 }
1584 if (!printed) {
1585 if (needSep) {
1586 pw.println();
1587 }
1588 needSep = true;
1589 pw.println(" Historical broadcasts [" + mQueueName + "]:");
1590 printed = true;
1591 }
1592 if (dumpAll) {
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001593 pw.print(" Historical Broadcast " + mQueueName + " #");
1594 pw.print(i); pw.println(":");
Dianne Hackborn865907d2015-10-21 17:12:53 -07001595 r.dump(pw, " ", sdf);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001596 } else {
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001597 pw.print(" #"); pw.print(i); pw.print(": "); pw.println(r);
1598 pw.print(" ");
1599 pw.println(r.intent.toShortString(false, true, true, false));
Dianne Hackborna40cfeb2013-03-25 17:49:36 -07001600 if (r.targetComp != null && r.targetComp != r.intent.getComponent()) {
1601 pw.print(" targetComp: "); pw.println(r.targetComp.toShortString());
1602 }
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001603 Bundle bundle = r.intent.getExtras();
1604 if (bundle != null) {
1605 pw.print(" extras: "); pw.println(bundle.toString());
1606 }
1607 }
Christopher Tatef278f122015-04-22 13:12:01 -07001608 } while (ringIndex != lastIndex);
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001609
1610 if (dumpPackage == null) {
Christopher Tatef278f122015-04-22 13:12:01 -07001611 lastIndex = ringIndex = mSummaryHistoryNext;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001612 if (dumpAll) {
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001613 printed = false;
Christopher Tatef278f122015-04-22 13:12:01 -07001614 i = -1;
1615 } else {
1616 // roll over the 'i' full dumps that have already been issued
1617 for (int j = i;
1618 j > 0 && ringIndex != lastIndex;) {
1619 ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_SUMMARY_HISTORY);
1620 BroadcastRecord r = mBroadcastHistory[ringIndex];
1621 if (r == null) {
1622 continue;
1623 }
1624 j--;
1625 }
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001626 }
Christopher Tatef278f122015-04-22 13:12:01 -07001627 // done skipping; dump the remainder of the ring. 'i' is still the ordinal within
1628 // the overall broadcast history.
Christopher Tatef278f122015-04-22 13:12:01 -07001629 do {
1630 ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_SUMMARY_HISTORY);
1631 Intent intent = mBroadcastSummaryHistory[ringIndex];
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001632 if (intent == null) {
Christopher Tatef278f122015-04-22 13:12:01 -07001633 continue;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001634 }
1635 if (!printed) {
1636 if (needSep) {
1637 pw.println();
1638 }
1639 needSep = true;
1640 pw.println(" Historical broadcasts summary [" + mQueueName + "]:");
1641 printed = true;
1642 }
1643 if (!dumpAll && i >= 50) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001644 pw.println(" ...");
1645 break;
1646 }
Christopher Tatef278f122015-04-22 13:12:01 -07001647 i++;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001648 pw.print(" #"); pw.print(i); pw.print(": ");
1649 pw.println(intent.toShortString(false, true, true, false));
Dianne Hackborn865907d2015-10-21 17:12:53 -07001650 pw.print(" ");
1651 TimeUtils.formatDuration(mSummaryHistoryDispatchTime[ringIndex]
1652 - mSummaryHistoryEnqueueTime[ringIndex], pw);
1653 pw.print(" dispatch ");
1654 TimeUtils.formatDuration(mSummaryHistoryFinishTime[ringIndex]
1655 - mSummaryHistoryDispatchTime[ringIndex], pw);
1656 pw.println(" finish");
1657 pw.print(" enq=");
1658 pw.print(sdf.format(new Date(mSummaryHistoryEnqueueTime[ringIndex])));
1659 pw.print(" disp=");
1660 pw.print(sdf.format(new Date(mSummaryHistoryDispatchTime[ringIndex])));
1661 pw.print(" fin=");
1662 pw.println(sdf.format(new Date(mSummaryHistoryFinishTime[ringIndex])));
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001663 Bundle bundle = intent.getExtras();
1664 if (bundle != null) {
1665 pw.print(" extras: "); pw.println(bundle.toString());
1666 }
Christopher Tatef278f122015-04-22 13:12:01 -07001667 } while (ringIndex != lastIndex);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001668 }
1669
1670 return needSep;
1671 }
1672}