blob: 572b975fbafdb5b24466f06b665abe84b37be9c3 [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 -080020import android.os.Handler;
21import android.os.Message;
22
23import java.lang.ref.WeakReference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25/** @hide */
26public class Registrant
27{
Andrei Onea24ec3212019-03-15 17:35:05 +000028 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029 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 Onea24ec3212019-03-15 17:35:05 +000037 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 public void
39 clear()
40 {
41 refH = null;
42 userObj = null;
43 }
44
Andrei Onea24ec3212019-03-15 17:35:05 +000045 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 public void
47 notifyRegistrant()
48 {
49 internalNotifyRegistrant (null, null);
50 }
51
Andrei Onea24ec3212019-03-15 17:35:05 +000052 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 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 Onea24ec3212019-03-15 17:35:05 +000068 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 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 Onea24ec3212019-03-15 17:35:05 +000097 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 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 Satayev70507ed2019-07-29 13:18:27 +0100117 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 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