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; |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 21 | import android.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.util.Log; |
| 23 | import android.util.Printer; |
| 24 | |
| 25 | import 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 |
kopriva | 9b5c039 | 2018-09-10 14:02:58 -0700 | [diff] [blame] | 37 | * 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] | 38 | * 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 Palmer | 4285514 | 2010-09-09 17:04:31 -0700 | [diff] [blame] | 64 | * 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] | 65 | * in the Handler's message queue and processed when appropriate. |
| 66 | */ |
| 67 | public 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 Susla | a38fbf6 | 2017-03-14 10:26:10 -0700 | [diff] [blame] | 75 | private static Handler MAIN_THREAD_HANDLER = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | |
| 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 Grafov | dcc5357 | 2017-03-10 19:39:14 +0000 | [diff] [blame] | 82 | /** |
| 83 | * @param msg A {@link android.os.Message Message} object |
| 84 | * @return True if no further handling is desired |
| 85 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 86 | boolean handleMessage(@NonNull Message msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Subclasses must implement this to receive messages. |
| 91 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 92 | public void handleMessage(@NonNull Message msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Handle system messages here. |
| 97 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 98 | public void dispatchMessage(@NonNull Message msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | 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 Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 112 | * 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] | 113 | * current thread. |
| 114 | * |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 115 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | */ |
| 118 | public Handler() { |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 119 | this(null, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 123 | * 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] | 124 | * current thread and takes a callback interface in which you can handle |
| 125 | * messages. |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 126 | * |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 132 | public Handler(@Nullable Callback callback) { |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 133 | 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 Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 141 | public Handler(@NonNull Looper looper) { |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 142 | 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 Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 152 | public Handler(@NonNull Looper looper, @Nullable Callback callback) { |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 153 | 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 Brown | 9840c07 | 2014-11-11 20:21:21 -0800 | [diff] [blame] | 164 | * with respect to synchronous messages. Asynchronous messages are not subject to |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 165 | * 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 Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 172 | @UnsupportedAppUsage |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 173 | 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 Brown | 9840c07 | 2014-11-11 20:21:21 -0800 | [diff] [blame] | 185 | * with respect to synchronous messages. Asynchronous messages are not subject to |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 186 | * 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 Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 194 | public Handler(@Nullable Callback callback, boolean async) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | 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 Susla | 8f07ee1 | 2017-11-14 17:41:03 -0800 | [diff] [blame] | 207 | "Can't create handler inside thread " + Thread.currentThread() |
| 208 | + " that has not called Looper.prepare()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | } |
| 210 | mQueue = mLooper.mQueue; |
| 211 | mCallback = callback; |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 212 | mAsynchronous = async; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 216 | * 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] | 217 | * 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 Brown | 9840c07 | 2014-11-11 20:21:21 -0800 | [diff] [blame] | 224 | * with respect to synchronous messages. Asynchronous messages are not subject to |
Adam Powell | 8709ba8 | 2018-02-21 10:18:25 -0800 | [diff] [blame] | 225 | * the synchronization barriers introduced by conditions such as display vsync. |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 226 | * |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 227 | * @param looper The looper, must not be null. |
| 228 | * @param callback The callback interface in which to handle messages, or null. |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 229 | * @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 Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 234 | @UnsupportedAppUsage |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 235 | public Handler(@NonNull Looper looper, @Nullable Callback callback, boolean async) { |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 236 | mLooper = looper; |
| 237 | mQueue = looper.mQueue; |
| 238 | mCallback = callback; |
| 239 | mAsynchronous = async; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Adam Powell | 8709ba8 | 2018-02-21 10:18:25 -0800 | [diff] [blame] | 242 | /** |
| 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 Susla | a38fbf6 | 2017-03-14 10:26:10 -0700 | [diff] [blame] | 279 | /** @hide */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 280 | @UnsupportedAppUsage |
Eugene Susla | a38fbf6 | 2017-03-14 10:26:10 -0700 | [diff] [blame] | 281 | @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 Sharkey | 74cd3de | 2016-04-06 17:40:54 -0600 | [diff] [blame] | 295 | /** {@hide} */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 296 | @NonNull |
| 297 | public String getTraceName(@NonNull Message message) { |
Jeff Sharkey | 74cd3de | 2016-04-06 17:40:54 -0600 | [diff] [blame] | 298 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | /** |
Romain Guy | f928469 | 2011-07-13 18:46:21 -0700 | [diff] [blame] | 309 | * 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 Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 316 | @NonNull |
| 317 | public String getMessageName(@NonNull Message message) { |
Romain Guy | f928469 | 2011-07-13 18:46:21 -0700 | [diff] [blame] | 318 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | * 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 Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 329 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | 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 Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 341 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | 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 Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 356 | @NonNull |
| 357 | public final Message obtainMessage(int what, @Nullable Object obj) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | 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 Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 370 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | 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 Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 386 | @NonNull |
| 387 | 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] | 388 | 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 Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 402 | public final boolean post(@NonNull Runnable r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | 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 Christie | a8cf4f2 | 2013-12-19 18:30:06 -0800 | [diff] [blame] | 410 | * 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] | 411 | * 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 Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 424 | public final boolean postAtTime(@NonNull Runnable r, long uptimeMillis) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | 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 Christie | a8cf4f2 | 2013-12-19 18:30:06 -0800 | [diff] [blame] | 432 | * 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] | 433 | * 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 Wharton | 820e3dd | 2018-01-02 22:18:24 -0500 | [diff] [blame] | 436 | * @param token An instance which can be used to cancel {@code r} via |
| 437 | * {@link #removeCallbacksAndMessages}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | * @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 Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 450 | public final boolean postAtTime( |
| 451 | @NonNull Runnable r, @Nullable Object token, long uptimeMillis) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | 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 Christie | a8cf4f2 | 2013-12-19 18:30:06 -0800 | [diff] [blame] | 460 | * <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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | * |
| 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 Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 474 | public final boolean postDelayed(@NonNull Runnable r, long delayMillis) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | return sendMessageDelayed(getPostMessage(r), delayMillis); |
| 476 | } |
| 477 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 478 | /** @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 | /** |
Jake Wharton | 820e3dd | 2018-01-02 22:18:24 -0500 | [diff] [blame] | 484 | * 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 Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 504 | public final boolean postDelayed( |
| 505 | @NonNull Runnable r, @Nullable Object token, long delayMillis) { |
Jake Wharton | 820e3dd | 2018-01-02 22:18:24 -0500 | [diff] [blame] | 506 | return sendMessageDelayed(getPostMessage(r, token), delayMillis); |
| 507 | } |
| 508 | |
| 509 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | * 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 Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 524 | public final boolean postAtFrontOfQueue(@NonNull Runnable r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 525 | return sendMessageAtFrontOfQueue(getPostMessage(r)); |
| 526 | } |
| 527 | |
| 528 | /** |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 529 | * Runs the specified task synchronously. |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 530 | * <p> |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 531 | * 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 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 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 Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 538 | * </p><p> |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 539 | * 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 Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 543 | * </p><p> |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 544 | * 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 Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 547 | * </p><p> |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 548 | * 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 Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 551 | * </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 Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 556 | * |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 557 | * @param r The Runnable that will be executed synchronously. |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 558 | * @param timeout The timeout in milliseconds, or 0 to wait indefinitely. |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 559 | * |
| 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 Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 568 | public final boolean runWithScissors(@NonNull Runnable r, long timeout) { |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 569 | if (r == null) { |
| 570 | throw new IllegalArgumentException("runnable must not be null"); |
| 571 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 572 | if (timeout < 0) { |
| 573 | throw new IllegalArgumentException("timeout must be non-negative"); |
| 574 | } |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 575 | |
| 576 | if (Looper.myLooper() == mLooper) { |
| 577 | r.run(); |
| 578 | return true; |
| 579 | } |
| 580 | |
| 581 | BlockingRunnable br = new BlockingRunnable(r); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 582 | return br.postAndWait(this, timeout); |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | * Remove any pending posts of Runnable r that are in the message queue. |
| 587 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 588 | public final void removeCallbacks(@NonNull Runnable r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 589 | mQueue.removeMessages(this, r, null); |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Remove any pending posts of Runnable <var>r</var> with Object |
Dianne Hackborn | 466ed24 | 2011-07-21 18:16:31 -0700 | [diff] [blame] | 594 | * <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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 597 | 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] | 598 | 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 Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 610 | public final boolean sendMessage(@NonNull Message msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | 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 Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 669 | public final boolean sendMessageDelayed(@NonNull Message msg, long delayMillis) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 670 | 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 Christie | a8cf4f2 | 2013-12-19 18:30:06 -0800 | [diff] [blame] | 680 | * 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] | 681 | * 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 Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 695 | public boolean sendMessageAtTime(@NonNull Message msg, long uptimeMillis) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | MessageQueue queue = mQueue; |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 697 | if (queue == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 698 | RuntimeException e = new RuntimeException( |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 699 | this + " sendMessageAtTime() called with no mQueue"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 700 | Log.w("Looper", e.getMessage(), e); |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 701 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 702 | } |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 703 | return enqueueMessage(queue, msg, uptimeMillis); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 704 | } |
| 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 Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 718 | public final boolean sendMessageAtFrontOfQueue(@NonNull Message msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | MessageQueue queue = mQueue; |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 720 | if (queue == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | RuntimeException e = new RuntimeException( |
| 722 | this + " sendMessageAtTime() called with no mQueue"); |
| 723 | Log.w("Looper", e.getMessage(), e); |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 724 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 725 | } |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 726 | return enqueueMessage(queue, msg, 0); |
| 727 | } |
| 728 | |
Eugene Susla | 9f35ca9 | 2018-02-12 16:17:26 -0800 | [diff] [blame] | 729 | /** |
| 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 Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 738 | public final boolean executeOrSendMessage(@NonNull Message msg) { |
Eugene Susla | 9f35ca9 | 2018-02-12 16:17:26 -0800 | [diff] [blame] | 739 | if (mLooper == Looper.myLooper()) { |
| 740 | dispatchMessage(msg); |
| 741 | return true; |
| 742 | } |
| 743 | return sendMessage(msg); |
| 744 | } |
| 745 | |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 746 | private boolean enqueueMessage(@NonNull MessageQueue queue, @NonNull Message msg, |
| 747 | long uptimeMillis) { |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 748 | msg.target = this; |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 749 | msg.workSourceUid = ThreadLocalWorkSource.getUid(); |
Marcin Oczeretko | ec75872 | 2018-09-12 12:53:47 +0100 | [diff] [blame] | 750 | |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 751 | if (mAsynchronous) { |
| 752 | msg.setAsynchronous(true); |
| 753 | } |
| 754 | return queue.enqueueMessage(msg, uptimeMillis); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 755 | } |
| 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 Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 762 | mQueue.removeMessages(this, what, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | /** |
| 766 | * 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] | 767 | * '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] | 768 | * all messages will be removed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 769 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 770 | public final void removeMessages(int what, @Nullable Object object) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 771 | mQueue.removeMessages(this, what, object); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | /** |
| 775 | * Remove any pending posts of callbacks and sent messages whose |
Dianne Hackborn | 466ed24 | 2011-07-21 18:16:31 -0700 | [diff] [blame] | 776 | * <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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 778 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 779 | public final void removeCallbacksAndMessages(@Nullable Object token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 780 | 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 Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 788 | return mQueue.hasMessages(this, what, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | /** |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 792 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 800 | * Check if there are any pending posts of messages with code 'what' and |
| 801 | * whose obj is 'object' in the message queue. |
| 802 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 803 | public final boolean hasMessages(int what, @Nullable Object object) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 804 | return mQueue.hasMessages(this, what, object); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 805 | } |
| 806 | |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 807 | /** |
| 808 | * Check if there are any pending posts of messages with callback r in |
| 809 | * the message queue. |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 810 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 811 | public final boolean hasCallbacks(@NonNull Runnable r) { |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 812 | return mQueue.hasMessages(this, r, null); |
| 813 | } |
| 814 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 815 | // if we can get rid of this method, the handler need not remember its loop |
| 816 | // we could instead export a getMessageQueue() method... |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 817 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 818 | public final Looper getLooper() { |
| 819 | return mLooper; |
| 820 | } |
| 821 | |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 822 | 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] | 823 | 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 Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 831 | /** |
| 832 | * @hide |
| 833 | */ |
Jake Wharton | 720530b | 2019-03-08 12:05:53 -0500 | [diff] [blame] | 834 | public final void dumpMine(@NonNull Printer pw, @NonNull String prefix) { |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 835 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 843 | @Override |
| 844 | public String toString() { |
Kristian Monsen | 588d856 | 2011-08-04 12:55:33 +0100 | [diff] [blame] | 845 | return "Handler (" + getClass().getName() + ") {" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 846 | + Integer.toHexString(System.identityHashCode(this)) |
| 847 | + "}"; |
| 848 | } |
| 849 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 850 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 851 | final IMessenger getIMessenger() { |
| 852 | synchronized (mQueue) { |
| 853 | if (mMessenger != null) { |
| 854 | return mMessenger; |
| 855 | } |
| 856 | mMessenger = new MessengerImpl(); |
| 857 | return mMessenger; |
| 858 | } |
| 859 | } |
Brad Fitzpatrick | 333b8cb | 2010-08-26 12:04:57 -0700 | [diff] [blame] | 860 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | private final class MessengerImpl extends IMessenger.Stub { |
| 862 | public void send(Message msg) { |
Dianne Hackborn | cb3ed1d | 2014-06-27 18:37:06 -0700 | [diff] [blame] | 863 | msg.sendingUid = Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | Handler.this.sendMessage(msg); |
| 865 | } |
| 866 | } |
Brad Fitzpatrick | 333b8cb | 2010-08-26 12:04:57 -0700 | [diff] [blame] | 867 | |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 868 | private static Message getPostMessage(Runnable r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 869 | Message m = Message.obtain(); |
| 870 | m.callback = r; |
| 871 | return m; |
| 872 | } |
| 873 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 874 | @UnsupportedAppUsage |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 875 | private static Message getPostMessage(Runnable r, Object token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 876 | Message m = Message.obtain(); |
| 877 | m.obj = token; |
| 878 | m.callback = r; |
| 879 | return m; |
| 880 | } |
| 881 | |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 882 | private static void handleCallback(Message message) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 883 | message.callback.run(); |
| 884 | } |
| 885 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 886 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 887 | final Looper mLooper; |
Jeff Sharkey | 74cd3de | 2016-04-06 17:40:54 -0600 | [diff] [blame] | 888 | final MessageQueue mQueue; |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 889 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 890 | final Callback mCallback; |
Jeff Brown | 109025d | 2012-08-14 20:41:30 -0700 | [diff] [blame] | 891 | final boolean mAsynchronous; |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 892 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 893 | IMessenger mMessenger; |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 894 | |
| 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 Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 915 | public boolean postAndWait(Handler handler, long timeout) { |
Jeff Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 916 | if (!handler.post(this)) { |
| 917 | return false; |
| 918 | } |
| 919 | |
| 920 | synchronized (this) { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 921 | 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 Brown | c53abc4 | 2012-08-29 04:43:25 -0700 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | } |
| 942 | return true; |
| 943 | } |
| 944 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | } |