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 | |
| 19 | import android.content.Context; |
| 20 | import android.os.Handler; |
| 21 | import android.os.Looper; |
| 22 | import android.os.Message; |
| 23 | |
| 24 | public class HandlerCaller { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | final Looper mMainLooper; |
| 26 | final Handler mH; |
| 27 | |
| 28 | final Callback mCallback; |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 29 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | class MyHandler extends Handler { |
Mita Yun | ed218c7 | 2012-12-06 17:18:25 -0800 | [diff] [blame] | 31 | MyHandler(Looper looper, boolean async) { |
| 32 | super(looper, null, async); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | } |
Mita Yun | ed218c7 | 2012-12-06 17:18:25 -0800 | [diff] [blame] | 34 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | @Override |
| 36 | public void handleMessage(Message msg) { |
| 37 | mCallback.executeMessage(msg); |
| 38 | } |
| 39 | } |
Mita Yun | ed218c7 | 2012-12-06 17:18:25 -0800 | [diff] [blame] | 40 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | public interface Callback { |
| 42 | public void executeMessage(Message msg); |
| 43 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | |
Mita Yun | ed218c7 | 2012-12-06 17:18:25 -0800 | [diff] [blame] | 45 | public HandlerCaller(Context context, Looper looper, Callback callback, |
| 46 | boolean asyncHandler) { |
Mita Yun | ed218c7 | 2012-12-06 17:18:25 -0800 | [diff] [blame] | 47 | mMainLooper = looper != null ? looper : context.getMainLooper(); |
| 48 | mH = new MyHandler(mMainLooper, asyncHandler); |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 49 | mCallback = callback; |
| 50 | } |
| 51 | |
Jeff Brown | 3d110b2 | 2014-11-21 19:01:13 -0800 | [diff] [blame] | 52 | public Handler getHandler() { |
| 53 | return mH; |
| 54 | } |
| 55 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | 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 | } |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 68 | |
| 69 | public void sendMessageDelayed(Message msg, long delayMillis) { |
| 70 | mH.sendMessageDelayed(msg, delayMillis); |
| 71 | } |
| 72 | |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 73 | public boolean hasMessages(int what) { |
| 74 | return mH.hasMessages(what); |
| 75 | } |
| 76 | |
Dianne Hackborn | 8df8b2b | 2009-08-17 15:15:18 -0700 | [diff] [blame] | 77 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | public void sendMessage(Message msg) { |
| 86 | mH.sendMessage(msg); |
| 87 | } |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 88 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | 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 Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 118 | SomeArgs args = SomeArgs.obtain(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | 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 Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 132 | public Message obtainMessageII(int what, int arg1, int arg2) { |
| 133 | return mH.obtainMessage(what, arg1, arg2); |
| 134 | } |
| 135 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | 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 Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 144 | public Message obtainMessageIIOO(int what, int arg1, int arg2, |
| 145 | Object arg3, Object arg4) { |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 146 | SomeArgs args = SomeArgs.obtain(); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 147 | args.arg1 = arg3; |
| 148 | args.arg2 = arg4; |
| 149 | return mH.obtainMessage(what, arg1, arg2, args); |
| 150 | } |
| 151 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | public Message obtainMessageIOO(int what, int arg1, Object arg2, Object arg3) { |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 153 | SomeArgs args = SomeArgs.obtain(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | args.arg1 = arg2; |
| 155 | args.arg2 = arg3; |
| 156 | return mH.obtainMessage(what, arg1, 0, args); |
| 157 | } |
| 158 | |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 159 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | public Message obtainMessageOO(int what, Object arg1, Object arg2) { |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 168 | SomeArgs args = SomeArgs.obtain(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | 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 Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 175 | SomeArgs args = SomeArgs.obtain(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | 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 Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 184 | SomeArgs args = SomeArgs.obtain(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | 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 Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 192 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | public Message obtainMessageIIII(int what, int arg1, int arg2, |
| 204 | int arg3, int arg4) { |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 205 | SomeArgs args = SomeArgs.obtain(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | 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 Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 215 | SomeArgs args = SomeArgs.obtain(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | 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 Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 227 | SomeArgs args = SomeArgs.obtain(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | 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 | } |