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