blob: 9c017dfff967cda33c9201880ddc1c95fe906d10 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Andrei Onea24ec3212019-03-15 17:35:05 +000019import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
21import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022
23/** @hide */
24public class RegistrantList
25{
26 ArrayList registrants = new ArrayList(); // of Registrant
27
Andrei Onea24ec3212019-03-15 17:35:05 +000028 @UnsupportedAppUsage
Artur Satayev751e5512019-11-15 19:12:49 +000029 public RegistrantList() {
30 }
31
32 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033 public synchronized void
34 add(Handler h, int what, Object obj)
35 {
36 add(new Registrant(h, what, obj));
37 }
38
Andrei Onea24ec3212019-03-15 17:35:05 +000039 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 public synchronized void
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
Andrei Onea24ec3212019-03-15 17:35:05 +000048 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 public synchronized void
50 add(Registrant r)
51 {
52 removeCleared();
53 registrants.add(r);
54 }
55
Andrei Onea24ec3212019-03-15 17:35:05 +000056 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 public synchronized void
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
Andrei Onea24ec3212019-03-15 17:35:05 +000069 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 public synchronized int
71 size()
72 {
73 return registrants.size();
74 }
75
Artur Satayev70507ed2019-07-29 13:18:27 +010076 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 public synchronized Object
78 get(int index)
79 {
80 return registrants.get(index);
81 }
82
83 private synchronized void
84 internalNotifyRegistrants (Object result, Throwable exception)
85 {
86 for (int i = 0, s = registrants.size(); i < s ; i++) {
87 Registrant r = (Registrant) registrants.get(i);
88 r.internalNotifyRegistrant(result, exception);
89 }
90 }
91
Andrei Onea24ec3212019-03-15 17:35:05 +000092 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 public /*synchronized*/ void
94 notifyRegistrants()
95 {
96 internalNotifyRegistrants(null, null);
97 }
98
99 public /*synchronized*/ void
100 notifyException(Throwable exception)
101 {
102 internalNotifyRegistrants (null, exception);
103 }
104
Andrei Onea24ec3212019-03-15 17:35:05 +0000105 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 public /*synchronized*/ void
107 notifyResult(Object result)
108 {
109 internalNotifyRegistrants (result, null);
110 }
111
112
Andrei Onea24ec3212019-03-15 17:35:05 +0000113 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 public /*synchronized*/ void
115 notifyRegistrants(AsyncResult ar)
116 {
117 internalNotifyRegistrants(ar.result, ar.exception);
118 }
119
Andrei Onea24ec3212019-03-15 17:35:05 +0000120 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 public synchronized void
122 remove(Handler h)
123 {
124 for (int i = 0, s = registrants.size() ; i < s ; i++) {
125 Registrant r = (Registrant) registrants.get(i);
126 Handler rh;
127
128 rh = r.getHandler();
129
130 /* Clean up both the requested registrant and
131 * any now-collected registrants
132 */
133 if (rh == null || rh == h) {
134 r.clear();
135 }
136 }
137
138 removeCleared();
139 }
140}