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

Commit 168dc4c4 authored by Jernej Virag's avatar Jernej Virag Committed by Android Build Coastguard Worker
Browse files

Use thread/iteration safe collection for proximity listeners

CopyOnWrite datastructures essentially do what the existing code did: they ensure that iteration operates on an immutable copy of the list and never throw ConcurrentModificationException. They're also more efficient because they do copy only on add, not each iteration.

Bug: 354293532
Test: atest ProximitySensorImplDualTest ProximitySensorImplSingleTest
Flag: EXEMPT inplace refactor
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:91478b22c2bcd1d617325a37e10a24726c5e430b)
Merged-In: If687969a7ef81e33e8f41b28a751ca60fd90349f
Change-Id: If687969a7ef81e33e8f41b28a751ca60fd90349f
parent 88ff584b
Loading
Loading
Loading
Loading
+5 −6
Original line number Diff line number Diff line
@@ -26,8 +26,8 @@ import com.android.systemui.statusbar.policy.DevicePostureController;
import com.android.systemui.util.concurrency.DelayableExecutor;
import com.android.systemui.util.concurrency.Execution;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.inject.Inject;
@@ -64,7 +64,7 @@ class ProximitySensorImpl implements ProximitySensor {
    ThresholdSensor mSecondaryThresholdSensor;
    private final DelayableExecutor mDelayableExecutor;
    private final Execution mExecution;
    private final List<ThresholdSensor.Listener> mListeners = new ArrayList<>();
    private final Set<Listener> mListeners = new CopyOnWriteArraySet<>();
    private String mTag = null;
    @VisibleForTesting protected boolean mPaused;
    private ThresholdSensorEvent mLastPrimaryEvent;
@@ -246,7 +246,7 @@ class ProximitySensorImpl implements ProximitySensor {
    public void unregister(ThresholdSensor.Listener listener) {
        mExecution.assertIsMainThread();
        mListeners.remove(listener);
        if (mListeners.size() == 0) {
        if (mListeners.isEmpty()) {
            unregisterInternal();
        }
    }
@@ -296,8 +296,7 @@ class ProximitySensorImpl implements ProximitySensor {
        }
        if (mLastEvent != null) {
            ThresholdSensorEvent lastEvent = mLastEvent;  // Listeners can null out mLastEvent.
            List<ThresholdSensor.Listener> listeners = new ArrayList<>(mListeners);
            listeners.forEach(proximitySensorListener ->
            mListeners.forEach(proximitySensorListener ->
                    proximitySensorListener.onThresholdCrossed(lastEvent));
        }