blob: 705cc5d75b7243762a4264b161b66b40bcde42fb [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
19import android.os.Handler;
20import android.os.Message;
21
22import java.lang.ref.WeakReference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
24/** @hide */
25public class Registrant
26{
27 public
28 Registrant(Handler h, int what, Object obj)
29 {
30 refH = new WeakReference(h);
31 this.what = what;
32 userObj = obj;
33 }
34
35 public void
36 clear()
37 {
38 refH = null;
39 userObj = null;
40 }
41
42 public void
43 notifyRegistrant()
44 {
45 internalNotifyRegistrant (null, null);
46 }
47
48 public void
49 notifyResult(Object result)
50 {
51 internalNotifyRegistrant (result, null);
52 }
53
54 public void
55 notifyException(Throwable exception)
56 {
57 internalNotifyRegistrant (null, exception);
58 }
59
60 /**
61 * This makes a copy of @param ar
62 */
63 public void
64 notifyRegistrant(AsyncResult ar)
65 {
66 internalNotifyRegistrant (ar.result, ar.exception);
67 }
68
69 /*package*/ void
70 internalNotifyRegistrant (Object result, Throwable exception)
71 {
72 Handler h = getHandler();
73
74 if (h == null) {
75 clear();
76 } else {
77 Message msg = Message.obtain();
78
79 msg.what = what;
80
81 msg.obj = new AsyncResult(userObj, result, exception);
82
83 h.sendMessage(msg);
84 }
85 }
86
87 /**
88 * NOTE: May return null if weak reference has been collected
89 */
90
91 public Message
92 messageForRegistrant()
93 {
94 Handler h = getHandler();
95
96 if (h == null) {
97 clear();
98
99 return null;
100 } else {
101 Message msg = h.obtainMessage();
102
103 msg.what = what;
104 msg.obj = userObj;
105
106 return msg;
107 }
108 }
109
110 public Handler
111 getHandler()
112 {
113 if (refH == null)
114 return null;
115
116 return (Handler) refH.get();
117 }
118
119 WeakReference refH;
120 int what;
121 Object userObj;
122}
123