blob: 574ca4a340eef32a9213f56c1227e391f529a0e9 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Dianne Hackbornf51f6122013-02-04 18:23:34 -080019import android.app.AppOpsManager;
Dianne Hackborna750a632015-06-16 17:18:23 -070020import android.app.BroadcastOptions;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070021import android.content.IIntentReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.ComponentName;
23import android.content.Intent;
24import android.content.pm.ActivityInfo;
25import android.content.pm.ResolveInfo;
26import android.os.Binder;
27import android.os.Bundle;
28import android.os.IBinder;
29import android.os.SystemClock;
Wale Ogunwaleca1c1252015-05-15 12:49:13 -070030import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.util.PrintWriterPrinter;
Dianne Hackborn39792d22010-08-19 18:01:52 -070032import android.util.TimeUtils;
Yi Jin129fc6c2017-09-28 15:48:38 -070033import android.util.proto.ProtoOutputStream;
34
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import java.io.PrintWriter;
Dianne Hackborn865907d2015-10-21 17:12:53 -070036import java.text.SimpleDateFormat;
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -070037import java.util.Arrays;
Dianne Hackbornd99b2932011-08-18 14:39:58 -070038import java.util.Date;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import java.util.List;
Wale Ogunwaleca1c1252015-05-15 12:49:13 -070040import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
42/**
43 * An active intent broadcast.
44 */
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070045final class BroadcastRecord extends Binder {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 final Intent intent; // the original intent that generated us
Dianne Hackborna40cfeb2013-03-25 17:49:36 -070047 final ComponentName targetComp; // original component name set on the intent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 final ProcessRecord callerApp; // process that sent this
49 final String callerPackage; // who sent this
50 final int callingPid; // the pid of who sent this
51 final int callingUid; // the uid of who sent this
Chad Brubaker816c83b2017-03-02 10:27:59 -080052 final boolean callerInstantApp; // caller is an Instant App?
Dianne Hackborn68d881c2009-10-05 13:58:17 -070053 final boolean ordered; // serialize the send to receivers?
54 final boolean sticky; // originated from existing sticky data?
Dianne Hackborn12527f92009-11-11 17:39:50 -080055 final boolean initialSticky; // initial broadcast from register to sticky?
Dianne Hackborn786b4402012-08-27 15:14:02 -070056 final int userId; // user id this broadcast was for
Ben Gruver49660c72013-08-06 19:54:08 -070057 final String resolvedType; // the resolved data type
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -070058 final String[] requiredPermissions; // permissions the caller has required
Dianne Hackbornf51f6122013-02-04 18:23:34 -080059 final int appOp; // an app op that is associated with this broadcast
Dianne Hackborna750a632015-06-16 17:18:23 -070060 final BroadcastOptions options; // BroadcastOptions supplied by caller
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 final List receivers; // contains BroadcastFilter and ResolveInfo
Dianne Hackborne0e413e2015-12-09 17:22:26 -080062 final int[] delivery; // delivery state of each receiver
Johannes Carlssonb5a86542010-10-27 10:08:10 +020063 IIntentReceiver resultTo; // who receives final result if non-null
Jeff Brown9fb3fd12014-09-29 15:32:12 -070064 long enqueueClockTime; // the clock time the broadcast was enqueued
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 long dispatchTime; // when dispatch started on this set of receivers
Dianne Hackbornd99b2932011-08-18 14:39:58 -070066 long dispatchClockTime; // the clock time the dispatch started
Dianne Hackborn12527f92009-11-11 17:39:50 -080067 long receiverTime; // when current receiver started for timeouts.
68 long finishTime; // when we finished the broadcast.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 int resultCode; // current result code value.
70 String resultData; // current result data value.
71 Bundle resultExtras; // current result extra data values.
72 boolean resultAbort; // current result abortBroadcast value.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 int nextReceiver; // next receiver to be executed.
74 IBinder receiver; // who is currently running, null if none.
75 int state;
76 int anrCount; // has this broadcast record hit any ANRs?
Dianne Hackbornbc02a392016-06-02 17:15:08 -070077 int manifestCount; // number of manifest receivers dispatched.
78 int manifestSkipCount; // number of manifest receivers skipped.
Dianne Hackborn40c8db52012-02-10 18:59:48 -080079 BroadcastQueue queue; // the outbound queue handling this broadcast
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
81 static final int IDLE = 0;
82 static final int APP_RECEIVE = 1;
83 static final int CALL_IN_RECEIVE = 2;
84 static final int CALL_DONE_RECEIVE = 3;
Dianne Hackborn6285a322013-09-18 12:09:47 -070085 static final int WAITING_SERVICES = 4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
Dianne Hackborne0e413e2015-12-09 17:22:26 -080087 static final int DELIVERY_PENDING = 0;
88 static final int DELIVERY_DELIVERED = 1;
89 static final int DELIVERY_SKIPPED = 2;
90 static final int DELIVERY_TIMEOUT = 3;
91
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 // The following are set when we are calling a receiver (one that
93 // was found in our list of registered receivers).
94 BroadcastFilter curFilter;
95
96 // The following are set only when we are launching a receiver (one
97 // that was found by querying the package manager).
98 ProcessRecord curApp; // hosting application of current receiver.
99 ComponentName curComponent; // the receiver class that is currently running.
100 ActivityInfo curReceiver; // info about the receiver that is currently running.
101
Dianne Hackborn865907d2015-10-21 17:12:53 -0700102 void dump(PrintWriter pw, String prefix, SimpleDateFormat sdf) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700103 final long now = SystemClock.uptimeMillis();
104
Dianne Hackborn786b4402012-08-27 15:14:02 -0700105 pw.print(prefix); pw.print(this); pw.print(" to user "); pw.println(userId);
Dianne Hackbornc0bd7472012-10-09 14:00:30 -0700106 pw.print(prefix); pw.println(intent.toInsecureString());
Dianne Hackborna40cfeb2013-03-25 17:49:36 -0700107 if (targetComp != null && targetComp != intent.getComponent()) {
108 pw.print(prefix); pw.print(" targetComp: "); pw.println(targetComp.toShortString());
109 }
Dianne Hackbornc0bd7472012-10-09 14:00:30 -0700110 Bundle bundle = intent.getExtras();
111 if (bundle != null) {
Dianne Hackborna40cfeb2013-03-25 17:49:36 -0700112 pw.print(prefix); pw.print(" extras: "); pw.println(bundle.toString());
Dianne Hackborn12527f92009-11-11 17:39:50 -0800113 }
Dianne Hackborn043fcd92010-10-06 14:27:34 -0700114 pw.print(prefix); pw.print("caller="); pw.print(callerPackage); pw.print(" ");
115 pw.print(callerApp != null ? callerApp.toShortString() : "null");
Dianne Hackborn39792d22010-08-19 18:01:52 -0700116 pw.print(" pid="); pw.print(callingPid);
117 pw.print(" uid="); pw.println(callingUid);
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700118 if ((requiredPermissions != null && requiredPermissions.length > 0)
119 || appOp != AppOpsManager.OP_NONE) {
120 pw.print(prefix); pw.print("requiredPermissions=");
121 pw.print(Arrays.toString(requiredPermissions));
122 pw.print(" appOp="); pw.println(appOp);
Dianne Hackborn12527f92009-11-11 17:39:50 -0800123 }
Dianne Hackborna750a632015-06-16 17:18:23 -0700124 if (options != null) {
125 pw.print(prefix); pw.print("options="); pw.println(options.toBundle());
126 }
Jeff Brown9fb3fd12014-09-29 15:32:12 -0700127 pw.print(prefix); pw.print("enqueueClockTime=");
Dianne Hackborn865907d2015-10-21 17:12:53 -0700128 pw.print(sdf.format(new Date(enqueueClockTime)));
Jeff Brown9fb3fd12014-09-29 15:32:12 -0700129 pw.print(" dispatchClockTime=");
Dianne Hackborn865907d2015-10-21 17:12:53 -0700130 pw.println(sdf.format(new Date(dispatchClockTime)));
Dianne Hackborn39792d22010-08-19 18:01:52 -0700131 pw.print(prefix); pw.print("dispatchTime=");
132 TimeUtils.formatDuration(dispatchTime, now, pw);
Dianne Hackborn865907d2015-10-21 17:12:53 -0700133 pw.print(" (");
134 TimeUtils.formatDuration(dispatchClockTime-enqueueClockTime, pw);
135 pw.print(" since enq)");
Dianne Hackborn12527f92009-11-11 17:39:50 -0800136 if (finishTime != 0) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700137 pw.print(" finishTime="); TimeUtils.formatDuration(finishTime, now, pw);
Dianne Hackborn865907d2015-10-21 17:12:53 -0700138 pw.print(" (");
139 TimeUtils.formatDuration(finishTime-dispatchTime, pw);
140 pw.print(" since disp)");
Dianne Hackborn12527f92009-11-11 17:39:50 -0800141 } else {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700142 pw.print(" receiverTime="); TimeUtils.formatDuration(receiverTime, now, pw);
Dianne Hackborn12527f92009-11-11 17:39:50 -0800143 }
Dianne Hackborn39792d22010-08-19 18:01:52 -0700144 pw.println("");
Dianne Hackborn12527f92009-11-11 17:39:50 -0800145 if (anrCount != 0) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700146 pw.print(prefix); pw.print("anrCount="); pw.println(anrCount);
Dianne Hackborn12527f92009-11-11 17:39:50 -0800147 }
148 if (resultTo != null || resultCode != -1 || resultData != null) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700149 pw.print(prefix); pw.print("resultTo="); pw.print(resultTo);
150 pw.print(" resultCode="); pw.print(resultCode);
151 pw.print(" resultData="); pw.println(resultData);
Dianne Hackborn12527f92009-11-11 17:39:50 -0800152 }
153 if (resultExtras != null) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700154 pw.print(prefix); pw.print("resultExtras="); pw.println(resultExtras);
Dianne Hackborn12527f92009-11-11 17:39:50 -0800155 }
156 if (resultAbort || ordered || sticky || initialSticky) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700157 pw.print(prefix); pw.print("resultAbort="); pw.print(resultAbort);
158 pw.print(" ordered="); pw.print(ordered);
159 pw.print(" sticky="); pw.print(sticky);
160 pw.print(" initialSticky="); pw.println(initialSticky);
Dianne Hackborn12527f92009-11-11 17:39:50 -0800161 }
162 if (nextReceiver != 0 || receiver != null) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700163 pw.print(prefix); pw.print("nextReceiver="); pw.print(nextReceiver);
164 pw.print(" receiver="); pw.println(receiver);
Dianne Hackborn12527f92009-11-11 17:39:50 -0800165 }
166 if (curFilter != null) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700167 pw.print(prefix); pw.print("curFilter="); pw.println(curFilter);
Dianne Hackborn12527f92009-11-11 17:39:50 -0800168 }
169 if (curReceiver != null) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700170 pw.print(prefix); pw.print("curReceiver="); pw.println(curReceiver);
Dianne Hackborn12527f92009-11-11 17:39:50 -0800171 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 if (curApp != null) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700173 pw.print(prefix); pw.print("curApp="); pw.println(curApp);
174 pw.print(prefix); pw.print("curComponent=");
175 pw.println((curComponent != null ? curComponent.toShortString() : "--"));
Dianne Hackborn399cccb2010-04-13 22:57:49 -0700176 if (curReceiver != null && curReceiver.applicationInfo != null) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700177 pw.print(prefix); pw.print("curSourceDir=");
178 pw.println(curReceiver.applicationInfo.sourceDir);
Dianne Hackborn399cccb2010-04-13 22:57:49 -0700179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 }
Dianne Hackborn786b4402012-08-27 15:14:02 -0700181 if (state != IDLE) {
182 String stateStr = " (?)";
183 switch (state) {
184 case APP_RECEIVE: stateStr=" (APP_RECEIVE)"; break;
185 case CALL_IN_RECEIVE: stateStr=" (CALL_IN_RECEIVE)"; break;
186 case CALL_DONE_RECEIVE: stateStr=" (CALL_DONE_RECEIVE)"; break;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700187 case WAITING_SERVICES: stateStr=" (WAITING_SERVICES)"; break;
Dianne Hackborn786b4402012-08-27 15:14:02 -0700188 }
189 pw.print(prefix); pw.print("state="); pw.print(state); pw.println(stateStr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 final int N = receivers != null ? receivers.size() : 0;
192 String p2 = prefix + " ";
193 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
Wale Ogunwaleca1c1252015-05-15 12:49:13 -0700194 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 Object o = receivers.get(i);
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800196 pw.print(prefix);
197 switch (delivery[i]) {
198 case DELIVERY_PENDING: pw.print("Pending"); break;
199 case DELIVERY_DELIVERED: pw.print("Deliver"); break;
200 case DELIVERY_SKIPPED: pw.print("Skipped"); break;
201 case DELIVERY_TIMEOUT: pw.print("Timeout"); break;
202 default: pw.print("???????"); break;
203 }
204 pw.print(" #"); pw.print(i); pw.print(": ");
205 if (o instanceof BroadcastFilter) {
206 pw.println(o);
207 ((BroadcastFilter) o).dumpBrief(pw, p2);
208 } else if (o instanceof ResolveInfo) {
209 pw.println("(manifest)");
210 ((ResolveInfo) o).dump(printer, p2, 0);
211 } else {
212 pw.println(o);
213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 }
215 }
216
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800217 BroadcastRecord(BroadcastQueue _queue,
Christopher Tatef46723b2012-01-26 14:19:24 -0800218 Intent _intent, ProcessRecord _callerApp, String _callerPackage,
Chad Brubaker816c83b2017-03-02 10:27:59 -0800219 int _callingPid, int _callingUid, boolean _callerInstantApp, String _resolvedType,
220 String[] _requiredPermissions, int _appOp, BroadcastOptions _options, List _receivers,
221 IIntentReceiver _resultTo, int _resultCode, String _resultData, Bundle _resultExtras,
222 boolean _serialized, boolean _sticky, boolean _initialSticky, int _userId) {
Dianne Hackborn448489a2016-09-29 10:55:10 -0700223 if (_intent == null) {
224 throw new NullPointerException("Can't construct with a null intent");
225 }
Christopher Tatef46723b2012-01-26 14:19:24 -0800226 queue = _queue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 intent = _intent;
Dianne Hackborna40cfeb2013-03-25 17:49:36 -0700228 targetComp = _intent.getComponent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 callerApp = _callerApp;
230 callerPackage = _callerPackage;
231 callingPid = _callingPid;
232 callingUid = _callingUid;
Chad Brubaker816c83b2017-03-02 10:27:59 -0800233 callerInstantApp = _callerInstantApp;
Ben Gruver49660c72013-08-06 19:54:08 -0700234 resolvedType = _resolvedType;
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700235 requiredPermissions = _requiredPermissions;
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800236 appOp = _appOp;
Dianne Hackborna750a632015-06-16 17:18:23 -0700237 options = _options;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 receivers = _receivers;
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800239 delivery = new int[_receivers != null ? _receivers.size() : 0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 resultTo = _resultTo;
241 resultCode = _resultCode;
242 resultData = _resultData;
243 resultExtras = _resultExtras;
244 ordered = _serialized;
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700245 sticky = _sticky;
Dianne Hackborn12527f92009-11-11 17:39:50 -0800246 initialSticky = _initialSticky;
Dianne Hackborn786b4402012-08-27 15:14:02 -0700247 userId = _userId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 nextReceiver = 0;
249 state = IDLE;
250 }
251
Makoto Onuki97f82f22017-05-31 16:20:21 -0700252 /**
253 * Copy constructor which takes a different intent.
254 * Only used by {@link #maybeStripForHistory}.
255 */
256 private BroadcastRecord(BroadcastRecord from, Intent newIntent) {
257 intent = newIntent;
258 targetComp = newIntent.getComponent();
259
260 callerApp = from.callerApp;
261 callerPackage = from.callerPackage;
262 callingPid = from.callingPid;
263 callingUid = from.callingUid;
264 callerInstantApp = from.callerInstantApp;
265 ordered = from.ordered;
266 sticky = from.sticky;
267 initialSticky = from.initialSticky;
268 userId = from.userId;
269 resolvedType = from.resolvedType;
270 requiredPermissions = from.requiredPermissions;
271 appOp = from.appOp;
272 options = from.options;
273 receivers = from.receivers;
274 delivery = from.delivery;
275 resultTo = from.resultTo;
276 enqueueClockTime = from.enqueueClockTime;
277 dispatchTime = from.dispatchTime;
278 dispatchClockTime = from.dispatchClockTime;
279 receiverTime = from.receiverTime;
280 finishTime = from.finishTime;
281 resultCode = from.resultCode;
282 resultData = from.resultData;
283 resultExtras = from.resultExtras;
284 resultAbort = from.resultAbort;
285 nextReceiver = from.nextReceiver;
286 receiver = from.receiver;
287 state = from.state;
288 anrCount = from.anrCount;
289 manifestCount = from.manifestCount;
290 manifestSkipCount = from.manifestSkipCount;
291 queue = from.queue;
292 }
293
294 public BroadcastRecord maybeStripForHistory() {
295 if (!intent.canStripForHistory()) {
296 return this;
297 }
298 return new BroadcastRecord(this, intent.maybeStripForHistory());
299 }
300
Wale Ogunwaleca1c1252015-05-15 12:49:13 -0700301 boolean cleanupDisabledPackageReceiversLocked(
302 String packageName, Set<String> filterByClasses, int userId, boolean doit) {
303 if ((userId != UserHandle.USER_ALL && this.userId != userId) || receivers == null) {
304 return false;
305 }
306
307 boolean didSomething = false;
308 Object o;
309 for (int i = receivers.size() - 1; i >= 0; i--) {
310 o = receivers.get(i);
311 if (!(o instanceof ResolveInfo)) {
312 continue;
313 }
314 ActivityInfo info = ((ResolveInfo)o).activityInfo;
315
316 final boolean sameComponent = packageName == null
317 || (info.applicationInfo.packageName.equals(packageName)
318 && (filterByClasses == null || filterByClasses.contains(info.name)));
319 if (sameComponent) {
320 if (!doit) {
321 return true;
322 }
323 didSomething = true;
324 receivers.remove(i);
Wale Ogunwale9a6e13c2015-08-03 15:21:44 -0700325 if (i < nextReceiver) {
326 nextReceiver--;
327 }
Wale Ogunwaleca1c1252015-05-15 12:49:13 -0700328 }
329 }
Wale Ogunwale9a6e13c2015-08-03 15:21:44 -0700330 nextReceiver = Math.min(nextReceiver, receivers.size());
Wale Ogunwaleca1c1252015-05-15 12:49:13 -0700331
332 return didSomething;
333 }
334
Yi Jin129fc6c2017-09-28 15:48:38 -0700335 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 public String toString() {
337 return "BroadcastRecord{"
338 + Integer.toHexString(System.identityHashCode(this))
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700339 + " u" + userId + " " + intent.getAction() + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 }
Yi Jin129fc6c2017-09-28 15:48:38 -0700341
342 public void writeToProto(ProtoOutputStream proto, long fieldId) {
343 long token = proto.start(fieldId);
344 proto.write(BroadcastRecordProto.USER_ID, userId);
345 proto.write(BroadcastRecordProto.INTENT_ACTION, intent.getAction());
346 proto.end(token);
347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348}