blob: d6afd049f156393c48a78e0fa028bc931aaa232a [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
Artur Satayevafdb23a2019-12-10 17:47:53 +000019import android.compat.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
21import java.lang.ref.WeakReference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022
23/** @hide */
24public class Registrant
25{
Andrei Onea24ec3212019-03-15 17:35:05 +000026 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027 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 Onea24ec3212019-03-15 17:35:05 +000035 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036 public void
37 clear()
38 {
39 refH = null;
40 userObj = null;
41 }
42
Andrei Onea24ec3212019-03-15 17:35:05 +000043 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 public void
45 notifyRegistrant()
46 {
47 internalNotifyRegistrant (null, null);
48 }
49
Andrei Onea24ec3212019-03-15 17:35:05 +000050 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 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 Onea24ec3212019-03-15 17:35:05 +000066 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 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 Onea24ec3212019-03-15 17:35:05 +000095 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 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 Satayev70507ed2019-07-29 13:18:27 +0100115 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 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