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 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 { 25 final Looper mMainLooper; 26 final Handler mH; 27 28 final Callback mCallback; 29 30 class MyHandler extends Handler { MyHandler(Looper looper, boolean async)31 MyHandler(Looper looper, boolean async) { 32 super(looper, null, async); 33 } 34 35 @Override handleMessage(Message msg)36 public void handleMessage(Message msg) { 37 mCallback.executeMessage(msg); 38 } 39 } 40 41 public interface Callback { executeMessage(Message msg)42 public void executeMessage(Message msg); 43 } 44 HandlerCaller(Context context, Looper looper, Callback callback, boolean asyncHandler)45 public HandlerCaller(Context context, Looper looper, Callback callback, 46 boolean asyncHandler) { 47 mMainLooper = looper != null ? looper : context.getMainLooper(); 48 mH = new MyHandler(mMainLooper, asyncHandler); 49 mCallback = callback; 50 } 51 getHandler()52 public Handler getHandler() { 53 return mH; 54 } 55 executeOrSendMessage(Message msg)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 } 68 sendMessageDelayed(Message msg, long delayMillis)69 public void sendMessageDelayed(Message msg, long delayMillis) { 70 mH.sendMessageDelayed(msg, delayMillis); 71 } 72 hasMessages(int what)73 public boolean hasMessages(int what) { 74 return mH.hasMessages(what); 75 } 76 removeMessages(int what)77 public void removeMessages(int what) { 78 mH.removeMessages(what); 79 } 80 removeMessages(int what, Object obj)81 public void removeMessages(int what, Object obj) { 82 mH.removeMessages(what, obj); 83 } 84 sendMessage(Message msg)85 public void sendMessage(Message msg) { 86 mH.sendMessage(msg); 87 } 88 sendMessageAndWait(Message msg)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 obtainMessage(int what)109 public Message obtainMessage(int what) { 110 return mH.obtainMessage(what); 111 } 112 obtainMessageBO(int what, boolean arg1, Object arg2)113 public Message obtainMessageBO(int what, boolean arg1, Object arg2) { 114 return mH.obtainMessage(what, arg1 ? 1 : 0, 0, arg2); 115 } 116 obtainMessageBOO(int what, boolean arg1, Object arg2, Object arg3)117 public Message obtainMessageBOO(int what, boolean arg1, Object arg2, Object arg3) { 118 SomeArgs args = SomeArgs.obtain(); 119 args.arg1 = arg2; 120 args.arg2 = arg3; 121 return mH.obtainMessage(what, arg1 ? 1 : 0, 0, args); 122 } 123 obtainMessageO(int what, Object arg1)124 public Message obtainMessageO(int what, Object arg1) { 125 return mH.obtainMessage(what, 0, 0, arg1); 126 } 127 obtainMessageI(int what, int arg1)128 public Message obtainMessageI(int what, int arg1) { 129 return mH.obtainMessage(what, arg1, 0); 130 } 131 obtainMessageII(int what, int arg1, int arg2)132 public Message obtainMessageII(int what, int arg1, int arg2) { 133 return mH.obtainMessage(what, arg1, arg2); 134 } 135 obtainMessageIO(int what, int arg1, Object arg2)136 public Message obtainMessageIO(int what, int arg1, Object arg2) { 137 return mH.obtainMessage(what, arg1, 0, arg2); 138 } 139 obtainMessageIIO(int what, int arg1, int arg2, Object arg3)140 public Message obtainMessageIIO(int what, int arg1, int arg2, Object arg3) { 141 return mH.obtainMessage(what, arg1, arg2, arg3); 142 } 143 obtainMessageIIOO(int what, int arg1, int arg2, Object arg3, Object arg4)144 public Message obtainMessageIIOO(int what, int arg1, int arg2, 145 Object arg3, Object arg4) { 146 SomeArgs args = SomeArgs.obtain(); 147 args.arg1 = arg3; 148 args.arg2 = arg4; 149 return mH.obtainMessage(what, arg1, arg2, args); 150 } 151 obtainMessageIOO(int what, int arg1, Object arg2, Object arg3)152 public Message obtainMessageIOO(int what, int arg1, Object arg2, Object arg3) { 153 SomeArgs args = SomeArgs.obtain(); 154 args.arg1 = arg2; 155 args.arg2 = arg3; 156 return mH.obtainMessage(what, arg1, 0, args); 157 } 158 obtainMessageIOOO(int what, int arg1, Object arg2, Object arg3, Object arg4)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 obtainMessageIIOOO(int what, int arg1, int arg2, Object arg3, Object arg4, Object arg5)167 public Message obtainMessageIIOOO(int what, int arg1, int arg2, Object arg3, Object arg4, 168 Object arg5) { 169 SomeArgs args = SomeArgs.obtain(); 170 args.arg1 = arg3; 171 args.arg2 = arg4; 172 args.arg3 = arg5; 173 return mH.obtainMessage(what, arg1, arg2, args); 174 } 175 obtainMessageIIOOOO(int what, int arg1, int arg2, Object arg3, Object arg4, Object arg5, Object arg6)176 public Message obtainMessageIIOOOO(int what, int arg1, int arg2, Object arg3, Object arg4, 177 Object arg5, Object arg6) { 178 SomeArgs args = SomeArgs.obtain(); 179 args.arg1 = arg3; 180 args.arg2 = arg4; 181 args.arg3 = arg5; 182 args.arg4 = arg6; 183 return mH.obtainMessage(what, arg1, arg2, args); 184 } 185 obtainMessageOO(int what, Object arg1, Object arg2)186 public Message obtainMessageOO(int what, Object arg1, Object arg2) { 187 SomeArgs args = SomeArgs.obtain(); 188 args.arg1 = arg1; 189 args.arg2 = arg2; 190 return mH.obtainMessage(what, 0, 0, args); 191 } 192 obtainMessageOOO(int what, Object arg1, Object arg2, Object arg3)193 public Message obtainMessageOOO(int what, Object arg1, Object arg2, Object arg3) { 194 SomeArgs args = SomeArgs.obtain(); 195 args.arg1 = arg1; 196 args.arg2 = arg2; 197 args.arg3 = arg3; 198 return mH.obtainMessage(what, 0, 0, args); 199 } 200 obtainMessageOOOO(int what, Object arg1, Object arg2, Object arg3, Object arg4)201 public Message obtainMessageOOOO(int what, Object arg1, Object arg2, 202 Object arg3, Object arg4) { 203 SomeArgs args = SomeArgs.obtain(); 204 args.arg1 = arg1; 205 args.arg2 = arg2; 206 args.arg3 = arg3; 207 args.arg4 = arg4; 208 return mH.obtainMessage(what, 0, 0, args); 209 } 210 obtainMessageOOOOO(int what, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5)211 public Message obtainMessageOOOOO(int what, Object arg1, Object arg2, 212 Object arg3, Object arg4, Object arg5) { 213 SomeArgs args = SomeArgs.obtain(); 214 args.arg1 = arg1; 215 args.arg2 = arg2; 216 args.arg3 = arg3; 217 args.arg4 = arg4; 218 args.arg5 = arg5; 219 return mH.obtainMessage(what, 0, 0, args); 220 } 221 obtainMessageOOOOII(int what, Object arg1, Object arg2, Object arg3, Object arg4, int arg5, int arg6)222 public Message obtainMessageOOOOII(int what, Object arg1, Object arg2, 223 Object arg3, Object arg4, int arg5, int arg6) { 224 SomeArgs args = SomeArgs.obtain(); 225 args.arg1 = arg1; 226 args.arg2 = arg2; 227 args.arg3 = arg3; 228 args.arg4 = arg4; 229 args.argi5 = arg5; 230 args.argi6 = arg6; 231 return mH.obtainMessage(what, 0, 0, args); 232 } 233 obtainMessageIIII(int what, int arg1, int arg2, int arg3, int arg4)234 public Message obtainMessageIIII(int what, int arg1, int arg2, 235 int arg3, int arg4) { 236 SomeArgs args = SomeArgs.obtain(); 237 args.argi1 = arg1; 238 args.argi2 = arg2; 239 args.argi3 = arg3; 240 args.argi4 = arg4; 241 return mH.obtainMessage(what, 0, 0, args); 242 } 243 obtainMessageIIIIII(int what, int arg1, int arg2, int arg3, int arg4, int arg5, int arg6)244 public Message obtainMessageIIIIII(int what, int arg1, int arg2, 245 int arg3, int arg4, int arg5, int arg6) { 246 SomeArgs args = SomeArgs.obtain(); 247 args.argi1 = arg1; 248 args.argi2 = arg2; 249 args.argi3 = arg3; 250 args.argi4 = arg4; 251 args.argi5 = arg5; 252 args.argi6 = arg6; 253 return mH.obtainMessage(what, 0, 0, args); 254 } 255 obtainMessageIIIIO(int what, int arg1, int arg2, int arg3, int arg4, Object arg5)256 public Message obtainMessageIIIIO(int what, int arg1, int arg2, 257 int arg3, int arg4, Object arg5) { 258 SomeArgs args = SomeArgs.obtain(); 259 args.arg1 = arg5; 260 args.argi1 = arg1; 261 args.argi2 = arg2; 262 args.argi3 = arg3; 263 args.argi4 = arg4; 264 return mH.obtainMessage(what, 0, 0, args); 265 } 266 } 267