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