Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ed40b277 authored by Lucas Silva's avatar Lucas Silva
Browse files

Remove old weak references to callbacks when iterating over callbacks in

Communal logic.

Test: atest CommunalSourceMonitorTest
Change-Id: I7d9e2039c69149dbb45d3e749f69db64b0a1b3ae
parent e2024745
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -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> callback : mCallbacks) {
            Callback cb = callback.get();
            if (cb != null) {
        Iterator<WeakReference<Callback>> 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);