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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | import android.util.AndroidRuntimeException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.util.Log; |
| 21 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 22 | import java.util.ArrayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Low-level class holding the list of messages to be dispatched by a |
| 26 | * {@link Looper}. Messages are not added directly to a MessageQueue, |
| 27 | * but rather through {@link Handler} objects associated with the Looper. |
| 28 | * |
| 29 | * <p>You can retrieve the MessageQueue for the current thread with |
| 30 | * {@link Looper#myQueue() Looper.myQueue()}. |
| 31 | */ |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 32 | public final class MessageQueue { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 33 | // True if the message queue can be quit. |
| 34 | private final boolean mQuitAllowed; |
| 35 | |
| 36 | @SuppressWarnings("unused") |
| 37 | private int mPtr; // used by native code |
| 38 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | Message mMessages; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 40 | private final ArrayList<IdleHandler> mIdleHandlers = new ArrayList<IdleHandler>(); |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 41 | private IdleHandler[] mPendingIdleHandlers; |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 42 | private boolean mQuiting; |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 43 | |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 44 | // Indicates whether next() is blocked waiting in pollOnce() with a non-zero timeout. |
| 45 | private boolean mBlocked; |
| 46 | |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 47 | // The next barrier token. |
| 48 | // Barriers are indicated by messages with a null target whose arg1 field carries the token. |
| 49 | private int mNextBarrierToken; |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 50 | |
Jeff Brown | 86469346 | 2013-01-28 14:25:53 -0800 | [diff] [blame] | 51 | private native static int nativeInit(); |
| 52 | private native static void nativeDestroy(int ptr); |
| 53 | private native static void nativePollOnce(int ptr, int timeoutMillis); |
| 54 | private native static void nativeWake(int ptr); |
Dianne Hackborn | efa92b2 | 2013-05-03 14:11:43 -0700 | [diff] [blame] | 55 | private native static boolean nativeIsIdling(int ptr); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 56 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | /** |
| 58 | * Callback interface for discovering when a thread is going to block |
| 59 | * waiting for more messages. |
| 60 | */ |
| 61 | public static interface IdleHandler { |
| 62 | /** |
| 63 | * Called when the message queue has run out of messages and will now |
| 64 | * wait for more. Return true to keep your idle handler active, false |
| 65 | * to have it removed. This may be called if there are still messages |
| 66 | * pending in the queue, but they are all scheduled to be dispatched |
| 67 | * after the current time. |
| 68 | */ |
| 69 | boolean queueIdle(); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Add a new {@link IdleHandler} to this message queue. This may be |
| 74 | * removed automatically for you by returning false from |
| 75 | * {@link IdleHandler#queueIdle IdleHandler.queueIdle()} when it is |
| 76 | * invoked, or explicitly removing it with {@link #removeIdleHandler}. |
| 77 | * |
| 78 | * <p>This method is safe to call from any thread. |
| 79 | * |
| 80 | * @param handler The IdleHandler to be added. |
| 81 | */ |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 82 | public void addIdleHandler(IdleHandler handler) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | if (handler == null) { |
| 84 | throw new NullPointerException("Can't add a null IdleHandler"); |
| 85 | } |
| 86 | synchronized (this) { |
| 87 | mIdleHandlers.add(handler); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Remove an {@link IdleHandler} from the queue that was previously added |
| 93 | * with {@link #addIdleHandler}. If the given object is not currently |
| 94 | * in the idle list, nothing is done. |
| 95 | * |
| 96 | * @param handler The IdleHandler to be removed. |
| 97 | */ |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 98 | public void removeIdleHandler(IdleHandler handler) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | synchronized (this) { |
| 100 | mIdleHandlers.remove(handler); |
| 101 | } |
| 102 | } |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 103 | |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 104 | MessageQueue(boolean quitAllowed) { |
| 105 | mQuitAllowed = quitAllowed; |
Jeff Brown | 86469346 | 2013-01-28 14:25:53 -0800 | [diff] [blame] | 106 | mPtr = nativeInit(); |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 107 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 108 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 109 | @Override |
| 110 | protected void finalize() throws Throwable { |
| 111 | try { |
Jeff Brown | 86469346 | 2013-01-28 14:25:53 -0800 | [diff] [blame] | 112 | dispose(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 113 | } finally { |
| 114 | super.finalize(); |
| 115 | } |
| 116 | } |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 117 | |
Jeff Brown | 86469346 | 2013-01-28 14:25:53 -0800 | [diff] [blame] | 118 | private void dispose() { |
| 119 | if (mPtr != 0) { |
| 120 | nativeDestroy(mPtr); |
| 121 | mPtr = 0; |
| 122 | } |
| 123 | } |
| 124 | |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 125 | Message next() { |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 126 | int pendingIdleHandlerCount = -1; // -1 only during first iteration |
| 127 | int nextPollTimeoutMillis = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 129 | for (;;) { |
| 130 | if (nextPollTimeoutMillis != 0) { |
| 131 | Binder.flushPendingCommands(); |
| 132 | } |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 133 | nativePollOnce(mPtr, nextPollTimeoutMillis); |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 134 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | synchronized (this) { |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 136 | // Try to retrieve the next message. Return if found. |
| 137 | final long now = SystemClock.uptimeMillis(); |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 138 | Message prevMsg = null; |
| 139 | Message msg = mMessages; |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 140 | if (msg != null && msg.target == null) { |
| 141 | // Stalled by a barrier. Find the next asynchronous message in the queue. |
| 142 | do { |
| 143 | prevMsg = msg; |
| 144 | msg = msg.next; |
| 145 | } while (msg != null && !msg.isAsynchronous()); |
| 146 | } |
| 147 | if (msg != null) { |
| 148 | if (now < msg.when) { |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 149 | // Next message is not ready. Set a timeout to wake up when it is ready. |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 150 | nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE); |
| 151 | } else { |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 152 | // Got a message. |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 153 | mBlocked = false; |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 154 | if (prevMsg != null) { |
| 155 | prevMsg.next = msg.next; |
| 156 | } else { |
| 157 | mMessages = msg.next; |
| 158 | } |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 159 | msg.next = null; |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 160 | if (false) Log.v("MessageQueue", "Returning message: " + msg); |
Jeff Brown | 85c3fe4 | 2010-09-21 16:45:16 -0700 | [diff] [blame] | 161 | msg.markInUse(); |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 162 | return msg; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 164 | } else { |
| 165 | // No more messages. |
| 166 | nextPollTimeoutMillis = -1; |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Jeff Brown | 024136f | 2013-04-11 19:21:32 -0700 | [diff] [blame] | 169 | // Process the quit message now that all pending messages have been handled. |
| 170 | if (mQuiting) { |
| 171 | dispose(); |
| 172 | return null; |
| 173 | } |
| 174 | |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 175 | // If first time idle, then get the number of idlers to run. |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 176 | // Idle handles only run if the queue is empty or if the first message |
| 177 | // in the queue (possibly a barrier) is due to be handled in the future. |
| 178 | if (pendingIdleHandlerCount < 0 |
| 179 | && (mMessages == null || now < mMessages.when)) { |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 180 | pendingIdleHandlerCount = mIdleHandlers.size(); |
| 181 | } |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 182 | if (pendingIdleHandlerCount <= 0) { |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 183 | // No idle handlers to run. Loop and wait some more. |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 184 | mBlocked = true; |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 185 | continue; |
| 186 | } |
| 187 | |
| 188 | if (mPendingIdleHandlers == null) { |
| 189 | mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)]; |
| 190 | } |
| 191 | mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers); |
| 192 | } |
| 193 | |
| 194 | // Run the idle handlers. |
| 195 | // We only ever reach this code block during the first iteration. |
| 196 | for (int i = 0; i < pendingIdleHandlerCount; i++) { |
| 197 | final IdleHandler idler = mPendingIdleHandlers[i]; |
| 198 | mPendingIdleHandlers[i] = null; // release the reference to the handler |
| 199 | |
| 200 | boolean keep = false; |
| 201 | try { |
| 202 | keep = idler.queueIdle(); |
| 203 | } catch (Throwable t) { |
| 204 | Log.wtf("MessageQueue", "IdleHandler threw exception", t); |
| 205 | } |
| 206 | |
| 207 | if (!keep) { |
| 208 | synchronized (this) { |
| 209 | mIdleHandlers.remove(idler); |
| 210 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | } |
| 212 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 214 | // Reset the idle handler count to 0 so we do not run them again. |
| 215 | pendingIdleHandlerCount = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 217 | // While calling an idle handler, a new message could have been delivered |
| 218 | // so go back and look again for a pending message without waiting. |
| 219 | nextPollTimeoutMillis = 0; |
| 220 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 223 | void quit(boolean safe) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 224 | if (!mQuitAllowed) { |
| 225 | throw new RuntimeException("Main thread not allowed to quit."); |
| 226 | } |
| 227 | |
| 228 | synchronized (this) { |
| 229 | if (mQuiting) { |
| 230 | return; |
| 231 | } |
| 232 | mQuiting = true; |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 233 | |
| 234 | if (safe) { |
| 235 | removeAllFutureMessagesLocked(); |
| 236 | } else { |
| 237 | removeAllMessagesLocked(); |
| 238 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 239 | } |
| 240 | nativeWake(mPtr); |
| 241 | } |
| 242 | |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 243 | int enqueueSyncBarrier(long when) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 244 | // Enqueue a new sync barrier token. |
| 245 | // We don't need to wake the queue because the purpose of a barrier is to stall it. |
| 246 | synchronized (this) { |
| 247 | final int token = mNextBarrierToken++; |
| 248 | final Message msg = Message.obtain(); |
| 249 | msg.arg1 = token; |
| 250 | |
| 251 | Message prev = null; |
| 252 | Message p = mMessages; |
| 253 | if (when != 0) { |
| 254 | while (p != null && p.when <= when) { |
| 255 | prev = p; |
| 256 | p = p.next; |
| 257 | } |
| 258 | } |
| 259 | if (prev != null) { // invariant: p == prev.next |
| 260 | msg.next = p; |
| 261 | prev.next = msg; |
| 262 | } else { |
| 263 | msg.next = p; |
| 264 | mMessages = msg; |
| 265 | } |
| 266 | return token; |
| 267 | } |
| 268 | } |
| 269 | |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 270 | void removeSyncBarrier(int token) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 271 | // Remove a sync barrier token from the queue. |
| 272 | // If the queue is no longer stalled by a barrier then wake it. |
| 273 | final boolean needWake; |
| 274 | synchronized (this) { |
| 275 | Message prev = null; |
| 276 | Message p = mMessages; |
| 277 | while (p != null && (p.target != null || p.arg1 != token)) { |
| 278 | prev = p; |
| 279 | p = p.next; |
| 280 | } |
| 281 | if (p == null) { |
| 282 | throw new IllegalStateException("The specified message queue synchronization " |
| 283 | + " barrier token has not been posted or has already been removed."); |
| 284 | } |
| 285 | if (prev != null) { |
| 286 | prev.next = p.next; |
| 287 | needWake = false; |
| 288 | } else { |
| 289 | mMessages = p.next; |
| 290 | needWake = mMessages == null || mMessages.target != null; |
| 291 | } |
| 292 | p.recycle(); |
| 293 | } |
| 294 | if (needWake) { |
| 295 | nativeWake(mPtr); |
| 296 | } |
| 297 | } |
| 298 | |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 299 | boolean enqueueMessage(Message msg, long when) { |
Wink Saville | a334e7c | 2010-08-24 10:56:30 -0700 | [diff] [blame] | 300 | if (msg.isInUse()) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 301 | throw new AndroidRuntimeException(msg + " This message is already in use."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 303 | if (msg.target == null) { |
| 304 | throw new AndroidRuntimeException("Message must have a target."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 306 | |
| 307 | boolean needWake; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | synchronized (this) { |
| 309 | if (mQuiting) { |
| 310 | RuntimeException e = new RuntimeException( |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 311 | msg.target + " sending message to a Handler on a dead thread"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | Log.w("MessageQueue", e.getMessage(), e); |
| 313 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | msg.when = when; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | Message p = mMessages; |
| 318 | if (p == null || when == 0 || when < p.when) { |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 319 | // New head, wake up the event queue if blocked. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | msg.next = p; |
| 321 | mMessages = msg; |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 322 | needWake = mBlocked; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | } else { |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 324 | // Inserted within the middle of the queue. Usually we don't have to wake |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 325 | // up the event queue unless there is a barrier at the head of the queue |
| 326 | // and the message is the earliest asynchronous message in the queue. |
| 327 | needWake = mBlocked && p.target == null && msg.isAsynchronous(); |
| 328 | Message prev; |
| 329 | for (;;) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | prev = p; |
| 331 | p = p.next; |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 332 | if (p == null || when < p.when) { |
| 333 | break; |
| 334 | } |
| 335 | if (needWake && p.isAsynchronous()) { |
| 336 | needWake = false; |
| 337 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 339 | msg.next = p; // invariant: p == prev.next |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | prev.next = msg; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | } |
| 342 | } |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 343 | if (needWake) { |
| 344 | nativeWake(mPtr); |
| 345 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | return true; |
| 347 | } |
| 348 | |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 349 | boolean hasMessages(Handler h, int what, Object object) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 350 | if (h == null) { |
| 351 | return false; |
| 352 | } |
| 353 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | synchronized (this) { |
| 355 | Message p = mMessages; |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 356 | while (p != null) { |
| 357 | if (p.target == h && p.what == what && (object == null || p.obj == object)) { |
| 358 | return true; |
| 359 | } |
| 360 | p = p.next; |
| 361 | } |
| 362 | return false; |
| 363 | } |
| 364 | } |
| 365 | |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 366 | boolean hasMessages(Handler h, Runnable r, Object object) { |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 367 | if (h == null) { |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | synchronized (this) { |
| 372 | Message p = mMessages; |
| 373 | while (p != null) { |
| 374 | if (p.target == h && p.callback == r && (object == null || p.obj == object)) { |
| 375 | return true; |
| 376 | } |
| 377 | p = p.next; |
| 378 | } |
| 379 | return false; |
| 380 | } |
| 381 | } |
| 382 | |
Dianne Hackborn | efa92b2 | 2013-05-03 14:11:43 -0700 | [diff] [blame] | 383 | boolean isIdling() { |
| 384 | return nativeIsIdling(mPtr); |
| 385 | } |
| 386 | |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 387 | void removeMessages(Handler h, int what, Object object) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 388 | if (h == null) { |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | synchronized (this) { |
| 393 | Message p = mMessages; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 | |
| 395 | // Remove all messages at front. |
| 396 | while (p != null && p.target == h && p.what == what |
| 397 | && (object == null || p.obj == object)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | Message n = p.next; |
| 399 | mMessages = n; |
| 400 | p.recycle(); |
| 401 | p = n; |
| 402 | } |
| 403 | |
| 404 | // Remove all messages after front. |
| 405 | while (p != null) { |
| 406 | Message n = p.next; |
| 407 | if (n != null) { |
| 408 | if (n.target == h && n.what == what |
| 409 | && (object == null || n.obj == object)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | Message nn = n.next; |
| 411 | n.recycle(); |
| 412 | p.next = nn; |
| 413 | continue; |
| 414 | } |
| 415 | } |
| 416 | p = n; |
| 417 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 421 | void removeMessages(Handler h, Runnable r, Object object) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 422 | if (h == null || r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | return; |
| 424 | } |
| 425 | |
| 426 | synchronized (this) { |
| 427 | Message p = mMessages; |
| 428 | |
| 429 | // Remove all messages at front. |
| 430 | while (p != null && p.target == h && p.callback == r |
| 431 | && (object == null || p.obj == object)) { |
| 432 | Message n = p.next; |
| 433 | mMessages = n; |
| 434 | p.recycle(); |
| 435 | p = n; |
| 436 | } |
| 437 | |
| 438 | // Remove all messages after front. |
| 439 | while (p != null) { |
| 440 | Message n = p.next; |
| 441 | if (n != null) { |
| 442 | if (n.target == h && n.callback == r |
| 443 | && (object == null || n.obj == object)) { |
| 444 | Message nn = n.next; |
| 445 | n.recycle(); |
| 446 | p.next = nn; |
| 447 | continue; |
| 448 | } |
| 449 | } |
| 450 | p = n; |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 455 | void removeCallbacksAndMessages(Handler h, Object object) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 456 | if (h == null) { |
| 457 | return; |
| 458 | } |
| 459 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | synchronized (this) { |
| 461 | Message p = mMessages; |
| 462 | |
| 463 | // Remove all messages at front. |
| 464 | while (p != null && p.target == h |
| 465 | && (object == null || p.obj == object)) { |
| 466 | Message n = p.next; |
| 467 | mMessages = n; |
| 468 | p.recycle(); |
| 469 | p = n; |
| 470 | } |
| 471 | |
| 472 | // Remove all messages after front. |
| 473 | while (p != null) { |
| 474 | Message n = p.next; |
| 475 | if (n != null) { |
| 476 | if (n.target == h && (object == null || n.obj == object)) { |
| 477 | Message nn = n.next; |
| 478 | n.recycle(); |
| 479 | p.next = nn; |
| 480 | continue; |
| 481 | } |
| 482 | } |
| 483 | p = n; |
| 484 | } |
| 485 | } |
| 486 | } |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 487 | |
| 488 | private void removeAllMessagesLocked() { |
| 489 | Message p = mMessages; |
| 490 | while (p != null) { |
| 491 | Message n = p.next; |
| 492 | p.recycle(); |
| 493 | p = n; |
| 494 | } |
| 495 | mMessages = null; |
| 496 | } |
| 497 | |
| 498 | private void removeAllFutureMessagesLocked() { |
| 499 | final long now = SystemClock.uptimeMillis(); |
| 500 | Message p = mMessages; |
| 501 | if (p != null) { |
| 502 | if (p.when > now) { |
| 503 | removeAllMessagesLocked(); |
| 504 | } else { |
| 505 | Message n; |
| 506 | for (;;) { |
| 507 | n = p.next; |
| 508 | if (n == null) { |
| 509 | return; |
| 510 | } |
| 511 | if (n.when > now) { |
| 512 | break; |
| 513 | } |
| 514 | p = n; |
| 515 | } |
| 516 | p.next = null; |
| 517 | do { |
| 518 | p = n; |
| 519 | n = p.next; |
| 520 | p.recycle(); |
| 521 | } while (n != null); |
| 522 | } |
| 523 | } |
| 524 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 525 | } |