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 | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 19 | import android.annotation.IntDef; |
| 20 | import android.annotation.NonNull; |
Jeff Sharkey | c609116 | 2018-06-29 17:15:40 -0600 | [diff] [blame] | 21 | import android.annotation.TestApi; |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 22 | import android.annotation.UnsupportedAppUsage; |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 23 | import android.os.MessageQueueProto; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.util.Log; |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 25 | import android.util.Printer; |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 26 | import android.util.SparseArray; |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 27 | import android.util.proto.ProtoOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 29 | import java.io.FileDescriptor; |
| 30 | import java.lang.annotation.Retention; |
| 31 | import java.lang.annotation.RetentionPolicy; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 32 | import java.util.ArrayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Low-level class holding the list of messages to be dispatched by a |
| 36 | * {@link Looper}. Messages are not added directly to a MessageQueue, |
| 37 | * but rather through {@link Handler} objects associated with the Looper. |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 38 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | * <p>You can retrieve the MessageQueue for the current thread with |
| 40 | * {@link Looper#myQueue() Looper.myQueue()}. |
| 41 | */ |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 42 | public final class MessageQueue { |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 43 | private static final String TAG = "MessageQueue"; |
| 44 | private static final boolean DEBUG = false; |
| 45 | |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 46 | // True if the message queue can be quit. |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 47 | @UnsupportedAppUsage |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 48 | private final boolean mQuitAllowed; |
| 49 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 50 | @UnsupportedAppUsage |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 51 | @SuppressWarnings("unused") |
Ashok Bhat | 63a3715 | 2014-01-10 14:15:21 +0000 | [diff] [blame] | 52 | private long mPtr; // used by native code |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 53 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 54 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | Message mMessages; |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 56 | @UnsupportedAppUsage |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 57 | private final ArrayList<IdleHandler> mIdleHandlers = new ArrayList<IdleHandler>(); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 58 | private SparseArray<FileDescriptorRecord> mFileDescriptorRecords; |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 59 | private IdleHandler[] mPendingIdleHandlers; |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 60 | private boolean mQuitting; |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 61 | |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 62 | // Indicates whether next() is blocked waiting in pollOnce() with a non-zero timeout. |
| 63 | private boolean mBlocked; |
| 64 | |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 65 | // The next barrier token. |
| 66 | // Barriers are indicated by messages with a null target whose arg1 field carries the token. |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 67 | @UnsupportedAppUsage |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 68 | private int mNextBarrierToken; |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 69 | |
Ashok Bhat | 63a3715 | 2014-01-10 14:15:21 +0000 | [diff] [blame] | 70 | private native static long nativeInit(); |
| 71 | private native static void nativeDestroy(long ptr); |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 72 | @UnsupportedAppUsage |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 73 | private native void nativePollOnce(long ptr, int timeoutMillis); /*non-static for callbacks*/ |
Ashok Bhat | 63a3715 | 2014-01-10 14:15:21 +0000 | [diff] [blame] | 74 | private native static void nativeWake(long ptr); |
Jeff Brown | 6c7b41a | 2015-02-26 14:43:53 -0800 | [diff] [blame] | 75 | private native static boolean nativeIsPolling(long ptr); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 76 | private native static void nativeSetFileDescriptorEvents(long ptr, int fd, int events); |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 77 | |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 78 | MessageQueue(boolean quitAllowed) { |
| 79 | mQuitAllowed = quitAllowed; |
Jeff Brown | 86469346 | 2013-01-28 14:25:53 -0800 | [diff] [blame] | 80 | mPtr = nativeInit(); |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 81 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 82 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 83 | @Override |
| 84 | protected void finalize() throws Throwable { |
| 85 | try { |
Jeff Brown | 86469346 | 2013-01-28 14:25:53 -0800 | [diff] [blame] | 86 | dispose(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 87 | } finally { |
| 88 | super.finalize(); |
| 89 | } |
| 90 | } |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 91 | |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 92 | // Disposes of the underlying message queue. |
| 93 | // Must only be called on the looper thread or the finalizer. |
Jeff Brown | 86469346 | 2013-01-28 14:25:53 -0800 | [diff] [blame] | 94 | private void dispose() { |
| 95 | if (mPtr != 0) { |
| 96 | nativeDestroy(mPtr); |
| 97 | mPtr = 0; |
| 98 | } |
| 99 | } |
| 100 | |
Jeff Brown | 6c7b41a | 2015-02-26 14:43:53 -0800 | [diff] [blame] | 101 | /** |
| 102 | * Returns true if the looper has no pending messages which are due to be processed. |
| 103 | * |
| 104 | * <p>This method is safe to call from any thread. |
| 105 | * |
| 106 | * @return True if the looper is idle. |
| 107 | */ |
| 108 | public boolean isIdle() { |
| 109 | synchronized (this) { |
| 110 | final long now = SystemClock.uptimeMillis(); |
| 111 | return mMessages == null || now < mMessages.when; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Add a new {@link IdleHandler} to this message queue. This may be |
| 117 | * removed automatically for you by returning false from |
| 118 | * {@link IdleHandler#queueIdle IdleHandler.queueIdle()} when it is |
| 119 | * invoked, or explicitly removing it with {@link #removeIdleHandler}. |
| 120 | * |
| 121 | * <p>This method is safe to call from any thread. |
| 122 | * |
| 123 | * @param handler The IdleHandler to be added. |
| 124 | */ |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 125 | public void addIdleHandler(@NonNull IdleHandler handler) { |
Jeff Brown | 6c7b41a | 2015-02-26 14:43:53 -0800 | [diff] [blame] | 126 | if (handler == null) { |
| 127 | throw new NullPointerException("Can't add a null IdleHandler"); |
| 128 | } |
| 129 | synchronized (this) { |
| 130 | mIdleHandlers.add(handler); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Remove an {@link IdleHandler} from the queue that was previously added |
| 136 | * with {@link #addIdleHandler}. If the given object is not currently |
| 137 | * in the idle list, nothing is done. |
| 138 | * |
| 139 | * <p>This method is safe to call from any thread. |
| 140 | * |
| 141 | * @param handler The IdleHandler to be removed. |
| 142 | */ |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 143 | public void removeIdleHandler(@NonNull IdleHandler handler) { |
Jeff Brown | 6c7b41a | 2015-02-26 14:43:53 -0800 | [diff] [blame] | 144 | synchronized (this) { |
| 145 | mIdleHandlers.remove(handler); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Returns whether this looper's thread is currently polling for more work to do. |
| 151 | * This is a good signal that the loop is still alive rather than being stuck |
| 152 | * handling a callback. Note that this method is intrinsically racy, since the |
| 153 | * state of the loop can change before you get the result back. |
| 154 | * |
| 155 | * <p>This method is safe to call from any thread. |
| 156 | * |
| 157 | * @return True if the looper is currently polling for events. |
| 158 | * @hide |
| 159 | */ |
| 160 | public boolean isPolling() { |
| 161 | synchronized (this) { |
| 162 | return isPollingLocked(); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | private boolean isPollingLocked() { |
| 167 | // If the loop is quitting then it must not be idling. |
| 168 | // We can assume mPtr != 0 when mQuitting is false. |
| 169 | return !mQuitting && nativeIsPolling(mPtr); |
| 170 | } |
| 171 | |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 172 | /** |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 173 | * Adds a file descriptor listener to receive notification when file descriptor |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 174 | * related events occur. |
| 175 | * <p> |
| 176 | * If the file descriptor has already been registered, the specified events |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 177 | * and listener will replace any that were previously associated with it. |
| 178 | * It is not possible to set more than one listener per file descriptor. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 179 | * </p><p> |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 180 | * It is important to always unregister the listener when the file descriptor |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 181 | * is no longer of use. |
| 182 | * </p> |
| 183 | * |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 184 | * @param fd The file descriptor for which a listener will be registered. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 185 | * @param events The set of events to receive: a combination of the |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 186 | * {@link OnFileDescriptorEventListener#EVENT_INPUT}, |
| 187 | * {@link OnFileDescriptorEventListener#EVENT_OUTPUT}, and |
| 188 | * {@link OnFileDescriptorEventListener#EVENT_ERROR} event masks. If the requested |
| 189 | * set of events is zero, then the listener is unregistered. |
| 190 | * @param listener The listener to invoke when file descriptor events occur. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 191 | * |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 192 | * @see OnFileDescriptorEventListener |
| 193 | * @see #removeOnFileDescriptorEventListener |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 194 | */ |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 195 | public void addOnFileDescriptorEventListener(@NonNull FileDescriptor fd, |
| 196 | @OnFileDescriptorEventListener.Events int events, |
| 197 | @NonNull OnFileDescriptorEventListener listener) { |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 198 | if (fd == null) { |
| 199 | throw new IllegalArgumentException("fd must not be null"); |
| 200 | } |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 201 | if (listener == null) { |
| 202 | throw new IllegalArgumentException("listener must not be null"); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | synchronized (this) { |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 206 | updateOnFileDescriptorEventListenerLocked(fd, events, listener); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 211 | * Removes a file descriptor listener. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 212 | * <p> |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 213 | * This method does nothing if no listener has been registered for the |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 214 | * specified file descriptor. |
| 215 | * </p> |
| 216 | * |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 217 | * @param fd The file descriptor whose listener will be unregistered. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 218 | * |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 219 | * @see OnFileDescriptorEventListener |
| 220 | * @see #addOnFileDescriptorEventListener |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 221 | */ |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 222 | public void removeOnFileDescriptorEventListener(@NonNull FileDescriptor fd) { |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 223 | if (fd == null) { |
| 224 | throw new IllegalArgumentException("fd must not be null"); |
| 225 | } |
| 226 | |
| 227 | synchronized (this) { |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 228 | updateOnFileDescriptorEventListenerLocked(fd, 0, null); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 232 | private void updateOnFileDescriptorEventListenerLocked(FileDescriptor fd, int events, |
| 233 | OnFileDescriptorEventListener listener) { |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 234 | final int fdNum = fd.getInt$(); |
| 235 | |
| 236 | int index = -1; |
| 237 | FileDescriptorRecord record = null; |
| 238 | if (mFileDescriptorRecords != null) { |
| 239 | index = mFileDescriptorRecords.indexOfKey(fdNum); |
| 240 | if (index >= 0) { |
| 241 | record = mFileDescriptorRecords.valueAt(index); |
| 242 | if (record != null && record.mEvents == events) { |
| 243 | return; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if (events != 0) { |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 249 | events |= OnFileDescriptorEventListener.EVENT_ERROR; |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 250 | if (record == null) { |
| 251 | if (mFileDescriptorRecords == null) { |
| 252 | mFileDescriptorRecords = new SparseArray<FileDescriptorRecord>(); |
| 253 | } |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 254 | record = new FileDescriptorRecord(fd, events, listener); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 255 | mFileDescriptorRecords.put(fdNum, record); |
| 256 | } else { |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 257 | record.mListener = listener; |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 258 | record.mEvents = events; |
| 259 | record.mSeq += 1; |
| 260 | } |
| 261 | nativeSetFileDescriptorEvents(mPtr, fdNum, events); |
| 262 | } else if (record != null) { |
| 263 | record.mEvents = 0; |
| 264 | mFileDescriptorRecords.removeAt(index); |
David Pursell | c16b775 | 2017-08-07 16:32:36 -0700 | [diff] [blame] | 265 | nativeSetFileDescriptorEvents(mPtr, fdNum, 0); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
| 269 | // Called from native code. |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 270 | @UnsupportedAppUsage |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 271 | private int dispatchEvents(int fd, int events) { |
| 272 | // Get the file descriptor record and any state that might change. |
| 273 | final FileDescriptorRecord record; |
| 274 | final int oldWatchedEvents; |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 275 | final OnFileDescriptorEventListener listener; |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 276 | final int seq; |
| 277 | synchronized (this) { |
| 278 | record = mFileDescriptorRecords.get(fd); |
| 279 | if (record == null) { |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 280 | return 0; // spurious, no listener registered |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | oldWatchedEvents = record.mEvents; |
| 284 | events &= oldWatchedEvents; // filter events based on current watched set |
| 285 | if (events == 0) { |
| 286 | return oldWatchedEvents; // spurious, watched events changed |
| 287 | } |
| 288 | |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 289 | listener = record.mListener; |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 290 | seq = record.mSeq; |
| 291 | } |
| 292 | |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 293 | // Invoke the listener outside of the lock. |
| 294 | int newWatchedEvents = listener.onFileDescriptorEvents( |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 295 | record.mDescriptor, events); |
| 296 | if (newWatchedEvents != 0) { |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 297 | newWatchedEvents |= OnFileDescriptorEventListener.EVENT_ERROR; |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 300 | // Update the file descriptor record if the listener changed the set of |
| 301 | // events to watch and the listener itself hasn't been updated since. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 302 | if (newWatchedEvents != oldWatchedEvents) { |
| 303 | synchronized (this) { |
| 304 | int index = mFileDescriptorRecords.indexOfKey(fd); |
| 305 | if (index >= 0 && mFileDescriptorRecords.valueAt(index) == record |
| 306 | && record.mSeq == seq) { |
| 307 | record.mEvents = newWatchedEvents; |
| 308 | if (newWatchedEvents == 0) { |
| 309 | mFileDescriptorRecords.removeAt(index); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // Return the new set of events to watch for native code to take care of. |
| 316 | return newWatchedEvents; |
| 317 | } |
| 318 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 319 | @UnsupportedAppUsage |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 320 | Message next() { |
Jeff Brown | 6058390 | 2013-09-09 16:14:20 -0700 | [diff] [blame] | 321 | // Return here if the message loop has already quit and been disposed. |
| 322 | // This can happen if the application tries to restart a looper after quit |
| 323 | // which is not supported. |
Narayan Kamath | ab86434 | 2014-01-16 11:22:52 +0000 | [diff] [blame] | 324 | final long ptr = mPtr; |
Jeff Brown | 6058390 | 2013-09-09 16:14:20 -0700 | [diff] [blame] | 325 | if (ptr == 0) { |
| 326 | return null; |
| 327 | } |
| 328 | |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 329 | int pendingIdleHandlerCount = -1; // -1 only during first iteration |
| 330 | int nextPollTimeoutMillis = 0; |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 331 | for (;;) { |
| 332 | if (nextPollTimeoutMillis != 0) { |
| 333 | Binder.flushPendingCommands(); |
| 334 | } |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 335 | |
Jeff Brown | 6058390 | 2013-09-09 16:14:20 -0700 | [diff] [blame] | 336 | nativePollOnce(ptr, nextPollTimeoutMillis); |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 337 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | synchronized (this) { |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 339 | // Try to retrieve the next message. Return if found. |
| 340 | final long now = SystemClock.uptimeMillis(); |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 341 | Message prevMsg = null; |
| 342 | Message msg = mMessages; |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 343 | if (msg != null && msg.target == null) { |
| 344 | // Stalled by a barrier. Find the next asynchronous message in the queue. |
| 345 | do { |
| 346 | prevMsg = msg; |
| 347 | msg = msg.next; |
| 348 | } while (msg != null && !msg.isAsynchronous()); |
| 349 | } |
| 350 | if (msg != null) { |
| 351 | if (now < msg.when) { |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 352 | // 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] | 353 | nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE); |
| 354 | } else { |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 355 | // Got a message. |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 356 | mBlocked = false; |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 357 | if (prevMsg != null) { |
| 358 | prevMsg.next = msg.next; |
| 359 | } else { |
| 360 | mMessages = msg.next; |
| 361 | } |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 362 | msg.next = null; |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 363 | if (DEBUG) Log.v(TAG, "Returning message: " + msg); |
| 364 | msg.markInUse(); |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 365 | return msg; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 367 | } else { |
| 368 | // No more messages. |
| 369 | nextPollTimeoutMillis = -1; |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Jeff Brown | 024136f | 2013-04-11 19:21:32 -0700 | [diff] [blame] | 372 | // Process the quit message now that all pending messages have been handled. |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 373 | if (mQuitting) { |
Jeff Brown | 024136f | 2013-04-11 19:21:32 -0700 | [diff] [blame] | 374 | dispose(); |
| 375 | return null; |
| 376 | } |
| 377 | |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 378 | // If first time idle, then get the number of idlers to run. |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 379 | // Idle handles only run if the queue is empty or if the first message |
| 380 | // in the queue (possibly a barrier) is due to be handled in the future. |
| 381 | if (pendingIdleHandlerCount < 0 |
| 382 | && (mMessages == null || now < mMessages.when)) { |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 383 | pendingIdleHandlerCount = mIdleHandlers.size(); |
| 384 | } |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 385 | if (pendingIdleHandlerCount <= 0) { |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 386 | // No idle handlers to run. Loop and wait some more. |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 387 | mBlocked = true; |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 388 | continue; |
| 389 | } |
| 390 | |
| 391 | if (mPendingIdleHandlers == null) { |
| 392 | mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)]; |
| 393 | } |
| 394 | mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers); |
| 395 | } |
| 396 | |
| 397 | // Run the idle handlers. |
| 398 | // We only ever reach this code block during the first iteration. |
| 399 | for (int i = 0; i < pendingIdleHandlerCount; i++) { |
| 400 | final IdleHandler idler = mPendingIdleHandlers[i]; |
| 401 | mPendingIdleHandlers[i] = null; // release the reference to the handler |
| 402 | |
| 403 | boolean keep = false; |
| 404 | try { |
| 405 | keep = idler.queueIdle(); |
| 406 | } catch (Throwable t) { |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 407 | Log.wtf(TAG, "IdleHandler threw exception", t); |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | if (!keep) { |
| 411 | synchronized (this) { |
| 412 | mIdleHandlers.remove(idler); |
| 413 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | } |
| 415 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 417 | // Reset the idle handler count to 0 so we do not run them again. |
| 418 | pendingIdleHandlerCount = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 420 | // While calling an idle handler, a new message could have been delivered |
| 421 | // so go back and look again for a pending message without waiting. |
| 422 | nextPollTimeoutMillis = 0; |
| 423 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 426 | void quit(boolean safe) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 427 | if (!mQuitAllowed) { |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 428 | throw new IllegalStateException("Main thread not allowed to quit."); |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | synchronized (this) { |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 432 | if (mQuitting) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 433 | return; |
| 434 | } |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 435 | mQuitting = true; |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 436 | |
| 437 | if (safe) { |
| 438 | removeAllFutureMessagesLocked(); |
| 439 | } else { |
| 440 | removeAllMessagesLocked(); |
| 441 | } |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 442 | |
| 443 | // We can assume mPtr != 0 because mQuitting was previously false. |
| 444 | nativeWake(mPtr); |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 445 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Jeff Brown | 3d4e7efe | 2015-02-26 15:34:16 -0800 | [diff] [blame] | 448 | /** |
| 449 | * Posts a synchronization barrier to the Looper's message queue. |
| 450 | * |
| 451 | * Message processing occurs as usual until the message queue encounters the |
| 452 | * synchronization barrier that has been posted. When the barrier is encountered, |
| 453 | * later synchronous messages in the queue are stalled (prevented from being executed) |
| 454 | * until the barrier is released by calling {@link #removeSyncBarrier} and specifying |
| 455 | * the token that identifies the synchronization barrier. |
| 456 | * |
| 457 | * This method is used to immediately postpone execution of all subsequently posted |
| 458 | * synchronous messages until a condition is met that releases the barrier. |
| 459 | * Asynchronous messages (see {@link Message#isAsynchronous} are exempt from the barrier |
| 460 | * and continue to be processed as usual. |
| 461 | * |
| 462 | * This call must be always matched by a call to {@link #removeSyncBarrier} with |
| 463 | * the same token to ensure that the message queue resumes normal operation. |
| 464 | * Otherwise the application will probably hang! |
| 465 | * |
| 466 | * @return A token that uniquely identifies the barrier. This token must be |
| 467 | * passed to {@link #removeSyncBarrier} to release the barrier. |
| 468 | * |
| 469 | * @hide |
| 470 | */ |
Artur Satayev | f0b7d0b | 2019-11-04 11:16:45 +0000 | [diff] [blame] | 471 | @UnsupportedAppUsage |
Jeff Sharkey | c609116 | 2018-06-29 17:15:40 -0600 | [diff] [blame] | 472 | @TestApi |
Jeff Brown | 3d4e7efe | 2015-02-26 15:34:16 -0800 | [diff] [blame] | 473 | public int postSyncBarrier() { |
| 474 | return postSyncBarrier(SystemClock.uptimeMillis()); |
| 475 | } |
| 476 | |
| 477 | private int postSyncBarrier(long when) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 478 | // Enqueue a new sync barrier token. |
| 479 | // We don't need to wake the queue because the purpose of a barrier is to stall it. |
| 480 | synchronized (this) { |
| 481 | final int token = mNextBarrierToken++; |
| 482 | final Message msg = Message.obtain(); |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 483 | msg.markInUse(); |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 484 | msg.when = when; |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 485 | msg.arg1 = token; |
| 486 | |
| 487 | Message prev = null; |
| 488 | Message p = mMessages; |
| 489 | if (when != 0) { |
| 490 | while (p != null && p.when <= when) { |
| 491 | prev = p; |
| 492 | p = p.next; |
| 493 | } |
| 494 | } |
| 495 | if (prev != null) { // invariant: p == prev.next |
| 496 | msg.next = p; |
| 497 | prev.next = msg; |
| 498 | } else { |
| 499 | msg.next = p; |
| 500 | mMessages = msg; |
| 501 | } |
| 502 | return token; |
| 503 | } |
| 504 | } |
| 505 | |
Jeff Brown | 3d4e7efe | 2015-02-26 15:34:16 -0800 | [diff] [blame] | 506 | /** |
| 507 | * Removes a synchronization barrier. |
| 508 | * |
| 509 | * @param token The synchronization barrier token that was returned by |
| 510 | * {@link #postSyncBarrier}. |
| 511 | * |
| 512 | * @throws IllegalStateException if the barrier was not found. |
| 513 | * |
| 514 | * @hide |
| 515 | */ |
Artur Satayev | f0b7d0b | 2019-11-04 11:16:45 +0000 | [diff] [blame] | 516 | @UnsupportedAppUsage |
Jeff Sharkey | c609116 | 2018-06-29 17:15:40 -0600 | [diff] [blame] | 517 | @TestApi |
Jeff Brown | 3d4e7efe | 2015-02-26 15:34:16 -0800 | [diff] [blame] | 518 | public void removeSyncBarrier(int token) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 519 | // Remove a sync barrier token from the queue. |
| 520 | // If the queue is no longer stalled by a barrier then wake it. |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 521 | synchronized (this) { |
| 522 | Message prev = null; |
| 523 | Message p = mMessages; |
| 524 | while (p != null && (p.target != null || p.arg1 != token)) { |
| 525 | prev = p; |
| 526 | p = p.next; |
| 527 | } |
| 528 | if (p == null) { |
| 529 | throw new IllegalStateException("The specified message queue synchronization " |
| 530 | + " barrier token has not been posted or has already been removed."); |
| 531 | } |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 532 | final boolean needWake; |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 533 | if (prev != null) { |
| 534 | prev.next = p.next; |
| 535 | needWake = false; |
| 536 | } else { |
| 537 | mMessages = p.next; |
| 538 | needWake = mMessages == null || mMessages.target != null; |
| 539 | } |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 540 | p.recycleUnchecked(); |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 541 | |
| 542 | // If the loop is quitting then it is already awake. |
| 543 | // We can assume mPtr != 0 when mQuitting is false. |
| 544 | if (needWake && !mQuitting) { |
| 545 | nativeWake(mPtr); |
| 546 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 550 | boolean enqueueMessage(Message msg, long when) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 551 | if (msg.target == null) { |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 552 | throw new IllegalArgumentException("Message must have a target."); |
| 553 | } |
| 554 | if (msg.isInUse()) { |
| 555 | throw new IllegalStateException(msg + " This message is already in use."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 556 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 557 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | synchronized (this) { |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 559 | if (mQuitting) { |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 560 | IllegalStateException e = new IllegalStateException( |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 561 | msg.target + " sending message to a Handler on a dead thread"); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 562 | Log.w(TAG, e.getMessage(), e); |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 563 | msg.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 564 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | } |
| 566 | |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 567 | msg.markInUse(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 568 | msg.when = when; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | Message p = mMessages; |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 570 | boolean needWake; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | if (p == null || when == 0 || when < p.when) { |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 572 | // New head, wake up the event queue if blocked. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 573 | msg.next = p; |
| 574 | mMessages = msg; |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 575 | needWake = mBlocked; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | } else { |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 577 | // 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] | 578 | // up the event queue unless there is a barrier at the head of the queue |
| 579 | // and the message is the earliest asynchronous message in the queue. |
| 580 | needWake = mBlocked && p.target == null && msg.isAsynchronous(); |
| 581 | Message prev; |
| 582 | for (;;) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | prev = p; |
| 584 | p = p.next; |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 585 | if (p == null || when < p.when) { |
| 586 | break; |
| 587 | } |
| 588 | if (needWake && p.isAsynchronous()) { |
| 589 | needWake = false; |
| 590 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 591 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 592 | msg.next = p; // invariant: p == prev.next |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | prev.next = msg; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 594 | } |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 595 | |
| 596 | // We can assume mPtr != 0 because mQuitting is false. |
| 597 | if (needWake) { |
| 598 | nativeWake(mPtr); |
| 599 | } |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 600 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 601 | return true; |
| 602 | } |
| 603 | |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 604 | boolean hasMessages(Handler h, int what, Object object) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 605 | if (h == null) { |
| 606 | return false; |
| 607 | } |
| 608 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 609 | synchronized (this) { |
| 610 | Message p = mMessages; |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 611 | while (p != null) { |
| 612 | if (p.target == h && p.what == what && (object == null || p.obj == object)) { |
| 613 | return true; |
| 614 | } |
| 615 | p = p.next; |
| 616 | } |
| 617 | return false; |
| 618 | } |
| 619 | } |
| 620 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 621 | @UnsupportedAppUsage |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 622 | boolean hasMessages(Handler h, Runnable r, Object object) { |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 623 | if (h == null) { |
| 624 | return false; |
| 625 | } |
| 626 | |
| 627 | synchronized (this) { |
| 628 | Message p = mMessages; |
| 629 | while (p != null) { |
| 630 | if (p.target == h && p.callback == r && (object == null || p.obj == object)) { |
| 631 | return true; |
| 632 | } |
| 633 | p = p.next; |
| 634 | } |
| 635 | return false; |
| 636 | } |
| 637 | } |
| 638 | |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 639 | boolean hasMessages(Handler h) { |
| 640 | if (h == null) { |
| 641 | return false; |
| 642 | } |
| 643 | |
| 644 | synchronized (this) { |
| 645 | Message p = mMessages; |
| 646 | while (p != null) { |
| 647 | if (p.target == h) { |
| 648 | return true; |
| 649 | } |
| 650 | p = p.next; |
| 651 | } |
| 652 | return false; |
| 653 | } |
| 654 | } |
| 655 | |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 656 | void removeMessages(Handler h, int what, Object object) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 657 | if (h == null) { |
| 658 | return; |
| 659 | } |
| 660 | |
| 661 | synchronized (this) { |
| 662 | Message p = mMessages; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 663 | |
| 664 | // Remove all messages at front. |
| 665 | while (p != null && p.target == h && p.what == what |
| 666 | && (object == null || p.obj == object)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 667 | Message n = p.next; |
| 668 | mMessages = n; |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 669 | p.recycleUnchecked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 670 | p = n; |
| 671 | } |
| 672 | |
| 673 | // Remove all messages after front. |
| 674 | while (p != null) { |
| 675 | Message n = p.next; |
| 676 | if (n != null) { |
| 677 | if (n.target == h && n.what == what |
| 678 | && (object == null || n.obj == object)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 679 | Message nn = n.next; |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 680 | n.recycleUnchecked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 681 | p.next = nn; |
| 682 | continue; |
| 683 | } |
| 684 | } |
| 685 | p = n; |
| 686 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 690 | void removeMessages(Handler h, Runnable r, Object object) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 691 | if (h == null || r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | return; |
| 693 | } |
| 694 | |
| 695 | synchronized (this) { |
| 696 | Message p = mMessages; |
| 697 | |
| 698 | // Remove all messages at front. |
| 699 | while (p != null && p.target == h && p.callback == r |
| 700 | && (object == null || p.obj == object)) { |
| 701 | Message n = p.next; |
| 702 | mMessages = n; |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 703 | p.recycleUnchecked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 704 | p = n; |
| 705 | } |
| 706 | |
| 707 | // Remove all messages after front. |
| 708 | while (p != null) { |
| 709 | Message n = p.next; |
| 710 | if (n != null) { |
| 711 | if (n.target == h && n.callback == r |
| 712 | && (object == null || n.obj == object)) { |
| 713 | Message nn = n.next; |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 714 | n.recycleUnchecked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 715 | p.next = nn; |
| 716 | continue; |
| 717 | } |
| 718 | } |
| 719 | p = n; |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 724 | void removeCallbacksAndMessages(Handler h, Object object) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 725 | if (h == null) { |
| 726 | return; |
| 727 | } |
| 728 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 729 | synchronized (this) { |
| 730 | Message p = mMessages; |
| 731 | |
| 732 | // Remove all messages at front. |
| 733 | while (p != null && p.target == h |
| 734 | && (object == null || p.obj == object)) { |
| 735 | Message n = p.next; |
| 736 | mMessages = n; |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 737 | p.recycleUnchecked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 738 | p = n; |
| 739 | } |
| 740 | |
| 741 | // Remove all messages after front. |
| 742 | while (p != null) { |
| 743 | Message n = p.next; |
| 744 | if (n != null) { |
| 745 | if (n.target == h && (object == null || n.obj == object)) { |
| 746 | Message nn = n.next; |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 747 | n.recycleUnchecked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 748 | p.next = nn; |
| 749 | continue; |
| 750 | } |
| 751 | } |
| 752 | p = n; |
| 753 | } |
| 754 | } |
| 755 | } |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 756 | |
| 757 | private void removeAllMessagesLocked() { |
| 758 | Message p = mMessages; |
| 759 | while (p != null) { |
| 760 | Message n = p.next; |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 761 | p.recycleUnchecked(); |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 762 | p = n; |
| 763 | } |
| 764 | mMessages = null; |
| 765 | } |
| 766 | |
| 767 | private void removeAllFutureMessagesLocked() { |
| 768 | final long now = SystemClock.uptimeMillis(); |
| 769 | Message p = mMessages; |
| 770 | if (p != null) { |
| 771 | if (p.when > now) { |
| 772 | removeAllMessagesLocked(); |
| 773 | } else { |
| 774 | Message n; |
| 775 | for (;;) { |
| 776 | n = p.next; |
| 777 | if (n == null) { |
| 778 | return; |
| 779 | } |
| 780 | if (n.when > now) { |
| 781 | break; |
| 782 | } |
| 783 | p = n; |
| 784 | } |
| 785 | p.next = null; |
| 786 | do { |
| 787 | p = n; |
| 788 | n = p.next; |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 789 | p.recycleUnchecked(); |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 790 | } while (n != null); |
| 791 | } |
| 792 | } |
| 793 | } |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 794 | |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 795 | void dump(Printer pw, String prefix, Handler h) { |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 796 | synchronized (this) { |
| 797 | long now = SystemClock.uptimeMillis(); |
| 798 | int n = 0; |
| 799 | for (Message msg = mMessages; msg != null; msg = msg.next) { |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 800 | if (h == null || h == msg.target) { |
| 801 | pw.println(prefix + "Message " + n + ": " + msg.toString(now)); |
| 802 | } |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 803 | n++; |
| 804 | } |
Jeff Brown | 6c7b41a | 2015-02-26 14:43:53 -0800 | [diff] [blame] | 805 | pw.println(prefix + "(Total messages: " + n + ", polling=" + isPollingLocked() |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 806 | + ", quitting=" + mQuitting + ")"); |
| 807 | } |
| 808 | } |
Jeff Brown | 6c7b41a | 2015-02-26 14:43:53 -0800 | [diff] [blame] | 809 | |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 810 | void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 811 | final long messageQueueToken = proto.start(fieldId); |
| 812 | synchronized (this) { |
| 813 | for (Message msg = mMessages; msg != null; msg = msg.next) { |
| 814 | msg.writeToProto(proto, MessageQueueProto.MESSAGES); |
| 815 | } |
| 816 | proto.write(MessageQueueProto.IS_POLLING_LOCKED, isPollingLocked()); |
| 817 | proto.write(MessageQueueProto.IS_QUITTING, mQuitting); |
| 818 | } |
| 819 | proto.end(messageQueueToken); |
| 820 | } |
| 821 | |
Jeff Brown | 6c7b41a | 2015-02-26 14:43:53 -0800 | [diff] [blame] | 822 | /** |
| 823 | * Callback interface for discovering when a thread is going to block |
| 824 | * waiting for more messages. |
| 825 | */ |
| 826 | public static interface IdleHandler { |
| 827 | /** |
| 828 | * Called when the message queue has run out of messages and will now |
| 829 | * wait for more. Return true to keep your idle handler active, false |
| 830 | * to have it removed. This may be called if there are still messages |
| 831 | * pending in the queue, but they are all scheduled to be dispatched |
| 832 | * after the current time. |
| 833 | */ |
| 834 | boolean queueIdle(); |
| 835 | } |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 836 | |
| 837 | /** |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 838 | * A listener which is invoked when file descriptor related events occur. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 839 | */ |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 840 | public interface OnFileDescriptorEventListener { |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 841 | /** |
| 842 | * File descriptor event: Indicates that the file descriptor is ready for input |
| 843 | * operations, such as reading. |
| 844 | * <p> |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 845 | * The listener should read all available data from the file descriptor |
| 846 | * then return <code>true</code> to keep the listener active or <code>false</code> |
| 847 | * to remove the listener. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 848 | * </p><p> |
| 849 | * In the case of a socket, this event may be generated to indicate |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 850 | * that there is at least one incoming connection that the listener |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 851 | * should accept. |
| 852 | * </p><p> |
| 853 | * This event will only be generated if the {@link #EVENT_INPUT} event mask was |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 854 | * specified when the listener was added. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 855 | * </p> |
| 856 | */ |
| 857 | public static final int EVENT_INPUT = 1 << 0; |
| 858 | |
| 859 | /** |
| 860 | * File descriptor event: Indicates that the file descriptor is ready for output |
| 861 | * operations, such as writing. |
| 862 | * <p> |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 863 | * The listener should write as much data as it needs. If it could not |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 864 | * write everything at once, then it should return <code>true</code> to |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 865 | * keep the listener active. Otherwise, it should return <code>false</code> |
| 866 | * to remove the listener then re-register it later when it needs to write |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 867 | * something else. |
| 868 | * </p><p> |
| 869 | * This event will only be generated if the {@link #EVENT_OUTPUT} event mask was |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 870 | * specified when the listener was added. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 871 | * </p> |
| 872 | */ |
| 873 | public static final int EVENT_OUTPUT = 1 << 1; |
| 874 | |
| 875 | /** |
| 876 | * File descriptor event: Indicates that the file descriptor encountered a |
| 877 | * fatal error. |
| 878 | * <p> |
| 879 | * File descriptor errors can occur for various reasons. One common error |
| 880 | * is when the remote peer of a socket or pipe closes its end of the connection. |
| 881 | * </p><p> |
| 882 | * This event may be generated at any time regardless of whether the |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 883 | * {@link #EVENT_ERROR} event mask was specified when the listener was added. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 884 | * </p> |
| 885 | */ |
| 886 | public static final int EVENT_ERROR = 1 << 2; |
| 887 | |
| 888 | /** @hide */ |
| 889 | @Retention(RetentionPolicy.SOURCE) |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 890 | @IntDef(flag = true, prefix = { "EVENT_" }, value = { |
| 891 | EVENT_INPUT, |
| 892 | EVENT_OUTPUT, |
| 893 | EVENT_ERROR |
| 894 | }) |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 895 | public @interface Events {} |
| 896 | |
| 897 | /** |
| 898 | * Called when a file descriptor receives events. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 899 | * |
| 900 | * @param fd The file descriptor. |
| 901 | * @param events The set of events that occurred: a combination of the |
| 902 | * {@link #EVENT_INPUT}, {@link #EVENT_OUTPUT}, and {@link #EVENT_ERROR} event masks. |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 903 | * @return The new set of events to watch, or 0 to unregister the listener. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 904 | * |
| 905 | * @see #EVENT_INPUT |
| 906 | * @see #EVENT_OUTPUT |
| 907 | * @see #EVENT_ERROR |
| 908 | */ |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 909 | @Events int onFileDescriptorEvents(@NonNull FileDescriptor fd, @Events int events); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | private static final class FileDescriptorRecord { |
| 913 | public final FileDescriptor mDescriptor; |
| 914 | public int mEvents; |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 915 | public OnFileDescriptorEventListener mListener; |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 916 | public int mSeq; |
| 917 | |
| 918 | public FileDescriptorRecord(FileDescriptor descriptor, |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 919 | int events, OnFileDescriptorEventListener listener) { |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 920 | mDescriptor = descriptor; |
| 921 | mEvents = events; |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 922 | mListener = listener; |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 923 | } |
| 924 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | } |