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