Kenny Root | 15a4d2f | 2010-03-11 18:20:12 -0800 | [diff] [blame] | 1 | /* |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | package com.android.internal.os; |
| 18 | |
Artur Satayev | ed5a6ae | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 19 | import android.compat.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.os.Handler; |
| 22 | import android.os.Looper; |
| 23 | import android.os.Message; |
| 24 | |
Eugene Susla | 07d718b | 2018-08-20 15:18:47 -0700 | [diff] [blame] | 25 | /** |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | public class HandlerCaller { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | final Looper mMainLooper; |
| 33 | final Handler mH; |
| 34 | |
| 35 | final Callback mCallback; |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 36 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | class MyHandler extends Handler { |
Mita Yun | ed218c7 | 2012-12-06 17:18:25 -0800 | [diff] [blame] | 38 | MyHandler(Looper looper, boolean async) { |
| 39 | super(looper, null, async); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | } |
Mita Yun | ed218c7 | 2012-12-06 17:18:25 -0800 | [diff] [blame] | 41 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | @Override |
| 43 | public void handleMessage(Message msg) { |
| 44 | mCallback.executeMessage(msg); |
| 45 | } |
| 46 | } |
Mita Yun | ed218c7 | 2012-12-06 17:18:25 -0800 | [diff] [blame] | 47 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | public interface Callback { |
| 49 | public void executeMessage(Message msg); |
| 50 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | |
Mita Yun | ed218c7 | 2012-12-06 17:18:25 -0800 | [diff] [blame] | 52 | public HandlerCaller(Context context, Looper looper, Callback callback, |
| 53 | boolean asyncHandler) { |
Mita Yun | ed218c7 | 2012-12-06 17:18:25 -0800 | [diff] [blame] | 54 | mMainLooper = looper != null ? looper : context.getMainLooper(); |
| 55 | mH = new MyHandler(mMainLooper, asyncHandler); |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 56 | mCallback = callback; |
| 57 | } |
| 58 | |
Jeff Brown | 3d110b2 | 2014-11-21 19:01:13 -0800 | [diff] [blame] | 59 | public Handler getHandler() { |
| 60 | return mH; |
| 61 | } |
| 62 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | 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 | } |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 75 | |
| 76 | public void sendMessageDelayed(Message msg, long delayMillis) { |
| 77 | mH.sendMessageDelayed(msg, delayMillis); |
| 78 | } |
| 79 | |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 80 | public boolean hasMessages(int what) { |
| 81 | return mH.hasMessages(what); |
| 82 | } |
| 83 | |
Dianne Hackborn | 8df8b2b | 2009-08-17 15:15:18 -0700 | [diff] [blame] | 84 | 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 Inwood | af972c8 | 2018-08-20 14:13:20 +0100 | [diff] [blame] | 92 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | public void sendMessage(Message msg) { |
| 94 | mH.sendMessage(msg); |
| 95 | } |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 96 | |
| 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 Inwood | af972c8 | 2018-08-20 14:13:20 +0100 | [diff] [blame] | 117 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | 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 Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 127 | SomeArgs args = SomeArgs.obtain(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | args.arg1 = arg2; |
| 129 | args.arg2 = arg3; |
| 130 | return mH.obtainMessage(what, arg1 ? 1 : 0, 0, args); |
| 131 | } |
| 132 | |
Mathew Inwood | af972c8 | 2018-08-20 14:13:20 +0100 | [diff] [blame] | 133 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | 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 Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 142 | public Message obtainMessageII(int what, int arg1, int arg2) { |
| 143 | return mH.obtainMessage(what, arg1, arg2); |
| 144 | } |
| 145 | |
Mathew Inwood | af972c8 | 2018-08-20 14:13:20 +0100 | [diff] [blame] | 146 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | 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 Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 155 | public Message obtainMessageIIOO(int what, int arg1, int arg2, |
| 156 | Object arg3, Object arg4) { |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 157 | SomeArgs args = SomeArgs.obtain(); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 158 | args.arg1 = arg3; |
| 159 | args.arg2 = arg4; |
| 160 | return mH.obtainMessage(what, arg1, arg2, args); |
| 161 | } |
| 162 | |
Mathew Inwood | af972c8 | 2018-08-20 14:13:20 +0100 | [diff] [blame] | 163 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | public Message obtainMessageIOO(int what, int arg1, Object arg2, Object arg3) { |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 165 | SomeArgs args = SomeArgs.obtain(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | args.arg1 = arg2; |
| 167 | args.arg2 = arg3; |
| 168 | return mH.obtainMessage(what, arg1, 0, args); |
| 169 | } |
| 170 | |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 171 | 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 Hackborn | 3d07c94 | 2015-03-13 18:02:54 -0700 | [diff] [blame] | 179 | 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 Yukawa | 6db3bfe | 2017-02-13 12:04:41 -0800 | [diff] [blame] | 188 | 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 Inwood | af972c8 | 2018-08-20 14:13:20 +0100 | [diff] [blame] | 198 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | public Message obtainMessageOO(int what, Object arg1, Object arg2) { |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 200 | SomeArgs args = SomeArgs.obtain(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | args.arg1 = arg1; |
| 202 | args.arg2 = arg2; |
| 203 | return mH.obtainMessage(what, 0, 0, args); |
| 204 | } |
| 205 | |
Mathew Inwood | af972c8 | 2018-08-20 14:13:20 +0100 | [diff] [blame] | 206 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | public Message obtainMessageOOO(int what, Object arg1, Object arg2, Object arg3) { |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 208 | SomeArgs args = SomeArgs.obtain(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | 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 Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 217 | SomeArgs args = SomeArgs.obtain(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | 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 Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 225 | 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 Yamasani | e8222e5 | 2016-04-08 15:28:47 -0700 | [diff] [blame] | 236 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | public Message obtainMessageIIII(int what, int arg1, int arg2, |
| 249 | int arg3, int arg4) { |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 250 | SomeArgs args = SomeArgs.obtain(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | 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 Yamasani | e8222e5 | 2016-04-08 15:28:47 -0700 | [diff] [blame] | 257 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | public Message obtainMessageIIIIII(int what, int arg1, int arg2, |
| 259 | int arg3, int arg4, int arg5, int arg6) { |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 260 | SomeArgs args = SomeArgs.obtain(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | 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 Yamasani | e8222e5 | 2016-04-08 15:28:47 -0700 | [diff] [blame] | 269 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | public Message obtainMessageIIIIO(int what, int arg1, int arg2, |
| 271 | int arg3, int arg4, Object arg5) { |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 272 | SomeArgs args = SomeArgs.obtain(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | 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 | } |