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