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 | |
Jeff Brown | 803c2af | 2015-03-05 10:52:53 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 21 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.util.Printer; |
Makoto Onuki | 9957151 | 2017-03-28 14:12:34 -0700 | [diff] [blame] | 23 | import android.util.Slog; |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 24 | import android.util.proto.ProtoOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Class used to run a message loop for a thread. Threads by default do |
| 28 | * not have a message loop associated with them; to create one, call |
| 29 | * {@link #prepare} in the thread that is to run the loop, and then |
| 30 | * {@link #loop} to have it process messages until the loop is stopped. |
Jeff Brown | 803c2af | 2015-03-05 10:52:53 -0800 | [diff] [blame] | 31 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | * <p>Most interaction with a message loop is through the |
| 33 | * {@link Handler} class. |
Jeff Brown | 803c2af | 2015-03-05 10:52:53 -0800 | [diff] [blame] | 34 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | * <p>This is a typical example of the implementation of a Looper thread, |
| 36 | * using the separation of {@link #prepare} and {@link #loop} to create an |
| 37 | * initial Handler to communicate with the Looper. |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 38 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | * <pre> |
| 40 | * class LooperThread extends Thread { |
| 41 | * public Handler mHandler; |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 42 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | * public void run() { |
| 44 | * Looper.prepare(); |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 45 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | * mHandler = new Handler() { |
| 47 | * public void handleMessage(Message msg) { |
| 48 | * // process incoming messages here |
| 49 | * } |
| 50 | * }; |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 51 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | * Looper.loop(); |
| 53 | * } |
| 54 | * }</pre> |
| 55 | */ |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 56 | public final class Looper { |
Jeff Brown | 6c7b41a | 2015-02-26 14:43:53 -0800 | [diff] [blame] | 57 | /* |
| 58 | * API Implementation Note: |
| 59 | * |
| 60 | * This class contains the code required to set up and manage an event loop |
| 61 | * based on MessageQueue. APIs that affect the state of the queue should be |
| 62 | * defined on MessageQueue or Handler rather than on Looper itself. For example, |
| 63 | * idle handlers and sync barriers are defined on the queue whereas preparing the |
Jeff Brown | 803c2af | 2015-03-05 10:52:53 -0800 | [diff] [blame] | 64 | * thread, looping, and quitting are defined on the looper. |
Jeff Brown | 6c7b41a | 2015-02-26 14:43:53 -0800 | [diff] [blame] | 65 | */ |
| 66 | |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 67 | private static final String TAG = "Looper"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | |
| 69 | // sThreadLocal.get() will return null unless you've called prepare(). |
Xavier Ducrohet | 7f9f99ea | 2011-08-11 10:16:17 -0700 | [diff] [blame] | 70 | static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>(); |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 71 | private static Looper sMainLooper; // guarded by Looper.class |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 72 | private static Observer sObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | |
| 74 | final MessageQueue mQueue; |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 75 | final Thread mThread; |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 76 | |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 77 | private Printer mLogging; |
Jeff Sharkey | 74cd3de | 2016-04-06 17:40:54 -0600 | [diff] [blame] | 78 | private long mTraceTag; |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 79 | |
Makoto Onuki | 712886f | 2018-04-27 15:22:50 -0700 | [diff] [blame] | 80 | /** |
| 81 | * If set, the looper will show a warning log if a message dispatch takes longer than this. |
| 82 | */ |
Makoto Onuki | 9957151 | 2017-03-28 14:12:34 -0700 | [diff] [blame] | 83 | private long mSlowDispatchThresholdMs; |
| 84 | |
Makoto Onuki | 712886f | 2018-04-27 15:22:50 -0700 | [diff] [blame] | 85 | /** |
| 86 | * If set, the looper will show a warning log if a message delivery (actual delivery time - |
| 87 | * post time) takes longer than this. |
| 88 | */ |
| 89 | private long mSlowDeliveryThresholdMs; |
| 90 | |
| 91 | /** Initialize the current thread as a looper. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | * This gives you a chance to create handlers that then reference |
| 93 | * this looper, before actually starting the loop. Be sure to call |
| 94 | * {@link #loop()} after calling this method, and end it by calling |
| 95 | * {@link #quit()}. |
| 96 | */ |
Romain Guy | f928469 | 2011-07-13 18:46:21 -0700 | [diff] [blame] | 97 | public static void prepare() { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 98 | prepare(true); |
| 99 | } |
| 100 | |
| 101 | private static void prepare(boolean quitAllowed) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | if (sThreadLocal.get() != null) { |
| 103 | throw new RuntimeException("Only one Looper may be created per thread"); |
| 104 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 105 | sThreadLocal.set(new Looper(quitAllowed)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | } |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * Initialize the current thread as a looper, marking it as an |
| 110 | * application's main looper. The main looper for your application |
| 111 | * is created by the Android environment, so you should never need |
| 112 | * to call this function yourself. See also: {@link #prepare()} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | */ |
Romain Guy | f928469 | 2011-07-13 18:46:21 -0700 | [diff] [blame] | 114 | public static void prepareMainLooper() { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 115 | prepare(false); |
| 116 | synchronized (Looper.class) { |
| 117 | if (sMainLooper != null) { |
| 118 | throw new IllegalStateException("The main Looper has already been prepared."); |
| 119 | } |
| 120 | sMainLooper = myLooper(); |
| 121 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | } |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 123 | |
Jeff Brown | 803c2af | 2015-03-05 10:52:53 -0800 | [diff] [blame] | 124 | /** |
| 125 | * Returns the application's main looper, which lives in the main thread of the application. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | */ |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 127 | public static Looper getMainLooper() { |
| 128 | synchronized (Looper.class) { |
| 129 | return sMainLooper; |
| 130 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 134 | * Set the transaction observer for all Loopers in this process. |
| 135 | * |
| 136 | * @hide |
| 137 | */ |
| 138 | public static void setObserver(@Nullable Observer observer) { |
| 139 | sObserver = observer; |
| 140 | } |
| 141 | |
| 142 | /** |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 143 | * Run the message queue in this thread. Be sure to call |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | * {@link #quit()} to end the loop. |
| 145 | */ |
Romain Guy | f928469 | 2011-07-13 18:46:21 -0700 | [diff] [blame] | 146 | public static void loop() { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 147 | final Looper me = myLooper(); |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 148 | if (me == null) { |
| 149 | throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread."); |
| 150 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 151 | final MessageQueue queue = me.mQueue; |
| 152 | |
Dianne Hackborn | e5dea75 | 2011-02-09 14:19:23 -0800 | [diff] [blame] | 153 | // Make sure the identity of this thread is that of the local process, |
| 154 | // and keep track of what that identity token actually is. |
| 155 | Binder.clearCallingIdentity(); |
| 156 | final long ident = Binder.clearCallingIdentity(); |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 157 | |
Makoto Onuki | 712886f | 2018-04-27 15:22:50 -0700 | [diff] [blame] | 158 | // Allow overriding a threshold with a system prop. e.g. |
| 159 | // adb shell 'setprop log.looper.1000.main.slow 1 && stop && start' |
| 160 | final int thresholdOverride = |
| 161 | SystemProperties.getInt("log.looper." |
| 162 | + Process.myUid() + "." |
| 163 | + Thread.currentThread().getName() |
| 164 | + ".slow", 0); |
| 165 | |
| 166 | boolean slowDeliveryDetected = false; |
| 167 | |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 168 | for (;;) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | Message msg = queue.next(); // might block |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 170 | if (msg == null) { |
| 171 | // No message indicates that the message queue is quitting. |
| 172 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 174 | |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 175 | // This must be in a local variable, in case a UI event sets the logger |
Jeff Sharkey | 74cd3de | 2016-04-06 17:40:54 -0600 | [diff] [blame] | 176 | final Printer logging = me.mLogging; |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 177 | if (logging != null) { |
| 178 | logging.println(">>>>> Dispatching to " + msg.target + " " + |
| 179 | msg.callback + ": " + msg.what); |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 180 | } |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 181 | // Make sure the observer won't change while processing a transaction. |
| 182 | final Observer observer = sObserver; |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 183 | |
Jeff Sharkey | 74cd3de | 2016-04-06 17:40:54 -0600 | [diff] [blame] | 184 | final long traceTag = me.mTraceTag; |
Makoto Onuki | 712886f | 2018-04-27 15:22:50 -0700 | [diff] [blame] | 185 | long slowDispatchThresholdMs = me.mSlowDispatchThresholdMs; |
| 186 | long slowDeliveryThresholdMs = me.mSlowDeliveryThresholdMs; |
| 187 | if (thresholdOverride > 0) { |
| 188 | slowDispatchThresholdMs = thresholdOverride; |
| 189 | slowDeliveryThresholdMs = thresholdOverride; |
| 190 | } |
| 191 | final boolean logSlowDelivery = (slowDeliveryThresholdMs > 0) && (msg.when > 0); |
| 192 | final boolean logSlowDispatch = (slowDispatchThresholdMs > 0); |
| 193 | |
| 194 | final boolean needStartTime = logSlowDelivery || logSlowDispatch; |
| 195 | final boolean needEndTime = logSlowDispatch; |
| 196 | |
Jorim Jaggi | 407c0be | 2016-08-01 13:31:55 +0200 | [diff] [blame] | 197 | if (traceTag != 0 && Trace.isTagEnabled(traceTag)) { |
Jeff Sharkey | 74cd3de | 2016-04-06 17:40:54 -0600 | [diff] [blame] | 198 | Trace.traceBegin(traceTag, msg.target.getTraceName(msg)); |
| 199 | } |
Makoto Onuki | 712886f | 2018-04-27 15:22:50 -0700 | [diff] [blame] | 200 | |
| 201 | final long dispatchStart = needStartTime ? SystemClock.uptimeMillis() : 0; |
| 202 | final long dispatchEnd; |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 203 | Object token = null; |
| 204 | if (observer != null) { |
| 205 | token = observer.messageDispatchStarting(); |
| 206 | } |
Jeff Sharkey | 74cd3de | 2016-04-06 17:40:54 -0600 | [diff] [blame] | 207 | try { |
Marcin Oczeretko | ec75872 | 2018-09-12 12:53:47 +0100 | [diff] [blame^] | 208 | ThreadLocalWorkSourceUid.set(msg.workSourceUid); |
Jeff Sharkey | 74cd3de | 2016-04-06 17:40:54 -0600 | [diff] [blame] | 209 | msg.target.dispatchMessage(msg); |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 210 | if (observer != null) { |
| 211 | observer.messageDispatched(token, msg); |
| 212 | } |
Makoto Onuki | 712886f | 2018-04-27 15:22:50 -0700 | [diff] [blame] | 213 | dispatchEnd = needEndTime ? SystemClock.uptimeMillis() : 0; |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 214 | } catch (Exception exception) { |
| 215 | if (observer != null) { |
| 216 | observer.dispatchingThrewException(token, msg, exception); |
| 217 | } |
| 218 | throw exception; |
Jeff Sharkey | 74cd3de | 2016-04-06 17:40:54 -0600 | [diff] [blame] | 219 | } finally { |
Marcin Oczeretko | ec75872 | 2018-09-12 12:53:47 +0100 | [diff] [blame^] | 220 | ThreadLocalWorkSourceUid.clear(); |
Jeff Sharkey | 74cd3de | 2016-04-06 17:40:54 -0600 | [diff] [blame] | 221 | if (traceTag != 0) { |
| 222 | Trace.traceEnd(traceTag); |
| 223 | } |
| 224 | } |
Makoto Onuki | 712886f | 2018-04-27 15:22:50 -0700 | [diff] [blame] | 225 | if (logSlowDelivery) { |
| 226 | if (slowDeliveryDetected) { |
| 227 | if ((dispatchStart - msg.when) <= 10) { |
| 228 | Slog.w(TAG, "Drained"); |
| 229 | slowDeliveryDetected = false; |
| 230 | } |
| 231 | } else { |
| 232 | if (showSlowLog(slowDeliveryThresholdMs, msg.when, dispatchStart, "delivery", |
| 233 | msg)) { |
| 234 | // Once we write a slow delivery log, suppress until the queue drains. |
| 235 | slowDeliveryDetected = true; |
| 236 | } |
Makoto Onuki | 9957151 | 2017-03-28 14:12:34 -0700 | [diff] [blame] | 237 | } |
| 238 | } |
Makoto Onuki | 712886f | 2018-04-27 15:22:50 -0700 | [diff] [blame] | 239 | if (logSlowDispatch) { |
| 240 | showSlowLog(slowDispatchThresholdMs, dispatchStart, dispatchEnd, "dispatch", msg); |
| 241 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 242 | |
| 243 | if (logging != null) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 244 | logging.println("<<<<< Finished to " + msg.target + " " + msg.callback); |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // Make sure that during the course of dispatching the |
| 248 | // identity of the thread wasn't corrupted. |
| 249 | final long newIdent = Binder.clearCallingIdentity(); |
| 250 | if (ident != newIdent) { |
| 251 | Log.wtf(TAG, "Thread identity changed from 0x" |
| 252 | + Long.toHexString(ident) + " to 0x" |
| 253 | + Long.toHexString(newIdent) + " while dispatching to " |
| 254 | + msg.target.getClass().getName() + " " |
| 255 | + msg.callback + " what=" + msg.what); |
| 256 | } |
| 257 | |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 258 | msg.recycleUnchecked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
Makoto Onuki | 712886f | 2018-04-27 15:22:50 -0700 | [diff] [blame] | 262 | private static boolean showSlowLog(long threshold, long measureStart, long measureEnd, |
| 263 | String what, Message msg) { |
| 264 | final long actualTime = measureEnd - measureStart; |
| 265 | if (actualTime < threshold) { |
| 266 | return false; |
| 267 | } |
| 268 | // For slow delivery, the current message isn't really important, but log it anyway. |
| 269 | Slog.w(TAG, "Slow " + what + " took " + actualTime + "ms " |
| 270 | + Thread.currentThread().getName() + " h=" |
| 271 | + msg.target.getClass().getName() + " c=" + msg.callback + " m=" + msg.what); |
| 272 | return true; |
| 273 | } |
| 274 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | /** |
| 276 | * Return the Looper object associated with the current thread. Returns |
| 277 | * null if the calling thread is not associated with a Looper. |
| 278 | */ |
Jeff Brown | 803c2af | 2015-03-05 10:52:53 -0800 | [diff] [blame] | 279 | public static @Nullable Looper myLooper() { |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 280 | return sThreadLocal.get(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | * Return the {@link MessageQueue} object associated with the current |
| 285 | * thread. This must be called from a thread running a Looper, or a |
| 286 | * NullPointerException will be thrown. |
| 287 | */ |
Jeff Brown | 803c2af | 2015-03-05 10:52:53 -0800 | [diff] [blame] | 288 | public static @NonNull MessageQueue myQueue() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | return myLooper().mQueue; |
| 290 | } |
| 291 | |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 292 | private Looper(boolean quitAllowed) { |
| 293 | mQueue = new MessageQueue(quitAllowed); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | mThread = Thread.currentThread(); |
| 295 | } |
| 296 | |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 297 | /** |
Jeff Brown | f9e989d | 2013-04-04 23:04:03 -0700 | [diff] [blame] | 298 | * Returns true if the current thread is this looper's thread. |
Jeff Brown | f9e989d | 2013-04-04 23:04:03 -0700 | [diff] [blame] | 299 | */ |
| 300 | public boolean isCurrentThread() { |
| 301 | return Thread.currentThread() == mThread; |
| 302 | } |
| 303 | |
| 304 | /** |
Jeff Brown | 803c2af | 2015-03-05 10:52:53 -0800 | [diff] [blame] | 305 | * Control logging of messages as they are processed by this Looper. If |
| 306 | * enabled, a log message will be written to <var>printer</var> |
| 307 | * at the beginning and ending of each message dispatch, identifying the |
| 308 | * target Handler and message contents. |
| 309 | * |
| 310 | * @param printer A Printer object that will receive log messages, or |
| 311 | * null to disable message logging. |
| 312 | */ |
| 313 | public void setMessageLogging(@Nullable Printer printer) { |
| 314 | mLogging = printer; |
| 315 | } |
| 316 | |
Jeff Sharkey | 74cd3de | 2016-04-06 17:40:54 -0600 | [diff] [blame] | 317 | /** {@hide} */ |
| 318 | public void setTraceTag(long traceTag) { |
| 319 | mTraceTag = traceTag; |
| 320 | } |
| 321 | |
Makoto Onuki | 712886f | 2018-04-27 15:22:50 -0700 | [diff] [blame] | 322 | /** |
| 323 | * Set a thresholds for slow dispatch/delivery log. |
| 324 | * {@hide} |
| 325 | */ |
| 326 | public void setSlowLogThresholdMs(long slowDispatchThresholdMs, long slowDeliveryThresholdMs) { |
Makoto Onuki | 9957151 | 2017-03-28 14:12:34 -0700 | [diff] [blame] | 327 | mSlowDispatchThresholdMs = slowDispatchThresholdMs; |
Makoto Onuki | 712886f | 2018-04-27 15:22:50 -0700 | [diff] [blame] | 328 | mSlowDeliveryThresholdMs = slowDeliveryThresholdMs; |
Makoto Onuki | 9957151 | 2017-03-28 14:12:34 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Jeff Brown | 803c2af | 2015-03-05 10:52:53 -0800 | [diff] [blame] | 331 | /** |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 332 | * Quits the looper. |
Jeff Brown | 024136f | 2013-04-11 19:21:32 -0700 | [diff] [blame] | 333 | * <p> |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 334 | * Causes the {@link #loop} method to terminate without processing any |
| 335 | * more messages in the message queue. |
Jeff Brown | 024136f | 2013-04-11 19:21:32 -0700 | [diff] [blame] | 336 | * </p><p> |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 337 | * Any attempt to post messages to the queue after the looper is asked to quit will fail. |
| 338 | * For example, the {@link Handler#sendMessage(Message)} method will return false. |
| 339 | * </p><p class="note"> |
| 340 | * Using this method may be unsafe because some messages may not be delivered |
| 341 | * before the looper terminates. Consider using {@link #quitSafely} instead to ensure |
| 342 | * that all pending work is completed in an orderly manner. |
Jeff Brown | 024136f | 2013-04-11 19:21:32 -0700 | [diff] [blame] | 343 | * </p> |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 344 | * |
| 345 | * @see #quitSafely |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 346 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | public void quit() { |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 348 | mQueue.quit(false); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Quits the looper safely. |
| 353 | * <p> |
| 354 | * Causes the {@link #loop} method to terminate as soon as all remaining messages |
| 355 | * in the message queue that are already due to be delivered have been handled. |
| 356 | * However pending delayed messages with due times in the future will not be |
| 357 | * delivered before the loop terminates. |
| 358 | * </p><p> |
| 359 | * Any attempt to post messages to the queue after the looper is asked to quit will fail. |
| 360 | * For example, the {@link Handler#sendMessage(Message)} method will return false. |
| 361 | * </p> |
| 362 | */ |
| 363 | public void quitSafely() { |
| 364 | mQueue.quit(true); |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /** |
Jeff Brown | 803c2af | 2015-03-05 10:52:53 -0800 | [diff] [blame] | 368 | * Gets the Thread associated with this Looper. |
| 369 | * |
| 370 | * @return The looper's thread. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | */ |
Jeff Brown | 803c2af | 2015-03-05 10:52:53 -0800 | [diff] [blame] | 372 | public @NonNull Thread getThread() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | return mThread; |
| 374 | } |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 375 | |
Jeff Brown | 803c2af | 2015-03-05 10:52:53 -0800 | [diff] [blame] | 376 | /** |
| 377 | * Gets this looper's message queue. |
| 378 | * |
| 379 | * @return The looper's message queue. |
| 380 | */ |
| 381 | public @NonNull MessageQueue getQueue() { |
Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 382 | return mQueue; |
| 383 | } |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 384 | |
Jeff Brown | 803c2af | 2015-03-05 10:52:53 -0800 | [diff] [blame] | 385 | /** |
| 386 | * Dumps the state of the looper for debugging purposes. |
| 387 | * |
| 388 | * @param pw A printer to receive the contents of the dump. |
| 389 | * @param prefix A prefix to prepend to each line which is printed. |
| 390 | */ |
| 391 | public void dump(@NonNull Printer pw, @NonNull String prefix) { |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 392 | pw.println(prefix + toString()); |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 393 | mQueue.dump(pw, prefix + " ", null); |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Dumps the state of the looper for debugging purposes. |
| 398 | * |
| 399 | * @param pw A printer to receive the contents of the dump. |
| 400 | * @param prefix A prefix to prepend to each line which is printed. |
| 401 | * @param handler Only dump messages for this Handler. |
| 402 | * @hide |
| 403 | */ |
| 404 | public void dump(@NonNull Printer pw, @NonNull String prefix, Handler handler) { |
| 405 | pw.println(prefix + toString()); |
| 406 | mQueue.dump(pw, prefix + " ", handler); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | } |
| 408 | |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 409 | /** @hide */ |
| 410 | public void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 411 | final long looperToken = proto.start(fieldId); |
| 412 | proto.write(LooperProto.THREAD_NAME, mThread.getName()); |
| 413 | proto.write(LooperProto.THREAD_ID, mThread.getId()); |
Kweku Adams | 15afdeb | 2018-09-04 11:54:46 -0700 | [diff] [blame] | 414 | if (mQueue != null) { |
| 415 | mQueue.writeToProto(proto, LooperProto.QUEUE); |
| 416 | } |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 417 | proto.end(looperToken); |
| 418 | } |
| 419 | |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 420 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | public String toString() { |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 422 | return "Looper (" + mThread.getName() + ", tid " + mThread.getId() |
| 423 | + ") {" + Integer.toHexString(System.identityHashCode(this)) + "}"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | } |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 425 | |
| 426 | /** {@hide} */ |
| 427 | public interface Observer { |
| 428 | /** |
| 429 | * Called right before a message is dispatched. |
| 430 | * |
| 431 | * <p> The token type is not specified to allow the implementation to specify its own type. |
| 432 | * |
| 433 | * @return a token used for collecting telemetry when dispatching a single message. |
| 434 | * The token token must be passed back exactly once to either |
| 435 | * {@link Observer#messageDispatched} or {@link Observer#dispatchingThrewException} |
| 436 | * and must not be reused again. |
| 437 | * |
| 438 | */ |
| 439 | Object messageDispatchStarting(); |
| 440 | |
| 441 | /** |
| 442 | * Called when a message was processed by a Handler. |
| 443 | * |
| 444 | * @param token Token obtained by previously calling |
| 445 | * {@link Observer#messageDispatchStarting} on the same Observer instance. |
| 446 | * @param msg The message that was dispatched. |
| 447 | */ |
| 448 | void messageDispatched(Object token, Message msg); |
| 449 | |
| 450 | /** |
| 451 | * Called when an exception was thrown while processing a message. |
| 452 | * |
| 453 | * @param token Token obtained by previously calling |
| 454 | * {@link Observer#messageDispatchStarting} on the same Observer instance. |
| 455 | * @param msg The message that was dispatched and caused an exception. |
| 456 | * @param exception The exception that was thrown. |
| 457 | */ |
| 458 | void dispatchingThrewException(Object token, Message msg, Exception exception); |
| 459 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | } |