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.os.Binder;
21 import android.os.IBinder;
22 import android.util.PrintWriterPrinter;
23 import android.util.Printer;
24 
25 import java.io.PrintWriter;
26 import java.util.ArrayList;
27 
28 /**
29  * A receiver object that has registered for one or more broadcasts.
30  * The ArrayList holds BroadcastFilter objects.
31  */
32 final class ReceiverList extends ArrayList<BroadcastFilter>
33         implements IBinder.DeathRecipient {
34     final ActivityManagerService owner;
35     public final IIntentReceiver receiver;
36     public final ProcessRecord app;
37     public final int pid;
38     public final int uid;
39     public final int userId;
40     BroadcastRecord curBroadcast = null;
41     boolean linkedToDeath = false;
42 
43     String stringName;
44 
ReceiverList(ActivityManagerService _owner, ProcessRecord _app, int _pid, int _uid, int _userId, IIntentReceiver _receiver)45     ReceiverList(ActivityManagerService _owner, ProcessRecord _app,
46             int _pid, int _uid, int _userId, IIntentReceiver _receiver) {
47         owner = _owner;
48         receiver = _receiver;
49         app = _app;
50         pid = _pid;
51         uid = _uid;
52         userId = _userId;
53     }
54 
55     // Want object identity, not the array identity we are inheriting.
equals(Object o)56     public boolean equals(Object o) {
57         return this == o;
58     }
hashCode()59     public int hashCode() {
60         return System.identityHashCode(this);
61     }
62 
binderDied()63     public void binderDied() {
64         linkedToDeath = false;
65         owner.unregisterReceiver(receiver);
66     }
67 
dumpLocal(PrintWriter pw, String prefix)68     void dumpLocal(PrintWriter pw, String prefix) {
69         pw.print(prefix); pw.print("app="); pw.print(app != null ? app.toShortString() : null);
70             pw.print(" pid="); pw.print(pid); pw.print(" uid="); pw.print(uid);
71             pw.print(" user="); pw.println(userId);
72         if (curBroadcast != null || linkedToDeath) {
73             pw.print(prefix); pw.print("curBroadcast="); pw.print(curBroadcast);
74                 pw.print(" linkedToDeath="); pw.println(linkedToDeath);
75         }
76     }
77 
dump(PrintWriter pw, String prefix)78     void dump(PrintWriter pw, String prefix) {
79         Printer pr = new PrintWriterPrinter(pw);
80         dumpLocal(pw, prefix);
81         String p2 = prefix + "  ";
82         final int N = size();
83         for (int i=0; i<N; i++) {
84             BroadcastFilter bf = get(i);
85             pw.print(prefix); pw.print("Filter #"); pw.print(i);
86                     pw.print(": BroadcastFilter{");
87                     pw.print(Integer.toHexString(System.identityHashCode(bf)));
88                     pw.println('}');
89             bf.dumpInReceiverList(pw, pr, p2);
90         }
91     }
92 
toString()93     public String toString() {
94         if (stringName != null) {
95             return stringName;
96         }
97         StringBuilder sb = new StringBuilder(128);
98         sb.append("ReceiverList{");
99         sb.append(Integer.toHexString(System.identityHashCode(this)));
100         sb.append(' ');
101         sb.append(pid);
102         sb.append(' ');
103         sb.append((app != null ? app.processName : "(unknown name)"));
104         sb.append('/');
105         sb.append(uid);
106         sb.append("/u");
107         sb.append(userId);
108         sb.append((receiver.asBinder() instanceof Binder) ? " local:" : " remote:");
109         sb.append(Integer.toHexString(System.identityHashCode(receiver.asBinder())));
110         sb.append('}');
111         return stringName = sb.toString();
112     }
113 }
114