The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Artur Satayev | afdb23a | 2019-12-10 17:47:53 +0000 | [diff] [blame] | 19 | import android.compat.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
| 21 | import java.util.ArrayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | |
| 23 | /** @hide */ |
| 24 | public class RegistrantList |
| 25 | { |
| 26 | ArrayList registrants = new ArrayList(); // of Registrant |
| 27 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 28 | @UnsupportedAppUsage |
Artur Satayev | 751e551 | 2019-11-15 19:12:49 +0000 | [diff] [blame] | 29 | public RegistrantList() { |
| 30 | } |
| 31 | |
| 32 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | public synchronized void |
| 34 | add(Handler h, int what, Object obj) |
| 35 | { |
| 36 | add(new Registrant(h, what, obj)); |
| 37 | } |
| 38 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 39 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | 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 Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 48 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | public synchronized void |
| 50 | add(Registrant r) |
| 51 | { |
| 52 | removeCleared(); |
| 53 | registrants.add(r); |
| 54 | } |
| 55 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 56 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | 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 Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 69 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | public synchronized int |
| 71 | size() |
| 72 | { |
| 73 | return registrants.size(); |
| 74 | } |
| 75 | |
Artur Satayev | 70507ed | 2019-07-29 13:18:27 +0100 | [diff] [blame] | 76 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | 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 Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 92 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | 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 Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 105 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | public /*synchronized*/ void |
| 107 | notifyResult(Object result) |
| 108 | { |
| 109 | internalNotifyRegistrants (result, null); |
| 110 | } |
| 111 | |
| 112 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 113 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | public /*synchronized*/ void |
| 115 | notifyRegistrants(AsyncResult ar) |
| 116 | { |
| 117 | internalNotifyRegistrants(ar.result, ar.exception); |
| 118 | } |
| 119 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 120 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | 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 | } |