From ed40b2776d72d3c4e5f82d38a66b44127d59b49f Mon Sep 17 00:00:00 2001 From: Lucas Silva Date: Wed, 18 Aug 2021 15:31:53 +0000 Subject: [PATCH] Remove old weak references to callbacks when iterating over callbacks in Communal logic. Test: atest CommunalSourceMonitorTest Change-Id: I7d9e2039c69149dbb45d3e749f69db64b0a1b3ae --- .../systemui/communal/CommunalSourceMonitor.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/communal/CommunalSourceMonitor.java b/packages/SystemUI/src/com/android/systemui/communal/CommunalSourceMonitor.java index 03693301b5f8c..032cd1e347ba4 100644 --- a/packages/SystemUI/src/com/android/systemui/communal/CommunalSourceMonitor.java +++ b/packages/SystemUI/src/com/android/systemui/communal/CommunalSourceMonitor.java @@ -32,6 +32,7 @@ import com.google.android.collect.Lists; import java.lang.ref.WeakReference; import java.util.ArrayList; +import java.util.Iterator; import javax.inject.Inject; @@ -103,9 +104,12 @@ public class CommunalSourceMonitor { private void executeOnSourceAvailableCallbacks() { // If the new source is valid, inform registered Callbacks of its presence. - for (WeakReference callback : mCallbacks) { - Callback cb = callback.get(); - if (cb != null) { + Iterator> itr = mCallbacks.iterator(); + while (itr.hasNext()) { + Callback cb = itr.next().get(); + if (cb == null) { + itr.remove(); + } else { cb.onSourceAvailable( (mCommunalEnabled && mCurrentSource != null) ? new WeakReference<>( mCurrentSource) : null);