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 
19 import android.compat.annotation.UnsupportedAppUsage;
20 
21 import java.lang.ref.WeakReference;
22 
23 /** @hide */
24 public class Registrant
25 {
26     @UnsupportedAppUsage
27     public
Registrant(Handler h, int what, Object obj)28     Registrant(Handler h, int what, Object obj)
29     {
30         refH = new WeakReference(h);
31         this.what = what;
32         userObj = obj;
33     }
34 
35     @UnsupportedAppUsage
36     public void
clear()37     clear()
38     {
39         refH = null;
40         userObj = null;
41     }
42 
43     @UnsupportedAppUsage
44     public void
notifyRegistrant()45     notifyRegistrant()
46     {
47         internalNotifyRegistrant (null, null);
48     }
49 
50     @UnsupportedAppUsage
51     public void
notifyResult(Object result)52     notifyResult(Object result)
53     {
54         internalNotifyRegistrant (result, null);
55     }
56 
57     public void
notifyException(Throwable exception)58     notifyException(Throwable exception)
59     {
60         internalNotifyRegistrant (null, exception);
61     }
62 
63     /**
64      * This makes a copy of @param ar
65      */
66     @UnsupportedAppUsage
67     public void
notifyRegistrant(AsyncResult ar)68     notifyRegistrant(AsyncResult ar)
69     {
70         internalNotifyRegistrant (ar.result, ar.exception);
71     }
72 
73     /*package*/ void
internalNotifyRegistrant(Object result, Throwable exception)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 
95     @UnsupportedAppUsage
96     public Message
messageForRegistrant()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 
115     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
116     public Handler
getHandler()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 
130