()
| 126 | } |
| 127 | |
| 128 | Message next() { |
| 129 | int pendingIdleHandlerCount = -1; // -1 only during first iteration |
| 130 | int nextPollTimeoutMillis = 0; |
| 131 | for (;;) { |
| 132 | if (nextPollTimeoutMillis != 0) { |
| 133 | Binder.flushPendingCommands(); |
| 134 | } |
| 135 | |
| 136 | // We can assume mPtr != 0 because the loop is obviously still running. |
| 137 | // The looper will not call this method after the loop quits. |
| 138 | nativePollOnce(mPtr, nextPollTimeoutMillis); |
| 139 | |
| 140 | synchronized (this) { |
| 141 | // Try to retrieve the next message. Return if found. |
| 142 | final long now = SystemClock.uptimeMillis(); |
| 143 | Message prevMsg = null; |
| 144 | Message msg = mMessages; |
| 145 | if (msg != null && msg.target == null) { |
| 146 | // Stalled by a barrier. Find the next asynchronous message in the queue. |
| 147 | do { |
| 148 | prevMsg = msg; |
| 149 | msg = msg.next; |
| 150 | } while (msg != null && !msg.isAsynchronous()); |
| 151 | } |
| 152 | if (msg != null) { |
| 153 | if (now < msg.when) { |
| 154 | // Next message is not ready. Set a timeout to wake up when it is ready. |
| 155 | nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE); |
| 156 | } else { |
| 157 | // Got a message. |
| 158 | mBlocked = false; |
| 159 | if (prevMsg != null) { |
| 160 | prevMsg.next = msg.next; |
| 161 | } else { |
| 162 | mMessages = msg.next; |
| 163 | } |
| 164 | msg.next = null; |
| 165 | if (false) Log.v("MessageQueue", "Returning message: " + msg); |
| 166 | msg.markInUse(); |
| 167 | return msg; |
| 168 | } |
| 169 | } else { |
| 170 | // No more messages. |
| 171 | nextPollTimeoutMillis = -1; |
| 172 | } |
| 173 | |
| 174 | // Process the quit message now that all pending messages have been handled. |
| 175 | if (mQuitting) { |
| 176 | dispose(); |
| 177 | return null; |
| 178 | } |
| 179 | |
| 180 | // If first time idle, then get the number of idlers to run. |
| 181 | // Idle handles only run if the queue is empty or if the first message |
| 182 | // in the queue (possibly a barrier) is due to be handled in the future. |
| 183 | if (pendingIdleHandlerCount < 0 |
| 184 | && (mMessages == null || now < mMessages.when)) { |
| 185 | pendingIdleHandlerCount = mIdleHandlers.size(); |
no test coverage detected