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 obtainMessageOO(int what, Object arg1, Object arg2)167 public Message obtainMessageOO(int what, Object arg1, Object arg2) { 168 SomeArgs args = SomeArgs.obtain(); 169 args.arg1 = arg1; 170 args.arg2 = arg2; 171 return mH.obtainMessage(what, 0, 0, args); 172 } 173 obtainMessageOOO(int what, Object arg1, Object arg2, Object arg3)174 public Message obtainMessageOOO(int what, Object arg1, Object arg2, Object arg3) { 175 SomeArgs args = SomeArgs.obtain(); 176 args.arg1 = arg1; 177 args.arg2 = arg2; 178 args.arg3 = arg3; 179 return mH.obtainMessage(what, 0, 0, args); 180 } 181 obtainMessageOOOO(int what, Object arg1, Object arg2, Object arg3, Object arg4)182 public Message obtainMessageOOOO(int what, Object arg1, Object arg2, 183 Object arg3, Object arg4) { 184 SomeArgs args = SomeArgs.obtain(); 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 obtainMessageOOOOO(int what, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5)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 obtainMessageIIII(int what, int arg1, int arg2, int arg3, int arg4)203 public Message obtainMessageIIII(int what, int arg1, int arg2, 204 int arg3, int arg4) { 205 SomeArgs args = SomeArgs.obtain(); 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 obtainMessageIIIIII(int what, int arg1, int arg2, int arg3, int arg4, int arg5, int arg6)213 public Message obtainMessageIIIIII(int what, int arg1, int arg2, 214 int arg3, int arg4, int arg5, int arg6) { 215 SomeArgs args = SomeArgs.obtain(); 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 obtainMessageIIIIO(int what, int arg1, int arg2, int arg3, int arg4, Object arg5)225 public Message obtainMessageIIIIO(int what, int arg1, int arg2, 226 int arg3, int arg4, Object arg5) { 227 SomeArgs args = SomeArgs.obtain(); 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 } 236