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 | |
Eugene Susla | a38fbf6 | 2017-03-14 10:26:10 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.util.Log; |
| 22 | import android.util.Printer; |
| 23 | |
| 24 | import java.lang.reflect.Modifier; |
| 25 | |
| 26 | /** |
| 27 | * A Handler allows you to send and process {@link Message} and Runnable |
| 28 | * objects associated with a thread's {@link MessageQueue}. Each Handler |
| 29 | * instance is associated with a single thread and that thread's message |
| 30 | * queue. When you create a new Handler, it is bound to the thread / |
| 31 | * message queue of the thread that is creating it -- from that point on, |
| 32 | * it will deliver messages and runnables to that message queue and execute |
| 33 | * them as they come out of the message queue. |
| 34 | * |
| 35 | * <p>There are two main uses for a Handler: (1) to schedule messages and |
kopriva | 9b5c039 | 2018-09-10 14:02:58 -0700 | [diff] [blame] | 36 | * runnables to be executed at some point in the future; and (2) to enqueue |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | * an action to be performed on a different thread than your own. |
| 38 | * |
| 39 | * <p>Scheduling messages is accomplished with the |
| 40 | * {@link #post}, {@link #postAtTime(Runnable, long)}, |
| 41 | * {@link #postDelayed}, {@link #sendEmptyMessage}, |
| 42 | * {@link #sendMessage}, {@link #sendMessageAtTime}, and |
| 43 | * {@link #sendMessageDelayed} methods. The <em>post</em> versions allow |
| 44 | * you to enqueue Runnable objects to be called by the message queue when |
| 45 | * they are received; the <em>sendMessage</em> versions allow you to enqueue |
| 46 | * a {@link Message} object containing a bundle of data that will be |
| 47 | * processed by the Handler's {@link #handleMessage} method (requiring that |
| 48 | * you implement a subclass of Handler). |
| 49 | * |
| 50 | * <p>When posting or sending to a Handler, you can either |
| 51 | * allow the item to be processed as soon as the message queue is ready |
| 52 | * to do so, or specify a delay before it gets processed or absolute time for |
| 53 | * it to be processed. The latter two allow you to implement timeouts, |
| 54 | * ticks, and other timing-based behavior. |
| 55 | * |
| 56 | * <p>When a |
| 57 | * process is created for your application, its main thread is dedicated to |
| 58 | * running a message queue that takes care of managing the top-level |
| 59 | * application objects (activities, broadcast receivers, etc) and any windows |
| 60 | * they create. You can create your own threads, and communicate back with |
| 61 | * the main application thread through a Handler. This is done by calling |
| 62 | * 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] | 63 | * 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] | 64 | * in the Handler's message queue and processed when appropriate. |
| 65 | */ |
| 66 | public class Handler { |
| 67 | /* |
| 68 | * Set this flag to true to detect anonymous, local or member classes |
| 69 | * that extend this Handler class and that are not static. These kind |
| 70 | * of classes can potentially create leaks. |
| 71 | */ |
| 72 | private static final boolean FIND_POTENTIAL_LEAKS = false; |
| 73 | private static final String TAG = "Handler"; |
Eugene Susla | a38fbf6 | 2017-03-14 10:26:10 -0700 | [diff] [blame] | 74 | private static Handler MAIN_THREAD_HANDLER = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Callback interface you can use when instantiating a Handler to avoid |
| 78 | * having to implement your own subclass of Handler. |
| 79 | */ |
| 80 | public interface Callback { |
Pavel Grafov | dcc5357 | 2017-03-10 19:39:14 +0000 | [diff] [blame] | 81 | /** |
| 82 | * @param msg A {@link android.os.Message Message} object |
| 83 | * @return True if no further handling is desired |
| 84 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 85 | boolean handleMessage(@NonNull Message msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Subclasses must implement this to receive messages. |
| 90 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 91 | public void handleMessage(@NonNull Message msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Handle system messages here. |
| 96 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 97 | public void dispatchMessage(@NonNull Message msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | if (msg.callback != null) { |
| 99 | handleCallback(msg); |
| 100 | } else { |
| 101 | if (mCallback != null) { |
| 102 | if (mCallback.handleMessage(msg)) { |
| 103 | return; |
| 104 | } |
| 105 | } |
| 106 | handleMessage(msg); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 111 | * Default constructor associates this handler with the {@link Looper} for the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | * current thread. |
| 113 | * |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 114 | * If this thread does not have a looper, this handler won't be able to receive messages |
| 115 | * so an exception is thrown. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | */ |
| 117 | public Handler() { |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 118 | this(null, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 122 | * Constructor associates this handler with the {@link Looper} for the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | * current thread and takes a callback interface in which you can handle |
| 124 | * messages. |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 125 | * |
| 126 | * If this thread does not have a looper, this handler won't be able to receive messages |
| 127 | * so an exception is thrown. |
| 128 | * |
| 129 | * @param callback The callback interface in which to handle messages, or null. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 131 | public Handler(@Nullable Callback callback) { |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 132 | this(callback, false); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Use the provided {@link Looper} instead of the default one. |
| 137 | * |
| 138 | * @param looper The looper, must not be null. |
| 139 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 140 | public Handler(@NonNull Looper looper) { |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 141 | this(looper, null, false); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Use the provided {@link Looper} instead of the default one and take a callback |
| 146 | * interface in which to handle messages. |
| 147 | * |
| 148 | * @param looper The looper, must not be null. |
| 149 | * @param callback The callback interface in which to handle messages, or null. |
| 150 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 151 | public Handler(@NonNull Looper looper, @Nullable Callback callback) { |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 152 | this(looper, callback, false); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Use the {@link Looper} for the current thread |
| 157 | * and set whether the handler should be asynchronous. |
| 158 | * |
| 159 | * Handlers are synchronous by default unless this constructor is used to make |
| 160 | * one that is strictly asynchronous. |
| 161 | * |
| 162 | * Asynchronous messages represent interrupts or events that do not require global ordering |
Jeff Brown | 9840c07 | 2014-11-11 20:21:21 -0800 | [diff] [blame] | 163 | * with respect to synchronous messages. Asynchronous messages are not subject to |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 164 | * the synchronization barriers introduced by {@link MessageQueue#enqueueSyncBarrier(long)}. |
| 165 | * |
| 166 | * @param async If true, the handler calls {@link Message#setAsynchronous(boolean)} for |
| 167 | * each {@link Message} that is sent to it or {@link Runnable} that is posted to it. |
| 168 | * |
| 169 | * @hide |
| 170 | */ |
| 171 | public Handler(boolean async) { |
| 172 | this(null, async); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Use the {@link Looper} for the current thread with the specified callback interface |
| 177 | * and set whether the handler should be asynchronous. |
| 178 | * |
| 179 | * Handlers are synchronous by default unless this constructor is used to make |
| 180 | * one that is strictly asynchronous. |
| 181 | * |
| 182 | * Asynchronous messages represent interrupts or events that do not require global ordering |
Jeff Brown | 9840c07 | 2014-11-11 20:21:21 -0800 | [diff] [blame] | 183 | * with respect to synchronous messages. Asynchronous messages are not subject to |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 184 | * the synchronization barriers introduced by {@link MessageQueue#enqueueSyncBarrier(long)}. |
| 185 | * |
| 186 | * @param callback The callback interface in which to handle messages, or null. |
| 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 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 192 | public Handler(@Nullable Callback callback, boolean async) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | if (FIND_POTENTIAL_LEAKS) { |
| 194 | final Class<? extends Handler> klass = getClass(); |
| 195 | if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) && |
| 196 | (klass.getModifiers() & Modifier.STATIC) == 0) { |
| 197 | Log.w(TAG, "The following Handler class should be static or leaks might occur: " + |
| 198 | klass.getCanonicalName()); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | mLooper = Looper.myLooper(); |
| 203 | if (mLooper == null) { |
| 204 | throw new RuntimeException( |
Eugene Susla | 8f07ee1 | 2017-11-14 17:41:03 -0800 | [diff] [blame] | 205 | "Can't create handler inside thread " + Thread.currentThread() |
| 206 | + " that has not called Looper.prepare()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | } |
| 208 | mQueue = mLooper.mQueue; |
| 209 | mCallback = callback; |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 210 | mAsynchronous = async; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /** |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 214 | * Use the provided {@link Looper} instead of the default one and take a callback |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 215 | * interface in which to handle messages. Also set whether the handler |
| 216 | * should be asynchronous. |
| 217 | * |
| 218 | * Handlers are synchronous by default unless this constructor is used to make |
| 219 | * one that is strictly asynchronous. |
| 220 | * |
| 221 | * Asynchronous messages represent interrupts or events that do not require global ordering |
Jeff Brown | 9840c07 | 2014-11-11 20:21:21 -0800 | [diff] [blame] | 222 | * with respect to synchronous messages. Asynchronous messages are not subject to |
Adam Powell | 8709ba8 | 2018-02-21 10:18:25 -0800 | [diff] [blame] | 223 | * the synchronization barriers introduced by conditions such as display vsync. |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 224 | * |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 225 | * @param looper The looper, must not be null. |
| 226 | * @param callback The callback interface in which to handle messages, or null. |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 227 | * @param async If true, the handler calls {@link Message#setAsynchronous(boolean)} for |
| 228 | * each {@link Message} that is sent to it or {@link Runnable} that is posted to it. |
| 229 | * |
| 230 | * @hide |
| 231 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 232 | public Handler(@NonNull Looper looper, @Nullable Callback callback, boolean async) { |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 233 | mLooper = looper; |
| 234 | mQueue = looper.mQueue; |
| 235 | mCallback = callback; |
| 236 | mAsynchronous = async; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Adam Powell | 8709ba8 | 2018-02-21 10:18:25 -0800 | [diff] [blame] | 239 | /** |
| 240 | * Create a new Handler whose posted messages and runnables are not subject to |
| 241 | * synchronization barriers such as display vsync. |
| 242 | * |
| 243 | * <p>Messages sent to an async handler are guaranteed to be ordered with respect to one another, |
| 244 | * but not necessarily with respect to messages from other Handlers.</p> |
| 245 | * |
| 246 | * @see #createAsync(Looper, Callback) to create an async Handler with custom message handling. |
| 247 | * |
| 248 | * @param looper the Looper that the new Handler should be bound to |
| 249 | * @return a new async Handler instance |
| 250 | */ |
| 251 | @NonNull |
| 252 | public static Handler createAsync(@NonNull Looper looper) { |
| 253 | if (looper == null) throw new NullPointerException("looper must not be null"); |
| 254 | return new Handler(looper, null, true); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Create a new Handler whose posted messages and runnables are not subject to |
| 259 | * synchronization barriers such as display vsync. |
| 260 | * |
| 261 | * <p>Messages sent to an async handler are guaranteed to be ordered with respect to one another, |
| 262 | * but not necessarily with respect to messages from other Handlers.</p> |
| 263 | * |
| 264 | * @see #createAsync(Looper) to create an async Handler without custom message handling. |
| 265 | * |
| 266 | * @param looper the Looper that the new Handler should be bound to |
| 267 | * @return a new async Handler instance |
| 268 | */ |
| 269 | @NonNull |
| 270 | public static Handler createAsync(@NonNull Looper looper, @NonNull Callback callback) { |
| 271 | if (looper == null) throw new NullPointerException("looper must not be null"); |
| 272 | if (callback == null) throw new NullPointerException("callback must not be null"); |
| 273 | return new Handler(looper, callback, true); |
| 274 | } |
| 275 | |
Eugene Susla | a38fbf6 | 2017-03-14 10:26:10 -0700 | [diff] [blame] | 276 | /** @hide */ |
| 277 | @NonNull |
| 278 | public static Handler getMain() { |
| 279 | if (MAIN_THREAD_HANDLER == null) { |
| 280 | MAIN_THREAD_HANDLER = new Handler(Looper.getMainLooper()); |
| 281 | } |
| 282 | return MAIN_THREAD_HANDLER; |
| 283 | } |
| 284 | |
| 285 | /** @hide */ |
| 286 | @NonNull |
| 287 | public static Handler mainIfNull(@Nullable Handler handler) { |
| 288 | return handler == null ? getMain() : handler; |
| 289 | } |
| 290 | |
Jeff Sharkey | 74cd3de | 2016-04-06 17:40:54 -0600 | [diff] [blame] | 291 | /** {@hide} */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 292 | @NonNull |
| 293 | public String getTraceName(@NonNull Message message) { |
Jeff Sharkey | 74cd3de | 2016-04-06 17:40:54 -0600 | [diff] [blame] | 294 | final StringBuilder sb = new StringBuilder(); |
| 295 | sb.append(getClass().getName()).append(": "); |
| 296 | if (message.callback != null) { |
| 297 | sb.append(message.callback.getClass().getName()); |
| 298 | } else { |
| 299 | sb.append("#").append(message.what); |
| 300 | } |
| 301 | return sb.toString(); |
| 302 | } |
| 303 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | /** |
Romain Guy | f928469 | 2011-07-13 18:46:21 -0700 | [diff] [blame] | 305 | * Returns a string representing the name of the specified message. |
| 306 | * The default implementation will either return the class name of the |
| 307 | * message callback if any, or the hexadecimal representation of the |
| 308 | * message "what" field. |
| 309 | * |
| 310 | * @param message The message whose name is being queried |
| 311 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 312 | @NonNull |
| 313 | public String getMessageName(@NonNull Message message) { |
Romain Guy | f928469 | 2011-07-13 18:46:21 -0700 | [diff] [blame] | 314 | if (message.callback != null) { |
| 315 | return message.callback.getClass().getName(); |
| 316 | } |
| 317 | return "0x" + Integer.toHexString(message.what); |
| 318 | } |
| 319 | |
| 320 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | * Returns a new {@link android.os.Message Message} from the global message pool. More efficient than |
| 322 | * creating and allocating new instances. The retrieved message has its handler set to this instance (Message.target == this). |
| 323 | * If you don't want that facility, just call Message.obtain() instead. |
| 324 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 325 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | public final Message obtainMessage() |
| 327 | { |
| 328 | return Message.obtain(this); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Same as {@link #obtainMessage()}, except that it also sets the what member of the returned Message. |
| 333 | * |
| 334 | * @param what Value to assign to the returned Message.what field. |
| 335 | * @return A Message from the global message pool. |
| 336 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 337 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | public final Message obtainMessage(int what) |
| 339 | { |
| 340 | return Message.obtain(this, what); |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * |
| 345 | * Same as {@link #obtainMessage()}, except that it also sets the what and obj members |
| 346 | * of the returned Message. |
| 347 | * |
| 348 | * @param what Value to assign to the returned Message.what field. |
| 349 | * @param obj Value to assign to the returned Message.obj field. |
| 350 | * @return A Message from the global message pool. |
| 351 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 352 | @NonNull |
| 353 | public final Message obtainMessage(int what, @Nullable Object obj) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | return Message.obtain(this, what, obj); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * |
| 359 | * Same as {@link #obtainMessage()}, except that it also sets the what, arg1 and arg2 members of the returned |
| 360 | * Message. |
| 361 | * @param what Value to assign to the returned Message.what field. |
| 362 | * @param arg1 Value to assign to the returned Message.arg1 field. |
| 363 | * @param arg2 Value to assign to the returned Message.arg2 field. |
| 364 | * @return A Message from the global message pool. |
| 365 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 366 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | public final Message obtainMessage(int what, int arg1, int arg2) |
| 368 | { |
| 369 | return Message.obtain(this, what, arg1, arg2); |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * |
| 374 | * Same as {@link #obtainMessage()}, except that it also sets the what, obj, arg1,and arg2 values on the |
| 375 | * returned Message. |
| 376 | * @param what Value to assign to the returned Message.what field. |
| 377 | * @param arg1 Value to assign to the returned Message.arg1 field. |
| 378 | * @param arg2 Value to assign to the returned Message.arg2 field. |
| 379 | * @param obj Value to assign to the returned Message.obj field. |
| 380 | * @return A Message from the global message pool. |
| 381 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 382 | @NonNull |
| 383 | public final Message obtainMessage(int what, int arg1, int arg2, @Nullable Object obj) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | return Message.obtain(this, what, arg1, arg2, obj); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Causes the Runnable r to be added to the message queue. |
| 389 | * The runnable will be run on the thread to which this handler is |
| 390 | * attached. |
| 391 | * |
| 392 | * @param r The Runnable that will be executed. |
| 393 | * |
| 394 | * @return Returns true if the Runnable was successfully placed in to the |
| 395 | * message queue. Returns false on failure, usually because the |
| 396 | * looper processing the message queue is exiting. |
| 397 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 398 | public final boolean post(@NonNull Runnable r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | return sendMessageDelayed(getPostMessage(r), 0); |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Causes the Runnable r to be added to the message queue, to be run |
| 404 | * at a specific time given by <var>uptimeMillis</var>. |
| 405 | * <b>The time-base is {@link android.os.SystemClock#uptimeMillis}.</b> |
David Christie | a8cf4f2 | 2013-12-19 18:30:06 -0800 | [diff] [blame] | 406 | * Time spent in deep sleep will add an additional delay to execution. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | * The runnable will be run on the thread to which this handler is attached. |
| 408 | * |
| 409 | * @param r The Runnable that will be executed. |
| 410 | * @param uptimeMillis The absolute time at which the callback should run, |
| 411 | * using the {@link android.os.SystemClock#uptimeMillis} time-base. |
| 412 | * |
| 413 | * @return Returns true if the Runnable was successfully placed in to the |
| 414 | * message queue. Returns false on failure, usually because the |
| 415 | * looper processing the message queue is exiting. Note that a |
| 416 | * result of true does not mean the Runnable will be processed -- if |
| 417 | * the looper is quit before the delivery time of the message |
| 418 | * occurs then the message will be dropped. |
| 419 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 420 | public final boolean postAtTime(@NonNull Runnable r, long uptimeMillis) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | return sendMessageAtTime(getPostMessage(r), uptimeMillis); |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Causes the Runnable r to be added to the message queue, to be run |
| 426 | * at a specific time given by <var>uptimeMillis</var>. |
| 427 | * <b>The time-base is {@link android.os.SystemClock#uptimeMillis}.</b> |
David Christie | a8cf4f2 | 2013-12-19 18:30:06 -0800 | [diff] [blame] | 428 | * Time spent in deep sleep will add an additional delay to execution. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | * The runnable will be run on the thread to which this handler is attached. |
| 430 | * |
| 431 | * @param r The Runnable that will be executed. |
Jake Wharton | 820e3dd | 2018-01-02 22:18:24 -0500 | [diff] [blame] | 432 | * @param token An instance which can be used to cancel {@code r} via |
| 433 | * {@link #removeCallbacksAndMessages}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | * @param uptimeMillis The absolute time at which the callback should run, |
| 435 | * using the {@link android.os.SystemClock#uptimeMillis} time-base. |
| 436 | * |
| 437 | * @return Returns true if the Runnable was successfully placed in to the |
| 438 | * message queue. Returns false on failure, usually because the |
| 439 | * looper processing the message queue is exiting. Note that a |
| 440 | * result of true does not mean the Runnable will be processed -- if |
| 441 | * the looper is quit before the delivery time of the message |
| 442 | * occurs then the message will be dropped. |
| 443 | * |
| 444 | * @see android.os.SystemClock#uptimeMillis |
| 445 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 446 | public final boolean postAtTime( |
| 447 | @NonNull Runnable r, @Nullable Object token, long uptimeMillis) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 448 | return sendMessageAtTime(getPostMessage(r, token), uptimeMillis); |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * Causes the Runnable r to be added to the message queue, to be run |
| 453 | * after the specified amount of time elapses. |
| 454 | * The runnable will be run on the thread to which this handler |
| 455 | * is attached. |
David Christie | a8cf4f2 | 2013-12-19 18:30:06 -0800 | [diff] [blame] | 456 | * <b>The time-base is {@link android.os.SystemClock#uptimeMillis}.</b> |
| 457 | * Time spent in deep sleep will add an additional delay to execution. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | * |
| 459 | * @param r The Runnable that will be executed. |
| 460 | * @param delayMillis The delay (in milliseconds) until the Runnable |
| 461 | * will be executed. |
| 462 | * |
| 463 | * @return Returns true if the Runnable was successfully placed in to the |
| 464 | * message queue. Returns false on failure, usually because the |
| 465 | * looper processing the message queue is exiting. Note that a |
| 466 | * result of true does not mean the Runnable will be processed -- |
| 467 | * if the looper is quit before the delivery time of the message |
| 468 | * occurs then the message will be dropped. |
| 469 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 470 | public final boolean postDelayed(@NonNull Runnable r, long delayMillis) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | return sendMessageDelayed(getPostMessage(r), delayMillis); |
| 472 | } |
| 473 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 474 | /** @hide */ |
| 475 | public final boolean postDelayed(Runnable r, int what, long delayMillis) { |
| 476 | return sendMessageDelayed(getPostMessage(r).setWhat(what), delayMillis); |
| 477 | } |
| 478 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | /** |
Jake Wharton | 820e3dd | 2018-01-02 22:18:24 -0500 | [diff] [blame] | 480 | * Causes the Runnable r to be added to the message queue, to be run |
| 481 | * after the specified amount of time elapses. |
| 482 | * The runnable will be run on the thread to which this handler |
| 483 | * is attached. |
| 484 | * <b>The time-base is {@link android.os.SystemClock#uptimeMillis}.</b> |
| 485 | * Time spent in deep sleep will add an additional delay to execution. |
| 486 | * |
| 487 | * @param r The Runnable that will be executed. |
| 488 | * @param token An instance which can be used to cancel {@code r} via |
| 489 | * {@link #removeCallbacksAndMessages}. |
| 490 | * @param delayMillis The delay (in milliseconds) until the Runnable |
| 491 | * will be executed. |
| 492 | * |
| 493 | * @return Returns true if the Runnable was successfully placed in to the |
| 494 | * message queue. Returns false on failure, usually because the |
| 495 | * looper processing the message queue is exiting. Note that a |
| 496 | * result of true does not mean the Runnable will be processed -- |
| 497 | * if the looper is quit before the delivery time of the message |
| 498 | * occurs then the message will be dropped. |
| 499 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 500 | public final boolean postDelayed( |
| 501 | @NonNull Runnable r, @Nullable Object token, long delayMillis) { |
Jake Wharton | 820e3dd | 2018-01-02 22:18:24 -0500 | [diff] [blame] | 502 | return sendMessageDelayed(getPostMessage(r, token), delayMillis); |
| 503 | } |
| 504 | |
| 505 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | * Posts a message to an object that implements Runnable. |
| 507 | * Causes the Runnable r to executed on the next iteration through the |
| 508 | * message queue. The runnable will be run on the thread to which this |
| 509 | * handler is attached. |
| 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 | * @param r The Runnable that will be executed. |
| 515 | * |
| 516 | * @return Returns true if the message was successfully placed in to the |
| 517 | * message queue. Returns false on failure, usually because the |
| 518 | * looper processing the message queue is exiting. |
| 519 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 520 | public final boolean postAtFrontOfQueue(@NonNull Runnable r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 521 | return sendMessageAtFrontOfQueue(getPostMessage(r)); |
| 522 | } |
| 523 | |
| 524 | /** |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 525 | * Runs the specified task synchronously. |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 526 | * <p> |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 527 | * If the current thread is the same as the handler thread, then the runnable |
| 528 | * runs immediately without being enqueued. Otherwise, posts the runnable |
| 529 | * to the handler and waits for it to complete before returning. |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 530 | * </p><p> |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 531 | * This method is dangerous! Improper use can result in deadlocks. |
| 532 | * Never call this method while any locks are held or use it in a |
| 533 | * possibly re-entrant manner. |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 534 | * </p><p> |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 535 | * This method is occasionally useful in situations where a background thread |
| 536 | * must synchronously await completion of a task that must run on the |
| 537 | * handler's thread. However, this problem is often a symptom of bad design. |
| 538 | * Consider improving the design (if possible) before resorting to this method. |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 539 | * </p><p> |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 540 | * One example of where you might want to use this method is when you just |
| 541 | * set up a Handler thread and need to perform some initialization steps on |
| 542 | * it before continuing execution. |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 543 | * </p><p> |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 544 | * If timeout occurs then this method returns <code>false</code> but the runnable |
| 545 | * will remain posted on the handler and may already be in progress or |
| 546 | * complete at a later time. |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 547 | * </p><p> |
| 548 | * When using this method, be sure to use {@link Looper#quitSafely} when |
| 549 | * quitting the looper. Otherwise {@link #runWithScissors} may hang indefinitely. |
| 550 | * (TODO: We should fix this by making MessageQueue aware of blocking runnables.) |
| 551 | * </p> |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 552 | * |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 553 | * @param r The Runnable that will be executed synchronously. |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 554 | * @param timeout The timeout in milliseconds, or 0 to wait indefinitely. |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 555 | * |
| 556 | * @return Returns true if the Runnable was successfully executed. |
| 557 | * Returns false on failure, usually because the |
| 558 | * looper processing the message queue is exiting. |
| 559 | * |
| 560 | * @hide This method is prone to abuse and should probably not be in the API. |
| 561 | * If we ever do make it part of the API, we might want to rename it to something |
| 562 | * less funny like runUnsafe(). |
| 563 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 564 | public final boolean runWithScissors(@NonNull Runnable r, long timeout) { |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 565 | if (r == null) { |
| 566 | throw new IllegalArgumentException("runnable must not be null"); |
| 567 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 568 | if (timeout < 0) { |
| 569 | throw new IllegalArgumentException("timeout must be non-negative"); |
| 570 | } |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 571 | |
| 572 | if (Looper.myLooper() == mLooper) { |
| 573 | r.run(); |
| 574 | return true; |
| 575 | } |
| 576 | |
| 577 | BlockingRunnable br = new BlockingRunnable(r); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 578 | return br.postAndWait(this, timeout); |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | * Remove any pending posts of Runnable r that are in the message queue. |
| 583 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 584 | public final void removeCallbacks(@NonNull Runnable r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | mQueue.removeMessages(this, r, null); |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * Remove any pending posts of Runnable <var>r</var> with Object |
Dianne Hackborn | 466ed24 | 2011-07-21 18:16:31 -0700 | [diff] [blame] | 590 | * <var>token</var> that are in the message queue. If <var>token</var> is null, |
| 591 | * all callbacks will be removed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 593 | public final void removeCallbacks(@NonNull Runnable r, @Nullable Object token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 594 | mQueue.removeMessages(this, r, token); |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * Pushes a message onto the end of the message queue after all pending messages |
| 599 | * before the current time. It will be received in {@link #handleMessage}, |
| 600 | * in the thread attached to this handler. |
| 601 | * |
| 602 | * @return Returns true if the message was successfully placed in to the |
| 603 | * message queue. Returns false on failure, usually because the |
| 604 | * looper processing the message queue is exiting. |
| 605 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 606 | public final boolean sendMessage(@NonNull Message msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 607 | return sendMessageDelayed(msg, 0); |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * Sends a Message containing only the what value. |
| 612 | * |
| 613 | * @return Returns true if the message was successfully placed in to the |
| 614 | * message queue. Returns false on failure, usually because the |
| 615 | * looper processing the message queue is exiting. |
| 616 | */ |
| 617 | public final boolean sendEmptyMessage(int what) |
| 618 | { |
| 619 | return sendEmptyMessageDelayed(what, 0); |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * Sends a Message containing only the what value, to be delivered |
| 624 | * after the specified amount of time elapses. |
| 625 | * @see #sendMessageDelayed(android.os.Message, long) |
| 626 | * |
| 627 | * @return Returns true if the message was successfully placed in to the |
| 628 | * message queue. Returns false on failure, usually because the |
| 629 | * looper processing the message queue is exiting. |
| 630 | */ |
| 631 | public final boolean sendEmptyMessageDelayed(int what, long delayMillis) { |
| 632 | Message msg = Message.obtain(); |
| 633 | msg.what = what; |
| 634 | return sendMessageDelayed(msg, delayMillis); |
| 635 | } |
| 636 | |
| 637 | /** |
| 638 | * Sends a Message containing only the what value, to be delivered |
| 639 | * at a specific time. |
| 640 | * @see #sendMessageAtTime(android.os.Message, long) |
| 641 | * |
| 642 | * @return Returns true if the message was successfully placed in to the |
| 643 | * message queue. Returns false on failure, usually because the |
| 644 | * looper processing the message queue is exiting. |
| 645 | */ |
| 646 | |
| 647 | public final boolean sendEmptyMessageAtTime(int what, long uptimeMillis) { |
| 648 | Message msg = Message.obtain(); |
| 649 | msg.what = what; |
| 650 | return sendMessageAtTime(msg, uptimeMillis); |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * Enqueue a message into the message queue after all pending messages |
| 655 | * before (current time + delayMillis). You will receive it in |
| 656 | * {@link #handleMessage}, in the thread attached to this handler. |
| 657 | * |
| 658 | * @return Returns true if the message was successfully placed in to the |
| 659 | * message queue. Returns false on failure, usually because the |
| 660 | * looper processing the message queue is exiting. Note that a |
| 661 | * result of true does not mean the message will be processed -- if |
| 662 | * the looper is quit before the delivery time of the message |
| 663 | * occurs then the message will be dropped. |
| 664 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 665 | public final boolean sendMessageDelayed(@NonNull Message msg, long delayMillis) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 666 | if (delayMillis < 0) { |
| 667 | delayMillis = 0; |
| 668 | } |
| 669 | return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis); |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * Enqueue a message into the message queue after all pending messages |
| 674 | * before the absolute time (in milliseconds) <var>uptimeMillis</var>. |
| 675 | * <b>The time-base is {@link android.os.SystemClock#uptimeMillis}.</b> |
David Christie | a8cf4f2 | 2013-12-19 18:30:06 -0800 | [diff] [blame] | 676 | * Time spent in deep sleep will add an additional delay to execution. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 677 | * You will receive it in {@link #handleMessage}, in the thread attached |
| 678 | * to this handler. |
| 679 | * |
| 680 | * @param uptimeMillis The absolute time at which the message should be |
| 681 | * delivered, using the |
| 682 | * {@link android.os.SystemClock#uptimeMillis} time-base. |
| 683 | * |
| 684 | * @return Returns true if the message was successfully placed in to the |
| 685 | * message queue. Returns false on failure, usually because the |
| 686 | * looper processing the message queue is exiting. Note that a |
| 687 | * result of true does not mean the message will be processed -- if |
| 688 | * the looper is quit before the delivery time of the message |
| 689 | * occurs then the message will be dropped. |
| 690 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 691 | public boolean sendMessageAtTime(@NonNull Message msg, long uptimeMillis) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | MessageQueue queue = mQueue; |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 693 | if (queue == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 694 | RuntimeException e = new RuntimeException( |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 695 | this + " sendMessageAtTime() called with no mQueue"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | Log.w("Looper", e.getMessage(), e); |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 697 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 698 | } |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 699 | return enqueueMessage(queue, msg, uptimeMillis); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | /** |
| 703 | * Enqueue a message at the front of the message queue, to be processed on |
| 704 | * the next iteration of the message loop. You will receive it in |
| 705 | * {@link #handleMessage}, in the thread attached to this handler. |
| 706 | * <b>This method is only for use in very special circumstances -- it |
| 707 | * can easily starve the message queue, cause ordering problems, or have |
| 708 | * other unexpected side-effects.</b> |
| 709 | * |
| 710 | * @return Returns true if the message was successfully placed in to the |
| 711 | * message queue. Returns false on failure, usually because the |
| 712 | * looper processing the message queue is exiting. |
| 713 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 714 | public final boolean sendMessageAtFrontOfQueue(@NonNull Message msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 715 | MessageQueue queue = mQueue; |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 716 | if (queue == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | RuntimeException e = new RuntimeException( |
| 718 | this + " sendMessageAtTime() called with no mQueue"); |
| 719 | Log.w("Looper", e.getMessage(), e); |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 720 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | } |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 722 | return enqueueMessage(queue, msg, 0); |
| 723 | } |
| 724 | |
Eugene Susla | 9f35ca9 | 2018-02-12 16:17:26 -0800 | [diff] [blame] | 725 | /** |
| 726 | * Executes the message synchronously if called on the same thread this handler corresponds to, |
| 727 | * or {@link #sendMessage pushes it to the queue} otherwise |
| 728 | * |
| 729 | * @return Returns true if the message was successfully ran or placed in to the |
| 730 | * message queue. Returns false on failure, usually because the |
| 731 | * looper processing the message queue is exiting. |
| 732 | * @hide |
| 733 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 734 | public final boolean executeOrSendMessage(@NonNull Message msg) { |
Eugene Susla | 9f35ca9 | 2018-02-12 16:17:26 -0800 | [diff] [blame] | 735 | if (mLooper == Looper.myLooper()) { |
| 736 | dispatchMessage(msg); |
| 737 | return true; |
| 738 | } |
| 739 | return sendMessage(msg); |
| 740 | } |
| 741 | |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 742 | private boolean enqueueMessage(@NonNull MessageQueue queue, @NonNull Message msg, |
| 743 | long uptimeMillis) { |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 744 | msg.target = this; |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 745 | msg.workSourceUid = ThreadLocalWorkSource.getUid(); |
Marcin Oczeretko | ec75872 | 2018-09-12 12:53:47 +0100 | [diff] [blame] | 746 | |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 747 | if (mAsynchronous) { |
| 748 | msg.setAsynchronous(true); |
| 749 | } |
| 750 | return queue.enqueueMessage(msg, uptimeMillis); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | /** |
| 754 | * Remove any pending posts of messages with code 'what' that are in the |
| 755 | * message queue. |
| 756 | */ |
| 757 | public final void removeMessages(int what) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 758 | mQueue.removeMessages(this, what, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | /** |
| 762 | * 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] | 763 | * '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] | 764 | * all messages will be removed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 766 | public final void removeMessages(int what, @Nullable Object object) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 767 | mQueue.removeMessages(this, what, object); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | /** |
| 771 | * Remove any pending posts of callbacks and sent messages whose |
Dianne Hackborn | 466ed24 | 2011-07-21 18:16:31 -0700 | [diff] [blame] | 772 | * <var>obj</var> is <var>token</var>. If <var>token</var> is null, |
| 773 | * all callbacks and messages will be removed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 774 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 775 | public final void removeCallbacksAndMessages(@Nullable Object token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 776 | mQueue.removeCallbacksAndMessages(this, token); |
| 777 | } |
| 778 | |
| 779 | /** |
| 780 | * Check if there are any pending posts of messages with code 'what' in |
| 781 | * the message queue. |
| 782 | */ |
| 783 | public final boolean hasMessages(int what) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 784 | return mQueue.hasMessages(this, what, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /** |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 788 | * Return whether there are any messages or callbacks currently scheduled on this handler. |
| 789 | * @hide |
| 790 | */ |
| 791 | public final boolean hasMessagesOrCallbacks() { |
| 792 | return mQueue.hasMessages(this); |
| 793 | } |
| 794 | |
| 795 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 796 | * Check if there are any pending posts of messages with code 'what' and |
| 797 | * whose obj is 'object' in the message queue. |
| 798 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 799 | public final boolean hasMessages(int what, @Nullable Object object) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 800 | return mQueue.hasMessages(this, what, object); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 801 | } |
| 802 | |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 803 | /** |
| 804 | * Check if there are any pending posts of messages with callback r in |
| 805 | * the message queue. |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 806 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 807 | public final boolean hasCallbacks(@NonNull Runnable r) { |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 808 | return mQueue.hasMessages(this, r, null); |
| 809 | } |
| 810 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 811 | // if we can get rid of this method, the handler need not remember its loop |
| 812 | // we could instead export a getMessageQueue() method... |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 813 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 814 | public final Looper getLooper() { |
| 815 | return mLooper; |
| 816 | } |
| 817 | |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 818 | public final void dump(@NonNull Printer pw, @NonNull String prefix) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 819 | pw.println(prefix + this + " @ " + SystemClock.uptimeMillis()); |
| 820 | if (mLooper == null) { |
| 821 | pw.println(prefix + "looper uninitialized"); |
| 822 | } else { |
| 823 | mLooper.dump(pw, prefix + " "); |
| 824 | } |
| 825 | } |
| 826 | |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 827 | /** |
| 828 | * @hide |
| 829 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame^] | 830 | public final void dumpMine(@NonNull Printer pw, @NonNull String prefix) { |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 831 | pw.println(prefix + this + " @ " + SystemClock.uptimeMillis()); |
| 832 | if (mLooper == null) { |
| 833 | pw.println(prefix + "looper uninitialized"); |
| 834 | } else { |
| 835 | mLooper.dump(pw, prefix + " ", this); |
| 836 | } |
| 837 | } |
| 838 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 839 | @Override |
| 840 | public String toString() { |
Kristian Monsen | 588d856 | 2011-08-04 12:55:33 +0100 | [diff] [blame] | 841 | return "Handler (" + getClass().getName() + ") {" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 842 | + Integer.toHexString(System.identityHashCode(this)) |
| 843 | + "}"; |
| 844 | } |
| 845 | |
| 846 | final IMessenger getIMessenger() { |
| 847 | synchronized (mQueue) { |
| 848 | if (mMessenger != null) { |
| 849 | return mMessenger; |
| 850 | } |
| 851 | mMessenger = new MessengerImpl(); |
| 852 | return mMessenger; |
| 853 | } |
| 854 | } |
Brad Fitzpatrick | 333b8cb | 2010-08-26 12:04:57 -0700 | [diff] [blame] | 855 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 856 | private final class MessengerImpl extends IMessenger.Stub { |
| 857 | public void send(Message msg) { |
Dianne Hackborn | cb3ed1d | 2014-06-27 18:37:06 -0700 | [diff] [blame] | 858 | msg.sendingUid = Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 859 | Handler.this.sendMessage(msg); |
| 860 | } |
| 861 | } |
Brad Fitzpatrick | 333b8cb | 2010-08-26 12:04:57 -0700 | [diff] [blame] | 862 | |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 863 | private static Message getPostMessage(Runnable r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | Message m = Message.obtain(); |
| 865 | m.callback = r; |
| 866 | return m; |
| 867 | } |
| 868 | |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 869 | private static Message getPostMessage(Runnable r, Object token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 870 | Message m = Message.obtain(); |
| 871 | m.obj = token; |
| 872 | m.callback = r; |
| 873 | return m; |
| 874 | } |
| 875 | |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 876 | private static void handleCallback(Message message) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 877 | message.callback.run(); |
| 878 | } |
| 879 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | final Looper mLooper; |
Jeff Sharkey | 74cd3de | 2016-04-06 17:40:54 -0600 | [diff] [blame] | 881 | final MessageQueue mQueue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 882 | final Callback mCallback; |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 883 | final boolean mAsynchronous; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 884 | IMessenger mMessenger; |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 885 | |
| 886 | private static final class BlockingRunnable implements Runnable { |
| 887 | private final Runnable mTask; |
| 888 | private boolean mDone; |
| 889 | |
| 890 | public BlockingRunnable(Runnable task) { |
| 891 | mTask = task; |
| 892 | } |
| 893 | |
| 894 | @Override |
| 895 | public void run() { |
| 896 | try { |
| 897 | mTask.run(); |
| 898 | } finally { |
| 899 | synchronized (this) { |
| 900 | mDone = true; |
| 901 | notifyAll(); |
| 902 | } |
| 903 | } |
| 904 | } |
| 905 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 906 | public boolean postAndWait(Handler handler, long timeout) { |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 907 | if (!handler.post(this)) { |
| 908 | return false; |
| 909 | } |
| 910 | |
| 911 | synchronized (this) { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 912 | if (timeout > 0) { |
| 913 | final long expirationTime = SystemClock.uptimeMillis() + timeout; |
| 914 | while (!mDone) { |
| 915 | long delay = expirationTime - SystemClock.uptimeMillis(); |
| 916 | if (delay <= 0) { |
| 917 | return false; // timeout |
| 918 | } |
| 919 | try { |
| 920 | wait(delay); |
| 921 | } catch (InterruptedException ex) { |
| 922 | } |
| 923 | } |
| 924 | } else { |
| 925 | while (!mDone) { |
| 926 | try { |
| 927 | wait(); |
| 928 | } catch (InterruptedException ex) { |
| 929 | } |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | } |
| 933 | return true; |
| 934 | } |
| 935 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 936 | } |