blob: 5e31b2e3bdd1b9c20b4082112ddfbd93cd575567 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package com.android.server.am;
18
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070019import android.content.IIntentReceiver;
Suprabh Shuklaff784952018-02-28 20:55:40 -080020import android.content.IntentFilter;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070021import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.os.IBinder;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070023import android.util.PrintWriterPrinter;
24import android.util.Printer;
Yi Jin129fc6c2017-09-28 15:48:38 -070025import android.util.proto.ProtoOutputStream;
Suprabh Shuklaff784952018-02-28 20:55:40 -080026
27import com.android.server.IntentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
29import java.io.PrintWriter;
30import java.util.ArrayList;
31
32/**
33 * A receiver object that has registered for one or more broadcasts.
34 * The ArrayList holds BroadcastFilter objects.
35 */
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070036final class ReceiverList extends ArrayList<BroadcastFilter>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037 implements IBinder.DeathRecipient {
38 final ActivityManagerService owner;
39 public final IIntentReceiver receiver;
40 public final ProcessRecord app;
41 public final int pid;
42 public final int uid;
Dianne Hackborn20e80982012-08-31 19:00:44 -070043 public final int userId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 BroadcastRecord curBroadcast = null;
45 boolean linkedToDeath = false;
46
Dianne Hackbornf210d6b2009-04-13 18:42:49 -070047 String stringName;
Yi Jin129fc6c2017-09-28 15:48:38 -070048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 ReceiverList(ActivityManagerService _owner, ProcessRecord _app,
Dianne Hackborn20e80982012-08-31 19:00:44 -070050 int _pid, int _uid, int _userId, IIntentReceiver _receiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 owner = _owner;
52 receiver = _receiver;
53 app = _app;
54 pid = _pid;
55 uid = _uid;
Dianne Hackborn20e80982012-08-31 19:00:44 -070056 userId = _userId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 }
58
59 // Want object identity, not the array identity we are inheriting.
60 public boolean equals(Object o) {
61 return this == o;
62 }
63 public int hashCode() {
64 return System.identityHashCode(this);
65 }
Yi Jin129fc6c2017-09-28 15:48:38 -070066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 public void binderDied() {
68 linkedToDeath = false;
69 owner.unregisterReceiver(receiver);
70 }
Yi Jin129fc6c2017-09-28 15:48:38 -070071
Suprabh Shuklaff784952018-02-28 20:55:40 -080072 public boolean containsFilter(IntentFilter filter) {
73 final int N = size();
74 for (int i = 0; i < N; i++) {
75 final BroadcastFilter f = get(i);
76 if (IntentResolver.filterEquals(f, filter)) {
77 return true;
78 }
79 }
80 return false;
81 }
82
Yi Jin129fc6c2017-09-28 15:48:38 -070083 void writeToProto(ProtoOutputStream proto, long fieldId) {
84 long token = proto.start(fieldId);
85 app.writeToProto(proto, ReceiverListProto.APP);
86 proto.write(ReceiverListProto.PID, pid);
87 proto.write(ReceiverListProto.UID, uid);
88 proto.write(ReceiverListProto.USER, userId);
89 if (curBroadcast != null) {
90 curBroadcast.writeToProto(proto, ReceiverListProto.CURRENT);
91 }
92 proto.write(ReceiverListProto.LINKED_TO_DEATH, linkedToDeath);
93 final int N = size();
94 for (int i=0; i<N; i++) {
95 BroadcastFilter bf = get(i);
96 bf.writeToProto(proto, ReceiverListProto.FILTERS);
97 }
98 proto.write(ReceiverListProto.HEX_HASH, Integer.toHexString(System.identityHashCode(this)));
99 proto.end(token);
100 }
101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 void dumpLocal(PrintWriter pw, String prefix) {
Evans Thomas880e5d42013-09-04 11:04:05 +0900103 pw.print(prefix); pw.print("app="); pw.print(app != null ? app.toShortString() : null);
Dianne Hackborn20e80982012-08-31 19:00:44 -0700104 pw.print(" pid="); pw.print(pid); pw.print(" uid="); pw.print(uid);
105 pw.print(" user="); pw.println(userId);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700106 if (curBroadcast != null || linkedToDeath) {
107 pw.print(prefix); pw.print("curBroadcast="); pw.print(curBroadcast);
108 pw.print(" linkedToDeath="); pw.println(linkedToDeath);
109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 }
Yi Jin129fc6c2017-09-28 15:48:38 -0700111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700113 Printer pr = new PrintWriterPrinter(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 dumpLocal(pw, prefix);
115 String p2 = prefix + " ";
116 final int N = size();
117 for (int i=0; i<N; i++) {
118 BroadcastFilter bf = get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700119 pw.print(prefix); pw.print("Filter #"); pw.print(i);
120 pw.print(": BroadcastFilter{");
121 pw.print(Integer.toHexString(System.identityHashCode(bf)));
122 pw.println('}');
123 bf.dumpInReceiverList(pw, pr, p2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 }
125 }
Yi Jin129fc6c2017-09-28 15:48:38 -0700126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 public String toString() {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700128 if (stringName != null) {
129 return stringName;
130 }
131 StringBuilder sb = new StringBuilder(128);
132 sb.append("ReceiverList{");
133 sb.append(Integer.toHexString(System.identityHashCode(this)));
134 sb.append(' ');
135 sb.append(pid);
136 sb.append(' ');
137 sb.append((app != null ? app.processName : "(unknown name)"));
138 sb.append('/');
139 sb.append(uid);
Dianne Hackborn20e80982012-08-31 19:00:44 -0700140 sb.append("/u");
141 sb.append(userId);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700142 sb.append((receiver.asBinder() instanceof Binder) ? " local:" : " remote:");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700143 sb.append(Integer.toHexString(System.identityHashCode(receiver.asBinder())));
144 sb.append('}');
145 return stringName = sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 }
147}