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
17package android.os;
18
19import android.os.Handler;
20import android.os.Message;
21
22import java.util.ArrayList;
23import java.util.HashMap;
24
25/** @hide */
26public class RegistrantList
27{
28    ArrayList   registrants = new ArrayList();      // of Registrant
29
30    public synchronized void
31    add(Handler h, int what, Object obj)
32    {
33        add(new Registrant(h, what, obj));
34    }
35
36    public synchronized void
37    addUnique(Handler h, int what, Object obj)
38    {
39        // if the handler is already in the registrant list, remove it
40        remove(h);
41        add(new Registrant(h, what, obj));
42    }
43
44    public synchronized void
45    add(Registrant r)
46    {
47        removeCleared();
48        registrants.add(r);
49    }
50
51    public synchronized void
52    removeCleared()
53    {
54        for (int i = registrants.size() - 1; i >= 0 ; i--) {
55            Registrant  r = (Registrant) registrants.get(i);
56
57            if (r.refH == null) {
58                registrants.remove(i);
59            }
60        }
61    }
62
63    public synchronized int
64    size()
65    {
66        return registrants.size();
67    }
68
69    public synchronized Object
70    get(int index)
71    {
72        return registrants.get(index);
73    }
74
75    private synchronized void
76    internalNotifyRegistrants (Object result, Throwable exception)
77    {
78       for (int i = 0, s = registrants.size(); i < s ; i++) {
79            Registrant  r = (Registrant) registrants.get(i);
80            r.internalNotifyRegistrant(result, exception);
81       }
82    }
83
84    public /*synchronized*/ void
85    notifyRegistrants()
86    {
87        internalNotifyRegistrants(null, null);
88    }
89
90    public /*synchronized*/ void
91    notifyException(Throwable exception)
92    {
93        internalNotifyRegistrants (null, exception);
94    }
95
96    public /*synchronized*/ void
97    notifyResult(Object result)
98    {
99        internalNotifyRegistrants (result, null);
100    }
101
102
103    public /*synchronized*/ void
104    notifyRegistrants(AsyncResult ar)
105    {
106        internalNotifyRegistrants(ar.result, ar.exception);
107    }
108
109    public synchronized void
110    remove(Handler h)
111    {
112        for (int i = 0, s = registrants.size() ; i < s ; i++) {
113            Registrant  r = (Registrant) registrants.get(i);
114            Handler     rh;
115
116            rh = r.getHandler();
117
118            /* Clean up both the requested registrant and
119             * any now-collected registrants
120             */
121            if (rh == null || rh == h) {
122                r.clear();
123            }
124        }
125
126        removeCleared();
127    }
128}
129