• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.annotation.UnsupportedAppUsage;
20 import android.os.Handler;
21 import android.os.Message;
22 
23 import java.lang.ref.WeakReference;
24 
25 /** @hide */
26 public class Registrant
27 {
28     @UnsupportedAppUsage
29     public
Registrant(Handler h, int what, Object obj)30     Registrant(Handler h, int what, Object obj)
31     {
32         refH = new WeakReference(h);
33         this.what = what;
34         userObj = obj;
35     }
36 
37     @UnsupportedAppUsage
38     public void
clear()39     clear()
40     {
41         refH = null;
42         userObj = null;
43     }
44 
45     @UnsupportedAppUsage
46     public void
notifyRegistrant()47     notifyRegistrant()
48     {
49         internalNotifyRegistrant (null, null);
50     }
51 
52     @UnsupportedAppUsage
53     public void
notifyResult(Object result)54     notifyResult(Object result)
55     {
56         internalNotifyRegistrant (result, null);
57     }
58 
59     public void
notifyException(Throwable exception)60     notifyException(Throwable exception)
61     {
62         internalNotifyRegistrant (null, exception);
63     }
64 
65     /**
66      * This makes a copy of @param ar
67      */
68     @UnsupportedAppUsage
69     public void
notifyRegistrant(AsyncResult ar)70     notifyRegistrant(AsyncResult ar)
71     {
72         internalNotifyRegistrant (ar.result, ar.exception);
73     }
74 
75     /*package*/ void
internalNotifyRegistrant(Object result, Throwable exception)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 
97     @UnsupportedAppUsage
98     public Message
messageForRegistrant()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 
117     public Handler
getHandler()118     getHandler()
119     {
120         if (refH == null)
121             return null;
122 
123         return (Handler) refH.get();
124     }
125 
126     WeakReference   refH;
127     int             what;
128     Object          userObj;
129 }
130 
131