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