The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 android.os; |
| 18 | |
| 19 | import android.util.Log; |
| 20 | import android.util.Printer; |
| 21 | |
| 22 | import java.lang.reflect.Modifier; |
| 23 | |
| 24 | /** |
| 25 | * A Handler allows you to send and process {@link Message} and Runnable |
| 26 | * objects associated with a thread's {@link MessageQueue}. Each Handler |
| 27 | * instance is associated with a single thread and that thread's message |
| 28 | * queue. When you create a new Handler, it is bound to the thread / |
| 29 | * message queue of the thread that is creating it -- from that point on, |
| 30 | * it will deliver messages and runnables to that message queue and execute |
| 31 | * them as they come out of the message queue. |
| 32 | * |
| 33 | * <p>There are two main uses for a Handler: (1) to schedule messages and |
| 34 | * runnables to be executed as some point in the future; and (2) to enqueue |
| 35 | * an action to be performed on a different thread than your own. |
| 36 | * |
| 37 | * <p>Scheduling messages is accomplished with the |
| 38 | * {@link #post}, {@link #postAtTime(Runnable, long)}, |
| 39 | * {@link #postDelayed}, {@link #sendEmptyMessage}, |
| 40 | * {@link #sendMessage}, {@link #sendMessageAtTime}, and |
| 41 | * {@link #sendMessageDelayed} methods. The <em>post</em> versions allow |
| 42 | * you to enqueue Runnable objects to be called by the message queue when |
| 43 | * they are received; the <em>sendMessage</em> versions allow you to enqueue |
| 44 | * a {@link Message} object containing a bundle of data that will be |
| 45 | * processed by the Handler's {@link #handleMessage} method (requiring that |
| 46 | * you implement a subclass of Handler). |
| 47 | * |
| 48 | * <p>When posting or sending to a Handler, you can either |
| 49 | * allow the item to be processed as soon as the message queue is ready |
| 50 | * to do so, or specify a delay before it gets processed or absolute time for |
| 51 | * it to be processed. The latter two allow you to implement timeouts, |
| 52 | * ticks, and other timing-based behavior. |
| 53 | * |
| 54 | * <p>When a |
| 55 | * process is created for your application, its main thread is dedicated to |
| 56 | * running a message queue that takes care of managing the top-level |
| 57 | * application objects (activities, broadcast receivers, etc) and any windows |
| 58 | * they create. You can create your own threads, and communicate back with |
| 59 | * the main application thread through a Handler. This is done by calling |
| 60 | * the same <em>post</em> or <em>sendMessage</em> methods as before, but from |
Chris Palmer | 4285514 | 2010-09-09 17:04:31 -0700 | [diff] [blame] | 61 | * your new thread. The given Runnable or Message will then be scheduled |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | * in the Handler's message queue and processed when appropriate. |
| 63 | */ |
| 64 | public class Handler { |
| 65 | /* |
| 66 | * Set this flag to true to detect anonymous, local or member classes |
| 67 | * that extend this Handler class and that are not static. These kind |
| 68 | * of classes can potentially create leaks. |
| 69 | */ |
| 70 | private static final boolean FIND_POTENTIAL_LEAKS = false; |
| 71 | private static final String TAG = "Handler"; |
| 72 | |
| 73 | /** |
| 74 | * Callback interface you can use when instantiating a Handler to avoid |
| 75 | * having to implement your own subclass of Handler. |
| 76 | */ |
| 77 | public interface Callback { |
| 78 | public boolean handleMessage(Message msg); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Subclasses must implement this to receive messages. |
| 83 | */ |
| 84 | public void handleMessage(Message msg) { |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Handle system messages here. |
| 89 | */ |
| 90 | public void dispatchMessage(Message msg) { |
| 91 | if (msg.callback != null) { |
| 92 | handleCallback(msg); |
| 93 | } else { |
| 94 | if (mCallback != null) { |
| 95 | if (mCallback.handleMessage(msg)) { |
| 96 | return; |
| 97 | } |
| 98 | } |
| 99 | handleMessage(msg); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Default constructor associates this handler with the queue for the |
| 105 | * current thread. |
| 106 | * |
| 107 | * If there isn't one, this handler won't be able to receive messages. |
| 108 | */ |
| 109 | public Handler() { |
| 110 | if (FIND_POTENTIAL_LEAKS) { |
| 111 | final Class<? extends Handler> klass = getClass(); |
| 112 | if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) && |
| 113 | (klass.getModifiers() & Modifier.STATIC) == 0) { |
| 114 | Log.w(TAG, "The following Handler class should be static or leaks might occur: " + |
| 115 | klass.getCanonicalName()); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | mLooper = Looper.myLooper(); |
| 120 | if (mLooper == null) { |
| 121 | throw new RuntimeException( |
| 122 | "Can't create handler inside thread that has not called Looper.prepare()"); |
| 123 | } |
| 124 | mQueue = mLooper.mQueue; |
| 125 | mCallback = null; |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame^] | 126 | mAsynchronous = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Constructor associates this handler with the queue for the |
| 131 | * current thread and takes a callback interface in which you can handle |
| 132 | * messages. |
| 133 | */ |
| 134 | public Handler(Callback callback) { |
| 135 | if (FIND_POTENTIAL_LEAKS) { |
| 136 | final Class<? extends Handler> klass = getClass(); |
| 137 | if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) && |
| 138 | (klass.getModifiers() & Modifier.STATIC) == 0) { |
| 139 | Log.w(TAG, "The following Handler class should be static or leaks might occur: " + |
| 140 | klass.getCanonicalName()); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | mLooper = Looper.myLooper(); |
| 145 | if (mLooper == null) { |
| 146 | throw new RuntimeException( |
| 147 | "Can't create handler inside thread that has not called Looper.prepare()"); |
| 148 | } |
| 149 | mQueue = mLooper.mQueue; |
| 150 | mCallback = callback; |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame^] | 151 | mAsynchronous = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Use the provided queue instead of the default one. |
| 156 | */ |
| 157 | public Handler(Looper looper) { |
| 158 | mLooper = looper; |
| 159 | mQueue = looper.mQueue; |
| 160 | mCallback = null; |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame^] | 161 | mAsynchronous = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Use the provided queue instead of the default one and take a callback |
| 166 | * interface in which to handle messages. |
| 167 | */ |
| 168 | public Handler(Looper looper, Callback callback) { |
| 169 | mLooper = looper; |
| 170 | mQueue = looper.mQueue; |
| 171 | mCallback = callback; |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame^] | 172 | mAsynchronous = false; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Use the provided queue instead of the default one and take a callback |
| 177 | * interface in which to handle messages. Also set whether the handler |
| 178 | * should be asynchronous. |
| 179 | * |
| 180 | * Handlers are synchronous by default unless this constructor is used to make |
| 181 | * one that is strictly asynchronous. |
| 182 | * |
| 183 | * Asynchronous messages represent interrupts or events that do not require global ordering |
| 184 | * with represent to synchronous messages. Asynchronous messages are not subject to |
| 185 | * the synchronization barriers introduced by {@link MessageQueue#enqueueSyncBarrier(long)}. |
| 186 | * |
| 187 | * @param async If true, the handler calls {@link Message#setAsynchronous(boolean)} for |
| 188 | * each {@link Message} that is sent to it or {@link Runnable} that is posted to it. |
| 189 | * |
| 190 | * @hide |
| 191 | */ |
| 192 | public Handler(Looper looper, Callback callback, boolean async) { |
| 193 | mLooper = looper; |
| 194 | mQueue = looper.mQueue; |
| 195 | mCallback = callback; |
| 196 | mAsynchronous = async; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
Romain Guy | f928469 | 2011-07-13 18:46:21 -0700 | [diff] [blame] | 200 | * Returns a string representing the name of the specified message. |
| 201 | * The default implementation will either return the class name of the |
| 202 | * message callback if any, or the hexadecimal representation of the |
| 203 | * message "what" field. |
| 204 | * |
| 205 | * @param message The message whose name is being queried |
| 206 | */ |
| 207 | public String getMessageName(Message message) { |
| 208 | if (message.callback != null) { |
| 209 | return message.callback.getClass().getName(); |
| 210 | } |
| 211 | return "0x" + Integer.toHexString(message.what); |
| 212 | } |
| 213 | |
| 214 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | * Returns a new {@link android.os.Message Message} from the global message pool. More efficient than |
| 216 | * creating and allocating new instances. The retrieved message has its handler set to this instance (Message.target == this). |
| 217 | * If you don't want that facility, just call Message.obtain() instead. |
| 218 | */ |
| 219 | public final Message obtainMessage() |
| 220 | { |
| 221 | return Message.obtain(this); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Same as {@link #obtainMessage()}, except that it also sets the what member of the returned Message. |
| 226 | * |
| 227 | * @param what Value to assign to the returned Message.what field. |
| 228 | * @return A Message from the global message pool. |
| 229 | */ |
| 230 | public final Message obtainMessage(int what) |
| 231 | { |
| 232 | return Message.obtain(this, what); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * |
| 237 | * Same as {@link #obtainMessage()}, except that it also sets the what and obj members |
| 238 | * of the returned Message. |
| 239 | * |
| 240 | * @param what Value to assign to the returned Message.what field. |
| 241 | * @param obj Value to assign to the returned Message.obj field. |
| 242 | * @return A Message from the global message pool. |
| 243 | */ |
| 244 | public final Message obtainMessage(int what, Object obj) |
| 245 | { |
| 246 | return Message.obtain(this, what, obj); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * |
| 251 | * Same as {@link #obtainMessage()}, except that it also sets the what, arg1 and arg2 members of the returned |
| 252 | * Message. |
| 253 | * @param what Value to assign to the returned Message.what field. |
| 254 | * @param arg1 Value to assign to the returned Message.arg1 field. |
| 255 | * @param arg2 Value to assign to the returned Message.arg2 field. |
| 256 | * @return A Message from the global message pool. |
| 257 | */ |
| 258 | public final Message obtainMessage(int what, int arg1, int arg2) |
| 259 | { |
| 260 | return Message.obtain(this, what, arg1, arg2); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * |
| 265 | * Same as {@link #obtainMessage()}, except that it also sets the what, obj, arg1,and arg2 values on the |
| 266 | * returned Message. |
| 267 | * @param what Value to assign to the returned Message.what field. |
| 268 | * @param arg1 Value to assign to the returned Message.arg1 field. |
| 269 | * @param arg2 Value to assign to the returned Message.arg2 field. |
| 270 | * @param obj Value to assign to the returned Message.obj field. |
| 271 | * @return A Message from the global message pool. |
| 272 | */ |
| 273 | public final Message obtainMessage(int what, int arg1, int arg2, Object obj) |
| 274 | { |
| 275 | return Message.obtain(this, what, arg1, arg2, obj); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Causes the Runnable r to be added to the message queue. |
| 280 | * The runnable will be run on the thread to which this handler is |
| 281 | * attached. |
| 282 | * |
| 283 | * @param r The Runnable that will be executed. |
| 284 | * |
| 285 | * @return Returns true if the Runnable was successfully placed in to the |
| 286 | * message queue. Returns false on failure, usually because the |
| 287 | * looper processing the message queue is exiting. |
| 288 | */ |
| 289 | public final boolean post(Runnable r) |
| 290 | { |
| 291 | return sendMessageDelayed(getPostMessage(r), 0); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Causes the Runnable r to be added to the message queue, to be run |
| 296 | * at a specific time given by <var>uptimeMillis</var>. |
| 297 | * <b>The time-base is {@link android.os.SystemClock#uptimeMillis}.</b> |
| 298 | * The runnable will be run on the thread to which this handler is attached. |
| 299 | * |
| 300 | * @param r The Runnable that will be executed. |
| 301 | * @param uptimeMillis The absolute time at which the callback should run, |
| 302 | * using the {@link android.os.SystemClock#uptimeMillis} time-base. |
| 303 | * |
| 304 | * @return Returns true if the Runnable was successfully placed in to the |
| 305 | * message queue. Returns false on failure, usually because the |
| 306 | * looper processing the message queue is exiting. Note that a |
| 307 | * result of true does not mean the Runnable will be processed -- if |
| 308 | * the looper is quit before the delivery time of the message |
| 309 | * occurs then the message will be dropped. |
| 310 | */ |
| 311 | public final boolean postAtTime(Runnable r, long uptimeMillis) |
| 312 | { |
| 313 | return sendMessageAtTime(getPostMessage(r), uptimeMillis); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Causes the Runnable r to be added to the message queue, to be run |
| 318 | * at a specific time given by <var>uptimeMillis</var>. |
| 319 | * <b>The time-base is {@link android.os.SystemClock#uptimeMillis}.</b> |
| 320 | * The runnable will be run on the thread to which this handler is attached. |
| 321 | * |
| 322 | * @param r The Runnable that will be executed. |
| 323 | * @param uptimeMillis The absolute time at which the callback should run, |
| 324 | * using the {@link android.os.SystemClock#uptimeMillis} time-base. |
| 325 | * |
| 326 | * @return Returns true if the Runnable was successfully placed in to the |
| 327 | * message queue. Returns false on failure, usually because the |
| 328 | * looper processing the message queue is exiting. Note that a |
| 329 | * result of true does not mean the Runnable will be processed -- if |
| 330 | * the looper is quit before the delivery time of the message |
| 331 | * occurs then the message will be dropped. |
| 332 | * |
| 333 | * @see android.os.SystemClock#uptimeMillis |
| 334 | */ |
| 335 | public final boolean postAtTime(Runnable r, Object token, long uptimeMillis) |
| 336 | { |
| 337 | return sendMessageAtTime(getPostMessage(r, token), uptimeMillis); |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Causes the Runnable r to be added to the message queue, to be run |
| 342 | * after the specified amount of time elapses. |
| 343 | * The runnable will be run on the thread to which this handler |
| 344 | * is attached. |
| 345 | * |
| 346 | * @param r The Runnable that will be executed. |
| 347 | * @param delayMillis The delay (in milliseconds) until the Runnable |
| 348 | * will be executed. |
| 349 | * |
| 350 | * @return Returns true if the Runnable was successfully placed in to the |
| 351 | * message queue. Returns false on failure, usually because the |
| 352 | * looper processing the message queue is exiting. Note that a |
| 353 | * result of true does not mean the Runnable will be processed -- |
| 354 | * if the looper is quit before the delivery time of the message |
| 355 | * occurs then the message will be dropped. |
| 356 | */ |
| 357 | public final boolean postDelayed(Runnable r, long delayMillis) |
| 358 | { |
| 359 | return sendMessageDelayed(getPostMessage(r), delayMillis); |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Posts a message to an object that implements Runnable. |
| 364 | * Causes the Runnable r to executed on the next iteration through the |
| 365 | * message queue. The runnable will be run on the thread to which this |
| 366 | * handler is attached. |
| 367 | * <b>This method is only for use in very special circumstances -- it |
| 368 | * can easily starve the message queue, cause ordering problems, or have |
| 369 | * other unexpected side-effects.</b> |
| 370 | * |
| 371 | * @param r The Runnable that will be executed. |
| 372 | * |
| 373 | * @return Returns true if the message was successfully placed in to the |
| 374 | * message queue. Returns false on failure, usually because the |
| 375 | * looper processing the message queue is exiting. |
| 376 | */ |
| 377 | public final boolean postAtFrontOfQueue(Runnable r) |
| 378 | { |
| 379 | return sendMessageAtFrontOfQueue(getPostMessage(r)); |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Remove any pending posts of Runnable r that are in the message queue. |
| 384 | */ |
| 385 | public final void removeCallbacks(Runnable r) |
| 386 | { |
| 387 | mQueue.removeMessages(this, r, null); |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Remove any pending posts of Runnable <var>r</var> with Object |
Dianne Hackborn | 466ed24 | 2011-07-21 18:16:31 -0700 | [diff] [blame] | 392 | * <var>token</var> that are in the message queue. If <var>token</var> is null, |
| 393 | * all callbacks will be removed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 | */ |
| 395 | public final void removeCallbacks(Runnable r, Object token) |
| 396 | { |
| 397 | mQueue.removeMessages(this, r, token); |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Pushes a message onto the end of the message queue after all pending messages |
| 402 | * before the current time. It will be received in {@link #handleMessage}, |
| 403 | * in the thread attached to this handler. |
| 404 | * |
| 405 | * @return Returns true if the message was successfully placed in to the |
| 406 | * message queue. Returns false on failure, usually because the |
| 407 | * looper processing the message queue is exiting. |
| 408 | */ |
| 409 | public final boolean sendMessage(Message msg) |
| 410 | { |
| 411 | return sendMessageDelayed(msg, 0); |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Sends a Message containing only the what value. |
| 416 | * |
| 417 | * @return Returns true if the message was successfully placed in to the |
| 418 | * message queue. Returns false on failure, usually because the |
| 419 | * looper processing the message queue is exiting. |
| 420 | */ |
| 421 | public final boolean sendEmptyMessage(int what) |
| 422 | { |
| 423 | return sendEmptyMessageDelayed(what, 0); |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Sends a Message containing only the what value, to be delivered |
| 428 | * after the specified amount of time elapses. |
| 429 | * @see #sendMessageDelayed(android.os.Message, long) |
| 430 | * |
| 431 | * @return Returns true if the message was successfully placed in to the |
| 432 | * message queue. Returns false on failure, usually because the |
| 433 | * looper processing the message queue is exiting. |
| 434 | */ |
| 435 | public final boolean sendEmptyMessageDelayed(int what, long delayMillis) { |
| 436 | Message msg = Message.obtain(); |
| 437 | msg.what = what; |
| 438 | return sendMessageDelayed(msg, delayMillis); |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Sends a Message containing only the what value, to be delivered |
| 443 | * at a specific time. |
| 444 | * @see #sendMessageAtTime(android.os.Message, long) |
| 445 | * |
| 446 | * @return Returns true if the message was successfully placed in to the |
| 447 | * message queue. Returns false on failure, usually because the |
| 448 | * looper processing the message queue is exiting. |
| 449 | */ |
| 450 | |
| 451 | public final boolean sendEmptyMessageAtTime(int what, long uptimeMillis) { |
| 452 | Message msg = Message.obtain(); |
| 453 | msg.what = what; |
| 454 | return sendMessageAtTime(msg, uptimeMillis); |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Enqueue a message into the message queue after all pending messages |
| 459 | * before (current time + delayMillis). You will receive it in |
| 460 | * {@link #handleMessage}, in the thread attached to this handler. |
| 461 | * |
| 462 | * @return Returns true if the message was successfully placed in to the |
| 463 | * message queue. Returns false on failure, usually because the |
| 464 | * looper processing the message queue is exiting. Note that a |
| 465 | * result of true does not mean the message will be processed -- if |
| 466 | * the looper is quit before the delivery time of the message |
| 467 | * occurs then the message will be dropped. |
| 468 | */ |
| 469 | public final boolean sendMessageDelayed(Message msg, long delayMillis) |
| 470 | { |
| 471 | if (delayMillis < 0) { |
| 472 | delayMillis = 0; |
| 473 | } |
| 474 | return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis); |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Enqueue a message into the message queue after all pending messages |
| 479 | * before the absolute time (in milliseconds) <var>uptimeMillis</var>. |
| 480 | * <b>The time-base is {@link android.os.SystemClock#uptimeMillis}.</b> |
| 481 | * You will receive it in {@link #handleMessage}, in the thread attached |
| 482 | * to this handler. |
| 483 | * |
| 484 | * @param uptimeMillis The absolute time at which the message should be |
| 485 | * delivered, using the |
| 486 | * {@link android.os.SystemClock#uptimeMillis} time-base. |
| 487 | * |
| 488 | * @return Returns true if the message was successfully placed in to the |
| 489 | * message queue. Returns false on failure, usually because the |
| 490 | * looper processing the message queue is exiting. Note that a |
| 491 | * result of true does not mean the message will be processed -- if |
| 492 | * the looper is quit before the delivery time of the message |
| 493 | * occurs then the message will be dropped. |
| 494 | */ |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame^] | 495 | public boolean sendMessageAtTime(Message msg, long uptimeMillis) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | MessageQueue queue = mQueue; |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame^] | 497 | if (queue == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | RuntimeException e = new RuntimeException( |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame^] | 499 | this + " sendMessageAtTime() called with no mQueue"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 | Log.w("Looper", e.getMessage(), e); |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame^] | 501 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | } |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame^] | 503 | return enqueueMessage(queue, msg, uptimeMillis); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Enqueue a message at the front of the message queue, to be processed on |
| 508 | * the next iteration of the message loop. You will receive it in |
| 509 | * {@link #handleMessage}, in the thread attached to this handler. |
| 510 | * <b>This method is only for use in very special circumstances -- it |
| 511 | * can easily starve the message queue, cause ordering problems, or have |
| 512 | * other unexpected side-effects.</b> |
| 513 | * |
| 514 | * @return Returns true if the message was successfully placed in to the |
| 515 | * message queue. Returns false on failure, usually because the |
| 516 | * looper processing the message queue is exiting. |
| 517 | */ |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame^] | 518 | public final boolean sendMessageAtFrontOfQueue(Message msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 519 | MessageQueue queue = mQueue; |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame^] | 520 | if (queue == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 521 | RuntimeException e = new RuntimeException( |
| 522 | this + " sendMessageAtTime() called with no mQueue"); |
| 523 | Log.w("Looper", e.getMessage(), e); |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame^] | 524 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 525 | } |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame^] | 526 | return enqueueMessage(queue, msg, 0); |
| 527 | } |
| 528 | |
| 529 | private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) { |
| 530 | msg.target = this; |
| 531 | if (mAsynchronous) { |
| 532 | msg.setAsynchronous(true); |
| 533 | } |
| 534 | return queue.enqueueMessage(msg, uptimeMillis); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Remove any pending posts of messages with code 'what' that are in the |
| 539 | * message queue. |
| 540 | */ |
| 541 | public final void removeMessages(int what) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 542 | mQueue.removeMessages(this, what, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Remove any pending posts of messages with code 'what' and whose obj is |
Jeff Brown | 32c8113 | 2012-04-30 16:28:32 -0700 | [diff] [blame] | 547 | * 'object' that are in the message queue. If <var>object</var> is null, |
Dianne Hackborn | 466ed24 | 2011-07-21 18:16:31 -0700 | [diff] [blame] | 548 | * all messages will be removed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | */ |
| 550 | public final void removeMessages(int what, Object object) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 551 | mQueue.removeMessages(this, what, object); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Remove any pending posts of callbacks and sent messages whose |
Dianne Hackborn | 466ed24 | 2011-07-21 18:16:31 -0700 | [diff] [blame] | 556 | * <var>obj</var> is <var>token</var>. If <var>token</var> is null, |
| 557 | * all callbacks and messages will be removed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | */ |
| 559 | public final void removeCallbacksAndMessages(Object token) { |
| 560 | mQueue.removeCallbacksAndMessages(this, token); |
| 561 | } |
| 562 | |
| 563 | /** |
| 564 | * Check if there are any pending posts of messages with code 'what' in |
| 565 | * the message queue. |
| 566 | */ |
| 567 | public final boolean hasMessages(int what) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 568 | return mQueue.hasMessages(this, what, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /** |
| 572 | * Check if there are any pending posts of messages with code 'what' and |
| 573 | * whose obj is 'object' in the message queue. |
| 574 | */ |
| 575 | public final boolean hasMessages(int what, Object object) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 576 | return mQueue.hasMessages(this, what, object); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | } |
| 578 | |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 579 | /** |
| 580 | * Check if there are any pending posts of messages with callback r in |
| 581 | * the message queue. |
| 582 | * |
| 583 | * @hide |
| 584 | */ |
| 585 | public final boolean hasCallbacks(Runnable r) { |
| 586 | return mQueue.hasMessages(this, r, null); |
| 587 | } |
| 588 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 589 | // if we can get rid of this method, the handler need not remember its loop |
| 590 | // we could instead export a getMessageQueue() method... |
| 591 | public final Looper getLooper() { |
| 592 | return mLooper; |
| 593 | } |
| 594 | |
| 595 | public final void dump(Printer pw, String prefix) { |
| 596 | pw.println(prefix + this + " @ " + SystemClock.uptimeMillis()); |
| 597 | if (mLooper == null) { |
| 598 | pw.println(prefix + "looper uninitialized"); |
| 599 | } else { |
| 600 | mLooper.dump(pw, prefix + " "); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | @Override |
| 605 | public String toString() { |
Kristian Monsen | 588d856 | 2011-08-04 12:55:33 +0100 | [diff] [blame] | 606 | return "Handler (" + getClass().getName() + ") {" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 607 | + Integer.toHexString(System.identityHashCode(this)) |
| 608 | + "}"; |
| 609 | } |
| 610 | |
| 611 | final IMessenger getIMessenger() { |
| 612 | synchronized (mQueue) { |
| 613 | if (mMessenger != null) { |
| 614 | return mMessenger; |
| 615 | } |
| 616 | mMessenger = new MessengerImpl(); |
| 617 | return mMessenger; |
| 618 | } |
| 619 | } |
Brad Fitzpatrick | 333b8cb | 2010-08-26 12:04:57 -0700 | [diff] [blame] | 620 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 621 | private final class MessengerImpl extends IMessenger.Stub { |
| 622 | public void send(Message msg) { |
| 623 | Handler.this.sendMessage(msg); |
| 624 | } |
| 625 | } |
Brad Fitzpatrick | 333b8cb | 2010-08-26 12:04:57 -0700 | [diff] [blame] | 626 | |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 627 | private static Message getPostMessage(Runnable r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | Message m = Message.obtain(); |
| 629 | m.callback = r; |
| 630 | return m; |
| 631 | } |
| 632 | |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 633 | private static Message getPostMessage(Runnable r, Object token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 634 | Message m = Message.obtain(); |
| 635 | m.obj = token; |
| 636 | m.callback = r; |
| 637 | return m; |
| 638 | } |
| 639 | |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 640 | private static void handleCallback(Message message) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | message.callback.run(); |
| 642 | } |
| 643 | |
| 644 | final MessageQueue mQueue; |
| 645 | final Looper mLooper; |
| 646 | final Callback mCallback; |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame^] | 647 | final boolean mAsynchronous; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 648 | IMessenger mMessenger; |
| 649 | } |