2 * Copyright (C) 2006 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 public class Broadcaster
27 * Sign up for notifications about something.
29 * When this broadcaster pushes a message with senderWhat in the what field,
30 * target will be sent a copy of that message with targetWhat in the what field.
32 public void request(int senderWhat, Handler target, int targetWhat)
35 Registration r = null;
37 r = new Registration();
38 r.senderWhat = senderWhat;
39 r.targets = new Handler[1];
40 r.targetWhats = new int[1];
41 r.targets[0] = target;
42 r.targetWhats[0] = targetWhat;
47 // find its place in the map
48 Registration start = mReg;
51 if (r.senderWhat >= senderWhat) {
57 if (r.senderWhat != senderWhat) {
58 // we didn't find a senderWhat match, but r is right
59 // after where it goes
60 Registration reg = new Registration();
61 reg.senderWhat = senderWhat;
62 reg.targets = new Handler[1];
63 reg.targetWhats = new int[1];
69 if (r == mReg && r.senderWhat > reg.senderWhat) {
77 Handler[] oldTargets = r.targets;
78 int[] oldWhats = r.targetWhats;
79 // check for duplicates, and don't do it if we are dup.
80 for (int i=0; i<n; i++) {
81 if (oldTargets[i] == target && oldWhats[i] == targetWhat) {
85 r.targets = new Handler[n+1];
86 System.arraycopy(oldTargets, 0, r.targets, 0, n);
87 r.targetWhats = new int[n+1];
88 System.arraycopy(oldWhats, 0, r.targetWhats, 0, n);
90 r.targets[n] = target;
91 r.targetWhats[n] = targetWhat;
97 * Unregister for notifications for this senderWhat/target/targetWhat tuple.
99 public void cancelRequest(int senderWhat, Handler target, int targetWhat)
101 synchronized (this) {
102 Registration start = mReg;
103 Registration r = start;
110 if (r.senderWhat >= senderWhat) {
114 } while (r != start);
116 if (r.senderWhat == senderWhat) {
117 Handler[] targets = r.targets;
118 int[] whats = r.targetWhats;
119 int oldLen = targets.length;
120 for (int i=0; i<oldLen; i++) {
121 if (targets[i] == target && whats[i] == targetWhat) {
122 r.targets = new Handler[oldLen-1];
123 r.targetWhats = new int[oldLen-1];
125 System.arraycopy(targets, 0, r.targets, 0, i);
126 System.arraycopy(whats, 0, r.targetWhats, 0, i);
129 int remainingLen = oldLen-i-1;
130 if (remainingLen != 0) {
131 System.arraycopy(targets, i+1, r.targets, i,
133 System.arraycopy(whats, i+1, r.targetWhats, i,
144 * For debugging purposes, print the registrations to System.out
146 public void dumpRegistrations()
148 synchronized (this) {
149 Registration start = mReg;
150 System.out.println("Broadcaster " + this + " {");
152 Registration r = start;
154 System.out.println(" senderWhat=" + r.senderWhat);
155 int n = r.targets.length;
156 for (int i=0; i<n; i++) {
157 System.out.println(" [" + r.targetWhats[i]
158 + "] " + r.targets[i]);
161 } while (r != start);
163 System.out.println("}");
168 * Send out msg. Anyone who has registered via the request() method will be
171 public void broadcast(Message msg)
173 synchronized (this) {
178 int senderWhat = msg.what;
179 Registration start = mReg;
180 Registration r = start;
182 if (r.senderWhat >= senderWhat) {
186 } while (r != start);
187 if (r.senderWhat == senderWhat) {
188 Handler[] targets = r.targets;
189 int[] whats = r.targetWhats;
190 int n = targets.length;
191 for (int i=0; i<n; i++) {
192 Handler target = targets[i];
193 Message m = Message.obtain();
196 target.sendMessage(m);
202 private class Registration
211 private Registration mReg;