blob: 6d7c9cfeb6b5688263346690b2ccdc3ba3fa20b6 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.os;
18
Brad Fitzpatrick1b298252010-11-23 17:16:47 -080019import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.util.Printer;
21
22/**
23 * Class used to run a message loop for a thread. Threads by default do
24 * not have a message loop associated with them; to create one, call
25 * {@link #prepare} in the thread that is to run the loop, and then
26 * {@link #loop} to have it process messages until the loop is stopped.
27 *
28 * <p>Most interaction with a message loop is through the
29 * {@link Handler} class.
30 *
31 * <p>This is a typical example of the implementation of a Looper thread,
32 * using the separation of {@link #prepare} and {@link #loop} to create an
33 * initial Handler to communicate with the Looper.
Brad Fitzpatrick1b298252010-11-23 17:16:47 -080034 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035 * <pre>
36 * class LooperThread extends Thread {
37 * public Handler mHandler;
Brad Fitzpatrick1b298252010-11-23 17:16:47 -080038 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 * public void run() {
40 * Looper.prepare();
Brad Fitzpatrick1b298252010-11-23 17:16:47 -080041 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 * mHandler = new Handler() {
43 * public void handleMessage(Message msg) {
44 * // process incoming messages here
45 * }
46 * };
Brad Fitzpatrick1b298252010-11-23 17:16:47 -080047 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 * Looper.loop();
49 * }
50 * }</pre>
51 */
Jeff Brown67fc67c2013-04-01 13:00:33 -070052public final class Looper {
Brad Fitzpatrick1b298252010-11-23 17:16:47 -080053 private static final String TAG = "Looper";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
55 // sThreadLocal.get() will return null unless you've called prepare().
Xavier Ducrohet7f9f99ea2011-08-11 10:16:17 -070056 static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();
Jeff Brown0f85ce32012-02-16 14:41:10 -080057 private static Looper sMainLooper; // guarded by Looper.class
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058
59 final MessageQueue mQueue;
Brad Fitzpatrick1b298252010-11-23 17:16:47 -080060 final Thread mThread;
Brad Fitzpatrick1b298252010-11-23 17:16:47 -080061
Jeff Brown0f85ce32012-02-16 14:41:10 -080062 private Printer mLogging;
Brad Fitzpatrick1b298252010-11-23 17:16:47 -080063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 /** Initialize the current thread as a looper.
65 * This gives you a chance to create handlers that then reference
66 * this looper, before actually starting the loop. Be sure to call
67 * {@link #loop()} after calling this method, and end it by calling
68 * {@link #quit()}.
69 */
Romain Guyf9284692011-07-13 18:46:21 -070070 public static void prepare() {
Jeff Brown0f85ce32012-02-16 14:41:10 -080071 prepare(true);
72 }
73
74 private static void prepare(boolean quitAllowed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 if (sThreadLocal.get() != null) {
76 throw new RuntimeException("Only one Looper may be created per thread");
77 }
Jeff Brown0f85ce32012-02-16 14:41:10 -080078 sThreadLocal.set(new Looper(quitAllowed));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 }
Brad Fitzpatrick1b298252010-11-23 17:16:47 -080080
81 /**
82 * Initialize the current thread as a looper, marking it as an
83 * application's main looper. The main looper for your application
84 * is created by the Android environment, so you should never need
85 * to call this function yourself. See also: {@link #prepare()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 */
Romain Guyf9284692011-07-13 18:46:21 -070087 public static void prepareMainLooper() {
Jeff Brown0f85ce32012-02-16 14:41:10 -080088 prepare(false);
89 synchronized (Looper.class) {
90 if (sMainLooper != null) {
91 throw new IllegalStateException("The main Looper has already been prepared.");
92 }
93 sMainLooper = myLooper();
94 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 }
Brad Fitzpatrick1b298252010-11-23 17:16:47 -080096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 /** Returns the application's main looper, which lives in the main thread of the application.
98 */
Jeff Brown0f85ce32012-02-16 14:41:10 -080099 public static Looper getMainLooper() {
100 synchronized (Looper.class) {
101 return sMainLooper;
102 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 }
104
105 /**
Brad Fitzpatrick1b298252010-11-23 17:16:47 -0800106 * Run the message queue in this thread. Be sure to call
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * {@link #quit()} to end the loop.
108 */
Romain Guyf9284692011-07-13 18:46:21 -0700109 public static void loop() {
Jeff Brown0f85ce32012-02-16 14:41:10 -0800110 final Looper me = myLooper();
Brad Fitzpatrick1b298252010-11-23 17:16:47 -0800111 if (me == null) {
112 throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
113 }
Jeff Brown0f85ce32012-02-16 14:41:10 -0800114 final MessageQueue queue = me.mQueue;
115
Dianne Hackborne5dea752011-02-09 14:19:23 -0800116 // Make sure the identity of this thread is that of the local process,
117 // and keep track of what that identity token actually is.
118 Binder.clearCallingIdentity();
119 final long ident = Binder.clearCallingIdentity();
Jeff Brown0f85ce32012-02-16 14:41:10 -0800120
121 for (;;) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 Message msg = queue.next(); // might block
Jeff Brown0f85ce32012-02-16 14:41:10 -0800123 if (msg == null) {
124 // No message indicates that the message queue is quitting.
125 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 }
Jeff Brown0f85ce32012-02-16 14:41:10 -0800127
Jeff Brown0f85ce32012-02-16 14:41:10 -0800128 // This must be in a local variable, in case a UI event sets the logger
129 Printer logging = me.mLogging;
130 if (logging != null) {
131 logging.println(">>>>> Dispatching to " + msg.target + " " +
132 msg.callback + ": " + msg.what);
Jeff Brown0f85ce32012-02-16 14:41:10 -0800133 }
134
135 msg.target.dispatchMessage(msg);
136
137 if (logging != null) {
Jeff Brown0f85ce32012-02-16 14:41:10 -0800138 logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
Jeff Brown0f85ce32012-02-16 14:41:10 -0800139 }
140
141 // Make sure that during the course of dispatching the
142 // identity of the thread wasn't corrupted.
143 final long newIdent = Binder.clearCallingIdentity();
144 if (ident != newIdent) {
145 Log.wtf(TAG, "Thread identity changed from 0x"
146 + Long.toHexString(ident) + " to 0x"
147 + Long.toHexString(newIdent) + " while dispatching to "
148 + msg.target.getClass().getName() + " "
149 + msg.callback + " what=" + msg.what);
150 }
151
Jeff Brown9867ed72014-02-28 14:00:57 -0800152 msg.recycleUnchecked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 }
154 }
155
156 /**
157 * Return the Looper object associated with the current thread. Returns
158 * null if the calling thread is not associated with a Looper.
159 */
Romain Guyf9284692011-07-13 18:46:21 -0700160 public static Looper myLooper() {
Brad Fitzpatrick1b298252010-11-23 17:16:47 -0800161 return sThreadLocal.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 }
163
164 /**
165 * Control logging of messages as they are processed by this Looper. If
166 * enabled, a log message will be written to <var>printer</var>
167 * at the beginning and ending of each message dispatch, identifying the
168 * target Handler and message contents.
169 *
170 * @param printer A Printer object that will receive log messages, or
171 * null to disable message logging.
172 */
173 public void setMessageLogging(Printer printer) {
174 mLogging = printer;
175 }
176
177 /**
178 * Return the {@link MessageQueue} object associated with the current
179 * thread. This must be called from a thread running a Looper, or a
180 * NullPointerException will be thrown.
181 */
Romain Guyf9284692011-07-13 18:46:21 -0700182 public static MessageQueue myQueue() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 return myLooper().mQueue;
184 }
185
Jeff Brown0f85ce32012-02-16 14:41:10 -0800186 private Looper(boolean quitAllowed) {
187 mQueue = new MessageQueue(quitAllowed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 mThread = Thread.currentThread();
189 }
190
Jeff Brown0f85ce32012-02-16 14:41:10 -0800191 /**
Jeff Brownf9e989d2013-04-04 23:04:03 -0700192 * Returns true if the current thread is this looper's thread.
193 * @hide
194 */
195 public boolean isCurrentThread() {
196 return Thread.currentThread() == mThread;
197 }
198
199 /**
Jeff Brown0f85ce32012-02-16 14:41:10 -0800200 * Quits the looper.
Jeff Brown024136f2013-04-11 19:21:32 -0700201 * <p>
Jeff Brown8b60e452013-04-18 15:17:48 -0700202 * Causes the {@link #loop} method to terminate without processing any
203 * more messages in the message queue.
Jeff Brown024136f2013-04-11 19:21:32 -0700204 * </p><p>
Jeff Brown8b60e452013-04-18 15:17:48 -0700205 * Any attempt to post messages to the queue after the looper is asked to quit will fail.
206 * For example, the {@link Handler#sendMessage(Message)} method will return false.
207 * </p><p class="note">
208 * Using this method may be unsafe because some messages may not be delivered
209 * before the looper terminates. Consider using {@link #quitSafely} instead to ensure
210 * that all pending work is completed in an orderly manner.
Jeff Brown024136f2013-04-11 19:21:32 -0700211 * </p>
Jeff Brown8b60e452013-04-18 15:17:48 -0700212 *
213 * @see #quitSafely
Jeff Brown0f85ce32012-02-16 14:41:10 -0800214 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 public void quit() {
Jeff Brown8b60e452013-04-18 15:17:48 -0700216 mQueue.quit(false);
217 }
218
219 /**
220 * Quits the looper safely.
221 * <p>
222 * Causes the {@link #loop} method to terminate as soon as all remaining messages
223 * in the message queue that are already due to be delivered have been handled.
224 * However pending delayed messages with due times in the future will not be
225 * delivered before the loop terminates.
226 * </p><p>
227 * Any attempt to post messages to the queue after the looper is asked to quit will fail.
228 * For example, the {@link Handler#sendMessage(Message)} method will return false.
229 * </p>
230 */
231 public void quitSafely() {
232 mQueue.quit(true);
Jeff Brown0f85ce32012-02-16 14:41:10 -0800233 }
234
235 /**
236 * Posts a synchronization barrier to the Looper's message queue.
237 *
238 * Message processing occurs as usual until the message queue encounters the
239 * synchronization barrier that has been posted. When the barrier is encountered,
240 * later synchronous messages in the queue are stalled (prevented from being executed)
241 * until the barrier is released by calling {@link #removeSyncBarrier} and specifying
242 * the token that identifies the synchronization barrier.
243 *
244 * This method is used to immediately postpone execution of all subsequently posted
245 * synchronous messages until a condition is met that releases the barrier.
246 * Asynchronous messages (see {@link Message#isAsynchronous} are exempt from the barrier
247 * and continue to be processed as usual.
248 *
249 * This call must be always matched by a call to {@link #removeSyncBarrier} with
250 * the same token to ensure that the message queue resumes normal operation.
251 * Otherwise the application will probably hang!
252 *
253 * @return A token that uniquely identifies the barrier. This token must be
254 * passed to {@link #removeSyncBarrier} to release the barrier.
255 *
256 * @hide
257 */
Jeff Brown67fc67c2013-04-01 13:00:33 -0700258 public int postSyncBarrier() {
Jeff Brown0f85ce32012-02-16 14:41:10 -0800259 return mQueue.enqueueSyncBarrier(SystemClock.uptimeMillis());
260 }
261
262
263 /**
264 * Removes a synchronization barrier.
265 *
266 * @param token The synchronization barrier token that was returned by
267 * {@link #postSyncBarrier}.
268 *
269 * @throws IllegalStateException if the barrier was not found.
270 *
271 * @hide
272 */
Jeff Brown67fc67c2013-04-01 13:00:33 -0700273 public void removeSyncBarrier(int token) {
Jeff Brown0f85ce32012-02-16 14:41:10 -0800274 mQueue.removeSyncBarrier(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 }
276
277 /**
278 * Return the Thread associated with this Looper.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 */
280 public Thread getThread() {
281 return mThread;
282 }
Brad Fitzpatrick1b298252010-11-23 17:16:47 -0800283
Jeff Browna41ca772010-08-11 14:46:32 -0700284 /** @hide */
285 public MessageQueue getQueue() {
286 return mQueue;
287 }
Brad Fitzpatrick1b298252010-11-23 17:16:47 -0800288
Dianne Hackbornefa92b22013-05-03 14:11:43 -0700289 /**
290 * Return whether this looper's thread is currently idle, waiting for new work
291 * to do. This is intrinsically racy, since its state can change before you get
292 * the result back.
293 * @hide
294 */
295 public boolean isIdling() {
296 return mQueue.isIdling();
297 }
298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 public void dump(Printer pw, String prefix) {
Jeff Brown5182c782013-10-15 20:31:52 -0700300 pw.println(prefix + toString());
301 mQueue.dump(pw, prefix + " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 }
303
304 public String toString() {
Jeff Brown5182c782013-10-15 20:31:52 -0700305 return "Looper (" + mThread.getName() + ", tid " + mThread.getId()
306 + ") {" + Integer.toHexString(System.identityHashCode(this)) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308}