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 String requiredPermission; // a permission the caller has required 48 final List receivers; // contains BroadcastFilter and ResolveInfo 49 IIntentReceiver resultTo; // who receives final result if non-null 50 long dispatchTime; // when dispatch started on this set of receivers 51 long dispatchClockTime; // the clock time the dispatch started 52 long receiverTime; // when current receiver started for timeouts. 53 long finishTime; // when we finished the broadcast. 54 int resultCode; // current result code value. 55 String resultData; // current result data value. 56 Bundle resultExtras; // current result extra data values. 57 boolean resultAbort; // current result abortBroadcast value. 58 int nextReceiver; // next receiver to be executed. 59 IBinder receiver; // who is currently running, null if none. 60 int state; 61 int anrCount; // has this broadcast record hit any ANRs? 62 63 static final int IDLE = 0; 64 static final int APP_RECEIVE = 1; 65 static final int CALL_IN_RECEIVE = 2; 66 static final int CALL_DONE_RECEIVE = 3; 67 68 // The following are set when we are calling a receiver (one that 69 // was found in our list of registered receivers). 70 BroadcastFilter curFilter; 71 72 // The following are set only when we are launching a receiver (one 73 // that was found by querying the package manager). 74 ProcessRecord curApp; // hosting application of current receiver. 75 ComponentName curComponent; // the receiver class that is currently running. 76 ActivityInfo curReceiver; // info about the receiver that is currently running. 77 78 void dump(PrintWriter pw, String prefix) { 79 final long now = SystemClock.uptimeMillis(); 80 81 pw.print(prefix); pw.println(this); 82 pw.print(prefix); pw.println(intent); 83 if (sticky) { 84 Bundle bundle = intent.getExtras(); 85 if (bundle != null) { 86 pw.print(prefix); pw.print("extras: "); pw.println(bundle.toString()); 87 } 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 String stateStr = " (?)"; 143 switch (state) { 144 case IDLE: stateStr=" (IDLE)"; break; 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 final int N = receivers != null ? receivers.size() : 0; 151 String p2 = prefix + " "; 152 PrintWriterPrinter printer = new PrintWriterPrinter(pw); 153 for (int i=0; i<N; i++) { 154 Object o = receivers.get(i); 155 pw.print(prefix); pw.print("Receiver #"); pw.print(i); 156 pw.print(": "); pw.println(o); 157 if (o instanceof BroadcastFilter) 158 ((BroadcastFilter)o).dumpBrief(pw, p2); 159 else if (o instanceof ResolveInfo) 160 ((ResolveInfo)o).dump(printer, p2); 161 } 162 } 163 164 BroadcastRecord(Intent _intent, ProcessRecord _callerApp, String _callerPackage, 165 int _callingPid, int _callingUid, String _requiredPermission, 166 List _receivers, IIntentReceiver _resultTo, int _resultCode, 167 String _resultData, Bundle _resultExtras, boolean _serialized, 168 boolean _sticky, boolean _initialSticky) { 169 intent = _intent; 170 callerApp = _callerApp; 171 callerPackage = _callerPackage; 172 callingPid = _callingPid; 173 callingUid = _callingUid; 174 requiredPermission = _requiredPermission; 175 receivers = _receivers; 176 resultTo = _resultTo; 177 resultCode = _resultCode; 178 resultData = _resultData; 179 resultExtras = _resultExtras; 180 ordered = _serialized; 181 sticky = _sticky; 182 initialSticky = _initialSticky; 183 nextReceiver = 0; 184 state = IDLE; 185 } 186 187 public String toString() { 188 return "BroadcastRecord{" 189 + Integer.toHexString(System.identityHashCode(this)) 190 + " " + intent.getAction() + "}"; 191 } 192 } 193