blob: 9af9edae9a3f595c787363465094441de0bc3cbc [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.os;
18
Eugene Suslaa38fbf62017-03-14 10:26:10 -070019import android.annotation.NonNull;
20import android.annotation.Nullable;
Andrei Onea24ec3212019-03-15 17:35:05 +000021import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.util.Log;
23import android.util.Printer;
24
25import java.lang.reflect.Modifier;
26
27/**
28 * A Handler allows you to send and process {@link Message} and Runnable
29 * objects associated with a thread's {@link MessageQueue}. Each Handler
30 * instance is associated with a single thread and that thread's message
31 * queue. When you create a new Handler, it is bound to the thread /
32 * message queue of the thread that is creating it -- from that point on,
33 * it will deliver messages and runnables to that message queue and execute
34 * them as they come out of the message queue.
35 *
36 * <p>There are two main uses for a Handler: (1) to schedule messages and
kopriva9b5c0392018-09-10 14:02:58 -070037 * runnables to be executed at some point in the future; and (2) to enqueue
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 * an action to be performed on a different thread than your own.
39 *
40 * <p>Scheduling messages is accomplished with the
41 * {@link #post}, {@link #postAtTime(Runnable, long)},
42 * {@link #postDelayed}, {@link #sendEmptyMessage},
43 * {@link #sendMessage}, {@link #sendMessageAtTime}, and
44 * {@link #sendMessageDelayed} methods. The <em>post</em> versions allow
45 * you to enqueue Runnable objects to be called by the message queue when
46 * they are received; the <em>sendMessage</em> versions allow you to enqueue
47 * a {@link Message} object containing a bundle of data that will be
48 * processed by the Handler's {@link #handleMessage} method (requiring that
49 * you implement a subclass of Handler).
50 *
51 * <p>When posting or sending to a Handler, you can either
52 * allow the item to be processed as soon as the message queue is ready
53 * to do so, or specify a delay before it gets processed or absolute time for
54 * it to be processed. The latter two allow you to implement timeouts,
55 * ticks, and other timing-based behavior.
56 *
57 * <p>When a
58 * process is created for your application, its main thread is dedicated to
59 * running a message queue that takes care of managing the top-level
60 * application objects (activities, broadcast receivers, etc) and any windows
61 * they create. You can create your own threads, and communicate back with
62 * the main application thread through a Handler. This is done by calling
63 * the same <em>post</em> or <em>sendMessage</em> methods as before, but from
Chris Palmer42855142010-09-09 17:04:31 -070064 * your new thread. The given Runnable or Message will then be scheduled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 * in the Handler's message queue and processed when appropriate.
66 */
67public class Handler {
68 /*
69 * Set this flag to true to detect anonymous, local or member classes
70 * that extend this Handler class and that are not static. These kind
71 * of classes can potentially create leaks.
72 */
73 private static final boolean FIND_POTENTIAL_LEAKS = false;
74 private static final String TAG = "Handler";
Eugene Suslaa38fbf62017-03-14 10:26:10 -070075 private static Handler MAIN_THREAD_HANDLER = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076
77 /**
78 * Callback interface you can use when instantiating a Handler to avoid
79 * having to implement your own subclass of Handler.
80 */
81 public interface Callback {
Pavel Grafovdcc53572017-03-10 19:39:14 +000082 /**
83 * @param msg A {@link android.os.Message Message} object
84 * @return True if no further handling is desired
85 */
Jake Wharton720530b2019-03-08 12:05:53 -050086 boolean handleMessage(@NonNull Message msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 }
88
89 /**
90 * Subclasses must implement this to receive messages.
91 */
Jake Wharton720530b2019-03-08 12:05:53 -050092 public void handleMessage(@NonNull Message msg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 }
94
95 /**
96 * Handle system messages here.
97 */
Jake Wharton720530b2019-03-08 12:05:53 -050098 public void dispatchMessage(@NonNull Message msg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 if (msg.callback != null) {
100 handleCallback(msg);
101 } else {
102 if (mCallback != null) {
103 if (mCallback.handleMessage(msg)) {
104 return;
105 }
106 }
107 handleMessage(msg);
108 }
109 }
110
111 /**
Jeff Browna2910d02012-08-25 12:29:46 -0700112 * Default constructor associates this handler with the {@link Looper} for the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 * current thread.
114 *
Jeff Browna2910d02012-08-25 12:29:46 -0700115 * If this thread does not have a looper, this handler won't be able to receive messages
116 * so an exception is thrown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 */
118 public Handler() {
Jeff Browna2910d02012-08-25 12:29:46 -0700119 this(null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 }
121
122 /**
Jeff Browna2910d02012-08-25 12:29:46 -0700123 * Constructor associates this handler with the {@link Looper} for the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 * current thread and takes a callback interface in which you can handle
125 * messages.
Jeff Browna2910d02012-08-25 12:29:46 -0700126 *
127 * If this thread does not have a looper, this handler won't be able to receive messages
128 * so an exception is thrown.
129 *
130 * @param callback The callback interface in which to handle messages, or null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 */
Jake Wharton720530b2019-03-08 12:05:53 -0500132 public Handler(@Nullable Callback callback) {
Jeff Browna2910d02012-08-25 12:29:46 -0700133 this(callback, false);
134 }
135
136 /**
137 * Use the provided {@link Looper} instead of the default one.
138 *
139 * @param looper The looper, must not be null.
140 */
Jake Wharton720530b2019-03-08 12:05:53 -0500141 public Handler(@NonNull Looper looper) {
Jeff Browna2910d02012-08-25 12:29:46 -0700142 this(looper, null, false);
143 }
144
145 /**
146 * Use the provided {@link Looper} instead of the default one and take a callback
147 * interface in which to handle messages.
148 *
149 * @param looper The looper, must not be null.
150 * @param callback The callback interface in which to handle messages, or null.
151 */
Jake Wharton720530b2019-03-08 12:05:53 -0500152 public Handler(@NonNull Looper looper, @Nullable Callback callback) {
Jeff Browna2910d02012-08-25 12:29:46 -0700153 this(looper, callback, false);
154 }
155
156 /**
157 * Use the {@link Looper} for the current thread
158 * and set whether the handler should be asynchronous.
159 *
160 * Handlers are synchronous by default unless this constructor is used to make
161 * one that is strictly asynchronous.
162 *
163 * Asynchronous messages represent interrupts or events that do not require global ordering
Jeff Brown9840c072014-11-11 20:21:21 -0800164 * with respect to synchronous messages. Asynchronous messages are not subject to
Jeff Browna2910d02012-08-25 12:29:46 -0700165 * the synchronization barriers introduced by {@link MessageQueue#enqueueSyncBarrier(long)}.
166 *
167 * @param async If true, the handler calls {@link Message#setAsynchronous(boolean)} for
168 * each {@link Message} that is sent to it or {@link Runnable} that is posted to it.
169 *
170 * @hide
171 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000172 @UnsupportedAppUsage
Jeff Browna2910d02012-08-25 12:29:46 -0700173 public Handler(boolean async) {
174 this(null, async);
175 }
176
177 /**
178 * Use the {@link Looper} for the current thread with the specified callback interface
179 * and set whether the handler should be asynchronous.
180 *
181 * Handlers are synchronous by default unless this constructor is used to make
182 * one that is strictly asynchronous.
183 *
184 * Asynchronous messages represent interrupts or events that do not require global ordering
Jeff Brown9840c072014-11-11 20:21:21 -0800185 * with respect to synchronous messages. Asynchronous messages are not subject to
Jeff Browna2910d02012-08-25 12:29:46 -0700186 * the synchronization barriers introduced by {@link MessageQueue#enqueueSyncBarrier(long)}.
187 *
188 * @param callback The callback interface in which to handle messages, or null.
189 * @param async If true, the handler calls {@link Message#setAsynchronous(boolean)} for
190 * each {@link Message} that is sent to it or {@link Runnable} that is posted to it.
191 *
192 * @hide
193 */
Jake Wharton720530b2019-03-08 12:05:53 -0500194 public Handler(@Nullable Callback callback, boolean async) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 if (FIND_POTENTIAL_LEAKS) {
196 final Class<? extends Handler> klass = getClass();
197 if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
198 (klass.getModifiers() & Modifier.STATIC) == 0) {
199 Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
200 klass.getCanonicalName());
201 }
202 }
203
204 mLooper = Looper.myLooper();
205 if (mLooper == null) {
206 throw new RuntimeException(
Eugene Susla8f07ee12017-11-14 17:41:03 -0800207 "Can't create handler inside thread " + Thread.currentThread()
208 + " that has not called Looper.prepare()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 }
210 mQueue = mLooper.mQueue;
211 mCallback = callback;
Jeff Browna2910d02012-08-25 12:29:46 -0700212 mAsynchronous = async;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 }
214
215 /**
Jeff Browna2910d02012-08-25 12:29:46 -0700216 * Use the provided {@link Looper} instead of the default one and take a callback
Jeff Brown109025d2012-08-14 20:41:30 -0700217 * interface in which to handle messages. Also set whether the handler
218 * should be asynchronous.
219 *
220 * Handlers are synchronous by default unless this constructor is used to make
221 * one that is strictly asynchronous.
222 *
223 * Asynchronous messages represent interrupts or events that do not require global ordering
Jeff Brown9840c072014-11-11 20:21:21 -0800224 * with respect to synchronous messages. Asynchronous messages are not subject to
Adam Powell8709ba82018-02-21 10:18:25 -0800225 * the synchronization barriers introduced by conditions such as display vsync.
Jeff Brown109025d2012-08-14 20:41:30 -0700226 *
Jeff Browna2910d02012-08-25 12:29:46 -0700227 * @param looper The looper, must not be null.
228 * @param callback The callback interface in which to handle messages, or null.
Jeff Brown109025d2012-08-14 20:41:30 -0700229 * @param async If true, the handler calls {@link Message#setAsynchronous(boolean)} for
230 * each {@link Message} that is sent to it or {@link Runnable} that is posted to it.
231 *
232 * @hide
233 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000234 @UnsupportedAppUsage
Jake Wharton720530b2019-03-08 12:05:53 -0500235 public Handler(@NonNull Looper looper, @Nullable Callback callback, boolean async) {
Jeff Brown109025d2012-08-14 20:41:30 -0700236 mLooper = looper;
237 mQueue = looper.mQueue;
238 mCallback = callback;
239 mAsynchronous = async;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 }
241
Adam Powell8709ba82018-02-21 10:18:25 -0800242 /**
243 * Create a new Handler whose posted messages and runnables are not subject to
244 * synchronization barriers such as display vsync.
245 *
246 * <p>Messages sent to an async handler are guaranteed to be ordered with respect to one another,
247 * but not necessarily with respect to messages from other Handlers.</p>
248 *
249 * @see #createAsync(Looper, Callback) to create an async Handler with custom message handling.
250 *
251 * @param looper the Looper that the new Handler should be bound to
252 * @return a new async Handler instance
253 */
254 @NonNull
255 public static Handler createAsync(@NonNull Looper looper) {
256 if (looper == null) throw new NullPointerException("looper must not be null");
257 return new Handler(looper, null, true);
258 }
259
260 /**
261 * Create a new Handler whose posted messages and runnables are not subject to
262 * synchronization barriers such as display vsync.
263 *
264 * <p>Messages sent to an async handler are guaranteed to be ordered with respect to one another,
265 * but not necessarily with respect to messages from other Handlers.</p>
266 *
267 * @see #createAsync(Looper) to create an async Handler without custom message handling.
268 *
269 * @param looper the Looper that the new Handler should be bound to
270 * @return a new async Handler instance
271 */
272 @NonNull
273 public static Handler createAsync(@NonNull Looper looper, @NonNull Callback callback) {
274 if (looper == null) throw new NullPointerException("looper must not be null");
275 if (callback == null) throw new NullPointerException("callback must not be null");
276 return new Handler(looper, callback, true);
277 }
278
Eugene Suslaa38fbf62017-03-14 10:26:10 -0700279 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000280 @UnsupportedAppUsage
Eugene Suslaa38fbf62017-03-14 10:26:10 -0700281 @NonNull
282 public static Handler getMain() {
283 if (MAIN_THREAD_HANDLER == null) {
284 MAIN_THREAD_HANDLER = new Handler(Looper.getMainLooper());
285 }
286 return MAIN_THREAD_HANDLER;
287 }
288
289 /** @hide */
290 @NonNull
291 public static Handler mainIfNull(@Nullable Handler handler) {
292 return handler == null ? getMain() : handler;
293 }
294
Jeff Sharkey74cd3de2016-04-06 17:40:54 -0600295 /** {@hide} */
Jake Wharton720530b2019-03-08 12:05:53 -0500296 @NonNull
297 public String getTraceName(@NonNull Message message) {
Jeff Sharkey74cd3de2016-04-06 17:40:54 -0600298 final StringBuilder sb = new StringBuilder();
299 sb.append(getClass().getName()).append(": ");
300 if (message.callback != null) {
301 sb.append(message.callback.getClass().getName());
302 } else {
303 sb.append("#").append(message.what);
304 }
305 return sb.toString();
306 }
307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 /**
Romain Guyf9284692011-07-13 18:46:21 -0700309 * Returns a string representing the name of the specified message.
310 * The default implementation will either return the class name of the
311 * message callback if any, or the hexadecimal representation of the
312 * message "what" field.
313 *
314 * @param message The message whose name is being queried
315 */
Jake Wharton720530b2019-03-08 12:05:53 -0500316 @NonNull
317 public String getMessageName(@NonNull Message message) {
Romain Guyf9284692011-07-13 18:46:21 -0700318 if (message.callback != null) {
319 return message.callback.getClass().getName();
320 }
321 return "0x" + Integer.toHexString(message.what);
322 }
323
324 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 * Returns a new {@link android.os.Message Message} from the global message pool. More efficient than
326 * creating and allocating new instances. The retrieved message has its handler set to this instance (Message.target == this).
327 * If you don't want that facility, just call Message.obtain() instead.
328 */
Jake Wharton720530b2019-03-08 12:05:53 -0500329 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 public final Message obtainMessage()
331 {
332 return Message.obtain(this);
333 }
334
335 /**
336 * Same as {@link #obtainMessage()}, except that it also sets the what member of the returned Message.
337 *
338 * @param what Value to assign to the returned Message.what field.
339 * @return A Message from the global message pool.
340 */
Jake Wharton720530b2019-03-08 12:05:53 -0500341 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 public final Message obtainMessage(int what)
343 {
344 return Message.obtain(this, what);
345 }
346
347 /**
348 *
349 * Same as {@link #obtainMessage()}, except that it also sets the what and obj members
350 * of the returned Message.
351 *
352 * @param what Value to assign to the returned Message.what field.
353 * @param obj Value to assign to the returned Message.obj field.
354 * @return A Message from the global message pool.
355 */
Jake Wharton720530b2019-03-08 12:05:53 -0500356 @NonNull
357 public final Message obtainMessage(int what, @Nullable Object obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 return Message.obtain(this, what, obj);
359 }
360
361 /**
362 *
363 * Same as {@link #obtainMessage()}, except that it also sets the what, arg1 and arg2 members of the returned
364 * Message.
365 * @param what Value to assign to the returned Message.what field.
366 * @param arg1 Value to assign to the returned Message.arg1 field.
367 * @param arg2 Value to assign to the returned Message.arg2 field.
368 * @return A Message from the global message pool.
369 */
Jake Wharton720530b2019-03-08 12:05:53 -0500370 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 public final Message obtainMessage(int what, int arg1, int arg2)
372 {
373 return Message.obtain(this, what, arg1, arg2);
374 }
375
376 /**
377 *
378 * Same as {@link #obtainMessage()}, except that it also sets the what, obj, arg1,and arg2 values on the
379 * returned Message.
380 * @param what Value to assign to the returned Message.what field.
381 * @param arg1 Value to assign to the returned Message.arg1 field.
382 * @param arg2 Value to assign to the returned Message.arg2 field.
383 * @param obj Value to assign to the returned Message.obj field.
384 * @return A Message from the global message pool.
385 */
Jake Wharton720530b2019-03-08 12:05:53 -0500386 @NonNull
387 public final Message obtainMessage(int what, int arg1, int arg2, @Nullable Object obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 return Message.obtain(this, what, arg1, arg2, obj);
389 }
390
391 /**
392 * Causes the Runnable r to be added to the message queue.
393 * The runnable will be run on the thread to which this handler is
394 * attached.
395 *
396 * @param r The Runnable that will be executed.
397 *
398 * @return Returns true if the Runnable was successfully placed in to the
399 * message queue. Returns false on failure, usually because the
400 * looper processing the message queue is exiting.
401 */
Jake Wharton720530b2019-03-08 12:05:53 -0500402 public final boolean post(@NonNull Runnable r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 return sendMessageDelayed(getPostMessage(r), 0);
404 }
405
406 /**
407 * Causes the Runnable r to be added to the message queue, to be run
408 * at a specific time given by <var>uptimeMillis</var>.
409 * <b>The time-base is {@link android.os.SystemClock#uptimeMillis}.</b>
David Christiea8cf4f22013-12-19 18:30:06 -0800410 * Time spent in deep sleep will add an additional delay to execution.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 * The runnable will be run on the thread to which this handler is attached.
412 *
413 * @param r The Runnable that will be executed.
414 * @param uptimeMillis The absolute time at which the callback should run,
415 * using the {@link android.os.SystemClock#uptimeMillis} time-base.
416 *
417 * @return Returns true if the Runnable was successfully placed in to the
418 * message queue. Returns false on failure, usually because the
419 * looper processing the message queue is exiting. Note that a
420 * result of true does not mean the Runnable will be processed -- if
421 * the looper is quit before the delivery time of the message
422 * occurs then the message will be dropped.
423 */
Jake Wharton720530b2019-03-08 12:05:53 -0500424 public final boolean postAtTime(@NonNull Runnable r, long uptimeMillis) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 return sendMessageAtTime(getPostMessage(r), uptimeMillis);
426 }
427
428 /**
429 * Causes the Runnable r to be added to the message queue, to be run
430 * at a specific time given by <var>uptimeMillis</var>.
431 * <b>The time-base is {@link android.os.SystemClock#uptimeMillis}.</b>
David Christiea8cf4f22013-12-19 18:30:06 -0800432 * Time spent in deep sleep will add an additional delay to execution.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 * The runnable will be run on the thread to which this handler is attached.
434 *
435 * @param r The Runnable that will be executed.
Jake Wharton820e3dd2018-01-02 22:18:24 -0500436 * @param token An instance which can be used to cancel {@code r} via
437 * {@link #removeCallbacksAndMessages}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 * @param uptimeMillis The absolute time at which the callback should run,
439 * using the {@link android.os.SystemClock#uptimeMillis} time-base.
440 *
441 * @return Returns true if the Runnable was successfully placed in to the
442 * message queue. Returns false on failure, usually because the
443 * looper processing the message queue is exiting. Note that a
444 * result of true does not mean the Runnable will be processed -- if
445 * the looper is quit before the delivery time of the message
446 * occurs then the message will be dropped.
447 *
448 * @see android.os.SystemClock#uptimeMillis
449 */
Jake Wharton720530b2019-03-08 12:05:53 -0500450 public final boolean postAtTime(
451 @NonNull Runnable r, @Nullable Object token, long uptimeMillis) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 return sendMessageAtTime(getPostMessage(r, token), uptimeMillis);
453 }
454
455 /**
456 * Causes the Runnable r to be added to the message queue, to be run
457 * after the specified amount of time elapses.
458 * The runnable will be run on the thread to which this handler
459 * is attached.
David Christiea8cf4f22013-12-19 18:30:06 -0800460 * <b>The time-base is {@link android.os.SystemClock#uptimeMillis}.</b>
461 * Time spent in deep sleep will add an additional delay to execution.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 *
463 * @param r The Runnable that will be executed.
464 * @param delayMillis The delay (in milliseconds) until the Runnable
465 * will be executed.
466 *
467 * @return Returns true if the Runnable was successfully placed in to the
468 * message queue. Returns false on failure, usually because the
469 * looper processing the message queue is exiting. Note that a
470 * result of true does not mean the Runnable will be processed --
471 * if the looper is quit before the delivery time of the message
472 * occurs then the message will be dropped.
473 */
Jake Wharton720530b2019-03-08 12:05:53 -0500474 public final boolean postDelayed(@NonNull Runnable r, long delayMillis) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 return sendMessageDelayed(getPostMessage(r), delayMillis);
476 }
477
Felipe Leme3fe6e922019-02-04 17:52:27 -0800478 /** @hide */
479 public final boolean postDelayed(Runnable r, int what, long delayMillis) {
480 return sendMessageDelayed(getPostMessage(r).setWhat(what), delayMillis);
481 }
482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 /**
Jake Wharton820e3dd2018-01-02 22:18:24 -0500484 * Causes the Runnable r to be added to the message queue, to be run
485 * after the specified amount of time elapses.
486 * The runnable will be run on the thread to which this handler
487 * is attached.
488 * <b>The time-base is {@link android.os.SystemClock#uptimeMillis}.</b>
489 * Time spent in deep sleep will add an additional delay to execution.
490 *
491 * @param r The Runnable that will be executed.
492 * @param token An instance which can be used to cancel {@code r} via
493 * {@link #removeCallbacksAndMessages}.
494 * @param delayMillis The delay (in milliseconds) until the Runnable
495 * will be executed.
496 *
497 * @return Returns true if the Runnable was successfully placed in to the
498 * message queue. Returns false on failure, usually because the
499 * looper processing the message queue is exiting. Note that a
500 * result of true does not mean the Runnable will be processed --
501 * if the looper is quit before the delivery time of the message
502 * occurs then the message will be dropped.
503 */
Jake Wharton720530b2019-03-08 12:05:53 -0500504 public final boolean postDelayed(
505 @NonNull Runnable r, @Nullable Object token, long delayMillis) {
Jake Wharton820e3dd2018-01-02 22:18:24 -0500506 return sendMessageDelayed(getPostMessage(r, token), delayMillis);
507 }
508
509 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 * Posts a message to an object that implements Runnable.
511 * Causes the Runnable r to executed on the next iteration through the
512 * message queue. The runnable will be run on the thread to which this
513 * handler is attached.
514 * <b>This method is only for use in very special circumstances -- it
515 * can easily starve the message queue, cause ordering problems, or have
516 * other unexpected side-effects.</b>
517 *
518 * @param r The Runnable that will be executed.
519 *
520 * @return Returns true if the message was successfully placed in to the
521 * message queue. Returns false on failure, usually because the
522 * looper processing the message queue is exiting.
523 */
Jake Wharton720530b2019-03-08 12:05:53 -0500524 public final boolean postAtFrontOfQueue(@NonNull Runnable r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 return sendMessageAtFrontOfQueue(getPostMessage(r));
526 }
527
528 /**
Jeff Brownc53abc42012-08-29 04:43:25 -0700529 * Runs the specified task synchronously.
Jeff Brown8b60e452013-04-18 15:17:48 -0700530 * <p>
Jeff Brownc53abc42012-08-29 04:43:25 -0700531 * If the current thread is the same as the handler thread, then the runnable
532 * runs immediately without being enqueued. Otherwise, posts the runnable
533 * to the handler and waits for it to complete before returning.
Jeff Brown8b60e452013-04-18 15:17:48 -0700534 * </p><p>
Jeff Brownc53abc42012-08-29 04:43:25 -0700535 * This method is dangerous! Improper use can result in deadlocks.
536 * Never call this method while any locks are held or use it in a
537 * possibly re-entrant manner.
Jeff Brown8b60e452013-04-18 15:17:48 -0700538 * </p><p>
Jeff Brownc53abc42012-08-29 04:43:25 -0700539 * This method is occasionally useful in situations where a background thread
540 * must synchronously await completion of a task that must run on the
541 * handler's thread. However, this problem is often a symptom of bad design.
542 * Consider improving the design (if possible) before resorting to this method.
Jeff Brown8b60e452013-04-18 15:17:48 -0700543 * </p><p>
Jeff Brownc53abc42012-08-29 04:43:25 -0700544 * One example of where you might want to use this method is when you just
545 * set up a Handler thread and need to perform some initialization steps on
546 * it before continuing execution.
Jeff Brown8b60e452013-04-18 15:17:48 -0700547 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700548 * If timeout occurs then this method returns <code>false</code> but the runnable
549 * will remain posted on the handler and may already be in progress or
550 * complete at a later time.
Jeff Brown8b60e452013-04-18 15:17:48 -0700551 * </p><p>
552 * When using this method, be sure to use {@link Looper#quitSafely} when
553 * quitting the looper. Otherwise {@link #runWithScissors} may hang indefinitely.
554 * (TODO: We should fix this by making MessageQueue aware of blocking runnables.)
555 * </p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700556 *
Jeff Brownc53abc42012-08-29 04:43:25 -0700557 * @param r The Runnable that will be executed synchronously.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700558 * @param timeout The timeout in milliseconds, or 0 to wait indefinitely.
Jeff Brownc53abc42012-08-29 04:43:25 -0700559 *
560 * @return Returns true if the Runnable was successfully executed.
561 * Returns false on failure, usually because the
562 * looper processing the message queue is exiting.
563 *
564 * @hide This method is prone to abuse and should probably not be in the API.
565 * If we ever do make it part of the API, we might want to rename it to something
566 * less funny like runUnsafe().
567 */
Jake Wharton720530b2019-03-08 12:05:53 -0500568 public final boolean runWithScissors(@NonNull Runnable r, long timeout) {
Jeff Brownc53abc42012-08-29 04:43:25 -0700569 if (r == null) {
570 throw new IllegalArgumentException("runnable must not be null");
571 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700572 if (timeout < 0) {
573 throw new IllegalArgumentException("timeout must be non-negative");
574 }
Jeff Brownc53abc42012-08-29 04:43:25 -0700575
576 if (Looper.myLooper() == mLooper) {
577 r.run();
578 return true;
579 }
580
581 BlockingRunnable br = new BlockingRunnable(r);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700582 return br.postAndWait(this, timeout);
Jeff Brownc53abc42012-08-29 04:43:25 -0700583 }
584
585 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 * Remove any pending posts of Runnable r that are in the message queue.
587 */
Jake Wharton720530b2019-03-08 12:05:53 -0500588 public final void removeCallbacks(@NonNull Runnable r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 mQueue.removeMessages(this, r, null);
590 }
591
592 /**
593 * Remove any pending posts of Runnable <var>r</var> with Object
Dianne Hackborn466ed242011-07-21 18:16:31 -0700594 * <var>token</var> that are in the message queue. If <var>token</var> is null,
595 * all callbacks will be removed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 */
Jake Wharton720530b2019-03-08 12:05:53 -0500597 public final void removeCallbacks(@NonNull Runnable r, @Nullable Object token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 mQueue.removeMessages(this, r, token);
599 }
600
601 /**
602 * Pushes a message onto the end of the message queue after all pending messages
603 * before the current time. It will be received in {@link #handleMessage},
604 * in the thread attached to this handler.
605 *
606 * @return Returns true if the message was successfully placed in to the
607 * message queue. Returns false on failure, usually because the
608 * looper processing the message queue is exiting.
609 */
Jake Wharton720530b2019-03-08 12:05:53 -0500610 public final boolean sendMessage(@NonNull Message msg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 return sendMessageDelayed(msg, 0);
612 }
613
614 /**
615 * Sends a Message containing only the what value.
616 *
617 * @return Returns true if the message was successfully placed in to the
618 * message queue. Returns false on failure, usually because the
619 * looper processing the message queue is exiting.
620 */
621 public final boolean sendEmptyMessage(int what)
622 {
623 return sendEmptyMessageDelayed(what, 0);
624 }
625
626 /**
627 * Sends a Message containing only the what value, to be delivered
628 * after the specified amount of time elapses.
629 * @see #sendMessageDelayed(android.os.Message, long)
630 *
631 * @return Returns true if the message was successfully placed in to the
632 * message queue. Returns false on failure, usually because the
633 * looper processing the message queue is exiting.
634 */
635 public final boolean sendEmptyMessageDelayed(int what, long delayMillis) {
636 Message msg = Message.obtain();
637 msg.what = what;
638 return sendMessageDelayed(msg, delayMillis);
639 }
640
641 /**
642 * Sends a Message containing only the what value, to be delivered
643 * at a specific time.
644 * @see #sendMessageAtTime(android.os.Message, long)
645 *
646 * @return Returns true if the message was successfully placed in to the
647 * message queue. Returns false on failure, usually because the
648 * looper processing the message queue is exiting.
649 */
650
651 public final boolean sendEmptyMessageAtTime(int what, long uptimeMillis) {
652 Message msg = Message.obtain();
653 msg.what = what;
654 return sendMessageAtTime(msg, uptimeMillis);
655 }
656
657 /**
658 * Enqueue a message into the message queue after all pending messages
659 * before (current time + delayMillis). You will receive it in
660 * {@link #handleMessage}, in the thread attached to this handler.
661 *
662 * @return Returns true if the message was successfully placed in to the
663 * message queue. Returns false on failure, usually because the
664 * looper processing the message queue is exiting. Note that a
665 * result of true does not mean the message will be processed -- if
666 * the looper is quit before the delivery time of the message
667 * occurs then the message will be dropped.
668 */
Jake Wharton720530b2019-03-08 12:05:53 -0500669 public final boolean sendMessageDelayed(@NonNull Message msg, long delayMillis) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 if (delayMillis < 0) {
671 delayMillis = 0;
672 }
673 return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
674 }
675
676 /**
677 * Enqueue a message into the message queue after all pending messages
678 * before the absolute time (in milliseconds) <var>uptimeMillis</var>.
679 * <b>The time-base is {@link android.os.SystemClock#uptimeMillis}.</b>
David Christiea8cf4f22013-12-19 18:30:06 -0800680 * Time spent in deep sleep will add an additional delay to execution.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 * You will receive it in {@link #handleMessage}, in the thread attached
682 * to this handler.
683 *
684 * @param uptimeMillis The absolute time at which the message should be
685 * delivered, using the
686 * {@link android.os.SystemClock#uptimeMillis} time-base.
687 *
688 * @return Returns true if the message was successfully placed in to the
689 * message queue. Returns false on failure, usually because the
690 * looper processing the message queue is exiting. Note that a
691 * result of true does not mean the message will be processed -- if
692 * the looper is quit before the delivery time of the message
693 * occurs then the message will be dropped.
694 */
Jake Wharton720530b2019-03-08 12:05:53 -0500695 public boolean sendMessageAtTime(@NonNull Message msg, long uptimeMillis) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 MessageQueue queue = mQueue;
Jeff Brown109025d2012-08-14 20:41:30 -0700697 if (queue == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 RuntimeException e = new RuntimeException(
Jeff Brown109025d2012-08-14 20:41:30 -0700699 this + " sendMessageAtTime() called with no mQueue");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 Log.w("Looper", e.getMessage(), e);
Jeff Brown109025d2012-08-14 20:41:30 -0700701 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 }
Jeff Brown109025d2012-08-14 20:41:30 -0700703 return enqueueMessage(queue, msg, uptimeMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 }
705
706 /**
707 * Enqueue a message at the front of the message queue, to be processed on
708 * the next iteration of the message loop. You will receive it in
709 * {@link #handleMessage}, in the thread attached to this handler.
710 * <b>This method is only for use in very special circumstances -- it
711 * can easily starve the message queue, cause ordering problems, or have
712 * other unexpected side-effects.</b>
713 *
714 * @return Returns true if the message was successfully placed in to the
715 * message queue. Returns false on failure, usually because the
716 * looper processing the message queue is exiting.
717 */
Jake Wharton720530b2019-03-08 12:05:53 -0500718 public final boolean sendMessageAtFrontOfQueue(@NonNull Message msg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 MessageQueue queue = mQueue;
Jeff Brown109025d2012-08-14 20:41:30 -0700720 if (queue == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 RuntimeException e = new RuntimeException(
722 this + " sendMessageAtTime() called with no mQueue");
723 Log.w("Looper", e.getMessage(), e);
Jeff Brown109025d2012-08-14 20:41:30 -0700724 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 }
Jeff Brown109025d2012-08-14 20:41:30 -0700726 return enqueueMessage(queue, msg, 0);
727 }
728
Eugene Susla9f35ca92018-02-12 16:17:26 -0800729 /**
730 * Executes the message synchronously if called on the same thread this handler corresponds to,
731 * or {@link #sendMessage pushes it to the queue} otherwise
732 *
733 * @return Returns true if the message was successfully ran or placed in to the
734 * message queue. Returns false on failure, usually because the
735 * looper processing the message queue is exiting.
736 * @hide
737 */
Jake Wharton720530b2019-03-08 12:05:53 -0500738 public final boolean executeOrSendMessage(@NonNull Message msg) {
Eugene Susla9f35ca92018-02-12 16:17:26 -0800739 if (mLooper == Looper.myLooper()) {
740 dispatchMessage(msg);
741 return true;
742 }
743 return sendMessage(msg);
744 }
745
Jake Wharton720530b2019-03-08 12:05:53 -0500746 private boolean enqueueMessage(@NonNull MessageQueue queue, @NonNull Message msg,
747 long uptimeMillis) {
Jeff Brown109025d2012-08-14 20:41:30 -0700748 msg.target = this;
Olivier Gaillardd542b1c2018-11-14 15:24:35 +0000749 msg.workSourceUid = ThreadLocalWorkSource.getUid();
Marcin Oczeretkoec758722018-09-12 12:53:47 +0100750
Jeff Brown109025d2012-08-14 20:41:30 -0700751 if (mAsynchronous) {
752 msg.setAsynchronous(true);
753 }
754 return queue.enqueueMessage(msg, uptimeMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 }
756
757 /**
758 * Remove any pending posts of messages with code 'what' that are in the
759 * message queue.
760 */
761 public final void removeMessages(int what) {
Jeff Brown0f85ce32012-02-16 14:41:10 -0800762 mQueue.removeMessages(this, what, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 }
764
765 /**
766 * Remove any pending posts of messages with code 'what' and whose obj is
Jeff Brown32c81132012-04-30 16:28:32 -0700767 * 'object' that are in the message queue. If <var>object</var> is null,
Dianne Hackborn466ed242011-07-21 18:16:31 -0700768 * all messages will be removed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 */
Jake Wharton720530b2019-03-08 12:05:53 -0500770 public final void removeMessages(int what, @Nullable Object object) {
Jeff Brown0f85ce32012-02-16 14:41:10 -0800771 mQueue.removeMessages(this, what, object);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 }
773
774 /**
775 * Remove any pending posts of callbacks and sent messages whose
Dianne Hackborn466ed242011-07-21 18:16:31 -0700776 * <var>obj</var> is <var>token</var>. If <var>token</var> is null,
777 * all callbacks and messages will be removed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 */
Jake Wharton720530b2019-03-08 12:05:53 -0500779 public final void removeCallbacksAndMessages(@Nullable Object token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 mQueue.removeCallbacksAndMessages(this, token);
781 }
782
783 /**
784 * Check if there are any pending posts of messages with code 'what' in
785 * the message queue.
786 */
787 public final boolean hasMessages(int what) {
Jeff Brown0f85ce32012-02-16 14:41:10 -0800788 return mQueue.hasMessages(this, what, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 }
790
791 /**
Dianne Hackborncb015632017-06-14 17:30:15 -0700792 * Return whether there are any messages or callbacks currently scheduled on this handler.
793 * @hide
794 */
795 public final boolean hasMessagesOrCallbacks() {
796 return mQueue.hasMessages(this);
797 }
798
799 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 * Check if there are any pending posts of messages with code 'what' and
801 * whose obj is 'object' in the message queue.
802 */
Jake Wharton720530b2019-03-08 12:05:53 -0500803 public final boolean hasMessages(int what, @Nullable Object object) {
Jeff Brown0f85ce32012-02-16 14:41:10 -0800804 return mQueue.hasMessages(this, what, object);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 }
806
Romain Guyba6be8a2012-04-23 18:22:09 -0700807 /**
808 * Check if there are any pending posts of messages with callback r in
809 * the message queue.
Romain Guyba6be8a2012-04-23 18:22:09 -0700810 */
Jake Wharton720530b2019-03-08 12:05:53 -0500811 public final boolean hasCallbacks(@NonNull Runnable r) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700812 return mQueue.hasMessages(this, r, null);
813 }
814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 // if we can get rid of this method, the handler need not remember its loop
816 // we could instead export a getMessageQueue() method...
Jake Wharton720530b2019-03-08 12:05:53 -0500817 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 public final Looper getLooper() {
819 return mLooper;
820 }
821
Jake Wharton720530b2019-03-08 12:05:53 -0500822 public final void dump(@NonNull Printer pw, @NonNull String prefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 pw.println(prefix + this + " @ " + SystemClock.uptimeMillis());
824 if (mLooper == null) {
825 pw.println(prefix + "looper uninitialized");
826 } else {
827 mLooper.dump(pw, prefix + " ");
828 }
829 }
830
Dianne Hackborncb015632017-06-14 17:30:15 -0700831 /**
832 * @hide
833 */
Jake Wharton720530b2019-03-08 12:05:53 -0500834 public final void dumpMine(@NonNull Printer pw, @NonNull String prefix) {
Dianne Hackborncb015632017-06-14 17:30:15 -0700835 pw.println(prefix + this + " @ " + SystemClock.uptimeMillis());
836 if (mLooper == null) {
837 pw.println(prefix + "looper uninitialized");
838 } else {
839 mLooper.dump(pw, prefix + " ", this);
840 }
841 }
842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 @Override
844 public String toString() {
Kristian Monsen588d8562011-08-04 12:55:33 +0100845 return "Handler (" + getClass().getName() + ") {"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 + Integer.toHexString(System.identityHashCode(this))
847 + "}";
848 }
849
Andrei Onea24ec3212019-03-15 17:35:05 +0000850 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 final IMessenger getIMessenger() {
852 synchronized (mQueue) {
853 if (mMessenger != null) {
854 return mMessenger;
855 }
856 mMessenger = new MessengerImpl();
857 return mMessenger;
858 }
859 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 private final class MessengerImpl extends IMessenger.Stub {
862 public void send(Message msg) {
Dianne Hackborncb3ed1d2014-06-27 18:37:06 -0700863 msg.sendingUid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 Handler.this.sendMessage(msg);
865 }
866 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700867
Romain Guyba6be8a2012-04-23 18:22:09 -0700868 private static Message getPostMessage(Runnable r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 Message m = Message.obtain();
870 m.callback = r;
871 return m;
872 }
873
Andrei Onea24ec3212019-03-15 17:35:05 +0000874 @UnsupportedAppUsage
Romain Guyba6be8a2012-04-23 18:22:09 -0700875 private static Message getPostMessage(Runnable r, Object token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 Message m = Message.obtain();
877 m.obj = token;
878 m.callback = r;
879 return m;
880 }
881
Romain Guyba6be8a2012-04-23 18:22:09 -0700882 private static void handleCallback(Message message) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 message.callback.run();
884 }
885
Andrei Onea24ec3212019-03-15 17:35:05 +0000886 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 final Looper mLooper;
Jeff Sharkey74cd3de2016-04-06 17:40:54 -0600888 final MessageQueue mQueue;
Andrei Onea24ec3212019-03-15 17:35:05 +0000889 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 final Callback mCallback;
Jeff Brown109025d2012-08-14 20:41:30 -0700891 final boolean mAsynchronous;
Andrei Onea24ec3212019-03-15 17:35:05 +0000892 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 IMessenger mMessenger;
Jeff Brownc53abc42012-08-29 04:43:25 -0700894
895 private static final class BlockingRunnable implements Runnable {
896 private final Runnable mTask;
897 private boolean mDone;
898
899 public BlockingRunnable(Runnable task) {
900 mTask = task;
901 }
902
903 @Override
904 public void run() {
905 try {
906 mTask.run();
907 } finally {
908 synchronized (this) {
909 mDone = true;
910 notifyAll();
911 }
912 }
913 }
914
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700915 public boolean postAndWait(Handler handler, long timeout) {
Jeff Brownc53abc42012-08-29 04:43:25 -0700916 if (!handler.post(this)) {
917 return false;
918 }
919
920 synchronized (this) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700921 if (timeout > 0) {
922 final long expirationTime = SystemClock.uptimeMillis() + timeout;
923 while (!mDone) {
924 long delay = expirationTime - SystemClock.uptimeMillis();
925 if (delay <= 0) {
926 return false; // timeout
927 }
928 try {
929 wait(delay);
930 } catch (InterruptedException ex) {
931 }
932 }
933 } else {
934 while (!mDone) {
935 try {
936 wait();
937 } catch (InterruptedException ex) {
938 }
Jeff Brownc53abc42012-08-29 04:43:25 -0700939 }
940 }
941 }
942 return true;
943 }
944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945}