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 | |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 19 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.util.Printer; |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 21 | import android.util.PrefixPrinter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * Class used to run a message loop for a thread. Threads by default do |
| 25 | * not have a message loop associated with them; to create one, call |
| 26 | * {@link #prepare} in the thread that is to run the loop, and then |
| 27 | * {@link #loop} to have it process messages until the loop is stopped. |
| 28 | * |
| 29 | * <p>Most interaction with a message loop is through the |
| 30 | * {@link Handler} class. |
| 31 | * |
| 32 | * <p>This is a typical example of the implementation of a Looper thread, |
| 33 | * using the separation of {@link #prepare} and {@link #loop} to create an |
| 34 | * initial Handler to communicate with the Looper. |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 35 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | * <pre> |
| 37 | * class LooperThread extends Thread { |
| 38 | * public Handler mHandler; |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 39 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | * public void run() { |
| 41 | * Looper.prepare(); |
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 | * mHandler = new Handler() { |
| 44 | * public void handleMessage(Message msg) { |
| 45 | * // process incoming messages here |
| 46 | * } |
| 47 | * }; |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 48 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | * Looper.loop(); |
| 50 | * } |
| 51 | * }</pre> |
| 52 | */ |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 53 | public final class Looper { |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 54 | private static final String TAG = "Looper"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | |
| 56 | // sThreadLocal.get() will return null unless you've called prepare(). |
Xavier Ducrohet | 7f9f99ea | 2011-08-11 10:16:17 -0700 | [diff] [blame] | 57 | static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>(); |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 58 | private static Looper sMainLooper; // guarded by Looper.class |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | |
| 60 | final MessageQueue mQueue; |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 61 | final Thread mThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | volatile boolean mRun; |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 63 | |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 64 | private Printer mLogging; |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 65 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | /** Initialize the current thread as a looper. |
| 67 | * This gives you a chance to create handlers that then reference |
| 68 | * this looper, before actually starting the loop. Be sure to call |
| 69 | * {@link #loop()} after calling this method, and end it by calling |
| 70 | * {@link #quit()}. |
| 71 | */ |
Romain Guy | f928469 | 2011-07-13 18:46:21 -0700 | [diff] [blame] | 72 | public static void prepare() { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 73 | prepare(true); |
| 74 | } |
| 75 | |
| 76 | private static void prepare(boolean quitAllowed) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | if (sThreadLocal.get() != null) { |
| 78 | throw new RuntimeException("Only one Looper may be created per thread"); |
| 79 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 80 | sThreadLocal.set(new Looper(quitAllowed)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | } |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * Initialize the current thread as a looper, marking it as an |
| 85 | * application's main looper. The main looper for your application |
| 86 | * is created by the Android environment, so you should never need |
| 87 | * to call this function yourself. See also: {@link #prepare()} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | */ |
Romain Guy | f928469 | 2011-07-13 18:46:21 -0700 | [diff] [blame] | 89 | public static void prepareMainLooper() { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 90 | prepare(false); |
| 91 | synchronized (Looper.class) { |
| 92 | if (sMainLooper != null) { |
| 93 | throw new IllegalStateException("The main Looper has already been prepared."); |
| 94 | } |
| 95 | sMainLooper = myLooper(); |
| 96 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | } |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 98 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | /** Returns the application's main looper, which lives in the main thread of the application. |
| 100 | */ |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 101 | public static Looper getMainLooper() { |
| 102 | synchronized (Looper.class) { |
| 103 | return sMainLooper; |
| 104 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 108 | * 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] | 109 | * {@link #quit()} to end the loop. |
| 110 | */ |
Romain Guy | f928469 | 2011-07-13 18:46:21 -0700 | [diff] [blame] | 111 | public static void loop() { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 112 | final Looper me = myLooper(); |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 113 | if (me == null) { |
| 114 | throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread."); |
| 115 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 116 | final MessageQueue queue = me.mQueue; |
| 117 | |
Dianne Hackborn | e5dea75 | 2011-02-09 14:19:23 -0800 | [diff] [blame] | 118 | // Make sure the identity of this thread is that of the local process, |
| 119 | // and keep track of what that identity token actually is. |
| 120 | Binder.clearCallingIdentity(); |
| 121 | final long ident = Binder.clearCallingIdentity(); |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 122 | |
| 123 | for (;;) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | Message msg = queue.next(); // might block |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 125 | if (msg == null) { |
| 126 | // No message indicates that the message queue is quitting. |
| 127 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 129 | |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 130 | // This must be in a local variable, in case a UI event sets the logger |
| 131 | Printer logging = me.mLogging; |
| 132 | if (logging != null) { |
| 133 | logging.println(">>>>> Dispatching to " + msg.target + " " + |
| 134 | msg.callback + ": " + msg.what); |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | msg.target.dispatchMessage(msg); |
| 138 | |
| 139 | if (logging != null) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 140 | logging.println("<<<<< Finished to " + msg.target + " " + msg.callback); |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // Make sure that during the course of dispatching the |
| 144 | // identity of the thread wasn't corrupted. |
| 145 | final long newIdent = Binder.clearCallingIdentity(); |
| 146 | if (ident != newIdent) { |
| 147 | Log.wtf(TAG, "Thread identity changed from 0x" |
| 148 | + Long.toHexString(ident) + " to 0x" |
| 149 | + Long.toHexString(newIdent) + " while dispatching to " |
| 150 | + msg.target.getClass().getName() + " " |
| 151 | + msg.callback + " what=" + msg.what); |
| 152 | } |
| 153 | |
| 154 | msg.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Return the Looper object associated with the current thread. Returns |
| 160 | * null if the calling thread is not associated with a Looper. |
| 161 | */ |
Romain Guy | f928469 | 2011-07-13 18:46:21 -0700 | [diff] [blame] | 162 | public static Looper myLooper() { |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 163 | return sThreadLocal.get(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Control logging of messages as they are processed by this Looper. If |
| 168 | * enabled, a log message will be written to <var>printer</var> |
| 169 | * at the beginning and ending of each message dispatch, identifying the |
| 170 | * target Handler and message contents. |
| 171 | * |
| 172 | * @param printer A Printer object that will receive log messages, or |
| 173 | * null to disable message logging. |
| 174 | */ |
| 175 | public void setMessageLogging(Printer printer) { |
| 176 | mLogging = printer; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Return the {@link MessageQueue} object associated with the current |
| 181 | * thread. This must be called from a thread running a Looper, or a |
| 182 | * NullPointerException will be thrown. |
| 183 | */ |
Romain Guy | f928469 | 2011-07-13 18:46:21 -0700 | [diff] [blame] | 184 | public static MessageQueue myQueue() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | return myLooper().mQueue; |
| 186 | } |
| 187 | |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 188 | private Looper(boolean quitAllowed) { |
| 189 | mQueue = new MessageQueue(quitAllowed); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | mRun = true; |
| 191 | mThread = Thread.currentThread(); |
| 192 | } |
| 193 | |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 194 | /** |
Jeff Brown | f9e989d | 2013-04-04 23:04:03 -0700 | [diff] [blame] | 195 | * Returns true if the current thread is this looper's thread. |
| 196 | * @hide |
| 197 | */ |
| 198 | public boolean isCurrentThread() { |
| 199 | return Thread.currentThread() == mThread; |
| 200 | } |
| 201 | |
| 202 | /** |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 203 | * Quits the looper. |
Jeff Brown | 024136f | 2013-04-11 19:21:32 -0700 | [diff] [blame^] | 204 | * <p> |
| 205 | * Causes the {@link #loop} method to terminate as soon as all remaining messages |
| 206 | * in the message queue that are already due to be delivered have been handled. |
| 207 | * However delayed messages with due times in the future may not be handled before |
| 208 | * the loop terminates. |
| 209 | * </p><p> |
| 210 | * Any attempt to post messages to the queue after {@link #quit} has been called |
| 211 | * will fail. For example, the {@link Handler#sendMessage(Message)} method will |
| 212 | * return false when the looper is being terminated. |
| 213 | * </p> |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 214 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | public void quit() { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 216 | mQueue.quit(); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Posts a synchronization barrier to the Looper's message queue. |
| 221 | * |
| 222 | * Message processing occurs as usual until the message queue encounters the |
| 223 | * synchronization barrier that has been posted. When the barrier is encountered, |
| 224 | * later synchronous messages in the queue are stalled (prevented from being executed) |
| 225 | * until the barrier is released by calling {@link #removeSyncBarrier} and specifying |
| 226 | * the token that identifies the synchronization barrier. |
| 227 | * |
| 228 | * This method is used to immediately postpone execution of all subsequently posted |
| 229 | * synchronous messages until a condition is met that releases the barrier. |
| 230 | * Asynchronous messages (see {@link Message#isAsynchronous} are exempt from the barrier |
| 231 | * and continue to be processed as usual. |
| 232 | * |
| 233 | * This call must be always matched by a call to {@link #removeSyncBarrier} with |
| 234 | * the same token to ensure that the message queue resumes normal operation. |
| 235 | * Otherwise the application will probably hang! |
| 236 | * |
| 237 | * @return A token that uniquely identifies the barrier. This token must be |
| 238 | * passed to {@link #removeSyncBarrier} to release the barrier. |
| 239 | * |
| 240 | * @hide |
| 241 | */ |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 242 | public int postSyncBarrier() { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 243 | return mQueue.enqueueSyncBarrier(SystemClock.uptimeMillis()); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | /** |
| 248 | * Removes a synchronization barrier. |
| 249 | * |
| 250 | * @param token The synchronization barrier token that was returned by |
| 251 | * {@link #postSyncBarrier}. |
| 252 | * |
| 253 | * @throws IllegalStateException if the barrier was not found. |
| 254 | * |
| 255 | * @hide |
| 256 | */ |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 257 | public void removeSyncBarrier(int token) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 258 | mQueue.removeSyncBarrier(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Return the Thread associated with this Looper. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | */ |
| 264 | public Thread getThread() { |
| 265 | return mThread; |
| 266 | } |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 267 | |
Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 268 | /** @hide */ |
| 269 | public MessageQueue getQueue() { |
| 270 | return mQueue; |
| 271 | } |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 272 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | public void dump(Printer pw, String prefix) { |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 274 | pw = PrefixPrinter.create(pw, prefix); |
| 275 | pw.println(this.toString()); |
| 276 | pw.println("mRun=" + mRun); |
| 277 | pw.println("mThread=" + mThread); |
| 278 | pw.println("mQueue=" + ((mQueue != null) ? mQueue : "(null")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | if (mQueue != null) { |
| 280 | synchronized (mQueue) { |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 281 | long now = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | Message msg = mQueue.mMessages; |
| 283 | int n = 0; |
| 284 | while (msg != null) { |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 285 | pw.println(" Message " + n + ": " + msg.toString(now)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | n++; |
| 287 | msg = msg.next; |
| 288 | } |
Brad Fitzpatrick | 1b29825 | 2010-11-23 17:16:47 -0800 | [diff] [blame] | 289 | pw.println("(Total messages: " + n + ")"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | public String toString() { |
Romain Guy | f928469 | 2011-07-13 18:46:21 -0700 | [diff] [blame] | 295 | return "Looper{" + Integer.toHexString(System.identityHashCode(this)) + "}"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | } |