diff --git a/services/core/java/com/android/server/am/BroadcastQueueModernImpl.java b/services/core/java/com/android/server/am/BroadcastQueueModernImpl.java index a994b1db7ca33..e9659b0f3b75a 100644 --- a/services/core/java/com/android/server/am/BroadcastQueueModernImpl.java +++ b/services/core/java/com/android/server/am/BroadcastQueueModernImpl.java @@ -506,16 +506,7 @@ class BroadcastQueueModernImpl extends BroadcastQueue { mService.updateOomAdjPendingTargetsLocked(OOM_ADJ_REASON_START_RECEIVER); } - if (waitingFor) { - mWaitingFor.removeIf((pair) -> { - if (pair.first.getAsBoolean()) { - pair.second.countDown(); - return true; - } else { - return false; - } - }); - } + checkAndRemoveWaitingFor(); traceEnd(cookie); } @@ -1156,6 +1147,9 @@ class BroadcastQueueModernImpl extends BroadcastQueue { mLocalHandler.removeMessages(MSG_DELIVERY_TIMEOUT_HARD, queue); } + // Given that a receiver just finished, check if the "waitingFor" conditions are met. + checkAndRemoveWaitingFor(); + if (early) { // This is an early receiver that was transmitted as part of a group. The delivery // state has been updated but don't make any further decisions. @@ -1446,7 +1440,7 @@ class BroadcastQueueModernImpl extends BroadcastQueue { waitFor(() -> isBeyondBarrierLocked(now, pw)); } - public void waitFor(@NonNull BooleanSupplier condition) { + private void waitFor(@NonNull BooleanSupplier condition) { final CountDownLatch latch = new CountDownLatch(1); synchronized (mService) { mWaitingFor.add(Pair.create(condition, latch)); @@ -1468,6 +1462,19 @@ class BroadcastQueueModernImpl extends BroadcastQueue { } } + private void checkAndRemoveWaitingFor() { + if (!mWaitingFor.isEmpty()) { + mWaitingFor.removeIf((pair) -> { + if (pair.first.getAsBoolean()) { + pair.second.countDown(); + return true; + } else { + return false; + } + }); + } + } + @Override public void forceDelayBroadcastDelivery(@NonNull String targetPackage, long delayedDurationMs) {