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.lang.ref.WeakReference; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | |
| 23 | /** @hide */ |
| 24 | public class Registrant |
| 25 | { |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 26 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | public |
| 28 | Registrant(Handler h, int what, Object obj) |
| 29 | { |
| 30 | refH = new WeakReference(h); |
| 31 | this.what = what; |
| 32 | userObj = obj; |
| 33 | } |
| 34 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 35 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | public void |
| 37 | clear() |
| 38 | { |
| 39 | refH = null; |
| 40 | userObj = null; |
| 41 | } |
| 42 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 43 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | public void |
| 45 | notifyRegistrant() |
| 46 | { |
| 47 | internalNotifyRegistrant (null, null); |
| 48 | } |
| 49 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 50 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | public void |
| 52 | notifyResult(Object result) |
| 53 | { |
| 54 | internalNotifyRegistrant (result, null); |
| 55 | } |
| 56 | |
| 57 | public void |
| 58 | notifyException(Throwable exception) |
| 59 | { |
| 60 | internalNotifyRegistrant (null, exception); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * This makes a copy of @param ar |
| 65 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 66 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | public void |
| 68 | notifyRegistrant(AsyncResult ar) |
| 69 | { |
| 70 | internalNotifyRegistrant (ar.result, ar.exception); |
| 71 | } |
| 72 | |
| 73 | /*package*/ void |
| 74 | internalNotifyRegistrant (Object result, Throwable exception) |
| 75 | { |
| 76 | Handler h = getHandler(); |
| 77 | |
| 78 | if (h == null) { |
| 79 | clear(); |
| 80 | } else { |
| 81 | Message msg = Message.obtain(); |
| 82 | |
| 83 | msg.what = what; |
| 84 | |
| 85 | msg.obj = new AsyncResult(userObj, result, exception); |
| 86 | |
| 87 | h.sendMessage(msg); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * NOTE: May return null if weak reference has been collected |
| 93 | */ |
| 94 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 95 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | public Message |
| 97 | messageForRegistrant() |
| 98 | { |
| 99 | Handler h = getHandler(); |
| 100 | |
| 101 | if (h == null) { |
| 102 | clear(); |
| 103 | |
| 104 | return null; |
| 105 | } else { |
| 106 | Message msg = h.obtainMessage(); |
| 107 | |
| 108 | msg.what = what; |
| 109 | msg.obj = userObj; |
| 110 | |
| 111 | return msg; |
| 112 | } |
| 113 | } |
| 114 | |
Artur Satayev | 70507ed | 2019-07-29 13:18:27 +0100 | [diff] [blame] | 115 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | public Handler |
| 117 | getHandler() |
| 118 | { |
| 119 | if (refH == null) |
| 120 | return null; |
| 121 | |
| 122 | return (Handler) refH.get(); |
| 123 | } |
| 124 | |
| 125 | WeakReference refH; |
| 126 | int what; |
| 127 | Object userObj; |
| 128 | } |
| 129 | |