blob: dd3d4e0d1be2de7948a39ab2caa98c36571339c2 [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
Chad Brubakerb7e34d52017-02-22 12:36:06 -0800626 // Ensure that broadcasts are only sent to other Instant Apps if they are marked as
627 // visible to Instant Apps.
628 final boolean visibleToInstantApps =
629 (r.intent.getFlags() & Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS) != 0;
630
631 if (!skip && !visibleToInstantApps && filter.instantApp
632 && filter.receiverList.uid != r.callingUid) {
633 Slog.w(TAG, "Instant App Denial: receiving "
634 + r.intent.toString()
635 + " to " + filter.receiverList.app
636 + " (pid=" + filter.receiverList.pid
637 + ", uid=" + filter.receiverList.uid + ")"
638 + " due to sender " + r.callerPackage
639 + " (uid " + r.callingUid + ")"
640 + " not specifying FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS");
641 skip = true;
642 }
643
Chad Brubaker816c83b2017-03-02 10:27:59 -0800644 if (!skip && !filter.visibleToInstantApp && r.callerInstantApp
645 && filter.receiverList.uid != r.callingUid) {
646 Slog.w(TAG, "Instant App Denial: receiving "
647 + r.intent.toString()
648 + " to " + filter.receiverList.app
649 + " (pid=" + filter.receiverList.pid
650 + ", uid=" + filter.receiverList.uid + ")"
651 + " requires receiver be visible to instant apps"
652 + " due to sender " + r.callerPackage
653 + " (uid " + r.callingUid + ")");
654 skip = true;
655 }
656
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800657 if (skip) {
658 r.delivery[index] = BroadcastRecord.DELIVERY_SKIPPED;
659 return;
660 }
Svet Ganov9c165d72015-12-01 19:52:26 -0800661
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800662 // If permissions need a review before any of the app components can run, we drop
663 // the broadcast and if the calling app is in the foreground and the broadcast is
664 // explicit we launch the review UI passing it a pending intent to send the skipped
665 // broadcast.
Svet Ganov77df6f32016-08-17 11:46:34 -0700666 if (mService.mPermissionReviewRequired) {
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800667 if (!requestStartTargetPermissionsReviewIfNeededLocked(r, filter.packageName,
668 filter.owningUserId)) {
669 r.delivery[index] = BroadcastRecord.DELIVERY_SKIPPED;
670 return;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800671 }
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800672 }
673
674 r.delivery[index] = BroadcastRecord.DELIVERY_DELIVERED;
675
676 // If this is not being sent as an ordered broadcast, then we
677 // don't want to touch the fields that keep track of the current
678 // state of ordered broadcasts.
679 if (ordered) {
680 r.receiver = filter.receiverList.receiver.asBinder();
681 r.curFilter = filter;
682 filter.receiverList.curBroadcast = r;
683 r.state = BroadcastRecord.CALL_IN_RECEIVE;
684 if (filter.receiverList.app != null) {
685 // Bump hosting application to no longer be in background
686 // scheduling class. Note that we can't do that if there
687 // isn't an app... but we can only be in that case for
688 // things that directly call the IActivityManager API, which
689 // are already core system stuff so don't matter for this.
690 r.curApp = filter.receiverList.app;
yangzhenyud509bc92016-08-31 18:26:46 +0800691 filter.receiverList.app.curReceivers.add(r);
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800692 mService.updateOomAdjLocked(r.curApp);
693 }
694 }
695 try {
696 if (DEBUG_BROADCAST_LIGHT) Slog.i(TAG_BROADCAST,
697 "Delivering to " + filter + " : " + r);
Amith Yamasanib5f3b5c2016-05-16 14:23:04 -0700698 if (filter.receiverList.app != null && filter.receiverList.app.inFullBackup) {
699 // Skip delivery if full backup in progress
700 // If it's an ordered broadcast, we need to continue to the next receiver.
701 if (ordered) {
702 skipReceiverLocked(r);
703 }
704 } else {
705 performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver,
706 new Intent(r.intent), r.resultCode, r.resultData,
707 r.resultExtras, r.ordered, r.initialSticky, r.userId);
708 }
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800709 if (ordered) {
710 r.state = BroadcastRecord.CALL_DONE_RECEIVE;
711 }
712 } catch (RemoteException e) {
713 Slog.w(TAG, "Failure sending broadcast " + r.intent, e);
714 if (ordered) {
715 r.receiver = null;
716 r.curFilter = null;
717 filter.receiverList.curBroadcast = null;
718 if (filter.receiverList.app != null) {
yangzhenyud509bc92016-08-31 18:26:46 +0800719 filter.receiverList.app.curReceivers.remove(r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800720 }
721 }
722 }
723 }
724
Svet Ganov9c165d72015-12-01 19:52:26 -0800725 private boolean requestStartTargetPermissionsReviewIfNeededLocked(
726 BroadcastRecord receiverRecord, String receivingPackageName,
727 final int receivingUserId) {
728 if (!mService.getPackageManagerInternalLocked().isPermissionsReviewRequired(
729 receivingPackageName, receivingUserId)) {
730 return true;
731 }
732
733 final boolean callerForeground = receiverRecord.callerApp != null
Dianne Hackborna49ad092016-03-03 13:39:10 -0800734 ? receiverRecord.callerApp.setSchedGroup != ProcessList.SCHED_GROUP_BACKGROUND
Svet Ganov9c165d72015-12-01 19:52:26 -0800735 : true;
736
737 // Show a permission review UI only for explicit broadcast from a foreground app
738 if (callerForeground && receiverRecord.intent.getComponent() != null) {
739 IIntentSender target = mService.getIntentSenderLocked(
740 ActivityManager.INTENT_SENDER_BROADCAST, receiverRecord.callerPackage,
741 receiverRecord.callingUid, receiverRecord.userId, null, null, 0,
742 new Intent[]{receiverRecord.intent},
743 new String[]{receiverRecord.intent.resolveType(mService.mContext
744 .getContentResolver())},
745 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
746 | PendingIntent.FLAG_IMMUTABLE, null);
747
748 final Intent intent = new Intent(Intent.ACTION_REVIEW_PERMISSIONS);
749 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
750 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
751 intent.putExtra(Intent.EXTRA_PACKAGE_NAME, receivingPackageName);
752 intent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));
753
754 if (DEBUG_PERMISSIONS_REVIEW) {
755 Slog.i(TAG, "u" + receivingUserId + " Launching permission review for package "
756 + receivingPackageName);
757 }
758
759 mHandler.post(new Runnable() {
760 @Override
761 public void run() {
762 mService.mContext.startActivityAsUser(intent, new UserHandle(receivingUserId));
763 }
764 });
765 } else {
766 Slog.w(TAG, "u" + receivingUserId + " Receiving a broadcast in package"
767 + receivingPackageName + " requires a permissions review");
768 }
769
770 return false;
771 }
772
Dianne Hackbornfd854ee2015-07-13 18:00:37 -0700773 final void scheduleTempWhitelistLocked(int uid, long duration, BroadcastRecord r) {
Dianne Hackborna750a632015-06-16 17:18:23 -0700774 if (duration > Integer.MAX_VALUE) {
775 duration = Integer.MAX_VALUE;
776 }
777 // XXX ideally we should pause the broadcast until everything behind this is done,
778 // or else we will likely start dispatching the broadcast before we have opened
779 // access to the app (there is a lot of asynchronicity behind this). It is probably
780 // not that big a deal, however, because the main purpose here is to allow apps
781 // to hold wake locks, and they will be able to acquire their wake lock immediately
782 // it just won't be enabled until we get through this work.
Dianne Hackbornfd854ee2015-07-13 18:00:37 -0700783 StringBuilder b = new StringBuilder();
784 b.append("broadcast:");
785 UserHandle.formatUid(b, r.callingUid);
786 b.append(":");
787 if (r.intent.getAction() != null) {
788 b.append(r.intent.getAction());
789 } else if (r.intent.getComponent() != null) {
790 b.append(r.intent.getComponent().flattenToShortString());
791 } else if (r.intent.getData() != null) {
792 b.append(r.intent.getData());
793 }
794 mHandler.obtainMessage(SCHEDULE_TEMP_WHITELIST_MSG, uid, (int)duration, b.toString())
795 .sendToTarget();
Dianne Hackborna750a632015-06-16 17:18:23 -0700796 }
797
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800798 final void processNextBroadcast(boolean fromMsg) {
799 synchronized(mService) {
800 BroadcastRecord r;
801
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800802 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "processNextBroadcast ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800803 + mQueueName + "]: "
Carmen Jacksona68e3452017-01-17 14:01:33 -0800804 + mParallelBroadcasts.size() + " parallel broadcasts, "
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800805 + mOrderedBroadcasts.size() + " ordered broadcasts");
806
807 mService.updateCpuStats();
808
809 if (fromMsg) {
810 mBroadcastsScheduled = false;
811 }
812
813 // First, deliver any non-serialized broadcasts right away.
814 while (mParallelBroadcasts.size() > 0) {
815 r = mParallelBroadcasts.remove(0);
816 r.dispatchTime = SystemClock.uptimeMillis();
817 r.dispatchClockTime = System.currentTimeMillis();
Carmen Jacksona68e3452017-01-17 14:01:33 -0800818
819 if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
820 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER,
821 createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_PENDING),
822 System.identityHashCode(r));
823 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
824 createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_DELIVERED),
825 System.identityHashCode(r));
826 }
827
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800828 final int N = r.receivers.size();
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800829 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Processing parallel broadcast ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800830 + mQueueName + "] " + r);
831 for (int i=0; i<N; i++) {
832 Object target = r.receivers.get(i);
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800833 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800834 "Delivering non-ordered on [" + mQueueName + "] to registered "
835 + target + ": " + r);
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800836 deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false, i);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800837 }
838 addBroadcastToHistoryLocked(r);
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800839 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Done with parallel broadcast ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800840 + mQueueName + "] " + r);
841 }
842
843 // Now take care of the next serialized one...
844
845 // If we are waiting for a process to come up to handle the next
846 // broadcast, then do nothing at this point. Just in case, we
847 // check that the process we're waiting for still exists.
848 if (mPendingBroadcast != null) {
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -0700849 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST,
850 "processNextBroadcast [" + mQueueName + "]: waiting for "
851 + mPendingBroadcast.curApp);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800852
853 boolean isDead;
854 synchronized (mService.mPidsSelfLocked) {
Dianne Hackborn9357b112013-10-03 18:27:48 -0700855 ProcessRecord proc = mService.mPidsSelfLocked.get(mPendingBroadcast.curApp.pid);
856 isDead = proc == null || proc.crashing;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800857 }
858 if (!isDead) {
859 // It's still alive, so keep waiting
860 return;
861 } else {
862 Slog.w(TAG, "pending app ["
863 + mQueueName + "]" + mPendingBroadcast.curApp
864 + " died before responding to broadcast");
865 mPendingBroadcast.state = BroadcastRecord.IDLE;
866 mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex;
867 mPendingBroadcast = null;
868 }
869 }
870
871 boolean looped = false;
872
873 do {
874 if (mOrderedBroadcasts.size() == 0) {
875 // No more broadcasts pending, so all done!
876 mService.scheduleAppGcsLocked();
877 if (looped) {
878 // If we had finished the last ordered broadcast, then
879 // make sure all processes have correct oom and sched
880 // adjustments.
881 mService.updateOomAdjLocked();
882 }
883 return;
884 }
885 r = mOrderedBroadcasts.get(0);
886 boolean forceReceive = false;
887
888 // Ensure that even if something goes awry with the timeout
889 // detection, we catch "hung" broadcasts here, discard them,
890 // and continue to make progress.
891 //
892 // This is only done if the system is ready so that PRE_BOOT_COMPLETED
893 // receivers don't get executed with timeouts. They're intended for
894 // one time heavy lifting after system upgrades and can take
895 // significant amounts of time.
896 int numReceivers = (r.receivers != null) ? r.receivers.size() : 0;
897 if (mService.mProcessesReady && r.dispatchTime > 0) {
898 long now = SystemClock.uptimeMillis();
899 if ((numReceivers > 0) &&
900 (now > r.dispatchTime + (2*mTimeoutPeriod*numReceivers))) {
901 Slog.w(TAG, "Hung broadcast ["
902 + mQueueName + "] discarded after timeout failure:"
903 + " now=" + now
904 + " dispatchTime=" + r.dispatchTime
905 + " startTime=" + r.receiverTime
906 + " intent=" + r.intent
907 + " numReceivers=" + numReceivers
908 + " nextReceiver=" + r.nextReceiver
909 + " state=" + r.state);
910 broadcastTimeoutLocked(false); // forcibly finish this broadcast
911 forceReceive = true;
912 r.state = BroadcastRecord.IDLE;
913 }
914 }
915
916 if (r.state != BroadcastRecord.IDLE) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800917 if (DEBUG_BROADCAST) Slog.d(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800918 "processNextBroadcast("
919 + mQueueName + ") called when not idle (state="
920 + r.state + ")");
921 return;
922 }
923
924 if (r.receivers == null || r.nextReceiver >= numReceivers
925 || r.resultAbort || forceReceive) {
926 // No more receivers for this broadcast! Send the final
927 // result if requested...
928 if (r.resultTo != null) {
929 try {
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800930 if (DEBUG_BROADCAST) Slog.i(TAG_BROADCAST,
Todd Kennedyd2f15112015-01-21 15:25:56 -0800931 "Finishing broadcast [" + mQueueName + "] "
932 + r.intent.getAction() + " app=" + r.callerApp);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800933 performReceiveLocked(r.callerApp, r.resultTo,
934 new Intent(r.intent), r.resultCode,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700935 r.resultData, r.resultExtras, false, false, r.userId);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800936 // Set this to null so that the reference
Dianne Hackborn9357b112013-10-03 18:27:48 -0700937 // (local and remote) isn't kept in the mBroadcastHistory.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800938 r.resultTo = null;
939 } catch (RemoteException e) {
Craig Mautner38c1a5f2014-07-21 15:36:37 +0000940 r.resultTo = null;
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800941 Slog.w(TAG, "Failure ["
942 + mQueueName + "] sending broadcast result of "
943 + r.intent, e);
Joe Onorato5869d1c2016-04-20 15:38:07 -0700944
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800945 }
946 }
947
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800948 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Cancelling BROADCAST_TIMEOUT_MSG");
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800949 cancelBroadcastTimeoutLocked();
950
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -0700951 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST,
952 "Finished with ordered broadcast " + r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800953
954 // ... and on to the next...
955 addBroadcastToHistoryLocked(r);
Dianne Hackbornbc02a392016-06-02 17:15:08 -0700956 if (r.intent.getComponent() == null && r.intent.getPackage() == null
957 && (r.intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) {
958 // This was an implicit broadcast... let's record it for posterity.
959 mService.addBroadcastStatLocked(r.intent.getAction(), r.callerPackage,
960 r.manifestCount, r.manifestSkipCount, r.finishTime-r.dispatchTime);
961 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800962 mOrderedBroadcasts.remove(0);
963 r = null;
964 looped = true;
965 continue;
966 }
967 } while (r == null);
968
969 // Get the next receiver...
970 int recIdx = r.nextReceiver++;
971
972 // Keep track of when this receiver started, and make sure there
973 // is a timeout message pending to kill it if need be.
974 r.receiverTime = SystemClock.uptimeMillis();
975 if (recIdx == 0) {
976 r.dispatchTime = r.receiverTime;
977 r.dispatchClockTime = System.currentTimeMillis();
Carmen Jacksona68e3452017-01-17 14:01:33 -0800978 if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
979 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER,
980 createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_PENDING),
981 System.identityHashCode(r));
982 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
983 createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_DELIVERED),
984 System.identityHashCode(r));
985 }
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800986 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Processing ordered broadcast ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800987 + mQueueName + "] " + r);
988 }
989 if (! mPendingBroadcastTimeoutMessage) {
990 long timeoutTime = r.receiverTime + mTimeoutPeriod;
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800991 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800992 "Submitting BROADCAST_TIMEOUT_MSG ["
993 + mQueueName + "] for " + r + " at " + timeoutTime);
994 setBroadcastTimeoutLocked(timeoutTime);
995 }
996
Dianne Hackborna750a632015-06-16 17:18:23 -0700997 final BroadcastOptions brOptions = r.options;
998 final Object nextReceiver = r.receivers.get(recIdx);
999
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001000 if (nextReceiver instanceof BroadcastFilter) {
1001 // Simple case: this is a registered receiver who gets
1002 // a direct call.
1003 BroadcastFilter filter = (BroadcastFilter)nextReceiver;
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001004 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001005 "Delivering ordered ["
1006 + mQueueName + "] to registered "
1007 + filter + ": " + r);
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001008 deliverToRegisteredReceiverLocked(r, filter, r.ordered, recIdx);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001009 if (r.receiver == null || !r.ordered) {
1010 // The receiver has already finished, so schedule to
1011 // process the next one.
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001012 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Quick finishing ["
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001013 + mQueueName + "]: ordered="
1014 + r.ordered + " receiver=" + r.receiver);
1015 r.state = BroadcastRecord.IDLE;
1016 scheduleBroadcastsLocked();
Dianne Hackborna750a632015-06-16 17:18:23 -07001017 } else {
1018 if (brOptions != null && brOptions.getTemporaryAppWhitelistDuration() > 0) {
1019 scheduleTempWhitelistLocked(filter.owningUid,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001020 brOptions.getTemporaryAppWhitelistDuration(), r);
Dianne Hackborna750a632015-06-16 17:18:23 -07001021 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001022 }
1023 return;
1024 }
1025
1026 // Hard case: need to instantiate the receiver, possibly
1027 // starting its application process to host it.
1028
1029 ResolveInfo info =
1030 (ResolveInfo)nextReceiver;
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001031 ComponentName component = new ComponentName(
1032 info.activityInfo.applicationInfo.packageName,
1033 info.activityInfo.name);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001034
1035 boolean skip = false;
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001036 if (brOptions != null &&
1037 (info.activityInfo.applicationInfo.targetSdkVersion
1038 < brOptions.getMinManifestReceiverApiLevel() ||
1039 info.activityInfo.applicationInfo.targetSdkVersion
1040 > brOptions.getMaxManifestReceiverApiLevel())) {
1041 skip = true;
1042 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001043 int perm = mService.checkComponentPermission(info.activityInfo.permission,
1044 r.callingPid, r.callingUid, info.activityInfo.applicationInfo.uid,
1045 info.activityInfo.exported);
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001046 if (!skip && perm != PackageManager.PERMISSION_GRANTED) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001047 if (!info.activityInfo.exported) {
1048 Slog.w(TAG, "Permission Denial: broadcasting "
1049 + r.intent.toString()
1050 + " from " + r.callerPackage + " (pid=" + r.callingPid
1051 + ", uid=" + r.callingUid + ")"
1052 + " is not exported from uid " + info.activityInfo.applicationInfo.uid
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001053 + " due to receiver " + component.flattenToShortString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001054 } else {
1055 Slog.w(TAG, "Permission Denial: broadcasting "
1056 + r.intent.toString()
1057 + " from " + r.callerPackage + " (pid=" + r.callingPid
1058 + ", uid=" + r.callingUid + ")"
1059 + " requires " + info.activityInfo.permission
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001060 + " due to receiver " + component.flattenToShortString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001061 }
1062 skip = true;
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001063 } else if (!skip && info.activityInfo.permission != null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001064 final int opCode = AppOpsManager.permissionToOpCode(info.activityInfo.permission);
1065 if (opCode != AppOpsManager.OP_NONE
1066 && mService.mAppOpsService.noteOperation(opCode, r.callingUid,
1067 r.callerPackage) != AppOpsManager.MODE_ALLOWED) {
1068 Slog.w(TAG, "Appop Denial: broadcasting "
1069 + r.intent.toString()
1070 + " from " + r.callerPackage + " (pid="
1071 + r.callingPid + ", uid=" + r.callingUid + ")"
1072 + " requires appop " + AppOpsManager.permissionToOp(
1073 info.activityInfo.permission)
1074 + " due to registered receiver "
1075 + component.flattenToShortString());
1076 skip = true;
1077 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001078 }
Svet Ganov99b60432015-06-27 13:15:22 -07001079 if (!skip && info.activityInfo.applicationInfo.uid != Process.SYSTEM_UID &&
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001080 r.requiredPermissions != null && r.requiredPermissions.length > 0) {
1081 for (int i = 0; i < r.requiredPermissions.length; i++) {
1082 String requiredPermission = r.requiredPermissions[i];
1083 try {
1084 perm = AppGlobals.getPackageManager().
1085 checkPermission(requiredPermission,
1086 info.activityInfo.applicationInfo.packageName,
1087 UserHandle
1088 .getUserId(info.activityInfo.applicationInfo.uid));
1089 } catch (RemoteException e) {
1090 perm = PackageManager.PERMISSION_DENIED;
1091 }
1092 if (perm != PackageManager.PERMISSION_GRANTED) {
1093 Slog.w(TAG, "Permission Denial: receiving "
1094 + r.intent + " to "
1095 + component.flattenToShortString()
1096 + " requires " + requiredPermission
1097 + " due to sender " + r.callerPackage
1098 + " (uid " + r.callingUid + ")");
1099 skip = true;
1100 break;
1101 }
1102 int appOp = AppOpsManager.permissionToOpCode(requiredPermission);
1103 if (appOp != AppOpsManager.OP_NONE && appOp != r.appOp
1104 && mService.mAppOpsService.noteOperation(appOp,
Fyodor Kupolove37520b2015-07-14 22:29:21 +00001105 info.activityInfo.applicationInfo.uid, info.activityInfo.packageName)
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001106 != AppOpsManager.MODE_ALLOWED) {
1107 Slog.w(TAG, "Appop Denial: receiving "
1108 + r.intent + " to "
1109 + component.flattenToShortString()
1110 + " requires appop " + AppOpsManager.permissionToOp(
1111 requiredPermission)
1112 + " due to sender " + r.callerPackage
1113 + " (uid " + r.callingUid + ")");
1114 skip = true;
1115 break;
1116 }
1117 }
1118 }
1119 if (!skip && r.appOp != AppOpsManager.OP_NONE
1120 && mService.mAppOpsService.noteOperation(r.appOp,
1121 info.activityInfo.applicationInfo.uid, info.activityInfo.packageName)
1122 != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001123 Slog.w(TAG, "Appop Denial: receiving "
1124 + r.intent + " to "
1125 + component.flattenToShortString()
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001126 + " requires appop " + AppOpsManager.opToName(r.appOp)
Svet Ganov99b60432015-06-27 13:15:22 -07001127 + " due to sender " + r.callerPackage
1128 + " (uid " + r.callingUid + ")");
1129 skip = true;
1130 }
Ben Gruver49660c72013-08-06 19:54:08 -07001131 if (!skip) {
1132 skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,
1133 r.callingPid, r.resolvedType, info.activityInfo.applicationInfo.uid);
1134 }
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001135 boolean isSingleton = false;
1136 try {
1137 isSingleton = mService.isSingleton(info.activityInfo.processName,
1138 info.activityInfo.applicationInfo,
1139 info.activityInfo.name, info.activityInfo.flags);
1140 } catch (SecurityException e) {
1141 Slog.w(TAG, e.getMessage());
1142 skip = true;
1143 }
1144 if ((info.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) {
1145 if (ActivityManager.checkUidPermission(
1146 android.Manifest.permission.INTERACT_ACROSS_USERS,
1147 info.activityInfo.applicationInfo.uid)
1148 != PackageManager.PERMISSION_GRANTED) {
1149 Slog.w(TAG, "Permission Denial: Receiver " + component.flattenToShortString()
1150 + " requests FLAG_SINGLE_USER, but app does not hold "
1151 + android.Manifest.permission.INTERACT_ACROSS_USERS);
1152 skip = true;
1153 }
1154 }
Chad Brubakerb7e34d52017-02-22 12:36:06 -08001155 if (!skip && info.activityInfo.applicationInfo.isInstantApp()
Chad Brubakerb7e34d52017-02-22 12:36:06 -08001156 && r.callingUid != info.activityInfo.applicationInfo.uid) {
1157 Slog.w(TAG, "Instant App Denial: receiving "
1158 + r.intent
1159 + " to " + component.flattenToShortString()
1160 + " due to sender " + r.callerPackage
1161 + " (uid " + r.callingUid + ")"
Chad Brubakerabd2b662017-03-16 11:24:16 -07001162 + " Instant Apps do not support manifest receivers");
Chad Brubakerb7e34d52017-02-22 12:36:06 -08001163 skip = true;
1164 }
Chad Brubaker816c83b2017-03-02 10:27:59 -08001165 if (!skip && r.callerInstantApp
1166 && (info.activityInfo.flags & ActivityInfo.FLAG_VISIBLE_TO_EPHEMERAL) == 0
1167 && r.callingUid != info.activityInfo.applicationInfo.uid) {
1168 Slog.w(TAG, "Instant App Denial: receiving "
1169 + r.intent
1170 + " to " + component.flattenToShortString()
1171 + " requires receiver have visibleToInstantApps set"
1172 + " due to sender " + r.callerPackage
1173 + " (uid " + r.callingUid + ")");
1174 skip = true;
1175 }
Dianne Hackbornbc02a392016-06-02 17:15:08 -07001176 if (!skip) {
1177 r.manifestCount++;
1178 } else {
1179 r.manifestSkipCount++;
1180 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001181 if (r.curApp != null && r.curApp.crashing) {
1182 // If the target process is crashing, just skip it.
Dianne Hackborn9357b112013-10-03 18:27:48 -07001183 Slog.w(TAG, "Skipping deliver ordered [" + mQueueName + "] " + r
1184 + " to " + r.curApp + ": process crashing");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001185 skip = true;
1186 }
Christopher Tateba629da2013-11-13 17:42:28 -08001187 if (!skip) {
1188 boolean isAvailable = false;
1189 try {
1190 isAvailable = AppGlobals.getPackageManager().isPackageAvailable(
1191 info.activityInfo.packageName,
1192 UserHandle.getUserId(info.activityInfo.applicationInfo.uid));
1193 } catch (Exception e) {
1194 // all such failures mean we skip this receiver
1195 Slog.w(TAG, "Exception getting recipient info for "
1196 + info.activityInfo.packageName, e);
1197 }
1198 if (!isAvailable) {
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -07001199 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
1200 "Skipping delivery to " + info.activityInfo.packageName + " / "
1201 + info.activityInfo.applicationInfo.uid
1202 + " : package no longer available");
Christopher Tateba629da2013-11-13 17:42:28 -08001203 skip = true;
1204 }
1205 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001206
Svet Ganov9c165d72015-12-01 19:52:26 -08001207 // If permissions need a review before any of the app components can run, we drop
1208 // the broadcast and if the calling app is in the foreground and the broadcast is
1209 // explicit we launch the review UI passing it a pending intent to send the skipped
1210 // broadcast.
Svet Ganov77df6f32016-08-17 11:46:34 -07001211 if (mService.mPermissionReviewRequired && !skip) {
Svet Ganov9c165d72015-12-01 19:52:26 -08001212 if (!requestStartTargetPermissionsReviewIfNeededLocked(r,
1213 info.activityInfo.packageName, UserHandle.getUserId(
1214 info.activityInfo.applicationInfo.uid))) {
1215 skip = true;
1216 }
1217 }
1218
Dianne Hackborn76e80092015-12-09 14:15:34 -08001219 // This is safe to do even if we are skipping the broadcast, and we need
1220 // this information now to evaluate whether it is going to be allowed to run.
1221 final int receiverUid = info.activityInfo.applicationInfo.uid;
1222 // If it's a singleton, it needs to be the same app or a special app
1223 if (r.callingUid != Process.SYSTEM_UID && isSingleton
1224 && mService.isValidSingletonCall(r.callingUid, receiverUid)) {
1225 info.activityInfo = mService.getActivityInfoForUser(info.activityInfo, 0);
1226 }
Dianne Hackborn2639c4b2015-12-04 13:11:09 -08001227 String targetProcess = info.activityInfo.processName;
1228 ProcessRecord app = mService.getProcessRecordLocked(targetProcess,
1229 info.activityInfo.applicationInfo.uid, false);
Dianne Hackborn76e80092015-12-09 14:15:34 -08001230
Dianne Hackborn2639c4b2015-12-04 13:11:09 -08001231 if (!skip) {
Dianne Hackbornc3af19a2017-01-20 17:00:44 -08001232 final int allowed = mService.getAppStartModeLocked(
1233 info.activityInfo.applicationInfo.uid, info.activityInfo.packageName,
1234 info.activityInfo.applicationInfo.targetSdkVersion, -1, true, false);
Dianne Hackborn2639c4b2015-12-04 13:11:09 -08001235 if (allowed != ActivityManager.APP_START_MODE_NORMAL) {
1236 // We won't allow this receiver to be launched if the app has been
Dianne Hackborne91f3e72016-03-25 18:48:15 -07001237 // completely disabled from launches, or it was not explicitly sent
1238 // to it and the app is in a state that should not receive it
Dianne Hackbornc3af19a2017-01-20 17:00:44 -08001239 // (depending on how getAppStartModeLocked has determined that).
Dianne Hackborn6ac42ae2015-12-08 17:22:10 -08001240 if (allowed == ActivityManager.APP_START_MODE_DISABLED) {
1241 Slog.w(TAG, "Background execution disabled: receiving "
1242 + r.intent + " to "
1243 + component.flattenToShortString());
1244 skip = true;
Dianne Hackborne91f3e72016-03-25 18:48:15 -07001245 } else if (((r.intent.getFlags()&Intent.FLAG_RECEIVER_EXCLUDE_BACKGROUND) != 0)
Dianne Hackborn2639c4b2015-12-04 13:11:09 -08001246 || (r.intent.getComponent() == null
Dianne Hackborne91f3e72016-03-25 18:48:15 -07001247 && r.intent.getPackage() == null
Dianne Hackborn6ac42ae2015-12-08 17:22:10 -08001248 && ((r.intent.getFlags()
1249 & Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND) == 0))) {
Dianne Hackborn7fc46d82017-02-14 15:20:06 -08001250 mService.addBackgroundCheckViolationLocked(r.intent.getAction(),
1251 component.getPackageName());
Dianne Hackborn2639c4b2015-12-04 13:11:09 -08001252 Slog.w(TAG, "Background execution not allowed: receiving "
1253 + r.intent + " to "
1254 + component.flattenToShortString());
1255 skip = true;
1256 }
1257 }
1258 }
1259
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001260 if (skip) {
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001261 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Wale Ogunwale3c36b8e2015-03-09 10:18:51 -07001262 "Skipping delivery of ordered [" + mQueueName + "] "
1263 + r + " for whatever reason");
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001264 r.delivery[recIdx] = BroadcastRecord.DELIVERY_SKIPPED;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001265 r.receiver = null;
1266 r.curFilter = null;
1267 r.state = BroadcastRecord.IDLE;
1268 scheduleBroadcastsLocked();
1269 return;
1270 }
1271
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001272 r.delivery[recIdx] = BroadcastRecord.DELIVERY_DELIVERED;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001273 r.state = BroadcastRecord.APP_RECEIVE;
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001274 r.curComponent = component;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001275 r.curReceiver = info.activityInfo;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001276 if (DEBUG_MU && r.callingUid > UserHandle.PER_USER_RANGE) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001277 Slog.v(TAG_MU, "Updated broadcast record activity info for secondary user, "
1278 + info.activityInfo + ", callingUid = " + r.callingUid + ", uid = "
1279 + info.activityInfo.applicationInfo.uid);
1280 }
1281
Dianne Hackborna750a632015-06-16 17:18:23 -07001282 if (brOptions != null && brOptions.getTemporaryAppWhitelistDuration() > 0) {
1283 scheduleTempWhitelistLocked(receiverUid,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001284 brOptions.getTemporaryAppWhitelistDuration(), r);
Dianne Hackborna750a632015-06-16 17:18:23 -07001285 }
1286
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001287 // Broadcast is being executed, its package can't be stopped.
1288 try {
1289 AppGlobals.getPackageManager().setPackageStoppedState(
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001290 r.curComponent.getPackageName(), false, UserHandle.getUserId(r.callingUid));
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001291 } catch (RemoteException e) {
1292 } catch (IllegalArgumentException e) {
1293 Slog.w(TAG, "Failed trying to unstop package "
1294 + r.curComponent.getPackageName() + ": " + e);
1295 }
1296
1297 // Is this receiver's application already running?
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001298 if (app != null && app.thread != null) {
1299 try {
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001300 app.addPackage(info.activityInfo.packageName,
1301 info.activityInfo.applicationInfo.versionCode, mService.mProcessStats);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001302 processCurBroadcastLocked(r, app);
1303 return;
1304 } catch (RemoteException e) {
1305 Slog.w(TAG, "Exception when sending broadcast to "
1306 + r.curComponent, e);
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001307 } catch (RuntimeException e) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07001308 Slog.wtf(TAG, "Failed sending broadcast to "
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001309 + r.curComponent + " with " + r.intent, e);
1310 // If some unexpected exception happened, just skip
1311 // this broadcast. At this point we are not in the call
1312 // from a client, so throwing an exception out from here
1313 // will crash the entire system instead of just whoever
1314 // sent the broadcast.
1315 logBroadcastReceiverDiscardLocked(r);
1316 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -07001317 r.resultExtras, r.resultAbort, false);
Dianne Hackborn6c5406a2012-11-29 16:18:01 -08001318 scheduleBroadcastsLocked();
1319 // We need to reset the state if we failed to start the receiver.
1320 r.state = BroadcastRecord.IDLE;
1321 return;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001322 }
1323
1324 // If a dead object exception was thrown -- fall through to
1325 // restart the application.
1326 }
1327
1328 // Not running -- get it started, to be executed when the app comes up.
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001329 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001330 "Need to start app ["
1331 + mQueueName + "] " + targetProcess + " for broadcast " + r);
1332 if ((r.curApp=mService.startProcessLocked(targetProcess,
1333 info.activityInfo.applicationInfo, true,
1334 r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND,
1335 "broadcast", r.curComponent,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001336 (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0, false, false))
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001337 == null) {
1338 // Ah, this recipient is unavailable. Finish it if necessary,
1339 // and mark the broadcast record as ready for the next.
1340 Slog.w(TAG, "Unable to launch app "
1341 + info.activityInfo.applicationInfo.packageName + "/"
1342 + info.activityInfo.applicationInfo.uid + " for broadcast "
1343 + r.intent + ": process is bad");
1344 logBroadcastReceiverDiscardLocked(r);
1345 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -07001346 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001347 scheduleBroadcastsLocked();
1348 r.state = BroadcastRecord.IDLE;
1349 return;
1350 }
1351
1352 mPendingBroadcast = r;
1353 mPendingBroadcastRecvIndex = recIdx;
1354 }
1355 }
1356
1357 final void setBroadcastTimeoutLocked(long timeoutTime) {
1358 if (! mPendingBroadcastTimeoutMessage) {
1359 Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG, this);
1360 mHandler.sendMessageAtTime(msg, timeoutTime);
1361 mPendingBroadcastTimeoutMessage = true;
1362 }
1363 }
1364
1365 final void cancelBroadcastTimeoutLocked() {
1366 if (mPendingBroadcastTimeoutMessage) {
1367 mHandler.removeMessages(BROADCAST_TIMEOUT_MSG, this);
1368 mPendingBroadcastTimeoutMessage = false;
1369 }
1370 }
1371
1372 final void broadcastTimeoutLocked(boolean fromMsg) {
1373 if (fromMsg) {
1374 mPendingBroadcastTimeoutMessage = false;
1375 }
1376
1377 if (mOrderedBroadcasts.size() == 0) {
1378 return;
1379 }
1380
1381 long now = SystemClock.uptimeMillis();
1382 BroadcastRecord r = mOrderedBroadcasts.get(0);
1383 if (fromMsg) {
1384 if (mService.mDidDexOpt) {
1385 // Delay timeouts until dexopt finishes.
1386 mService.mDidDexOpt = false;
1387 long timeoutTime = SystemClock.uptimeMillis() + mTimeoutPeriod;
1388 setBroadcastTimeoutLocked(timeoutTime);
1389 return;
1390 }
1391 if (!mService.mProcessesReady) {
1392 // Only process broadcast timeouts if the system is ready. That way
1393 // PRE_BOOT_COMPLETED broadcasts can't timeout as they are intended
1394 // to do heavy lifting for system up.
1395 return;
1396 }
1397
1398 long timeoutTime = r.receiverTime + mTimeoutPeriod;
1399 if (timeoutTime > now) {
1400 // We can observe premature timeouts because we do not cancel and reset the
1401 // broadcast timeout message after each receiver finishes. Instead, we set up
1402 // an initial timeout then kick it down the road a little further as needed
1403 // when it expires.
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001404 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001405 "Premature timeout ["
1406 + mQueueName + "] @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for "
1407 + timeoutTime);
1408 setBroadcastTimeoutLocked(timeoutTime);
1409 return;
1410 }
1411 }
1412
Dianne Hackborn6285a322013-09-18 12:09:47 -07001413 BroadcastRecord br = mOrderedBroadcasts.get(0);
1414 if (br.state == BroadcastRecord.WAITING_SERVICES) {
1415 // In this case the broadcast had already finished, but we had decided to wait
1416 // for started services to finish as well before going on. So if we have actually
1417 // waited long enough time timeout the broadcast, let's give up on the whole thing
1418 // and just move on to the next.
Wale Ogunwaled57969f2014-11-15 19:37:29 -08001419 Slog.i(TAG, "Waited long enough for: " + (br.curComponent != null
Dianne Hackborn6285a322013-09-18 12:09:47 -07001420 ? br.curComponent.flattenToShortString() : "(null)"));
1421 br.curComponent = null;
1422 br.state = BroadcastRecord.IDLE;
1423 processNextBroadcast(false);
1424 return;
1425 }
1426
1427 Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r. receiver
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001428 + ", started " + (now - r.receiverTime) + "ms ago");
1429 r.receiverTime = now;
1430 r.anrCount++;
1431
1432 // Current receiver has passed its expiration date.
1433 if (r.nextReceiver <= 0) {
1434 Slog.w(TAG, "Timeout on receiver with nextReceiver <= 0");
1435 return;
1436 }
1437
1438 ProcessRecord app = null;
1439 String anrMessage = null;
1440
1441 Object curReceiver = r.receivers.get(r.nextReceiver-1);
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001442 r.delivery[r.nextReceiver-1] = BroadcastRecord.DELIVERY_TIMEOUT;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001443 Slog.w(TAG, "Receiver during timeout: " + curReceiver);
1444 logBroadcastReceiverDiscardLocked(r);
1445 if (curReceiver instanceof BroadcastFilter) {
1446 BroadcastFilter bf = (BroadcastFilter)curReceiver;
1447 if (bf.receiverList.pid != 0
1448 && bf.receiverList.pid != ActivityManagerService.MY_PID) {
1449 synchronized (mService.mPidsSelfLocked) {
1450 app = mService.mPidsSelfLocked.get(
1451 bf.receiverList.pid);
1452 }
1453 }
1454 } else {
1455 app = r.curApp;
1456 }
1457
1458 if (app != null) {
1459 anrMessage = "Broadcast of " + r.intent.toString();
1460 }
1461
1462 if (mPendingBroadcast == r) {
1463 mPendingBroadcast = null;
1464 }
1465
1466 // Move on to the next receiver.
1467 finishReceiverLocked(r, r.resultCode, r.resultData,
Dianne Hackborn6285a322013-09-18 12:09:47 -07001468 r.resultExtras, r.resultAbort, false);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001469 scheduleBroadcastsLocked();
1470
1471 if (anrMessage != null) {
1472 // Post the ANR to the handler since we do not want to process ANRs while
1473 // potentially holding our lock.
1474 mHandler.post(new AppNotResponding(app, anrMessage));
1475 }
1476 }
1477
Christopher Tatef278f122015-04-22 13:12:01 -07001478 private final int ringAdvance(int x, final int increment, final int ringSize) {
1479 x += increment;
1480 if (x < 0) return (ringSize - 1);
1481 else if (x >= ringSize) return 0;
1482 else return x;
1483 }
1484
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001485 private final void addBroadcastToHistoryLocked(BroadcastRecord r) {
1486 if (r.callingUid < 0) {
1487 // This was from a registerReceiver() call; ignore it.
1488 return;
1489 }
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001490 r.finishTime = SystemClock.uptimeMillis();
Christopher Tatef278f122015-04-22 13:12:01 -07001491
Carmen Jacksona68e3452017-01-17 14:01:33 -08001492 if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
1493 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1494 createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_DELIVERED),
1495 System.identityHashCode(r));
1496 }
1497
Christopher Tatef278f122015-04-22 13:12:01 -07001498 mBroadcastHistory[mHistoryNext] = r;
1499 mHistoryNext = ringAdvance(mHistoryNext, 1, MAX_BROADCAST_HISTORY);
1500
1501 mBroadcastSummaryHistory[mSummaryHistoryNext] = r.intent;
1502 mSummaryHistoryEnqueueTime[mSummaryHistoryNext] = r.enqueueClockTime;
1503 mSummaryHistoryDispatchTime[mSummaryHistoryNext] = r.dispatchClockTime;
1504 mSummaryHistoryFinishTime[mSummaryHistoryNext] = System.currentTimeMillis();
1505 mSummaryHistoryNext = ringAdvance(mSummaryHistoryNext, 1, MAX_BROADCAST_SUMMARY_HISTORY);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001506 }
1507
Wale Ogunwaleca1c1252015-05-15 12:49:13 -07001508 boolean cleanupDisabledPackageReceiversLocked(
1509 String packageName, Set<String> filterByClasses, int userId, boolean doit) {
1510 boolean didSomething = false;
1511 for (int i = mParallelBroadcasts.size() - 1; i >= 0; i--) {
1512 didSomething |= mParallelBroadcasts.get(i).cleanupDisabledPackageReceiversLocked(
1513 packageName, filterByClasses, userId, doit);
1514 if (!doit && didSomething) {
1515 return true;
1516 }
1517 }
1518
1519 for (int i = mOrderedBroadcasts.size() - 1; i >= 0; i--) {
1520 didSomething |= mOrderedBroadcasts.get(i).cleanupDisabledPackageReceiversLocked(
1521 packageName, filterByClasses, userId, doit);
1522 if (!doit && didSomething) {
1523 return true;
1524 }
1525 }
1526
1527 return didSomething;
1528 }
1529
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001530 final void logBroadcastReceiverDiscardLocked(BroadcastRecord r) {
Wale Ogunwalea22c6322015-06-03 09:59:45 -07001531 final int logIndex = r.nextReceiver - 1;
1532 if (logIndex >= 0 && logIndex < r.receivers.size()) {
1533 Object curReceiver = r.receivers.get(logIndex);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001534 if (curReceiver instanceof BroadcastFilter) {
1535 BroadcastFilter bf = (BroadcastFilter) curReceiver;
1536 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001537 bf.owningUserId, System.identityHashCode(r),
Wale Ogunwalea22c6322015-06-03 09:59:45 -07001538 r.intent.getAction(), logIndex, System.identityHashCode(bf));
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001539 } else {
Wale Ogunwalea22c6322015-06-03 09:59:45 -07001540 ResolveInfo ri = (ResolveInfo) curReceiver;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001541 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001542 UserHandle.getUserId(ri.activityInfo.applicationInfo.uid),
Wale Ogunwalea22c6322015-06-03 09:59:45 -07001543 System.identityHashCode(r), r.intent.getAction(), logIndex, ri.toString());
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001544 }
1545 } else {
Wale Ogunwalea22c6322015-06-03 09:59:45 -07001546 if (logIndex < 0) Slog.w(TAG,
1547 "Discarding broadcast before first receiver is invoked: " + r);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001548 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001549 -1, System.identityHashCode(r),
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001550 r.intent.getAction(),
1551 r.nextReceiver,
1552 "NONE");
1553 }
1554 }
1555
Carmen Jacksona68e3452017-01-17 14:01:33 -08001556 private String createBroadcastTraceTitle(BroadcastRecord record, int state) {
1557 return String.format("Broadcast %s from %s (%s) %s",
1558 state == BroadcastRecord.DELIVERY_PENDING ? "in queue" : "dispatched",
1559 record.callerPackage == null ? "" : record.callerPackage,
1560 record.callerApp == null ? "process unknown" : record.callerApp.toShortString(),
1561 record.intent == null ? "" : record.intent.getAction());
1562 }
1563
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001564 final boolean dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args,
1565 int opti, boolean dumpAll, String dumpPackage, boolean needSep) {
Dianne Hackborn865907d2015-10-21 17:12:53 -07001566 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001567 if (mParallelBroadcasts.size() > 0 || mOrderedBroadcasts.size() > 0
1568 || mPendingBroadcast != null) {
1569 boolean printed = false;
Wale Ogunwaleca1c1252015-05-15 12:49:13 -07001570 for (int i = mParallelBroadcasts.size() - 1; i >= 0; i--) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001571 BroadcastRecord br = mParallelBroadcasts.get(i);
1572 if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) {
1573 continue;
1574 }
1575 if (!printed) {
1576 if (needSep) {
1577 pw.println();
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001578 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001579 needSep = true;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001580 printed = true;
1581 pw.println(" Active broadcasts [" + mQueueName + "]:");
1582 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001583 pw.println(" Active Broadcast " + mQueueName + " #" + i + ":");
Dianne Hackborn865907d2015-10-21 17:12:53 -07001584 br.dump(pw, " ", sdf);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001585 }
1586 printed = false;
1587 needSep = true;
Wale Ogunwaleca1c1252015-05-15 12:49:13 -07001588 for (int i = mOrderedBroadcasts.size() - 1; i >= 0; i--) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001589 BroadcastRecord br = mOrderedBroadcasts.get(i);
1590 if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) {
1591 continue;
1592 }
1593 if (!printed) {
1594 if (needSep) {
1595 pw.println();
1596 }
1597 needSep = true;
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001598 printed = true;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001599 pw.println(" Active ordered broadcasts [" + mQueueName + "]:");
1600 }
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001601 pw.println(" Active Ordered Broadcast " + mQueueName + " #" + i + ":");
Dianne Hackborn865907d2015-10-21 17:12:53 -07001602 mOrderedBroadcasts.get(i).dump(pw, " ", sdf);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001603 }
1604 if (dumpPackage == null || (mPendingBroadcast != null
1605 && dumpPackage.equals(mPendingBroadcast.callerPackage))) {
1606 if (needSep) {
1607 pw.println();
1608 }
1609 pw.println(" Pending broadcast [" + mQueueName + "]:");
1610 if (mPendingBroadcast != null) {
Dianne Hackborn865907d2015-10-21 17:12:53 -07001611 mPendingBroadcast.dump(pw, " ", sdf);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001612 } else {
1613 pw.println(" (null)");
1614 }
1615 needSep = true;
1616 }
1617 }
1618
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001619 int i;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001620 boolean printed = false;
Christopher Tatef278f122015-04-22 13:12:01 -07001621
1622 i = -1;
1623 int lastIndex = mHistoryNext;
1624 int ringIndex = lastIndex;
1625 do {
1626 // increasing index = more recent entry, and we want to print the most
1627 // recent first and work backwards, so we roll through the ring backwards.
1628 ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_HISTORY);
1629 BroadcastRecord r = mBroadcastHistory[ringIndex];
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001630 if (r == null) {
Christopher Tatef278f122015-04-22 13:12:01 -07001631 continue;
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001632 }
Christopher Tatef278f122015-04-22 13:12:01 -07001633
1634 i++; // genuine record of some sort even if we're filtering it out
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001635 if (dumpPackage != null && !dumpPackage.equals(r.callerPackage)) {
1636 continue;
1637 }
1638 if (!printed) {
1639 if (needSep) {
1640 pw.println();
1641 }
1642 needSep = true;
1643 pw.println(" Historical broadcasts [" + mQueueName + "]:");
1644 printed = true;
1645 }
1646 if (dumpAll) {
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -07001647 pw.print(" Historical Broadcast " + mQueueName + " #");
1648 pw.print(i); pw.println(":");
Dianne Hackborn865907d2015-10-21 17:12:53 -07001649 r.dump(pw, " ", sdf);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001650 } else {
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001651 pw.print(" #"); pw.print(i); pw.print(": "); pw.println(r);
1652 pw.print(" ");
1653 pw.println(r.intent.toShortString(false, true, true, false));
Dianne Hackborna40cfeb2013-03-25 17:49:36 -07001654 if (r.targetComp != null && r.targetComp != r.intent.getComponent()) {
1655 pw.print(" targetComp: "); pw.println(r.targetComp.toShortString());
1656 }
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001657 Bundle bundle = r.intent.getExtras();
1658 if (bundle != null) {
1659 pw.print(" extras: "); pw.println(bundle.toString());
1660 }
1661 }
Christopher Tatef278f122015-04-22 13:12:01 -07001662 } while (ringIndex != lastIndex);
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001663
1664 if (dumpPackage == null) {
Christopher Tatef278f122015-04-22 13:12:01 -07001665 lastIndex = ringIndex = mSummaryHistoryNext;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001666 if (dumpAll) {
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001667 printed = false;
Christopher Tatef278f122015-04-22 13:12:01 -07001668 i = -1;
1669 } else {
1670 // roll over the 'i' full dumps that have already been issued
1671 for (int j = i;
1672 j > 0 && ringIndex != lastIndex;) {
1673 ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_SUMMARY_HISTORY);
1674 BroadcastRecord r = mBroadcastHistory[ringIndex];
1675 if (r == null) {
1676 continue;
1677 }
1678 j--;
1679 }
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001680 }
Christopher Tatef278f122015-04-22 13:12:01 -07001681 // done skipping; dump the remainder of the ring. 'i' is still the ordinal within
1682 // the overall broadcast history.
Christopher Tatef278f122015-04-22 13:12:01 -07001683 do {
1684 ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_SUMMARY_HISTORY);
1685 Intent intent = mBroadcastSummaryHistory[ringIndex];
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001686 if (intent == null) {
Christopher Tatef278f122015-04-22 13:12:01 -07001687 continue;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001688 }
1689 if (!printed) {
1690 if (needSep) {
1691 pw.println();
1692 }
1693 needSep = true;
1694 pw.println(" Historical broadcasts summary [" + mQueueName + "]:");
1695 printed = true;
1696 }
1697 if (!dumpAll && i >= 50) {
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001698 pw.println(" ...");
1699 break;
1700 }
Christopher Tatef278f122015-04-22 13:12:01 -07001701 i++;
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001702 pw.print(" #"); pw.print(i); pw.print(": ");
1703 pw.println(intent.toShortString(false, true, true, false));
Dianne Hackborn865907d2015-10-21 17:12:53 -07001704 pw.print(" ");
1705 TimeUtils.formatDuration(mSummaryHistoryDispatchTime[ringIndex]
1706 - mSummaryHistoryEnqueueTime[ringIndex], pw);
1707 pw.print(" dispatch ");
1708 TimeUtils.formatDuration(mSummaryHistoryFinishTime[ringIndex]
1709 - mSummaryHistoryDispatchTime[ringIndex], pw);
1710 pw.println(" finish");
1711 pw.print(" enq=");
1712 pw.print(sdf.format(new Date(mSummaryHistoryEnqueueTime[ringIndex])));
1713 pw.print(" disp=");
1714 pw.print(sdf.format(new Date(mSummaryHistoryDispatchTime[ringIndex])));
1715 pw.print(" fin=");
1716 pw.println(sdf.format(new Date(mSummaryHistoryFinishTime[ringIndex])));
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001717 Bundle bundle = intent.getExtras();
1718 if (bundle != null) {
1719 pw.print(" extras: "); pw.println(bundle.toString());
1720 }
Christopher Tatef278f122015-04-22 13:12:01 -07001721 } while (ringIndex != lastIndex);
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001722 }
1723
1724 return needSep;
1725 }
1726}