blob: a11c815a94bcd5cca054d29fd56f1f20a01226f5 [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
Artur Satayeved5a6ae2019-12-10 17:47:54 +000019import android.compat.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
21import android.os.Handler;
22import android.os.Looper;
23import android.os.Message;
24
Eugene Susla07d718b2018-08-20 15:18:47 -070025/**
26 * @deprecated Use {@link com.android.internal.util.function.pooled.PooledLambda#obtainMessage}
27 * to achieve the same effect of storing multiple values in a message with the added typesafety
28 * and code continuity benefits.
29 */
30@Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031public class HandlerCaller {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032 final Looper mMainLooper;
33 final Handler mH;
34
35 final Callback mCallback;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037 class MyHandler extends Handler {
Mita Yuned218c72012-12-06 17:18:25 -080038 MyHandler(Looper looper, boolean async) {
39 super(looper, null, async);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 }
Mita Yuned218c72012-12-06 17:18:25 -080041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 @Override
43 public void handleMessage(Message msg) {
44 mCallback.executeMessage(msg);
45 }
46 }
Mita Yuned218c72012-12-06 17:18:25 -080047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 public interface Callback {
49 public void executeMessage(Message msg);
50 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
Mita Yuned218c72012-12-06 17:18:25 -080052 public HandlerCaller(Context context, Looper looper, Callback callback,
53 boolean asyncHandler) {
Mita Yuned218c72012-12-06 17:18:25 -080054 mMainLooper = looper != null ? looper : context.getMainLooper();
55 mH = new MyHandler(mMainLooper, asyncHandler);
Dianne Hackborn19382ac2009-09-11 21:13:37 -070056 mCallback = callback;
57 }
58
Jeff Brown3d110b22014-11-21 19:01:13 -080059 public Handler getHandler() {
60 return mH;
61 }
62
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 public void executeOrSendMessage(Message msg) {
64 // If we are calling this from the main thread, then we can call
65 // right through. Otherwise, we need to send the message to the
66 // main thread.
67 if (Looper.myLooper() == mMainLooper) {
68 mCallback.executeMessage(msg);
69 msg.recycle();
70 return;
71 }
72
73 mH.sendMessage(msg);
74 }
Svetoslav269403b2013-08-14 17:31:04 -070075
76 public void sendMessageDelayed(Message msg, long delayMillis) {
77 mH.sendMessageDelayed(msg, delayMillis);
78 }
79
Dianne Hackborn72c82ab2009-08-11 21:13:54 -070080 public boolean hasMessages(int what) {
81 return mH.hasMessages(what);
82 }
83
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -070084 public void removeMessages(int what) {
85 mH.removeMessages(what);
86 }
87
88 public void removeMessages(int what, Object obj) {
89 mH.removeMessages(what, obj);
90 }
91
Mathew Inwoodaf972c82018-08-20 14:13:20 +010092 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 public void sendMessage(Message msg) {
94 mH.sendMessage(msg);
95 }
Dianne Hackborn91097de2014-04-04 18:02:06 -070096
97 public SomeArgs sendMessageAndWait(Message msg) {
98 if (Looper.myLooper() == mH.getLooper()) {
99 throw new IllegalStateException("Can't wait on same thread as looper");
100 }
101 SomeArgs args = (SomeArgs)msg.obj;
102 args.mWaitState = SomeArgs.WAIT_WAITING;
103 mH.sendMessage(msg);
104 synchronized (args) {
105 while (args.mWaitState == SomeArgs.WAIT_WAITING) {
106 try {
107 args.wait();
108 } catch (InterruptedException e) {
109 return null;
110 }
111 }
112 }
113 args.mWaitState = SomeArgs.WAIT_NONE;
114 return args;
115 }
116
Mathew Inwoodaf972c82018-08-20 14:13:20 +0100117 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 public Message obtainMessage(int what) {
119 return mH.obtainMessage(what);
120 }
121
122 public Message obtainMessageBO(int what, boolean arg1, Object arg2) {
123 return mH.obtainMessage(what, arg1 ? 1 : 0, 0, arg2);
124 }
125
126 public Message obtainMessageBOO(int what, boolean arg1, Object arg2, Object arg3) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700127 SomeArgs args = SomeArgs.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 args.arg1 = arg2;
129 args.arg2 = arg3;
130 return mH.obtainMessage(what, arg1 ? 1 : 0, 0, args);
131 }
132
Mathew Inwoodaf972c82018-08-20 14:13:20 +0100133 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 public Message obtainMessageO(int what, Object arg1) {
135 return mH.obtainMessage(what, 0, 0, arg1);
136 }
137
138 public Message obtainMessageI(int what, int arg1) {
139 return mH.obtainMessage(what, arg1, 0);
140 }
141
The Android Open Source Project4df24232009-03-05 14:34:35 -0800142 public Message obtainMessageII(int what, int arg1, int arg2) {
143 return mH.obtainMessage(what, arg1, arg2);
144 }
145
Mathew Inwoodaf972c82018-08-20 14:13:20 +0100146 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 public Message obtainMessageIO(int what, int arg1, Object arg2) {
148 return mH.obtainMessage(what, arg1, 0, arg2);
149 }
150
151 public Message obtainMessageIIO(int what, int arg1, int arg2, Object arg3) {
152 return mH.obtainMessage(what, arg1, arg2, arg3);
153 }
154
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700155 public Message obtainMessageIIOO(int what, int arg1, int arg2,
156 Object arg3, Object arg4) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700157 SomeArgs args = SomeArgs.obtain();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700158 args.arg1 = arg3;
159 args.arg2 = arg4;
160 return mH.obtainMessage(what, arg1, arg2, args);
161 }
162
Mathew Inwoodaf972c82018-08-20 14:13:20 +0100163 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 public Message obtainMessageIOO(int what, int arg1, Object arg2, Object arg3) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700165 SomeArgs args = SomeArgs.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 args.arg1 = arg2;
167 args.arg2 = arg3;
168 return mH.obtainMessage(what, arg1, 0, args);
169 }
170
Dianne Hackborn91097de2014-04-04 18:02:06 -0700171 public Message obtainMessageIOOO(int what, int arg1, Object arg2, Object arg3, Object arg4) {
172 SomeArgs args = SomeArgs.obtain();
173 args.arg1 = arg2;
174 args.arg2 = arg3;
175 args.arg3 = arg4;
176 return mH.obtainMessage(what, arg1, 0, args);
177 }
178
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700179 public Message obtainMessageIIOOO(int what, int arg1, int arg2, Object arg3, Object arg4,
180 Object arg5) {
181 SomeArgs args = SomeArgs.obtain();
182 args.arg1 = arg3;
183 args.arg2 = arg4;
184 args.arg3 = arg5;
185 return mH.obtainMessage(what, arg1, arg2, args);
186 }
187
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800188 public Message obtainMessageIIOOOO(int what, int arg1, int arg2, Object arg3, Object arg4,
189 Object arg5, Object arg6) {
190 SomeArgs args = SomeArgs.obtain();
191 args.arg1 = arg3;
192 args.arg2 = arg4;
193 args.arg3 = arg5;
194 args.arg4 = arg6;
195 return mH.obtainMessage(what, arg1, arg2, args);
196 }
197
Mathew Inwoodaf972c82018-08-20 14:13:20 +0100198 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 public Message obtainMessageOO(int what, Object arg1, Object arg2) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700200 SomeArgs args = SomeArgs.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 args.arg1 = arg1;
202 args.arg2 = arg2;
203 return mH.obtainMessage(what, 0, 0, args);
204 }
205
Mathew Inwoodaf972c82018-08-20 14:13:20 +0100206 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 public Message obtainMessageOOO(int what, Object arg1, Object arg2, Object arg3) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700208 SomeArgs args = SomeArgs.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 args.arg1 = arg1;
210 args.arg2 = arg2;
211 args.arg3 = arg3;
212 return mH.obtainMessage(what, 0, 0, args);
213 }
214
215 public Message obtainMessageOOOO(int what, Object arg1, Object arg2,
216 Object arg3, Object arg4) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700217 SomeArgs args = SomeArgs.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 args.arg1 = arg1;
219 args.arg2 = arg2;
220 args.arg3 = arg3;
221 args.arg4 = arg4;
222 return mH.obtainMessage(what, 0, 0, args);
223 }
224
Dianne Hackborn91097de2014-04-04 18:02:06 -0700225 public Message obtainMessageOOOOO(int what, Object arg1, Object arg2,
226 Object arg3, Object arg4, Object arg5) {
227 SomeArgs args = SomeArgs.obtain();
228 args.arg1 = arg1;
229 args.arg2 = arg2;
230 args.arg3 = arg3;
231 args.arg4 = arg4;
232 args.arg5 = arg5;
233 return mH.obtainMessage(what, 0, 0, args);
234 }
235
Amith Yamasanie8222e52016-04-08 15:28:47 -0700236 public Message obtainMessageOOOOII(int what, Object arg1, Object arg2,
237 Object arg3, Object arg4, int arg5, int arg6) {
238 SomeArgs args = SomeArgs.obtain();
239 args.arg1 = arg1;
240 args.arg2 = arg2;
241 args.arg3 = arg3;
242 args.arg4 = arg4;
243 args.argi5 = arg5;
244 args.argi6 = arg6;
245 return mH.obtainMessage(what, 0, 0, args);
246 }
247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 public Message obtainMessageIIII(int what, int arg1, int arg2,
249 int arg3, int arg4) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700250 SomeArgs args = SomeArgs.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 args.argi1 = arg1;
252 args.argi2 = arg2;
253 args.argi3 = arg3;
254 args.argi4 = arg4;
255 return mH.obtainMessage(what, 0, 0, args);
256 }
Amith Yamasanie8222e52016-04-08 15:28:47 -0700257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 public Message obtainMessageIIIIII(int what, int arg1, int arg2,
259 int arg3, int arg4, int arg5, int arg6) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700260 SomeArgs args = SomeArgs.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 args.argi1 = arg1;
262 args.argi2 = arg2;
263 args.argi3 = arg3;
264 args.argi4 = arg4;
265 args.argi5 = arg5;
266 args.argi6 = arg6;
267 return mH.obtainMessage(what, 0, 0, args);
268 }
Amith Yamasanie8222e52016-04-08 15:28:47 -0700269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 public Message obtainMessageIIIIO(int what, int arg1, int arg2,
271 int arg3, int arg4, Object arg5) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700272 SomeArgs args = SomeArgs.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 args.arg1 = arg5;
274 args.argi1 = arg1;
275 args.argi2 = arg2;
276 args.argi3 = arg3;
277 args.argi4 = arg4;
278 return mH.obtainMessage(what, 0, 0, args);
279 }
280}