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

Commit f7882cb9 authored by Dave Mankoff's avatar Dave Mankoff
Browse files

Pause Primary Prox when Secondary Running.

This reduces usage of the primary threshold sensor by turning it off
when the secondary sensor is "safe" to use.

Fixes: 194021865
Test: atest SystemUITests
Change-Id: I95a10370bdecdd909b36259e47c1e450335eb778
parent 51e68e2a
Loading
Loading
Loading
Loading
+33 −15
Original line number Diff line number Diff line
@@ -87,15 +87,23 @@ public class ProximitySensor implements ThresholdSensor {
                    && (mLastPrimaryEvent == null
                    || !mLastPrimaryEvent.getBelow()
                    || !event.getBelow())) {
                mSecondaryThresholdSensor.pause();
                chooseSensor();
                if (mLastPrimaryEvent == null || !mLastPrimaryEvent.getBelow()) {
                    // Only check the secondary as long as the primary thinks we're near.
                    if (mCancelSecondaryRunnable != null) {
                        mCancelSecondaryRunnable.run();
                        mCancelSecondaryRunnable = null;
                    }
                    return;
                } else {
                    // Check this sensor again in a moment.
                    mCancelSecondaryRunnable = mDelayableExecutor.executeDelayed(
                            mSecondaryThresholdSensor::resume, SECONDARY_PING_INTERVAL_MS);
                    mCancelSecondaryRunnable = mDelayableExecutor.executeDelayed(() -> {
                        // This is safe because we know that mSecondaryThresholdSensor
                        // is loaded, otherwise we wouldn't be here.
                        mPrimaryThresholdSensor.pause();
                        mSecondaryThresholdSensor.resume();
                    },
                        SECONDARY_PING_INTERVAL_MS);
                }
            }
            logDebug("Secondary sensor event: " + event.getBelow() + ".");
