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