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

Commit 3de684ba authored by Dave Mankoff's avatar Dave Mankoff
Browse files

More defensive checks for ProximitySensor

The bug this is fixing may already be resolved, but I wanted to
add a few more defensive checks to be sure. Also, I believe this
fixes a case where multiple listeners could receive different
proximity events if the first one pauses the proximity sensor before
the second listener is triggered.

Fixes: 156356906
Test: atest SystemUITests
Change-Id: Icdb23b8822f536d3c845c119509d180306fd8ad6
parent 05463ee4
Loading
Loading
Loading
Loading
+10 −4
Original line number Diff line number Diff line
@@ -86,9 +86,12 @@ public class ProximitySensor implements ThresholdSensor {
        public void onThresholdCrossed(ThresholdSensorEvent event) {
            // If we no longer have a "below" signal and the secondary sensor is not
            // considered "safe", then we need to turn it off.
            if (!mSecondarySafe && (!mLastPrimaryEvent.getBelow() || !event.getBelow())) {
            if (!mSecondarySafe
                    && (mLastPrimaryEvent == null
                    || !mLastPrimaryEvent.getBelow()
                    || !event.getBelow())) {
                mSecondaryThresholdSensor.pause();
                if (!mLastPrimaryEvent.getBelow()) {
                if (mLastPrimaryEvent == null || !mLastPrimaryEvent.getBelow()) {
                    // Only check the secondary as long as the primary thinks we're near.
                    mCancelSecondaryRunnable = null;
                    return;
@@ -100,8 +103,10 @@ public class ProximitySensor implements ThresholdSensor {
            }
            logDebug("Secondary sensor event: " + event.getBelow() + ".");

            if (!mPaused) {
                onSensorEvent(event);
            }
        }
    };

    @Inject
@@ -252,9 +257,10 @@ public class ProximitySensor implements ThresholdSensor {
            return;
        }
        if (mLastEvent != null) {
            ThresholdSensorEvent lastEvent = mLastEvent;  // Listeners can null out mLastEvent.
            List<ThresholdSensor.Listener> listeners = new ArrayList<>(mListeners);
            listeners.forEach(proximitySensorListener ->
                    proximitySensorListener.onThresholdCrossed(mLastEvent));
                    proximitySensorListener.onThresholdCrossed(lastEvent));
        }

        mAlerting.set(false);
+26 −0
Original line number Diff line number Diff line
@@ -152,6 +152,32 @@ public class ProximitySensorDualTest extends SysuiTestCase {
        assertFalse(mProximitySensor.isRegistered());
    }

    @Test
    public void testUnregisterDuringCallback() {
        ThresholdSensor.Listener listenerA = event -> mProximitySensor.pause();
        TestableListener listenerB = new TestableListener();

        assertFalse(mProximitySensor.isRegistered());
        mProximitySensor.register(listenerA);
        mProximitySensor.register(listenerB);
        assertTrue(mProximitySensor.isRegistered());
        assertFalse(mThresholdSensorPrimary.isPaused());
        assertTrue(mThresholdSensorSecondary.isPaused());
        assertNull(listenerB.mLastEvent);

        // listenerA will pause the proximity sensor, unregistering it.
        mThresholdSensorPrimary.triggerEvent(true, 0);
        mThresholdSensorSecondary.triggerEvent(true, 0);
        assertTrue(listenerB.mLastEvent.getBelow());
        assertEquals(1, listenerB.mCallCount);


        // A second call to trigger it should be ignored.
        mThresholdSensorSecondary.triggerEvent(false, 0);
        assertTrue(listenerB.mLastEvent.getBelow());
        assertEquals(1, listenerB.mCallCount);
    }

    @Test
    public void testPauseAndResume() {
        TestableListener listener = new TestableListener();