blob: 13525043412e94a296e0c924209986195192338f [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.ComponentName;
Ng Zhi An1eb7d382018-08-24 13:27:54 -070022import android.content.IIntentReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import 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
Riddle Hsu7fdd90a2018-05-01 00:37:59 +080035import com.android.internal.annotations.VisibleForTesting;
36
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import java.io.PrintWriter;
Dianne Hackborn865907d2015-10-21 17:12:53 -070038import java.text.SimpleDateFormat;
Christopher Tate2f558d22019-01-17 16:58:31 -080039import java.util.ArrayList;
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -070040import java.util.Arrays;
Dianne Hackbornd99b2932011-08-18 14:39:58 -070041import java.util.Date;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import java.util.List;
Wale Ogunwaleca1c1252015-05-15 12:49:13 -070043import java.util.Set;
Christopher Tate2f558d22019-01-17 16:58:31 -080044import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
46/**
47 * An active intent broadcast.
48 */
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070049final class BroadcastRecord extends Binder {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 final Intent intent; // the original intent that generated us
Dianne Hackborna40cfeb2013-03-25 17:49:36 -070051 final ComponentName targetComp; // original component name set on the intent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 final ProcessRecord callerApp; // process that sent this
53 final String callerPackage; // who sent this
54 final int callingPid; // the pid of who sent this
55 final int callingUid; // the uid of who sent this
Chad Brubaker816c83b2017-03-02 10:27:59 -080056 final boolean callerInstantApp; // caller is an Instant App?
Dianne Hackborn68d881c2009-10-05 13:58:17 -070057 final boolean ordered; // serialize the send to receivers?
58 final boolean sticky; // originated from existing sticky data?
Dianne Hackborn12527f92009-11-11 17:39:50 -080059 final boolean initialSticky; // initial broadcast from register to sticky?
Dianne Hackborn786b4402012-08-27 15:14:02 -070060 final int userId; // user id this broadcast was for
Ben Gruver49660c72013-08-06 19:54:08 -070061 final String resolvedType; // the resolved data type
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -070062 final String[] requiredPermissions; // permissions the caller has required
Dianne Hackbornf51f6122013-02-04 18:23:34 -080063 final int appOp; // an app op that is associated with this broadcast
Dianne Hackborna750a632015-06-16 17:18:23 -070064 final BroadcastOptions options; // BroadcastOptions supplied by caller
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 final List receivers; // contains BroadcastFilter and ResolveInfo
Dianne Hackborne0e413e2015-12-09 17:22:26 -080066 final int[] delivery; // delivery state of each receiver
Ng Zhi An1eb7d382018-08-24 13:27:54 -070067 final long[] duration; // duration a receiver took to process broadcast
Johannes Carlssonb5a86542010-10-27 10:08:10 +020068 IIntentReceiver resultTo; // who receives final result if non-null
Christopher Tate2f558d22019-01-17 16:58:31 -080069 boolean deferred;
70 int splitCount; // refcount for result callback, when split
71 int splitToken; // identifier for cross-BroadcastRecord refcount
Jeff Brown9fb3fd12014-09-29 15:32:12 -070072 long enqueueClockTime; // the clock time the broadcast was enqueued
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 long dispatchTime; // when dispatch started on this set of receivers
Dianne Hackbornd99b2932011-08-18 14:39:58 -070074 long dispatchClockTime; // the clock time the dispatch started
Dianne Hackborn12527f92009-11-11 17:39:50 -080075 long receiverTime; // when current receiver started for timeouts.
76 long finishTime; // when we finished the broadcast.
Christopher Tate1b72ca32019-01-30 16:25:53 -080077 boolean timeoutExempt; // true if this broadcast is not subject to receiver timeouts
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 int resultCode; // current result code value.
79 String resultData; // current result data value.
80 Bundle resultExtras; // current result extra data values.
81 boolean resultAbort; // current result abortBroadcast value.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 int nextReceiver; // next receiver to be executed.
83 IBinder receiver; // who is currently running, null if none.
84 int state;
85 int anrCount; // has this broadcast record hit any ANRs?
Dianne Hackbornbc02a392016-06-02 17:15:08 -070086 int manifestCount; // number of manifest receivers dispatched.
87 int manifestSkipCount; // number of manifest receivers skipped.
Dianne Hackborn40c8db52012-02-10 18:59:48 -080088 BroadcastQueue queue; // the outbound queue handling this broadcast
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
Michal Karpinskiac116df2018-12-10 17:51:42 +000090 // if set to true, app's process will be temporarily whitelisted to start activities
91 // from background for the duration of the broadcast dispatch
92 final boolean allowBackgroundActivityStarts;
93
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 static final int IDLE = 0;
95 static final int APP_RECEIVE = 1;
96 static final int CALL_IN_RECEIVE = 2;
97 static final int CALL_DONE_RECEIVE = 3;
Dianne Hackborn6285a322013-09-18 12:09:47 -070098 static final int WAITING_SERVICES = 4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800100 static final int DELIVERY_PENDING = 0;
101 static final int DELIVERY_DELIVERED = 1;
102 static final int DELIVERY_SKIPPED = 2;
103 static final int DELIVERY_TIMEOUT = 3;
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 // The following are set when we are calling a receiver (one that
106 // was found in our list of registered receivers).
107 BroadcastFilter curFilter;
108
109 // The following are set only when we are launching a receiver (one
110 // that was found by querying the package manager).
111 ProcessRecord curApp; // hosting application of current receiver.
112 ComponentName curComponent; // the receiver class that is currently running.
113 ActivityInfo curReceiver; // info about the receiver that is currently running.
114
Christopher Tate2f558d22019-01-17 16:58:31 -0800115 // Private refcount-management bookkeeping; start > 0
116 static AtomicInteger sNextToken = new AtomicInteger(1);
117
Dianne Hackborn865907d2015-10-21 17:12:53 -0700118 void dump(PrintWriter pw, String prefix, SimpleDateFormat sdf) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700119 final long now = SystemClock.uptimeMillis();
120
Dianne Hackborn786b4402012-08-27 15:14:02 -0700121 pw.print(prefix); pw.print(this); pw.print(" to user "); pw.println(userId);
Dianne Hackbornc0bd7472012-10-09 14:00:30 -0700122 pw.print(prefix); pw.println(intent.toInsecureString());
Dianne Hackborna40cfeb2013-03-25 17:49:36 -0700123 if (targetComp != null && targetComp != intent.getComponent()) {
124 pw.print(prefix); pw.print(" targetComp: "); pw.println(targetComp.toShortString());
125 }
Dianne Hackbornc0bd7472012-10-09 14:00:30 -0700126 Bundle bundle = intent.getExtras();
127 if (bundle != null) {
Dianne Hackborna40cfeb2013-03-25 17:49:36 -0700128 pw.print(prefix); pw.print(" extras: "); pw.println(bundle.toString());
Dianne Hackborn12527f92009-11-11 17:39:50 -0800129 }
Dianne Hackborn043fcd92010-10-06 14:27:34 -0700130 pw.print(prefix); pw.print("caller="); pw.print(callerPackage); pw.print(" ");
131 pw.print(callerApp != null ? callerApp.toShortString() : "null");
Dianne Hackborn39792d22010-08-19 18:01:52 -0700132 pw.print(" pid="); pw.print(callingPid);
133 pw.print(" uid="); pw.println(callingUid);
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700134 if ((requiredPermissions != null && requiredPermissions.length > 0)
135 || appOp != AppOpsManager.OP_NONE) {
136 pw.print(prefix); pw.print("requiredPermissions=");
137 pw.print(Arrays.toString(requiredPermissions));
138 pw.print(" appOp="); pw.println(appOp);
Dianne Hackborn12527f92009-11-11 17:39:50 -0800139 }
Dianne Hackborna750a632015-06-16 17:18:23 -0700140 if (options != null) {
141 pw.print(prefix); pw.print("options="); pw.println(options.toBundle());
142 }
Jeff Brown9fb3fd12014-09-29 15:32:12 -0700143 pw.print(prefix); pw.print("enqueueClockTime=");
Dianne Hackborn865907d2015-10-21 17:12:53 -0700144 pw.print(sdf.format(new Date(enqueueClockTime)));
Jeff Brown9fb3fd12014-09-29 15:32:12 -0700145 pw.print(" dispatchClockTime=");
Dianne Hackborn865907d2015-10-21 17:12:53 -0700146 pw.println(sdf.format(new Date(dispatchClockTime)));
Dianne Hackborn39792d22010-08-19 18:01:52 -0700147 pw.print(prefix); pw.print("dispatchTime=");
148 TimeUtils.formatDuration(dispatchTime, now, pw);
Dianne Hackborn865907d2015-10-21 17:12:53 -0700149 pw.print(" (");
150 TimeUtils.formatDuration(dispatchClockTime-enqueueClockTime, pw);
151 pw.print(" since enq)");
Dianne Hackborn12527f92009-11-11 17:39:50 -0800152 if (finishTime != 0) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700153 pw.print(" finishTime="); TimeUtils.formatDuration(finishTime, now, pw);
Dianne Hackborn865907d2015-10-21 17:12:53 -0700154 pw.print(" (");
155 TimeUtils.formatDuration(finishTime-dispatchTime, pw);
156 pw.print(" since disp)");
Dianne Hackborn12527f92009-11-11 17:39:50 -0800157 } else {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700158 pw.print(" receiverTime="); TimeUtils.formatDuration(receiverTime, now, pw);
Dianne Hackborn12527f92009-11-11 17:39:50 -0800159 }
Dianne Hackborn39792d22010-08-19 18:01:52 -0700160 pw.println("");
Dianne Hackborn12527f92009-11-11 17:39:50 -0800161 if (anrCount != 0) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700162 pw.print(prefix); pw.print("anrCount="); pw.println(anrCount);
Dianne Hackborn12527f92009-11-11 17:39:50 -0800163 }
164 if (resultTo != null || resultCode != -1 || resultData != null) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700165 pw.print(prefix); pw.print("resultTo="); pw.print(resultTo);
166 pw.print(" resultCode="); pw.print(resultCode);
167 pw.print(" resultData="); pw.println(resultData);
Dianne Hackborn12527f92009-11-11 17:39:50 -0800168 }
169 if (resultExtras != null) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700170 pw.print(prefix); pw.print("resultExtras="); pw.println(resultExtras);
Dianne Hackborn12527f92009-11-11 17:39:50 -0800171 }
172 if (resultAbort || ordered || sticky || initialSticky) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700173 pw.print(prefix); pw.print("resultAbort="); pw.print(resultAbort);
174 pw.print(" ordered="); pw.print(ordered);
175 pw.print(" sticky="); pw.print(sticky);
176 pw.print(" initialSticky="); pw.println(initialSticky);
Dianne Hackborn12527f92009-11-11 17:39:50 -0800177 }
178 if (nextReceiver != 0 || receiver != null) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700179 pw.print(prefix); pw.print("nextReceiver="); pw.print(nextReceiver);
180 pw.print(" receiver="); pw.println(receiver);
Dianne Hackborn12527f92009-11-11 17:39:50 -0800181 }
182 if (curFilter != null) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700183 pw.print(prefix); pw.print("curFilter="); pw.println(curFilter);
Dianne Hackborn12527f92009-11-11 17:39:50 -0800184 }
185 if (curReceiver != null) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700186 pw.print(prefix); pw.print("curReceiver="); pw.println(curReceiver);
Dianne Hackborn12527f92009-11-11 17:39:50 -0800187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 if (curApp != null) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700189 pw.print(prefix); pw.print("curApp="); pw.println(curApp);
190 pw.print(prefix); pw.print("curComponent=");
191 pw.println((curComponent != null ? curComponent.toShortString() : "--"));
Dianne Hackborn399cccb2010-04-13 22:57:49 -0700192 if (curReceiver != null && curReceiver.applicationInfo != null) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700193 pw.print(prefix); pw.print("curSourceDir=");
194 pw.println(curReceiver.applicationInfo.sourceDir);
Dianne Hackborn399cccb2010-04-13 22:57:49 -0700195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 }
Dianne Hackborn786b4402012-08-27 15:14:02 -0700197 if (state != IDLE) {
198 String stateStr = " (?)";
199 switch (state) {
200 case APP_RECEIVE: stateStr=" (APP_RECEIVE)"; break;
201 case CALL_IN_RECEIVE: stateStr=" (CALL_IN_RECEIVE)"; break;
202 case CALL_DONE_RECEIVE: stateStr=" (CALL_DONE_RECEIVE)"; break;
Dianne Hackborn6285a322013-09-18 12:09:47 -0700203 case WAITING_SERVICES: stateStr=" (WAITING_SERVICES)"; break;
Dianne Hackborn786b4402012-08-27 15:14:02 -0700204 }
205 pw.print(prefix); pw.print("state="); pw.print(state); pw.println(stateStr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 final int N = receivers != null ? receivers.size() : 0;
208 String p2 = prefix + " ";
209 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
Wale Ogunwaleca1c1252015-05-15 12:49:13 -0700210 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 Object o = receivers.get(i);
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800212 pw.print(prefix);
213 switch (delivery[i]) {
214 case DELIVERY_PENDING: pw.print("Pending"); break;
215 case DELIVERY_DELIVERED: pw.print("Deliver"); break;
216 case DELIVERY_SKIPPED: pw.print("Skipped"); break;
217 case DELIVERY_TIMEOUT: pw.print("Timeout"); break;
218 default: pw.print("???????"); break;
219 }
Ng Zhi An1eb7d382018-08-24 13:27:54 -0700220 pw.print(" "); TimeUtils.formatDuration(duration[i], pw);
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800221 pw.print(" #"); pw.print(i); pw.print(": ");
222 if (o instanceof BroadcastFilter) {
223 pw.println(o);
224 ((BroadcastFilter) o).dumpBrief(pw, p2);
225 } else if (o instanceof ResolveInfo) {
226 pw.println("(manifest)");
227 ((ResolveInfo) o).dump(printer, p2, 0);
228 } else {
229 pw.println(o);
230 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 }
232 }
233
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800234 BroadcastRecord(BroadcastQueue _queue,
Christopher Tatef46723b2012-01-26 14:19:24 -0800235 Intent _intent, ProcessRecord _callerApp, String _callerPackage,
Chad Brubaker816c83b2017-03-02 10:27:59 -0800236 int _callingPid, int _callingUid, boolean _callerInstantApp, String _resolvedType,
237 String[] _requiredPermissions, int _appOp, BroadcastOptions _options, List _receivers,
238 IIntentReceiver _resultTo, int _resultCode, String _resultData, Bundle _resultExtras,
Michal Karpinskiac116df2018-12-10 17:51:42 +0000239 boolean _serialized, boolean _sticky, boolean _initialSticky, int _userId,
Christopher Tate1b72ca32019-01-30 16:25:53 -0800240 boolean _allowBackgroundActivityStarts, boolean _timeoutExempt) {
Dianne Hackborn448489a2016-09-29 10:55:10 -0700241 if (_intent == null) {
242 throw new NullPointerException("Can't construct with a null intent");
243 }
Christopher Tatef46723b2012-01-26 14:19:24 -0800244 queue = _queue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 intent = _intent;
Dianne Hackborna40cfeb2013-03-25 17:49:36 -0700246 targetComp = _intent.getComponent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 callerApp = _callerApp;
248 callerPackage = _callerPackage;
249 callingPid = _callingPid;
250 callingUid = _callingUid;
Chad Brubaker816c83b2017-03-02 10:27:59 -0800251 callerInstantApp = _callerInstantApp;
Ben Gruver49660c72013-08-06 19:54:08 -0700252 resolvedType = _resolvedType;
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700253 requiredPermissions = _requiredPermissions;
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800254 appOp = _appOp;
Dianne Hackborna750a632015-06-16 17:18:23 -0700255 options = _options;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 receivers = _receivers;
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800257 delivery = new int[_receivers != null ? _receivers.size() : 0];
Ng Zhi An1eb7d382018-08-24 13:27:54 -0700258 duration = new long[delivery.length];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 resultTo = _resultTo;
260 resultCode = _resultCode;
261 resultData = _resultData;
262 resultExtras = _resultExtras;
263 ordered = _serialized;
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700264 sticky = _sticky;
Dianne Hackborn12527f92009-11-11 17:39:50 -0800265 initialSticky = _initialSticky;
Dianne Hackborn786b4402012-08-27 15:14:02 -0700266 userId = _userId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 nextReceiver = 0;
268 state = IDLE;
Michal Karpinskiac116df2018-12-10 17:51:42 +0000269 allowBackgroundActivityStarts = _allowBackgroundActivityStarts;
Christopher Tate1b72ca32019-01-30 16:25:53 -0800270 timeoutExempt = _timeoutExempt;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 }
272
Makoto Onuki97f82f22017-05-31 16:20:21 -0700273 /**
274 * Copy constructor which takes a different intent.
275 * Only used by {@link #maybeStripForHistory}.
276 */
277 private BroadcastRecord(BroadcastRecord from, Intent newIntent) {
278 intent = newIntent;
279 targetComp = newIntent.getComponent();
280
281 callerApp = from.callerApp;
282 callerPackage = from.callerPackage;
283 callingPid = from.callingPid;
284 callingUid = from.callingUid;
285 callerInstantApp = from.callerInstantApp;
286 ordered = from.ordered;
287 sticky = from.sticky;
288 initialSticky = from.initialSticky;
289 userId = from.userId;
290 resolvedType = from.resolvedType;
291 requiredPermissions = from.requiredPermissions;
292 appOp = from.appOp;
293 options = from.options;
294 receivers = from.receivers;
295 delivery = from.delivery;
Ng Zhi An1eb7d382018-08-24 13:27:54 -0700296 duration = from.duration;
Makoto Onuki97f82f22017-05-31 16:20:21 -0700297 resultTo = from.resultTo;
298 enqueueClockTime = from.enqueueClockTime;
299 dispatchTime = from.dispatchTime;
300 dispatchClockTime = from.dispatchClockTime;
301 receiverTime = from.receiverTime;
302 finishTime = from.finishTime;
303 resultCode = from.resultCode;
304 resultData = from.resultData;
305 resultExtras = from.resultExtras;
306 resultAbort = from.resultAbort;
307 nextReceiver = from.nextReceiver;
308 receiver = from.receiver;
309 state = from.state;
310 anrCount = from.anrCount;
311 manifestCount = from.manifestCount;
312 manifestSkipCount = from.manifestSkipCount;
313 queue = from.queue;
Michal Karpinskiac116df2018-12-10 17:51:42 +0000314 allowBackgroundActivityStarts = from.allowBackgroundActivityStarts;
Christopher Tate1b72ca32019-01-30 16:25:53 -0800315 timeoutExempt = from.timeoutExempt;
Makoto Onuki97f82f22017-05-31 16:20:21 -0700316 }
317
Christopher Tate2f558d22019-01-17 16:58:31 -0800318 /**
319 * Split off a new BroadcastRecord that clones this one, but contains only the
320 * recipient records for the current (just-finished) receiver's app, starting
321 * after the just-finished receiver [i.e. at r.nextReceiver]. Returns null
322 * if there are no matching subsequent receivers in this BroadcastRecord.
323 */
324 BroadcastRecord splitRecipientsLocked(int slowAppUid, int startingAt) {
325 // Do we actually have any matching receivers down the line...?
326 ArrayList splitReceivers = null;
327 for (int i = startingAt; i < receivers.size(); ) {
328 Object o = receivers.get(i);
329 if (getReceiverUid(o) == slowAppUid) {
330 if (splitReceivers == null) {
331 splitReceivers = new ArrayList<>();
332 }
333 splitReceivers.add(o);
334 receivers.remove(i);
Christopher Tate2f558d22019-01-17 16:58:31 -0800335 } else {
336 i++;
337 }
338 }
339
340 // No later receivers in the same app, so we have no more to do
341 if (splitReceivers == null) {
342 return null;
343 }
344
345 // build a new BroadcastRecord around that single-target list
346 BroadcastRecord split = new BroadcastRecord(queue, intent, callerApp,
347 callerPackage, callingPid, callingUid, callerInstantApp, resolvedType,
348 requiredPermissions, appOp, options, splitReceivers, resultTo, resultCode,
349 resultData, resultExtras, ordered, sticky, initialSticky, userId,
Christopher Tate1b72ca32019-01-30 16:25:53 -0800350 allowBackgroundActivityStarts, timeoutExempt);
Christopher Tate2f558d22019-01-17 16:58:31 -0800351
Christopher Tate41c56912019-03-01 18:56:58 -0800352 split.splitToken = this.splitToken;
Christopher Tate2f558d22019-01-17 16:58:31 -0800353 return split;
354 }
355
356 int getReceiverUid(Object receiver) {
357 if (receiver instanceof BroadcastFilter) {
358 return ((BroadcastFilter) receiver).owningUid;
359 } else /* if (receiver instanceof ResolveInfo) */ {
360 return ((ResolveInfo) receiver).activityInfo.applicationInfo.uid;
361 }
362 }
363
Makoto Onuki97f82f22017-05-31 16:20:21 -0700364 public BroadcastRecord maybeStripForHistory() {
365 if (!intent.canStripForHistory()) {
366 return this;
367 }
368 return new BroadcastRecord(this, intent.maybeStripForHistory());
369 }
370
Riddle Hsu7fdd90a2018-05-01 00:37:59 +0800371 @VisibleForTesting
Wale Ogunwaleca1c1252015-05-15 12:49:13 -0700372 boolean cleanupDisabledPackageReceiversLocked(
373 String packageName, Set<String> filterByClasses, int userId, boolean doit) {
Riddle Hsu7fdd90a2018-05-01 00:37:59 +0800374 if (receivers == null) {
375 return false;
376 }
377
378 final boolean cleanupAllUsers = userId == UserHandle.USER_ALL;
379 final boolean sendToAllUsers = this.userId == UserHandle.USER_ALL;
380 if (this.userId != userId && !cleanupAllUsers && !sendToAllUsers) {
Wale Ogunwaleca1c1252015-05-15 12:49:13 -0700381 return false;
382 }
383
384 boolean didSomething = false;
385 Object o;
386 for (int i = receivers.size() - 1; i >= 0; i--) {
387 o = receivers.get(i);
388 if (!(o instanceof ResolveInfo)) {
389 continue;
390 }
391 ActivityInfo info = ((ResolveInfo)o).activityInfo;
392
393 final boolean sameComponent = packageName == null
394 || (info.applicationInfo.packageName.equals(packageName)
395 && (filterByClasses == null || filterByClasses.contains(info.name)));
Riddle Hsu7fdd90a2018-05-01 00:37:59 +0800396 if (sameComponent && (cleanupAllUsers
397 || UserHandle.getUserId(info.applicationInfo.uid) == userId)) {
Wale Ogunwaleca1c1252015-05-15 12:49:13 -0700398 if (!doit) {
399 return true;
400 }
401 didSomething = true;
402 receivers.remove(i);
Wale Ogunwale9a6e13c2015-08-03 15:21:44 -0700403 if (i < nextReceiver) {
404 nextReceiver--;
405 }
Wale Ogunwaleca1c1252015-05-15 12:49:13 -0700406 }
407 }
Wale Ogunwale9a6e13c2015-08-03 15:21:44 -0700408 nextReceiver = Math.min(nextReceiver, receivers.size());
Wale Ogunwaleca1c1252015-05-15 12:49:13 -0700409
410 return didSomething;
411 }
412
Yi Jin129fc6c2017-09-28 15:48:38 -0700413 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 public String toString() {
415 return "BroadcastRecord{"
416 + Integer.toHexString(System.identityHashCode(this))
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700417 + " u" + userId + " " + intent.getAction() + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 }
Yi Jin129fc6c2017-09-28 15:48:38 -0700419
420 public void writeToProto(ProtoOutputStream proto, long fieldId) {
421 long token = proto.start(fieldId);
422 proto.write(BroadcastRecordProto.USER_ID, userId);
423 proto.write(BroadcastRecordProto.INTENT_ACTION, intent.getAction());
424 proto.end(token);
425 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426}