blob: 3546e17698b9fa867f20aa13236330c1c6e2f39a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 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 android.os;
18
Dianne Hackborn390517b2013-05-30 15:03:32 -070019import android.util.ArrayMap;
20
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021/**
22 * Takes care of the grunt work of maintaining a list of remote interfaces,
23 * typically for the use of performing callbacks from a
24 * {@link android.app.Service} to its clients. In particular, this:
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -070025 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026 * <ul>
27 * <li> Keeps track of a set of registered {@link IInterface} callbacks,
28 * taking care to identify them through their underlying unique {@link IBinder}
29 * (by calling {@link IInterface#asBinder IInterface.asBinder()}.
30 * <li> Attaches a {@link IBinder.DeathRecipient IBinder.DeathRecipient} to
31 * each registered interface, so that it can be cleaned out of the list if its
32 * process goes away.
33 * <li> Performs locking of the underlying list of interfaces to deal with
34 * multithreaded incoming calls, and a thread-safe way to iterate over a
35 * snapshot of the list without holding its lock.
36 * </ul>
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -070037 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 * <p>To use this class, simply create a single instance along with your
39 * service, and call its {@link #register} and {@link #unregister} methods
40 * as client register and unregister with your service. To call back on to
41 * the registered clients, use {@link #beginBroadcast},
42 * {@link #getBroadcastItem}, and {@link #finishBroadcast}.
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -070043 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 * <p>If a registered callback's process goes away, this class will take
45 * care of automatically removing it from the list. If you want to do
46 * additional work in this situation, you can create a subclass that
47 * implements the {@link #onCallbackDied} method.
48 */
49public class RemoteCallbackList<E extends IInterface> {
Dianne Hackborn390517b2013-05-30 15:03:32 -070050 /*package*/ ArrayMap<IBinder, Callback> mCallbacks
51 = new ArrayMap<IBinder, Callback>();
Dianne Hackborn231cc602009-04-27 17:10:36 -070052 private Object[] mActiveBroadcast;
Dianne Hackbornb06ea702009-07-13 13:07:51 -070053 private int mBroadcastCount = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 private boolean mKilled = false;
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -070055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 private final class Callback implements IBinder.DeathRecipient {
57 final E mCallback;
Dianne Hackborn231cc602009-04-27 17:10:36 -070058 final Object mCookie;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
Dianne Hackborn231cc602009-04-27 17:10:36 -070060 Callback(E callback, Object cookie) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 mCallback = callback;
Dianne Hackborn231cc602009-04-27 17:10:36 -070062 mCookie = cookie;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 }
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -070064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 public void binderDied() {
66 synchronized (mCallbacks) {
67 mCallbacks.remove(mCallback.asBinder());
68 }
Dianne Hackborn231cc602009-04-27 17:10:36 -070069 onCallbackDied(mCallback, mCookie);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 }
71 }
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -070072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 /**
Dianne Hackborn231cc602009-04-27 17:10:36 -070074 * Simple version of {@link RemoteCallbackList#register(E, Object)}
75 * that does not take a cookie object.
76 */
77 public boolean register(E callback) {
78 return register(callback, null);
79 }
Dianne Hackborn231cc602009-04-27 17:10:36 -070080 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 * Add a new callback to the list. This callback will remain in the list
82 * until a corresponding call to {@link #unregister} or its hosting process
83 * goes away. If the callback was already registered (determined by
84 * checking to see if the {@link IInterface#asBinder callback.asBinder()}
85 * object is already in the list), then it will be left as-is.
86 * Registrations are not counted; a single call to {@link #unregister}
87 * will remove a callback after any number calls to register it.
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -070088 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 * @param callback The callback interface to be added to the list. Must
90 * not be null -- passing null here will cause a NullPointerException.
91 * Most services will want to check for null before calling this with
92 * an object given from a client, so that clients can't crash the
93 * service with bad data.
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -070094 *
Dianne Hackborn231cc602009-04-27 17:10:36 -070095 * @param cookie Optional additional data to be associated with this
96 * callback.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 *
98 * @return Returns true if the callback was successfully added to the list.
99 * Returns false if it was not added, either because {@link #kill} had
100 * previously been called or the callback's process has gone away.
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700101 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 * @see #unregister
103 * @see #kill
104 * @see #onCallbackDied
105 */
Dianne Hackborn231cc602009-04-27 17:10:36 -0700106 public boolean register(E callback, Object cookie) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 synchronized (mCallbacks) {
108 if (mKilled) {
109 return false;
110 }
111 IBinder binder = callback.asBinder();
112 try {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700113 Callback cb = new Callback(callback, cookie);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 binder.linkToDeath(cb, 0);
115 mCallbacks.put(binder, cb);
116 return true;
117 } catch (RemoteException e) {
118 return false;
119 }
120 }
121 }
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 /**
124 * Remove from the list a callback that was previously added with
125 * {@link #register}. This uses the
126 * {@link IInterface#asBinder callback.asBinder()} object to correctly
127 * find the previous registration.
128 * Registrations are not counted; a single unregister call will remove
129 * a callback after any number calls to {@link #register} for it.
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700130 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 * @param callback The callback to be removed from the list. Passing
132 * null here will cause a NullPointerException, so you will generally want
133 * to check for null before calling.
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700134 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 * @return Returns true if the callback was found and unregistered. Returns
136 * false if the given callback was not found on the list.
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700137 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * @see #register
139 */
140 public boolean unregister(E callback) {
141 synchronized (mCallbacks) {
142 Callback cb = mCallbacks.remove(callback.asBinder());
143 if (cb != null) {
144 cb.mCallback.asBinder().unlinkToDeath(cb, 0);
145 return true;
146 }
147 return false;
148 }
149 }
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 /**
152 * Disable this callback list. All registered callbacks are unregistered,
153 * and the list is disabled so that future calls to {@link #register} will
154 * fail. This should be used when a Service is stopping, to prevent clients
155 * from registering callbacks after it is stopped.
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700156 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 * @see #register
158 */
159 public void kill() {
160 synchronized (mCallbacks) {
Dianne Hackborn390517b2013-05-30 15:03:32 -0700161 for (int cbi=mCallbacks.size()-1; cbi>=0; cbi--) {
162 Callback cb = mCallbacks.valueAt(cbi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 cb.mCallback.asBinder().unlinkToDeath(cb, 0);
164 }
165 mCallbacks.clear();
166 mKilled = true;
167 }
168 }
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 /**
Dianne Hackborn231cc602009-04-27 17:10:36 -0700171 * Old version of {@link #onCallbackDied(E, Object)} that
172 * does not provide a cookie.
173 */
174 public void onCallbackDied(E callback) {
175 }
176
177 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 * Called when the process hosting a callback in the list has gone away.
Dianne Hackborn231cc602009-04-27 17:10:36 -0700179 * The default implementation calls {@link #onCallbackDied(E)}
180 * for backwards compatibility.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 *
182 * @param callback The callback whose process has died. Note that, since
183 * its process has died, you can not make any calls on to this interface.
184 * You can, however, retrieve its IBinder and compare it with another
185 * IBinder to see if it is the same object.
Dianne Hackborn231cc602009-04-27 17:10:36 -0700186 * @param cookie The cookie object original provided to
187 * {@link #register(E, Object)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 *
189 * @see #register
190 */
Dianne Hackborn231cc602009-04-27 17:10:36 -0700191 public void onCallbackDied(E callback, Object cookie) {
192 onCallbackDied(callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 }
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 /**
196 * Prepare to start making calls to the currently registered callbacks.
197 * This creates a copy of the callback list, which you can retrieve items
198 * from using {@link #getBroadcastItem}. Note that only one broadcast can
199 * be active at a time, so you must be sure to always call this from the
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700200 * same thread (usually by scheduling with {@link Handler}) or
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 * do your own synchronization. You must call {@link #finishBroadcast}
202 * when done.
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700203 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 * <p>A typical loop delivering a broadcast looks like this:
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700205 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 * <pre>
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700207 * int i = callbacks.beginBroadcast();
Dianne Hackborndace2302009-07-14 12:51:00 -0700208 * while (i &gt; 0) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700209 * i--;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 * try {
211 * callbacks.getBroadcastItem(i).somethingHappened();
212 * } catch (RemoteException e) {
213 * // The RemoteCallbackList will take care of removing
214 * // the dead object for us.
215 * }
216 * }
217 * callbacks.finishBroadcast();</pre>
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700218 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 * @return Returns the number of callbacks in the broadcast, to be used
220 * with {@link #getBroadcastItem} to determine the range of indices you
221 * can supply.
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700222 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 * @see #getBroadcastItem
224 * @see #finishBroadcast
225 */
226 public int beginBroadcast() {
227 synchronized (mCallbacks) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700228 if (mBroadcastCount > 0) {
229 throw new IllegalStateException(
230 "beginBroadcast() called while already in a broadcast");
231 }
232
233 final int N = mBroadcastCount = mCallbacks.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 if (N <= 0) {
235 return 0;
236 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700237 Object[] active = mActiveBroadcast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 if (active == null || active.length < N) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700239 mActiveBroadcast = active = new Object[N];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 }
Dianne Hackborn390517b2013-05-30 15:03:32 -0700241 for (int i=0; i<N; i++) {
242 active[i] = mCallbacks.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 }
Dianne Hackborn390517b2013-05-30 15:03:32 -0700244 return N;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 }
246 }
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 /**
249 * Retrieve an item in the active broadcast that was previously started
250 * with {@link #beginBroadcast}. This can <em>only</em> be called after
251 * the broadcast is started, and its data is no longer valid after
252 * calling {@link #finishBroadcast}.
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700253 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * <p>Note that it is possible for the process of one of the returned
255 * callbacks to go away before you call it, so you will need to catch
256 * {@link RemoteException} when calling on to the returned object.
257 * The callback list itself, however, will take care of unregistering
258 * these objects once it detects that it is no longer valid, so you can
259 * handle such an exception by simply ignoring it.
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700260 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 * @param index Which of the registered callbacks you would like to
262 * retrieve. Ranges from 0 to 1-{@link #beginBroadcast}.
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700263 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 * @return Returns the callback interface that you can call. This will
265 * always be non-null.
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700266 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 * @see #beginBroadcast
268 */
269 public E getBroadcastItem(int index) {
Dianne Hackborn231cc602009-04-27 17:10:36 -0700270 return ((Callback)mActiveBroadcast[index]).mCallback;
271 }
272
273 /**
274 * Retrieve the cookie associated with the item
275 * returned by {@link #getBroadcastItem(int)}.
276 *
277 * @see #getBroadcastItem
278 */
279 public Object getBroadcastCookie(int index) {
280 return ((Callback)mActiveBroadcast[index]).mCookie;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 }
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 /**
284 * Clean up the state of a broadcast previously initiated by calling
285 * {@link #beginBroadcast}. This must always be called when you are done
286 * with a broadcast.
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700287 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 * @see #beginBroadcast
289 */
290 public void finishBroadcast() {
Makoto Onuki7bdb9ce2016-09-15 12:52:57 -0700291 synchronized (mCallbacks) {
292 if (mBroadcastCount < 0) {
293 throw new IllegalStateException(
294 "finishBroadcast() called outside of a broadcast");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 }
Makoto Onuki7bdb9ce2016-09-15 12:52:57 -0700296
297 Object[] active = mActiveBroadcast;
298 if (active != null) {
299 final int N = mBroadcastCount;
300 for (int i=0; i<N; i++) {
301 active[i] = null;
302 }
303 }
304
305 mBroadcastCount = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 }
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -0700308
309 /**
310 * Returns the number of registered callbacks. Note that the number of registered
311 * callbacks may differ from the value returned by {@link #beginBroadcast()} since
312 * the former returns the number of callbacks registered at the time of the call
313 * and the second the number of callback to which the broadcast will be delivered.
314 * <p>
315 * This function is useful to decide whether to schedule a broadcast if this
316 * requires doing some work which otherwise would not be performed.
317 * </p>
318 *
319 * @return The size.
320 */
321 public int getRegisteredCallbackCount() {
322 synchronized (mCallbacks) {
323 if (mKilled) {
324 return 0;
325 }
326 return mCallbacks.size();
327 }
328 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329}