Home | History | Annotate | Download | only in am
      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.app.BroadcastOptions;
     21 import android.content.IIntentReceiver;
     22 import android.content.ComponentName;
     23 import android.content.Intent;
     24 import android.content.pm.ActivityInfo;
     25 import android.content.pm.ResolveInfo;
     26 import android.os.Binder;
     27 import android.os.Bundle;
     28 import android.os.IBinder;
     29 import android.os.SystemClock;
     30 import android.os.UserHandle;
     31 import android.util.PrintWriterPrinter;
     32 import android.util.TimeUtils;
     33 
     34 import java.io.PrintWriter;
     35 import java.text.SimpleDateFormat;
     36 import java.util.Arrays;
     37 import java.util.Date;
     38 import java.util.List;
     39 import java.util.Set;
     40 
     41 /**
     42  * An active intent broadcast.
     43  */
     44 final class BroadcastRecord extends Binder {
     45     final Intent intent;    // the original intent that generated us
     46     final ComponentName targetComp; // original component name set on the intent
     47     final ProcessRecord callerApp; // process that sent this
     48     final String callerPackage; // who sent this
     49     final int callingPid;   // the pid of who sent this
     50     final int callingUid;   // the uid of who sent this
     51     final boolean ordered;  // serialize the send to receivers?
     52     final boolean sticky;   // originated from existing sticky data?
     53     final boolean initialSticky; // initial broadcast from register to sticky?
     54     final int userId;       // user id this broadcast was for
     55     final String resolvedType; // the resolved data type
     56     final String[] requiredPermissions; // permissions the caller has required
     57     final int appOp;        // an app op that is associated with this broadcast
     58     final BroadcastOptions options; // BroadcastOptions supplied by caller
     59     final List receivers;   // contains BroadcastFilter and ResolveInfo
     60     final int[] delivery;   // delivery state of each receiver
     61     IIntentReceiver resultTo; // who receives final result if non-null
     62     long enqueueClockTime;  // the clock time the broadcast was enqueued
     63     long dispatchTime;      // when dispatch started on this set of receivers
     64     long dispatchClockTime; // the clock time the dispatch started
     65     long receiverTime;      // when current receiver started for timeouts.
     66     long finishTime;        // when we finished the broadcast.
     67     int resultCode;         // current result code value.
     68     String resultData;      // current result data value.
     69     Bundle resultExtras;    // current result extra data values.
     70     boolean resultAbort;    // current result abortBroadcast value.
     71     int nextReceiver;       // next receiver to be executed.
     72     IBinder receiver;       // who is currently running, null if none.
     73     int state;
     74     int anrCount;           // has this broadcast record hit any ANRs?
     75     int manifestCount;      // number of manifest receivers dispatched.
     76     int manifestSkipCount;  // number of manifest receivers skipped.
     77     BroadcastQueue queue;   // the outbound queue handling this broadcast
     78 
     79     static final int IDLE = 0;
     80     static final int APP_RECEIVE = 1;
     81     static final int CALL_IN_RECEIVE = 2;
     82     static final int CALL_DONE_RECEIVE = 3;
     83     static final int WAITING_SERVICES = 4;
     84 
     85     static final int DELIVERY_PENDING = 0;
     86     static final int DELIVERY_DELIVERED = 1;
     87     static final int DELIVERY_SKIPPED = 2;
     88     static final int DELIVERY_TIMEOUT = 3;
     89 
     90     // The following are set when we are calling a receiver (one that
     91     // was found in our list of registered receivers).
     92     BroadcastFilter curFilter;
     93 
     94     // The following are set only when we are launching a receiver (one
     95     // that was found by querying the package manager).
     96     ProcessRecord curApp;       // hosting application of current receiver.
     97     ComponentName curComponent; // the receiver class that is currently running.
     98     ActivityInfo curReceiver;   // info about the receiver that is currently running.
     99 
    100     void dump(PrintWriter pw, String prefix, SimpleDateFormat sdf) {
    101         final long now = SystemClock.uptimeMillis();
    102 
    103         pw.print(prefix); pw.print(this); pw.print(" to user "); pw.println(userId);
    104         pw.print(prefix); pw.println(intent.toInsecureString());
    105         if (targetComp != null && targetComp != intent.getComponent()) {
    106             pw.print(prefix); pw.print("  targetComp: "); pw.println(targetComp.toShortString());
    107         }
    108         Bundle bundle = intent.getExtras();
    109         if (bundle != null) {
    110             pw.print(prefix); pw.print("  extras: "); pw.println(bundle.toString());
    111         }
    112         pw.print(prefix); pw.print("caller="); pw.print(callerPackage); pw.print(" ");
    113                 pw.print(callerApp != null ? callerApp.toShortString() : "null");
    114                 pw.print(" pid="); pw.print(callingPid);
    115                 pw.print(" uid="); pw.println(callingUid);
    116         if ((requiredPermissions != null && requiredPermissions.length > 0)
    117                 || appOp != AppOpsManager.OP_NONE) {
    118             pw.print(prefix); pw.print("requiredPermissions=");
    119             pw.print(Arrays.toString(requiredPermissions));
    120             pw.print("  appOp="); pw.println(appOp);
    121         }
    122         if (options != null) {
    123             pw.print(prefix); pw.print("options="); pw.println(options.toBundle());
    124         }
    125         pw.print(prefix); pw.print("enqueueClockTime=");
    126                 pw.print(sdf.format(new Date(enqueueClockTime)));
    127                 pw.print(" dispatchClockTime=");
    128                 pw.println(sdf.format(new Date(dispatchClockTime)));
    129         pw.print(prefix); pw.print("dispatchTime=");
    130                 TimeUtils.formatDuration(dispatchTime, now, pw);
    131                 pw.print(" (");
    132                 TimeUtils.formatDuration(dispatchClockTime-enqueueClockTime, pw);
    133                 pw.print(" since enq)");
    134         if (finishTime != 0) {
    135             pw.print(" finishTime="); TimeUtils.formatDuration(finishTime, now, pw);
    136             pw.print(" (");
    137             TimeUtils.formatDuration(finishTime-dispatchTime, pw);
    138             pw.print(" since disp)");
    139         } else {
    140             pw.print(" receiverTime="); TimeUtils.formatDuration(receiverTime, now, pw);
    141         }
    142         pw.println("");
    143         if (anrCount != 0) {
    144             pw.print(prefix); pw.print("anrCount="); pw.println(anrCount);
    145         }
    146         if (resultTo != null || resultCode != -1 || resultData != null) {
    147             pw.print(prefix); pw.print("resultTo="); pw.print(resultTo);
    148                     pw.print(" resultCode="); pw.print(resultCode);
    149                     pw.print(" resultData="); pw.println(resultData);
    150         }
    151         if (resultExtras != null) {
    152             pw.print(prefix); pw.print("resultExtras="); pw.println(resultExtras);
    153         }
    154         if (resultAbort || ordered || sticky || initialSticky) {
    155             pw.print(prefix); pw.print("resultAbort="); pw.print(resultAbort);
    156                     pw.print(" ordered="); pw.print(ordered);
    157                     pw.print(" sticky="); pw.print(sticky);
    158                     pw.print(" initialSticky="); pw.println(initialSticky);
    159         }
    160         if (nextReceiver != 0 || receiver != null) {
    161             pw.print(prefix); pw.print("nextReceiver="); pw.print(nextReceiver);
    162                     pw.print(" receiver="); pw.println(receiver);
    163         }
    164         if (curFilter != null) {
    165             pw.print(prefix); pw.print("curFilter="); pw.println(curFilter);
    166         }
    167         if (curReceiver != null) {
    168             pw.print(prefix); pw.print("curReceiver="); pw.println(curReceiver);
    169         }
    170         if (curApp != null) {
    171             pw.print(prefix); pw.print("curApp="); pw.println(curApp);
    172             pw.print(prefix); pw.print("curComponent=");
    173                     pw.println((curComponent != null ? curComponent.toShortString() : "--"));
    174             if (curReceiver != null && curReceiver.applicationInfo != null) {
    175                 pw.print(prefix); pw.print("curSourceDir=");
    176                         pw.println(curReceiver.applicationInfo.sourceDir);
    177             }
    178         }
    179         if (state != IDLE) {
    180             String stateStr = " (?)";
    181             switch (state) {
    182                 case APP_RECEIVE:       stateStr=" (APP_RECEIVE)"; break;
    183                 case CALL_IN_RECEIVE:   stateStr=" (CALL_IN_RECEIVE)"; break;
    184                 case CALL_DONE_RECEIVE: stateStr=" (CALL_DONE_RECEIVE)"; break;
    185                 case WAITING_SERVICES:  stateStr=" (WAITING_SERVICES)"; break;
    186             }
    187             pw.print(prefix); pw.print("state="); pw.print(state); pw.println(stateStr);
    188         }
    189         final int N = receivers != null ? receivers.size() : 0;
    190         String p2 = prefix + "  ";
    191         PrintWriterPrinter printer = new PrintWriterPrinter(pw);
    192         for (int i = 0; i < N; i++) {
    193             Object o = receivers.get(i);
    194             pw.print(prefix);
    195             switch (delivery[i]) {
    196                 case DELIVERY_PENDING:   pw.print("Pending"); break;
    197                 case DELIVERY_DELIVERED: pw.print("Deliver"); break;
    198                 case DELIVERY_SKIPPED:   pw.print("Skipped"); break;
    199                 case DELIVERY_TIMEOUT:   pw.print("Timeout"); break;
    200                 default:                 pw.print("???????"); break;
    201             }
    202             pw.print(" #"); pw.print(i); pw.print(": ");
    203             if (o instanceof BroadcastFilter) {
    204                 pw.println(o);
    205                 ((BroadcastFilter) o).dumpBrief(pw, p2);
    206             } else if (o instanceof ResolveInfo) {
    207                 pw.println("(manifest)");
    208                 ((ResolveInfo) o).dump(printer, p2, 0);
    209             } else {
    210                 pw.println(o);
    211             }
    212         }
    213     }
    214 
    215     BroadcastRecord(BroadcastQueue _queue,
    216             Intent _intent, ProcessRecord _callerApp, String _callerPackage,
    217             int _callingPid, int _callingUid, String _resolvedType, String[] _requiredPermissions,
    218             int _appOp, BroadcastOptions _options, List _receivers, IIntentReceiver _resultTo,
    219             int _resultCode, String _resultData, Bundle _resultExtras, boolean _serialized,
    220             boolean _sticky, boolean _initialSticky,
    221             int _userId) {
    222         if (_intent == null) {
    223             throw new NullPointerException("Can't construct with a null intent");
    224         }
    225         queue = _queue;
    226         intent = _intent;
    227         targetComp = _intent.getComponent();
    228         callerApp = _callerApp;
    229         callerPackage = _callerPackage;
    230         callingPid = _callingPid;
    231         callingUid = _callingUid;
    232         resolvedType = _resolvedType;
    233         requiredPermissions = _requiredPermissions;
    234         appOp = _appOp;
    235         options = _options;
    236         receivers = _receivers;
    237         delivery = new int[_receivers != null ? _receivers.size() : 0];
    238         resultTo = _resultTo;
    239         resultCode = _resultCode;
    240         resultData = _resultData;
    241         resultExtras = _resultExtras;
    242         ordered = _serialized;
    243         sticky = _sticky;
    244         initialSticky = _initialSticky;
    245         userId = _userId;
    246         nextReceiver = 0;
    247         state = IDLE;
    248     }
    249 
    250     boolean cleanupDisabledPackageReceiversLocked(
    251             String packageName, Set<String> filterByClasses, int userId, boolean doit) {
    252         if ((userId != UserHandle.USER_ALL && this.userId != userId) || receivers == null) {
    253             return false;
    254         }
    255 
    256         boolean didSomething = false;
    257         Object o;
    258         for (int i = receivers.size() - 1; i >= 0; i--) {
    259             o = receivers.get(i);
    260             if (!(o instanceof ResolveInfo)) {
    261                 continue;
    262             }
    263             ActivityInfo info = ((ResolveInfo)o).activityInfo;
    264 
    265             final boolean sameComponent = packageName == null
    266                     || (info.applicationInfo.packageName.equals(packageName)
    267                     && (filterByClasses == null || filterByClasses.contains(info.name)));
    268             if (sameComponent) {
    269                 if (!doit) {
    270                     return true;
    271                 }
    272                 didSomething = true;
    273                 receivers.remove(i);
    274                 if (i < nextReceiver) {
    275                     nextReceiver--;
    276                 }
    277             }
    278         }
    279         nextReceiver = Math.min(nextReceiver, receivers.size());
    280 
    281         return didSomething;
    282     }
    283 
    284     public String toString() {
    285         return "BroadcastRecord{"
    286             + Integer.toHexString(System.identityHashCode(this))
    287             + " u" + userId + " " + intent.getAction() + "}";
    288     }
    289 }
    290