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