blob: 99286cba3d873286f256fd3c275296b9bb1efa0a [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
2 * Copyright (C) 2008 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package com.android.internal.os;
18
19import android.content.Context;
20import android.os.Handler;
21import android.os.Looper;
22import android.os.Message;
23
24public class HandlerCaller {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025 final Looper mMainLooper;
26 final Handler mH;
27
28 final Callback mCallback;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030 class MyHandler extends Handler {
Mita Yuned218c72012-12-06 17:18:25 -080031 MyHandler(Looper looper, boolean async) {
32 super(looper, null, async);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033 }
Mita Yuned218c72012-12-06 17:18:25 -080034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035 @Override
36 public void handleMessage(Message msg) {
37 mCallback.executeMessage(msg);
38 }
39 }
Mita Yuned218c72012-12-06 17:18:25 -080040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 public interface Callback {
42 public void executeMessage(Message msg);
43 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
Mita Yuned218c72012-12-06 17:18:25 -080045 public HandlerCaller(Context context, Looper looper, Callback callback,
46 boolean asyncHandler) {
Mita Yuned218c72012-12-06 17:18:25 -080047 mMainLooper = looper != null ? looper : context.getMainLooper();
48 mH = new MyHandler(mMainLooper, asyncHandler);
Dianne Hackborn19382ac2009-09-11 21:13:37 -070049 mCallback = callback;
50 }
51
Jeff Brown3d110b22014-11-21 19:01:13 -080052 public Handler getHandler() {
53 return mH;
54 }
55
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 public void executeOrSendMessage(Message msg) {
57 // If we are calling this from the main thread, then we can call
58 // right through. Otherwise, we need to send the message to the
59 // main thread.
60 if (Looper.myLooper() == mMainLooper) {
61 mCallback.executeMessage(msg);
62 msg.recycle();
63 return;
64 }
65
66 mH.sendMessage(msg);
67 }
Svetoslav269403b2013-08-14 17:31:04 -070068
69 public void sendMessageDelayed(Message msg, long delayMillis) {
70 mH.sendMessageDelayed(msg, delayMillis);
71 }
72
Dianne Hackborn72c82ab2009-08-11 21:13:54 -070073 public boolean hasMessages(int what) {
74 return mH.hasMessages(what);
75 }
76
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -070077 public void removeMessages(int what) {
78 mH.removeMessages(what);
79 }
80
81 public void removeMessages(int what, Object obj) {
82 mH.removeMessages(what, obj);
83 }
84
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 public void sendMessage(Message msg) {
86 mH.sendMessage(msg);
87 }
Dianne Hackborn91097de2014-04-04 18:02:06 -070088
89 public SomeArgs sendMessageAndWait(Message msg) {
90 if (Looper.myLooper() == mH.getLooper()) {
91 throw new IllegalStateException("Can't wait on same thread as looper");
92 }
93 SomeArgs args = (SomeArgs)msg.obj;
94 args.mWaitState = SomeArgs.WAIT_WAITING;
95 mH.sendMessage(msg);
96 synchronized (args) {
97 while (args.mWaitState == SomeArgs.WAIT_WAITING) {
98 try {
99 args.wait();
100 } catch (InterruptedException e) {
101 return null;
102 }
103 }
104 }
105 args.mWaitState = SomeArgs.WAIT_NONE;
106 return args;
107 }
108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 public Message obtainMessage(int what) {
110 return mH.obtainMessage(what);
111 }
112
113 public Message obtainMessageBO(int what, boolean arg1, Object arg2) {
114 return mH.obtainMessage(what, arg1 ? 1 : 0, 0, arg2);
115 }
116
117 public Message obtainMessageBOO(int what, boolean arg1, Object arg2, Object arg3) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700118 SomeArgs args = SomeArgs.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 args.arg1 = arg2;
120 args.arg2 = arg3;
121 return mH.obtainMessage(what, arg1 ? 1 : 0, 0, args);
122 }
123
124 public Message obtainMessageO(int what, Object arg1) {
125 return mH.obtainMessage(what, 0, 0, arg1);
126 }
127
128 public Message obtainMessageI(int what, int arg1) {
129 return mH.obtainMessage(what, arg1, 0);
130 }
131
The Android Open Source Project4df24232009-03-05 14:34:35 -0800132 public Message obtainMessageII(int what, int arg1, int arg2) {
133 return mH.obtainMessage(what, arg1, arg2);
134 }
135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 public Message obtainMessageIO(int what, int arg1, Object arg2) {
137 return mH.obtainMessage(what, arg1, 0, arg2);
138 }
139
140 public Message obtainMessageIIO(int what, int arg1, int arg2, Object arg3) {
141 return mH.obtainMessage(what, arg1, arg2, arg3);
142 }
143
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700144 public Message obtainMessageIIOO(int what, int arg1, int arg2,
145 Object arg3, Object arg4) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700146 SomeArgs args = SomeArgs.obtain();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700147 args.arg1 = arg3;
148 args.arg2 = arg4;
149 return mH.obtainMessage(what, arg1, arg2, args);
150 }
151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 public Message obtainMessageIOO(int what, int arg1, Object arg2, Object arg3) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700153 SomeArgs args = SomeArgs.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 args.arg1 = arg2;
155 args.arg2 = arg3;
156 return mH.obtainMessage(what, arg1, 0, args);
157 }
158
Dianne Hackborn91097de2014-04-04 18:02:06 -0700159 public Message obtainMessageIOOO(int what, int arg1, Object arg2, Object arg3, Object arg4) {
160 SomeArgs args = SomeArgs.obtain();
161 args.arg1 = arg2;
162 args.arg2 = arg3;
163 args.arg3 = arg4;
164 return mH.obtainMessage(what, arg1, 0, args);
165 }
166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 public Message obtainMessageOO(int what, Object arg1, Object arg2) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700168 SomeArgs args = SomeArgs.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 args.arg1 = arg1;
170 args.arg2 = arg2;
171 return mH.obtainMessage(what, 0, 0, args);
172 }
173
174 public Message obtainMessageOOO(int what, Object arg1, Object arg2, Object arg3) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700175 SomeArgs args = SomeArgs.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 args.arg1 = arg1;
177 args.arg2 = arg2;
178 args.arg3 = arg3;
179 return mH.obtainMessage(what, 0, 0, args);
180 }
181
182 public Message obtainMessageOOOO(int what, Object arg1, Object arg2,
183 Object arg3, Object arg4) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700184 SomeArgs args = SomeArgs.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 args.arg1 = arg1;
186 args.arg2 = arg2;
187 args.arg3 = arg3;
188 args.arg4 = arg4;
189 return mH.obtainMessage(what, 0, 0, args);
190 }
191
Dianne Hackborn91097de2014-04-04 18:02:06 -0700192 public Message obtainMessageOOOOO(int what, Object arg1, Object arg2,
193 Object arg3, Object arg4, Object arg5) {
194 SomeArgs args = SomeArgs.obtain();
195 args.arg1 = arg1;
196 args.arg2 = arg2;
197 args.arg3 = arg3;
198 args.arg4 = arg4;
199 args.arg5 = arg5;
200 return mH.obtainMessage(what, 0, 0, args);
201 }
202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 public Message obtainMessageIIII(int what, int arg1, int arg2,
204 int arg3, int arg4) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700205 SomeArgs args = SomeArgs.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 args.argi1 = arg1;
207 args.argi2 = arg2;
208 args.argi3 = arg3;
209 args.argi4 = arg4;
210 return mH.obtainMessage(what, 0, 0, args);
211 }
212
213 public Message obtainMessageIIIIII(int what, int arg1, int arg2,
214 int arg3, int arg4, int arg5, int arg6) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700215 SomeArgs args = SomeArgs.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 args.argi1 = arg1;
217 args.argi2 = arg2;
218 args.argi3 = arg3;
219 args.argi4 = arg4;
220 args.argi5 = arg5;
221 args.argi6 = arg6;
222 return mH.obtainMessage(what, 0, 0, args);
223 }
224
225 public Message obtainMessageIIIIO(int what, int arg1, int arg2,
226 int arg3, int arg4, Object arg5) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700227 SomeArgs args = SomeArgs.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 args.arg1 = arg5;
229 args.argi1 = arg1;
230 args.argi2 = arg2;
231 args.argi3 = arg3;
232 args.argi4 = arg4;
233 return mH.obtainMessage(what, 0, 0, args);
234 }
235}