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