@@ -159,12 +167,8 @@ public class ProximitySensor implements ThresholdSensor {
     * of what is reported by the primary sensor.
     */
    public void setSecondarySafe(boolean safe) {
        mSecondarySafe = safe;
        if (!mSecondarySafe) {
            mSecondaryThresholdSensor.pause();
        } else {
            mSecondaryThresholdSensor.resume();
        }
        mSecondarySafe = mSecondaryThresholdSensor.isLoaded() && safe;
        chooseSensor();
    }

    /**
@@ -209,16 +213,30 @@ public class ProximitySensor implements ThresholdSensor {
            return;
        }
        if (!mInitializedListeners) {
            mPrimaryThresholdSensor.register(mPrimaryEventListener);
            if (!mSecondarySafe) {
            mPrimaryThresholdSensor.pause();
            mSecondaryThresholdSensor.pause();
            }
            mPrimaryThresholdSensor.register(mPrimaryEventListener);
            mSecondaryThresholdSensor.register(mSecondaryEventListener);
            mInitializedListeners = true;
        }
        logDebug("Registering sensor listener");
        mPrimaryThresholdSensor.resume();

        mRegistered = true;
        chooseSensor();
    }

    private void chooseSensor() {
        mExecution.assertIsMainThread();
        if (!mRegistered || mPaused || mListeners.isEmpty()) {
            return;
        }
        if (mSecondarySafe) {
            mSecondaryThresholdSensor.resume();
            mPrimaryThresholdSensor.pause();
        } else {
            mPrimaryThresholdSensor.resume();
            mSecondaryThresholdSensor.pause();
        }
    }

    /**
@@ -312,7 +330,7 @@ public class ProximitySensor implements ThresholdSensor {
        }

        if (!mSecondarySafe && !event.getBelow()) {
            mSecondaryThresholdSensor.pause();
            chooseSensor();
        }

        mLastEvent = event;
+59 −29
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.systemui.util.sensors;

import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -338,30 +340,25 @@ public class ProximitySensorDualTest extends SysuiTestCase {
    @Test
    public void testSecondaryCancelsSecondary() {
        TestableListener listener = new TestableListener();
        ThresholdSensor.Listener cancelingListener = new ThresholdSensor.Listener() {
            @Override
            public void onThresholdCrossed(ThresholdSensor.ThresholdSensorEvent event) {
                mProximitySensor.pause();
            }
        };
        ThresholdSensor.Listener cancelingListener = event -> mProximitySensor.pause();

        mProximitySensor.register(listener);
        mProximitySensor.register(cancelingListener);
        assertNull(listener.mLastEvent);
        assertEquals(0, listener.mCallCount);
        assertThat(listener.mLastEvent).isNull();
        assertThat(listener.mCallCount).isEqualTo(0);

        mThresholdSensorPrimary.triggerEvent(true, 0);
        assertNull(listener.mLastEvent);
        assertEquals(0, listener.mCallCount);
        assertThat(listener.mLastEvent).isNull();
        assertThat(listener.mCallCount).isEqualTo(0);
        mThresholdSensorSecondary.triggerEvent(true, 0);
        assertTrue(listener.mLastEvent.getBelow());
        assertEquals(1, listener.mCallCount);
        assertThat(listener.mLastEvent.getBelow()).isTrue();
        assertThat(listener.mCallCount).isEqualTo(1);

        // The proximity sensor should now be canceled. Advancing the clock should do nothing.
        assertEquals(0, mFakeExecutor.numPending());
        assertThat(mFakeExecutor.numPending()).isEqualTo(0);
        mThresholdSensorSecondary.triggerEvent(false, 1);
        assertTrue(listener.mLastEvent.getBelow());
        assertEquals(1, listener.mCallCount);
        assertThat(listener.mLastEvent.getBelow()).isTrue();
        assertThat(listener.mCallCount).isEqualTo(1);

        mProximitySensor.unregister(listener);
    }
@@ -372,33 +369,66 @@ public class ProximitySensorDualTest extends SysuiTestCase {

        TestableListener listener = new TestableListener();

        // WE immediately register the secondary sensor.
        // We immediately register the secondary sensor.
        mProximitySensor.register(listener);
        assertFalse(mThresholdSensorPrimary.isPaused());
        assertFalse(mThresholdSensorSecondary.isPaused());
        assertNull(listener.mLastEvent);
        assertEquals(0, listener.mCallCount);
        assertThat(mThresholdSensorPrimary.isPaused()).isTrue();
        assertThat(mThresholdSensorSecondary.isPaused()).isFalse();
        assertThat(listener.mLastEvent).isNull();
        assertThat(listener.mCallCount).isEqualTo(0);

        mThresholdSensorPrimary.triggerEvent(true, 0);
        assertNull(listener.mLastEvent);
        assertEquals(0, listener.mCallCount);
        assertThat(listener.mLastEvent).isNull();
        assertThat(listener.mCallCount).isEqualTo(0);
        mThresholdSensorSecondary.triggerEvent(true, 0);
        assertTrue(listener.mLastEvent.getBelow());
        assertEquals(1, listener.mCallCount);
        assertThat(listener.mLastEvent.getBelow()).isTrue();
        assertThat(listener.mCallCount).isEqualTo(1);

        // The secondary sensor should now remain resumed indefinitely.
        assertFalse(mThresholdSensorSecondary.isPaused());
        assertThat(mThresholdSensorSecondary.isPaused()).isFalse();
        mThresholdSensorSecondary.triggerEvent(false, 1);
        assertFalse(listener.mLastEvent.getBelow());
        assertEquals(2, listener.mCallCount);
        assertThat(listener.mLastEvent.getBelow()).isFalse();
        assertThat(listener.mCallCount).isEqualTo(2);

        // The secondary is still running, and not polling with the executor.
        assertFalse(mThresholdSensorSecondary.isPaused());
        assertEquals(0, mFakeExecutor.numPending());
        assertThat(mThresholdSensorSecondary.isPaused()).isFalse();
        assertThat(mFakeExecutor.numPending()).isEqualTo(0);

        mProximitySensor.unregister(listener);
    }

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

        mProximitySensor.register(listener);

        assertThat(mThresholdSensorPrimary.isPaused()).isFalse();
        assertThat(mThresholdSensorSecondary.isPaused()).isTrue();

        mProximitySensor.setSecondarySafe(true);

        assertThat(mThresholdSensorPrimary.isPaused()).isTrue();
        assertThat(mThresholdSensorSecondary.isPaused()).isFalse();
    }

    @Test
    public void testSecondaryResumesPrimary() {
        mProximitySensor.setSecondarySafe(true);

        TestableListener listener = new TestableListener();
        mProximitySensor.register(listener);

        assertThat(mThresholdSensorPrimary.isPaused()).isTrue();
        assertThat(mThresholdSensorSecondary.isPaused()).isFalse();

        mProximitySensor.setSecondarySafe(false);

        assertThat(mThresholdSensorPrimary.isPaused()).isFalse();
        assertThat(mThresholdSensorSecondary.isPaused()).isTrue();


    }

    private static class TestableListener implements ThresholdSensor.Listener {
        ThresholdSensor.ThresholdSensorEvent mLastEvent;
        int mCallCount = 0;