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             msg.obj = new AsyncResult(userObj, result, exception);
85             h.sendMessage(msg);
86         }
87     }
88 
89     /**
90      * NOTE: May return null if weak reference has been collected
91      */
92 
93     @UnsupportedAppUsage
94     public Message
messageForRegistrant()95     messageForRegistrant()
96     {
97         Handler h = getHandler();
98 
99         if (h == null) {
100             clear();
101 
102             return null;
103         } else {
104             Message msg = h.obtainMessage();
105 
106             msg.what = what;
107             msg.obj = userObj;
108 
109             return msg;
110         }
111     }
112 
113     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
114     public Handler
getHandler()115     getHandler()
116     {
117         if (refH == null)
118             return null;
119 
120         return (Handler) refH.get();
121     }
122 
123     WeakReference   refH;
124     int             what;
125     Object          userObj;
126 }
127