• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.server.am;
18 
19 import android.app.AppOpsManager;
20 import android.app.BroadcastOptions;
21 import android.content.IIntentReceiver;
22 import android.content.ComponentName;
23 import android.content.Intent;
24 import android.content.pm.ActivityInfo;
25 import android.content.pm.ResolveInfo;
26 import android.os.Binder;
27 import android.os.Bundle;
28 import android.os.IBinder;
29 import android.os.SystemClock;
30 import android.os.UserHandle;
31 import android.util.PrintWriterPrinter;
32 import android.util.TimeUtils;
33 
34 import java.io.PrintWriter;
35 import java.text.SimpleDateFormat;
36 import java.util.Arrays;
37 import java.util.Date;
38 import java.util.List;
39 import java.util.Set;
40 
41 /**
42  * An active intent broadcast.
43  */
44 final class BroadcastRecord extends Binder {
45     final Intent intent;    // the original intent that generated us
46     final ComponentName targetComp; // original component name set on the intent
47     final ProcessRecord callerApp; // process that sent this
48     final String callerPackage; // who sent this
49     final int callingPid;   // the pid of who sent this
50     final int callingUid;   // the uid of who sent this
51     final boolean ordered;  // serialize the send to receivers?
52     final boolean sticky;   // originated from existing sticky data?
53     final boolean initialSticky; // initial broadcast from register to sticky?
54     final int userId;       // user id this broadcast was for
55     final String resolvedType; // the resolved data type
56     final String[] requiredPermissions; // permissions the caller has required
57     final int appOp;        // an app op that is associated with this broadcast
58     final BroadcastOptions options; // BroadcastOptions supplied by caller
59     final List receivers;   // contains BroadcastFilter and ResolveInfo
60     IIntentReceiver resultTo; // who receives final result if non-null
61     long enqueueClockTime;  // the clock time the broadcast was enqueued
62     long dispatchTime;      // when dispatch started on this set of receivers
63     long dispatchClockTime; // the clock time the dispatch started
64     long receiverTime;      // when current receiver started for timeouts.
65     long finishTime;        // when we finished the broadcast.
66     int resultCode;         // current result code value.
67     String resultData;      // current result data value.
68     Bundle resultExtras;    // current result extra data values.
69     boolean resultAbort;    // current result abortBroadcast value.
70     int nextReceiver;       // next receiver to be executed.
71     IBinder receiver;       // who is currently running, null if none.
72     int state;
73     int anrCount;           // has this broadcast record hit any ANRs?
74     BroadcastQueue queue;   // the outbound queue handling this broadcast
75 
76     static final int IDLE = 0;
77     static final int APP_RECEIVE = 1;
78     static final int CALL_IN_RECEIVE = 2;
79     static final int CALL_DONE_RECEIVE = 3;
80     static final int WAITING_SERVICES = 4;
81 
82     // The following are set when we are calling a receiver (one that
83     // was found in our list of registered receivers).
84     BroadcastFilter curFilter;
85 
86     // The following are set only when we are launching a receiver (one
87     // that was found by querying the package manager).
88     ProcessRecord curApp;       // hosting application of current receiver.
89     ComponentName curComponent; // the receiver class that is currently running.
90     ActivityInfo curReceiver;   // info about the receiver that is currently running.
91 
dump(PrintWriter pw, String prefix, SimpleDateFormat sdf)92     void dump(PrintWriter pw, String prefix, SimpleDateFormat sdf) {
93         final long now = SystemClock.uptimeMillis();
94 
95         pw.print(prefix); pw.print(this); pw.print(" to user "); pw.println(userId);
96         pw.print(prefix); pw.println(intent.toInsecureString());
97         if (targetComp != null && targetComp != intent.getComponent()) {
98             pw.print(prefix); pw.print("  targetComp: "); pw.println(targetComp.toShortString());
99         }
100         Bundle bundle = intent.getExtras();
101         if (bundle != null) {
102             pw.print(prefix); pw.print("  extras: "); pw.println(bundle.toString());
103         }
104         pw.print(prefix); pw.print("caller="); pw.print(callerPackage); pw.print(" ");
105                 pw.print(callerApp != null ? callerApp.toShortString() : "null");
106                 pw.print(" pid="); pw.print(callingPid);
107                 pw.print(" uid="); pw.println(callingUid);
108         if ((requiredPermissions != null && requiredPermissions.length > 0)
109                 || appOp != AppOpsManager.OP_NONE) {
110             pw.print(prefix); pw.print("requiredPermissions=");
111             pw.print(Arrays.toString(requiredPermissions));
112             pw.print("  appOp="); pw.println(appOp);
113         }
114         if (options != null) {
115             pw.print(prefix); pw.print("options="); pw.println(options.toBundle());
116         }
117         pw.print(prefix); pw.print("enqueueClockTime=");
118                 pw.print(sdf.format(new Date(enqueueClockTime)));
119                 pw.print(" dispatchClockTime=");
120                 pw.println(sdf.format(new Date(dispatchClockTime)));
121         pw.print(prefix); pw.print("dispatchTime=");
122                 TimeUtils.formatDuration(dispatchTime, now, pw);
123                 pw.print(" (");
124                 TimeUtils.formatDuration(dispatchClockTime-enqueueClockTime, pw);
125                 pw.print(" since enq)");
126         if (finishTime != 0) {
127             pw.print(" finishTime="); TimeUtils.formatDuration(finishTime, now, pw);
128             pw.print(" (");
129             TimeUtils.formatDuration(finishTime-dispatchTime, pw);
130             pw.print(" since disp)");
131         } else {
132             pw.print(" receiverTime="); TimeUtils.formatDuration(receiverTime, now, pw);
133         }
134         pw.println("");
135         if (anrCount != 0) {
136             pw.print(prefix); pw.print("anrCount="); pw.println(anrCount);
137         }
138         if (resultTo != null || resultCode != -1 || resultData != null) {
139             pw.print(prefix); pw.print("resultTo="); pw.print(resultTo);
140                     pw.print(" resultCode="); pw.print(resultCode);
141                     pw.print(" resultData="); pw.println(resultData);
142         }
143         if (resultExtras != null) {
144             pw.print(prefix); pw.print("resultExtras="); pw.println(resultExtras);
145         }
146         if (resultAbort || ordered || sticky || initialSticky) {
147             pw.print(prefix); pw.print("resultAbort="); pw.print(resultAbort);
148                     pw.print(" ordered="); pw.print(ordered);
149                     pw.print(" sticky="); pw.print(sticky);
150                     pw.print(" initialSticky="); pw.println(initialSticky);
151         }
152         if (nextReceiver != 0 || receiver != null) {
153             pw.print(prefix); pw.print("nextReceiver="); pw.print(nextReceiver);
154                     pw.print(" receiver="); pw.println(receiver);
155         }
156         if (curFilter != null) {
157             pw.print(prefix); pw.print("curFilter="); pw.println(curFilter);
158         }
159         if (curReceiver != null) {
160             pw.print(prefix); pw.print("curReceiver="); pw.println(curReceiver);
161         }
162         if (curApp != null) {
163             pw.print(prefix); pw.print("curApp="); pw.println(curApp);
164             pw.print(prefix); pw.print("curComponent=");
165                     pw.println((curComponent != null ? curComponent.toShortString() : "--"));
166             if (curReceiver != null && curReceiver.applicationInfo != null) {
167                 pw.print(prefix); pw.print("curSourceDir=");
168                         pw.println(curReceiver.applicationInfo.sourceDir);
169             }
170         }
171         if (state != IDLE) {
172             String stateStr = " (?)";
173             switch (state) {
174                 case APP_RECEIVE:       stateStr=" (APP_RECEIVE)"; break;
175                 case CALL_IN_RECEIVE:   stateStr=" (CALL_IN_RECEIVE)"; break;
176                 case CALL_DONE_RECEIVE: stateStr=" (CALL_DONE_RECEIVE)"; break;
177                 case WAITING_SERVICES:  stateStr=" (WAITING_SERVICES)"; break;
178             }
179             pw.print(prefix); pw.print("state="); pw.print(state); pw.println(stateStr);
180         }
181         final int N = receivers != null ? receivers.size() : 0;
182         String p2 = prefix + "  ";
183         PrintWriterPrinter printer = new PrintWriterPrinter(pw);
184         for (int i = 0; i < N; i++) {
185             Object o = receivers.get(i);
186             pw.print(prefix); pw.print("Receiver #"); pw.print(i);
187                     pw.print(": "); pw.println(o);
188             if (o instanceof BroadcastFilter)
189                 ((BroadcastFilter)o).dumpBrief(pw, p2);
190             else if (o instanceof ResolveInfo)
191                 ((ResolveInfo)o).dump(printer, p2);
192         }
193     }
194 
BroadcastRecord(BroadcastQueue _queue, Intent _intent, ProcessRecord _callerApp, String _callerPackage, int _callingPid, int _callingUid, String _resolvedType, String[] _requiredPermissions, int _appOp, BroadcastOptions _options, List _receivers, IIntentReceiver _resultTo, int _resultCode, String _resultData, Bundle _resultExtras, boolean _serialized, boolean _sticky, boolean _initialSticky, int _userId)195     BroadcastRecord(BroadcastQueue _queue,
196             Intent _intent, ProcessRecord _callerApp, String _callerPackage,
197             int _callingPid, int _callingUid, String _resolvedType, String[] _requiredPermissions,
198             int _appOp, BroadcastOptions _options, List _receivers, IIntentReceiver _resultTo,
199             int _resultCode, String _resultData, Bundle _resultExtras, boolean _serialized,
200             boolean _sticky, boolean _initialSticky,
201             int _userId) {
202         queue = _queue;
203         intent = _intent;
204         targetComp = _intent.getComponent();
205         callerApp = _callerApp;
206         callerPackage = _callerPackage;
207         callingPid = _callingPid;
208         callingUid = _callingUid;
209         resolvedType = _resolvedType;
210         requiredPermissions = _requiredPermissions;
211         appOp = _appOp;
212         options = _options;
213         receivers = _receivers;
214         resultTo = _resultTo;
215         resultCode = _resultCode;
216         resultData = _resultData;
217         resultExtras = _resultExtras;
218         ordered = _serialized;
219         sticky = _sticky;
220         initialSticky = _initialSticky;
221         userId = _userId;
222         nextReceiver = 0;
223         state = IDLE;
224     }
225 
cleanupDisabledPackageReceiversLocked( String packageName, Set<String> filterByClasses, int userId, boolean doit)226     boolean cleanupDisabledPackageReceiversLocked(
227             String packageName, Set<String> filterByClasses, int userId, boolean doit) {
228         if ((userId != UserHandle.USER_ALL && this.userId != userId) || receivers == null) {
229             return false;
230         }
231 
232         boolean didSomething = false;
233         Object o;
234         for (int i = receivers.size() - 1; i >= 0; i--) {
235             o = receivers.get(i);
236             if (!(o instanceof ResolveInfo)) {
237                 continue;
238             }
239             ActivityInfo info = ((ResolveInfo)o).activityInfo;
240 
241             final boolean sameComponent = packageName == null
242                     || (info.applicationInfo.packageName.equals(packageName)
243                     && (filterByClasses == null || filterByClasses.contains(info.name)));
244             if (sameComponent) {
245                 if (!doit) {
246                     return true;
247                 }
248                 didSomething = true;
249                 receivers.remove(i);
250                 if (i < nextReceiver) {
251                     nextReceiver--;
252                 }
253             }
254         }
255         nextReceiver = Math.min(nextReceiver, receivers.size());
256 
257         return didSomething;
258     }
259 
toString()260     public String toString() {
261         return "BroadcastRecord{"
262             + Integer.toHexString(System.identityHashCode(this))
263             + " u" + userId + " " + intent.getAction() + "}";
264     }
265 }
266