| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.TestApi; |
| 22 |
import android.compat.annotation.UnsupportedAppUsage; |
| 23 |
import android.util.Log; |
| 24 |
import android.util.Printer; |
| 25 |
import android.util.SparseArray; |
| 26 |
import android.util.proto.ProtoOutputStream; |
| 27 |
|
| 28 |
import java.io.FileDescriptor; |
| 29 |
import java.lang.annotation.Retention; |
| 30 |
import java.lang.annotation.RetentionPolicy; |
| 31 |
import java.util.ArrayList; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public final class MessageQueue { |
| 42 |
private static final String TAG = "MessageQueue"; |
| 43 |
private static final boolean DEBUG = false; |
| 44 |
|
| 45 |
|
| 46 |
@UnsupportedAppUsage |
| 47 |
private final boolean mQuitAllowed; |
| 48 |
|
| 49 |
@UnsupportedAppUsage |
| 50 |
@SuppressWarnings("unused") |
| 51 |
private long mPtr; |
| 52 |
|
| 53 |
@UnsupportedAppUsage |
| 54 |
Message mMessages; |
| 55 |
@UnsupportedAppUsage |
| 56 |
private final ArrayList<IdleHandler> mIdleHandlers = new ArrayList<IdleHandler>(); |
| 57 |
private SparseArray<FileDescriptorRecord> mFileDescriptorRecords; |
| 58 |
private IdleHandler[] mPendingIdleHandlers; |
| 59 |
private boolean mQuitting; |
| 60 |
|
| 61 |
|
| 62 |
private boolean mBlocked; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
@UnsupportedAppUsage |
| 67 |
private int mNextBarrierToken; |
| 68 |
|
| 69 |
private native static long nativeInit(); |
| 70 |
private native static void nativeDestroy(long ptr); |
| 71 |
@UnsupportedAppUsage |
| 72 |
private native void nativePollOnce(long ptr, int timeoutMillis); |
| 73 |
private native static void nativeWake(long ptr); |
| 74 |
private native static boolean nativeIsPolling(long ptr); |
| 75 |
private native static void nativeSetFileDescriptorEvents(long ptr, int fd, int events); |
| 76 |
|
| 77 |
MessageQueue(boolean quitAllowed) { |
| 78 |
mQuitAllowed = quitAllowed; |
| 79 |
mPtr = nativeInit(); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
protected void finalize() throws Throwable { |
| 84 |
try { |
| 85 |
dispose(); |
| 86 |
} finally { |
| 87 |
super.finalize(); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
private void dispose() { |
| 94 |
if (mPtr != 0) { |
| 95 |
nativeDestroy(mPtr); |
| 96 |
mPtr = 0; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public boolean isIdle() { |
| 108 |
synchronized (this) { |
| 109 |
final long now = SystemClock.uptimeMillis(); |
| 110 |
return mMessages == null || now < mMessages.when; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public void addIdleHandler(@NonNull IdleHandler handler) { |
| 125 |
if (handler == null) { |
| 126 |
throw new NullPointerException("Can't add a null IdleHandler"); |
| 127 |
} |
| 128 |
synchronized (this) { |
| 129 |
mIdleHandlers.add(handler); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public void removeIdleHandler(@NonNull IdleHandler handler) { |
| 143 |
synchronized (this) { |
| 144 |
mIdleHandlers.remove(handler); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public boolean isPolling() { |
| 160 |
synchronized (this) { |
| 161 |
return isPollingLocked(); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
private boolean isPollingLocked() { |
| 166 |
|
| 167 |
|
| 168 |
return !mQuitting && nativeIsPolling(mPtr); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public void addOnFileDescriptorEventListener(@NonNull FileDescriptor fd, |
| 195 |
@OnFileDescriptorEventListener.Events int events, |
| 196 |
@NonNull OnFileDescriptorEventListener listener) { |
| 197 |
if (fd == null) { |
| 198 |
throw new IllegalArgumentException("fd must not be null"); |
| 199 |
} |
| 200 |
if (listener == null) { |
| 201 |
throw new IllegalArgumentException("listener must not be null"); |
| 202 |
} |
| 203 |
|
| 204 |
synchronized (this) { |
| 205 |
updateOnFileDescriptorEventListenerLocked(fd, events, listener); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public void removeOnFileDescriptorEventListener(@NonNull FileDescriptor fd) { |
| 222 |
if (fd == null) { |
| 223 |
throw new IllegalArgumentException("fd must not be null"); |
| 224 |
} |
| 225 |
|
| 226 |
synchronized (this) { |
| 227 |
updateOnFileDescriptorEventListenerLocked(fd, 0, null); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
private void updateOnFileDescriptorEventListenerLocked(FileDescriptor fd, int events, |
| 232 |
OnFileDescriptorEventListener listener) { |
| 233 |
final int fdNum = fd.getInt$(); |
| 234 |
|
| 235 |
int index = -1; |
| 236 |
FileDescriptorRecord record = null; |
| 237 |
if (mFileDescriptorRecords != null) { |
| 238 |
index = mFileDescriptorRecords.indexOfKey(fdNum); |
| 239 |
if (index >= 0) { |
| 240 |
record = mFileDescriptorRecords.valueAt(index); |
| 241 |
if (record != null && record.mEvents == events) { |
| 242 |
return; |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
if (events != 0) { |
| 248 |
events |= OnFileDescriptorEventListener.EVENT_ERROR; |
| 249 |
if (record == null) { |
| 250 |
if (mFileDescriptorRecords == null) { |
| 251 |
mFileDescriptorRecords = new SparseArray<FileDescriptorRecord>(); |
| 252 |
} |
| 253 |
record = new FileDescriptorRecord(fd, events, listener); |
| 254 |
mFileDescriptorRecords.put(fdNum, record); |
| 255 |
} else { |
| 256 |
record.mListener = listener; |
| 257 |
record.mEvents = events; |
| 258 |
record.mSeq += 1; |
| 259 |
} |
| 260 |
nativeSetFileDescriptorEvents(mPtr, fdNum, events); |
| 261 |
} else if (record != null) { |
| 262 |
record.mEvents = 0; |
| 263 |
mFileDescriptorRecords.removeAt(index); |
| 264 |
nativeSetFileDescriptorEvents(mPtr, fdNum, 0); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 270 |
private int dispatchEvents(int fd, int events) { |
| 271 |
|
| 272 |
final FileDescriptorRecord record; |
| 273 |
final int oldWatchedEvents; |
| 274 |
final OnFileDescriptorEventListener listener; |
| 275 |
final int seq; |
| 276 |
synchronized (this) { |
| 277 |
record = mFileDescriptorRecords.get(fd); |
| 278 |
if (record == null) { |
| 279 |
return 0; |
| 280 |
} |
| 281 |
|
| 282 |
oldWatchedEvents = record.mEvents; |
| 283 |
events &= oldWatchedEvents; |
| 284 |
if (events == 0) { |
| 285 |
return oldWatchedEvents; |
| 286 |
} |
| 287 |
|
| 288 |
listener = record.mListener; |
| 289 |
seq = record.mSeq; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
int newWatchedEvents = listener.onFileDescriptorEvents( |
| 294 |
record.mDescriptor, events); |
| 295 |
if (newWatchedEvents != 0) { |
| 296 |
newWatchedEvents |= OnFileDescriptorEventListener.EVENT_ERROR; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
if (newWatchedEvents != oldWatchedEvents) { |
| 302 |
synchronized (this) { |
| 303 |
int index = mFileDescriptorRecords.indexOfKey(fd); |
| 304 |
if (index >= 0 && mFileDescriptorRecords.valueAt(index) == record |
| 305 |
&& record.mSeq == seq) { |
| 306 |
record.mEvents = newWatchedEvents; |
| 307 |
if (newWatchedEvents == 0) { |
| 308 |
mFileDescriptorRecords.removeAt(index); |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
return newWatchedEvents; |
| 316 |
} |
| 317 |
|
| 318 |
@UnsupportedAppUsage |
| 319 |
Message next() { |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
final long ptr = mPtr; |
| 324 |
if (ptr == 0) { |
| 325 |
return null; |
| 326 |
} |
| 327 |
|
| 328 |
int pendingIdleHandlerCount = -1; |
| 329 |
int nextPollTimeoutMillis = 0; |
| 330 |
for (;;) { |
| 331 |
if (nextPollTimeoutMillis != 0) { |
| 332 |
Binder.flushPendingCommands(); |
| 333 |
} |
| 334 |
|
| 335 |
nativePollOnce(ptr, nextPollTimeoutMillis); |
| 336 |
|
| 337 |
synchronized (this) { |
| 338 |
|
| 339 |
final long now = SystemClock.uptimeMillis(); |
| 340 |
Message prevMsg = null; |
| 341 |
Message msg = mMessages; |
| 342 |
if (msg != null && msg.target == null) { |
| 343 |
|
| 344 |
do { |
| 345 |
prevMsg = msg; |
| 346 |
msg = msg.next; |
| 347 |
} while (msg != null && !msg.isAsynchronous()); |
| 348 |
} |
| 349 |
if (msg != null) { |
| 350 |
if (now < msg.when) { |
| 351 |
|
| 352 |
nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE); |
| 353 |
} else { |
| 354 |
|
| 355 |
mBlocked = false; |
| 356 |
if (prevMsg != null) { |
| 357 |
prevMsg.next = msg.next; |
| 358 |
} else { |
| 359 |
mMessages = msg.next; |
| 360 |
} |
| 361 |
msg.next = null; |
| 362 |
if (DEBUG) Log.v(TAG, "Returning message: " + msg); |
| 363 |
msg.markInUse(); |
| 364 |
return msg; |
| 365 |
} |
| 366 |
} else { |
| 367 |
|
| 368 |
nextPollTimeoutMillis = -1; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
if (mQuitting) { |
| 373 |
dispose(); |
| 374 |
return null; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
if (pendingIdleHandlerCount < 0 |
| 381 |
&& (mMessages == null || now < mMessages.when)) { |
| 382 |
pendingIdleHandlerCount = mIdleHandlers.size(); |
| 383 |
} |
| 384 |
if (pendingIdleHandlerCount <= 0) { |
| 385 |
|
| 386 |
mBlocked = true; |
| 387 |
continue; |
| 388 |
} |
| 389 |
|
| 390 |
if (mPendingIdleHandlers == null) { |
| 391 |
mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)]; |
| 392 |
} |
| 393 |
mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
for (int i = 0; i < pendingIdleHandlerCount; i++) { |
| 399 |
final IdleHandler idler = mPendingIdleHandlers[i]; |
| 400 |
mPendingIdleHandlers[i] = null; |
| 401 |
|
| 402 |
boolean keep = false; |
| 403 |
try { |
| 404 |
keep = idler.queueIdle(); |
| 405 |
} catch (Throwable t) { |
| 406 |
Log.wtf(TAG, "IdleHandler threw exception", t); |
| 407 |
} |
| 408 |
|
| 409 |
if (!keep) { |
| 410 |
synchronized (this) { |
| 411 |
mIdleHandlers.remove(idler); |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
pendingIdleHandlerCount = 0; |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
nextPollTimeoutMillis = 0; |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
void quit(boolean safe) { |
| 426 |
if (!mQuitAllowed) { |
| 427 |
throw new IllegalStateException("Main thread not allowed to quit."); |
| 428 |
} |
| 429 |
|
| 430 |
synchronized (this) { |
| 431 |
if (mQuitting) { |
| 432 |
return; |
| 433 |
} |
| 434 |
mQuitting = true; |
| 435 |
|
| 436 |
if (safe) { |
| 437 |
removeAllFutureMessagesLocked(); |
| 438 |
} else { |
| 439 |
removeAllMessagesLocked(); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
nativeWake(mPtr); |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
@UnsupportedAppUsage |
| 471 |
@TestApi |
| 472 |
public int postSyncBarrier() { |
| 473 |
return postSyncBarrier(SystemClock.uptimeMillis()); |
| 474 |
} |
| 475 |
|
| 476 |
private int postSyncBarrier(long when) { |
| 477 |
|
| 478 |
|
| 479 |
synchronized (this) { |
| 480 |
final int token = mNextBarrierToken++; |
| 481 |
final Message msg = Message.obtain(); |
| 482 |
msg.markInUse(); |
| 483 |
msg.when = when; |
| 484 |
msg.arg1 = token; |
| 485 |
|
| 486 |
Message prev = null; |
| 487 |
Message p = mMessages; |
| 488 |
if (when != 0) { |
| 489 |
while (p != null && p.when <= when) { |
| 490 |
prev = p; |
| 491 |
p = p.next; |
| 492 |
} |
| 493 |
} |
| 494 |
if (prev != null) { |
| 495 |
msg.next = p; |
| 496 |
prev.next = msg; |
| 497 |
} else { |
| 498 |
msg.next = p; |
| 499 |
mMessages = msg; |
| 500 |
} |
| 501 |
return token; |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
@UnsupportedAppUsage |
| 516 |
@TestApi |
| 517 |
public void removeSyncBarrier(int token) { |
| 518 |
|
| 519 |
|
| 520 |
synchronized (this) { |
| 521 |
Message prev = null; |
| 522 |
Message p = mMessages; |
| 523 |
while (p != null && (p.target != null || p.arg1 != token)) { |
| 524 |
prev = p; |
| 525 |
p = p.next; |
| 526 |
} |
| 527 |
if (p == null) { |
| 528 |
throw new IllegalStateException("The specified message queue synchronization " |
| 529 |
+ " barrier token has not been posted or has already been removed."); |
| 530 |
} |
| 531 |
final boolean needWake; |
| 532 |
if (prev != null) { |
| 533 |
prev.next = p.next; |
| 534 |
needWake = false; |
| 535 |
} else { |
| 536 |
mMessages = p.next; |
| 537 |
needWake = mMessages == null || mMessages.target != null; |
| 538 |
} |
| 539 |
p.recycleUnchecked(); |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
if (needWake && !mQuitting) { |
| 544 |
nativeWake(mPtr); |
| 545 |
} |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
boolean enqueueMessage(Message msg, long when) { |
| 550 |
if (msg.target == null) { |
| 551 |
throw new IllegalArgumentException("Message must have a target."); |
| 552 |
} |
| 553 |
|
| 554 |
synchronized (this) { |
| 555 |
if (msg.isInUse()) { |
| 556 |
throw new IllegalStateException(msg + " This message is already in use."); |
| 557 |
} |
| 558 |
|
| 559 |
if (mQuitting) { |
| 560 |
IllegalStateException e = new IllegalStateException( |
| 561 |
msg.target + " sending message to a Handler on a dead thread"); |
| 562 |
Log.w(TAG, e.getMessage(), e); |
| 563 |
msg.recycle(); |
| 564 |
return false; |
| 565 |
} |
| 566 |
|
| 567 |
msg.markInUse(); |
| 568 |
msg.when = when; |
| 569 |
Message p = mMessages; |
| 570 |
boolean needWake; |
| 571 |
if (p == null || when == 0 || when < p.when) { |
| 572 |
|
| 573 |
msg.next = p; |
| 574 |
mMessages = msg; |
| 575 |
needWake = mBlocked; |
| 576 |
} else { |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
needWake = mBlocked && p.target == null && msg.isAsynchronous(); |
| 581 |
Message prev; |
| 582 |
for (;;) { |
| 583 |
prev = p; |
| 584 |
p = p.next; |
| 585 |
if (p == null || when < p.when) { |
| 586 |
break; |
| 587 |
} |
| 588 |
if (needWake && p.isAsynchronous()) { |
| 589 |
needWake = false; |
| 590 |
} |
| 591 |
} |
| 592 |
msg.next = p; |
| 593 |
prev.next = msg; |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
if (needWake) { |
| 598 |
nativeWake(mPtr); |
| 599 |
} |
| 600 |
} |
| 601 |
return true; |
| 602 |
} |
| 603 |
|
| 604 |
boolean hasMessages(Handler h, int what, Object object) { |
| 605 |
if (h == null) { |
| 606 |
return false; |
| 607 |
} |
| 608 |
|
| 609 |
synchronized (this) { |
| 610 |
Message p = mMessages; |
| 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 |
|
| 621 |
boolean hasEqualMessages(Handler h, int what, Object object) { |
| 622 |
if (h == null) { |
| 623 |
return false; |
| 624 |
} |
| 625 |
|
| 626 |
synchronized (this) { |
| 627 |
Message p = mMessages; |
| 628 |
while (p != null) { |
| 629 |
if (p.target == h && p.what == what && (object == null || object.equals(p.obj))) { |
| 630 |
return true; |
| 631 |
} |
| 632 |
p = p.next; |
| 633 |
} |
| 634 |
return false; |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 639 |
boolean hasMessages(Handler h, Runnable r, Object object) { |
| 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 && p.callback == r && (object == null || p.obj == object)) { |
| 648 |
return true; |
| 649 |
} |
| 650 |
p = p.next; |
| 651 |
} |
| 652 |
return false; |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
boolean hasMessages(Handler h) { |
| 657 |
if (h == null) { |
| 658 |
return false; |
| 659 |
} |
| 660 |
|
| 661 |
synchronized (this) { |
| 662 |
Message p = mMessages; |
| 663 |
while (p != null) { |
| 664 |
if (p.target == h) { |
| 665 |
return true; |
| 666 |
} |
| 667 |
p = p.next; |
| 668 |
} |
| 669 |
return false; |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
void removeMessages(Handler h, int what, Object object) { |
| 674 |
if (h == null) { |
| 675 |
return; |
| 676 |
} |
| 677 |
|
| 678 |
synchronized (this) { |
| 679 |
Message p = mMessages; |
| 680 |
|
| 681 |
|
| 682 |
while (p != null && p.target == h && p.what == what |
| 683 |
&& (object == null || p.obj == object)) { |
| 684 |
Message n = p.next; |
| 685 |
mMessages = n; |
| 686 |
p.recycleUnchecked(); |
| 687 |
p = n; |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
while (p != null) { |
| 692 |
Message n = p.next; |
| 693 |
if (n != null) { |
| 694 |
if (n.target == h && n.what == what |
| 695 |
&& (object == null || n.obj == object)) { |
| 696 |
Message nn = n.next; |
| 697 |
n.recycleUnchecked(); |
| 698 |
p.next = nn; |
| 699 |
continue; |
| 700 |
} |
| 701 |
} |
| 702 |
p = n; |
| 703 |
} |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
void removeEqualMessages(Handler h, int what, Object object) { |
| 708 |
if (h == null) { |
| 709 |
return; |
| 710 |
} |
| 711 |
|
| 712 |
synchronized (this) { |
| 713 |
Message p = mMessages; |
| 714 |
|
| 715 |
|
| 716 |
while (p != null && p.target == h && p.what == what |
| 717 |
&& (object == null || object.equals(p.obj))) { |
| 718 |
Message n = p.next; |
| 719 |
mMessages = n; |
| 720 |
p.recycleUnchecked(); |
| 721 |
p = n; |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
while (p != null) { |
| 726 |
Message n = p.next; |
| 727 |
if (n != null) { |
| 728 |
if (n.target == h && n.what == what |
| 729 |
&& (object == null || object.equals(n.obj))) { |
| 730 |
Message nn = n.next; |
| 731 |
n.recycleUnchecked(); |
| 732 |
p.next = nn; |
| 733 |
continue; |
| 734 |
} |
| 735 |
} |
| 736 |
p = n; |
| 737 |
} |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
void removeMessages(Handler h, Runnable r, Object object) { |
| 742 |
if (h == null || r == null) { |
| 743 |
return; |
| 744 |
} |
| 745 |
|
| 746 |
synchronized (this) { |
| 747 |
Message p = mMessages; |
| 748 |
|
| 749 |
|
| 750 |
while (p != null && p.target == h && p.callback == r |
| 751 |
&& (object == null || p.obj == object)) { |
| 752 |
Message n = p.next; |
| 753 |
mMessages = n; |
| 754 |
p.recycleUnchecked(); |
| 755 |
p = n; |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
while (p != null) { |
| 760 |
Message n = p.next; |
| 761 |
if (n != null) { |
| 762 |
if (n.target == h && n.callback == r |
| 763 |
&& (object == null || n.obj == object)) { |
| 764 |
Message nn = n.next; |
| 765 |
n.recycleUnchecked(); |
| 766 |
p.next = nn; |
| 767 |
continue; |
| 768 |
} |
| 769 |
} |
| 770 |
p = n; |
| 771 |
} |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
void removeEqualMessages(Handler h, Runnable r, Object object) { |
| 776 |
if (h == null || r == null) { |
| 777 |
return; |
| 778 |
} |
| 779 |
|
| 780 |
synchronized (this) { |
| 781 |
Message p = mMessages; |
| 782 |
|
| 783 |
|
| 784 |
while (p != null && p.target == h && p.callback == r |
| 785 |
&& (object == null || object.equals(p.obj))) { |
| 786 |
Message n = p.next; |
| 787 |
mMessages = n; |
| 788 |
p.recycleUnchecked(); |
| 789 |
p = n; |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
while (p != null) { |
| 794 |
Message n = p.next; |
| 795 |
if (n != null) { |
| 796 |
if (n.target == h && n.callback == r |
| 797 |
&& (object == null || object.equals(n.obj))) { |
| 798 |
Message nn = n.next; |
| 799 |
n.recycleUnchecked(); |
| 800 |
p.next = nn; |
| 801 |
continue; |
| 802 |
} |
| 803 |
} |
| 804 |
p = n; |
| 805 |
} |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
void removeCallbacksAndMessages(Handler h, Object object) { |
| 811 |
if (h == null) { |
| 812 |
return; |
| 813 |
} |
| 814 |
|
| 815 |
synchronized (this) { |
| 816 |
Message p = mMessages; |
| 817 |
|
| 818 |
|
| 819 |
while (p != null && p.target == h |
| 820 |
&& (object == null || p.obj == object)) { |
| 821 |
Message n = p.next; |
| 822 |
mMessages = n; |
| 823 |
p.recycleUnchecked(); |
| 824 |
p = n; |
| 825 |
} |
| 826 |
|
| 827 |
|
| 828 |
while (p != null) { |
| 829 |
Message n = p.next; |
| 830 |
if (n != null) { |
| 831 |
if (n.target == h && (object == null || n.obj == object)) { |
| 832 |
Message nn = n.next; |
| 833 |
n.recycleUnchecked(); |
| 834 |
p.next = nn; |
| 835 |
continue; |
| 836 |
} |
| 837 |
} |
| 838 |
p = n; |
| 839 |
} |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
void removeCallbacksAndEqualMessages(Handler h, Object object) { |
| 844 |
if (h == null) { |
| 845 |
return; |
| 846 |
} |
| 847 |
|
| 848 |
synchronized (this) { |
| 849 |
Message p = mMessages; |
| 850 |
|
| 851 |
|
| 852 |
while (p != null && p.target == h |
| 853 |
&& (object == null || object.equals(p.obj))) { |
| 854 |
Message n = p.next; |
| 855 |
mMessages = n; |
| 856 |
p.recycleUnchecked(); |
| 857 |
p = n; |
| 858 |
} |
| 859 |
|
| 860 |
|
| 861 |
while (p != null) { |
| 862 |
Message n = p.next; |
| 863 |
if (n != null) { |
| 864 |
if (n.target == h && (object == null || object.equals(n.obj))) { |
| 865 |
Message nn = n.next; |
| 866 |
n.recycleUnchecked(); |
| 867 |
p.next = nn; |
| 868 |
continue; |
| 869 |
} |
| 870 |
} |
| 871 |
p = n; |
| 872 |
} |
| 873 |
} |
| 874 |
} |
| 875 |
|
| 876 |
private void removeAllMessagesLocked() { |
| 877 |
Message p = mMessages; |
| 878 |
while (p != null) { |
| 879 |
Message n = p.next; |
| 880 |
p.recycleUnchecked(); |
| 881 |
p = n; |
| 882 |
} |
| 883 |
mMessages = null; |
| 884 |
} |
| 885 |
|
| 886 |
private void removeAllFutureMessagesLocked() { |
| 887 |
final long now = SystemClock.uptimeMillis(); |
| 888 |
Message p = mMessages; |
| 889 |
if (p != null) { |
| 890 |
if (p.when > now) { |
| 891 |
removeAllMessagesLocked(); |
| 892 |
} else { |
| 893 |
Message n; |
| 894 |
for (;;) { |
| 895 |
n = p.next; |
| 896 |
if (n == null) { |
| 897 |
return; |
| 898 |
} |
| 899 |
if (n.when > now) { |
| 900 |
break; |
| 901 |
} |
| 902 |
p = n; |
| 903 |
} |
| 904 |
p.next = null; |
| 905 |
do { |
| 906 |
p = n; |
| 907 |
n = p.next; |
| 908 |
p.recycleUnchecked(); |
| 909 |
} while (n != null); |
| 910 |
} |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
void dump(Printer pw, String prefix, Handler h) { |
| 915 |
synchronized (this) { |
| 916 |
long now = SystemClock.uptimeMillis(); |
| 917 |
int n = 0; |
| 918 |
for (Message msg = mMessages; msg != null; msg = msg.next) { |
| 919 |
if (h == null || h == msg.target) { |
| 920 |
pw.println(prefix + "Message " + n + ": " + msg.toString(now)); |
| 921 |
} |
| 922 |
n++; |
| 923 |
} |
| 924 |
pw.println(prefix + "(Total messages: " + n + ", polling=" + isPollingLocked() |
| 925 |
+ ", quitting=" + mQuitting + ")"); |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
void dumpDebug(ProtoOutputStream proto, long fieldId) { |
| 930 |
final long messageQueueToken = proto.start(fieldId); |
| 931 |
synchronized (this) { |
| 932 |
for (Message msg = mMessages; msg != null; msg = msg.next) { |
| 933 |
msg.dumpDebug(proto, MessageQueueProto.MESSAGES); |
| 934 |
} |
| 935 |
proto.write(MessageQueueProto.IS_POLLING_LOCKED, isPollingLocked()); |
| 936 |
proto.write(MessageQueueProto.IS_QUITTING, mQuitting); |
| 937 |
} |
| 938 |
proto.end(messageQueueToken); |
| 939 |
} |
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
public static interface IdleHandler { |
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
boolean queueIdle(); |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
public interface OnFileDescriptorEventListener { |
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
public static final int EVENT_INPUT = 1 << 0; |
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
public static final int EVENT_OUTPUT = 1 << 1; |
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
public static final int EVENT_ERROR = 1 << 2; |
| 1006 |
|
| 1007 |
|
| 1008 |
@Retention(RetentionPolicy.SOURCE) |
| 1009 |
@IntDef(flag = true, prefix = { "EVENT_" }, value = { |
| 1010 |
EVENT_INPUT, |
| 1011 |
EVENT_OUTPUT, |
| 1012 |
EVENT_ERROR |
| 1013 |
}) |
| 1014 |
public @interface Events {} |
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
@Events int onFileDescriptorEvents(@NonNull FileDescriptor fd, @Events int events); |
| 1029 |
} |
| 1030 |
|
| 1031 |
private static final class FileDescriptorRecord { |
| 1032 |
public final FileDescriptor mDescriptor; |
| 1033 |
public int mEvents; |
| 1034 |
public OnFileDescriptorEventListener mListener; |
| 1035 |
public int mSeq; |
| 1036 |
|
| 1037 |
public FileDescriptorRecord(FileDescriptor descriptor, |
| 1038 |
int events, OnFileDescriptorEventListener listener) { |
| 1039 |
mDescriptor = descriptor; |
| 1040 |
mEvents = events; |
| 1041 |
mListener = listener; |
| 1042 |
} |
| 1043 |
} |
| 1044 |
} |