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.annotation.UnsupportedAppUsage; 20 import android.content.Context; 21 import android.os.Handler; 22 import android.os.Looper; 23 import android.os.Message; 24 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 31 public class HandlerCaller { 32 final Looper mMainLooper; 33 final Handler mH; 34 35 final Callback mCallback; 36 37 class MyHandler extends Handler { MyHandler(Looper looper, boolean async)38 MyHandler(Looper looper, boolean async) { 39 super(looper, null, async); 40 } 41 42 @Override handleMessage(Message msg)43 public void handleMessage(Message msg) { 44 mCallback.executeMessage(msg); 45 } 46 } 47 48 public interface Callback { executeMessage(Message msg)49 public void executeMessage(Message msg); 50 } 51 HandlerCaller(Context context, Looper looper, Callback callback, boolean asyncHandler)52 public HandlerCaller(Context context, Looper looper, Callback callback, 53 boolean asyncHandler) { 54 mMainLooper = looper != null ? looper : context.getMainLooper(); 55 mH = new MyHandler(mMainLooper, asyncHandler); 56 mCallback = callback; 57 } 58 getHandler()59 public Handler getHandler() { 60 return mH; 61 } 62 executeOrSendMessage(Message msg)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 } 75 sendMessageDelayed(Message msg, long delayMillis)76 public void sendMessageDelayed(Message msg, long delayMillis) { 77 mH.sendMessageDelayed(msg, delayMillis); 78 } 79 hasMessages(int what)80 public boolean hasMessages(int what) { 81 return mH.hasMessages(what); 82 } 83 removeMessages(int what)84 public void removeMessages(int what) { 85 mH.removeMessages(what); 86 } 87 removeMessages(int what, Object obj)88 public void removeMessages(int what, Object obj) { 89 mH.removeMessages(what, obj); 90 } 91 92 @UnsupportedAppUsage sendMessage(Message msg)93 public void sendMessage(Message msg) { 94 mH.sendMessage(msg); 95 } 96 sendMessageAndWait(Message msg)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 117 @UnsupportedAppUsage obtainMessage(int what)118 public Message obtainMessage(int what) { 119 return mH.obtainMessage(what); 120 } 121 obtainMessageBO(int what, boolean arg1, Object arg2)122 public Message obtainMessageBO(int what, boolean arg1, Object arg2) { 123 return mH.obtainMessage(what, arg1 ? 1 : 0, 0, arg2); 124 } 125 obtainMessageBOO(int what, boolean arg1, Object arg2, Object arg3)126 public Message obtainMessageBOO(int what, boolean arg1, Object arg2, Object arg3) { 127 SomeArgs args = SomeArgs.obtain(); 128 args.arg1 = arg2; 129 args.arg2 = arg3; 130 return mH.obtainMessage(what, arg1 ? 1 : 0, 0, args); 131 } 132 133 @UnsupportedAppUsage obtainMessageO(int what, Object arg1)134 public Message obtainMessageO(int what, Object arg1) { 135 return mH.obtainMessage(what, 0, 0, arg1); 136 } 137 obtainMessageI(int what, int arg1)138 public Message obtainMessageI(int what, int arg1) { 139 return mH.obtainMessage(what, arg1, 0); 140 } 141 obtainMessageII(int what, int arg1, int arg2)142 public Message obtainMessageII(int what, int arg1, int arg2) { 143 return mH.obtainMessage(what, arg1, arg2); 144 } 145 146 @UnsupportedAppUsage obtainMessageIO(int what, int arg1, Object arg2)147 public Message obtainMessageIO(int what, int arg1, Object arg2) { 148 return mH.obtainMessage(what, arg1, 0, arg2); 149 } 150 obtainMessageIIO(int what, int arg1, int arg2, Object arg3)151 public Message obtainMessageIIO(int what, int arg1, int arg2, Object arg3) { 152 return mH.obtainMessage(what, arg1, arg2, arg3); 153 } 154 obtainMessageIIOO(int what, int arg1, int arg2, Object arg3, Object arg4)155 public Message obtainMessageIIOO(int what, int arg1, int arg2, 156 Object arg3, Object arg4) { 157 SomeArgs args = SomeArgs.obtain(); 158 args.arg1 = arg3; 159 args.arg2 = arg4; 160 return mH.obtainMessage(what, arg1, arg2, args); 161 } 162 163 @UnsupportedAppUsage obtainMessageIOO(int what, int arg1, Object arg2, Object arg3)164 public Message obtainMessageIOO(int what, int arg1, Object arg2, Object arg3) { 165 SomeArgs args = SomeArgs.obtain(); 166 args.arg1 = arg2; 167 args.arg2 = arg3; 168 return mH.obtainMessage(what, arg1, 0, args); 169 } 170 obtainMessageIOOO(int what, int arg1, Object arg2, Object arg3, Object arg4)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 obtainMessageIIOOO(int what, int arg1, int arg2, Object arg3, Object arg4, Object arg5)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 obtainMessageIIOOOO(int what, int arg1, int arg2, Object arg3, Object arg4, Object arg5, Object arg6)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 198 @UnsupportedAppUsage obtainMessageOO(int what, Object arg1, Object arg2)199 public Message obtainMessageOO(int what, Object arg1, Object arg2) { 200 SomeArgs args = SomeArgs.obtain(); 201 args.arg1 = arg1; 202 args.arg2 = arg2; 203 return mH.obtainMessage(what, 0, 0, args); 204 } 205 206 @UnsupportedAppUsage obtainMessageOOO(int what, Object arg1, Object arg2, Object arg3)207 public Message obtainMessageOOO(int what, Object arg1, Object arg2, Object arg3) { 208 SomeArgs args = SomeArgs.obtain(); 209 args.arg1 = arg1; 210 args.arg2 = arg2; 211 args.arg3 = arg3; 212 return mH.obtainMessage(what, 0, 0, args); 213 } 214 obtainMessageOOOO(int what, Object arg1, Object arg2, Object arg3, Object arg4)215 public Message obtainMessageOOOO(int what, Object arg1, Object arg2, 216 Object arg3, Object arg4) { 217 SomeArgs args = SomeArgs.obtain(); 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 obtainMessageOOOOO(int what, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5)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 obtainMessageOOOOII(int what, Object arg1, Object arg2, Object arg3, Object arg4, int arg5, int arg6)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 obtainMessageIIII(int what, int arg1, int arg2, int arg3, int arg4)248 public Message obtainMessageIIII(int what, int arg1, int arg2, 249 int arg3, int arg4) { 250 SomeArgs args = SomeArgs.obtain(); 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 } 257 obtainMessageIIIIII(int what, int arg1, int arg2, int arg3, int arg4, int arg5, int arg6)258 public Message obtainMessageIIIIII(int what, int arg1, int arg2, 259 int arg3, int arg4, int arg5, int arg6) { 260 SomeArgs args = SomeArgs.obtain(); 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 } 269 obtainMessageIIIIO(int what, int arg1, int arg2, int arg3, int arg4, Object arg5)270 public Message obtainMessageIIIIO(int what, int arg1, int arg2, 271 int arg3, int arg4, Object arg5) { 272 SomeArgs args = SomeArgs.obtain(); 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 } 281