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

Commit 6296e112 authored by Joe Bolinger's avatar Joe Bolinger Committed by Android (Google) Code Review
Browse files

Merge "Allow awaitLux to fetch a value after destroy." into tm-qpr-dev

parents 9d3dbd19 4daff35b
Loading
Loading
Loading
Loading
+7 −6
Original line number Diff line number Diff line
@@ -120,7 +120,7 @@ final class ALSProbe implements Probe {

        // if a final consumer is set it will call destroy/disable on the next value if requested
        if (!mDestroyed && mNextConsumer == null) {
            disableLightSensorLoggingLocked();
            disableLightSensorLoggingLocked(false /* destroying */);
        }
    }

@@ -130,7 +130,7 @@ final class ALSProbe implements Probe {

        // if a final consumer is set it will call destroy/disable on the next value if requested
        if (!mDestroyed && mNextConsumer == null) {
            disableLightSensorLoggingLocked();
            disableLightSensorLoggingLocked(true /* destroying */);
            mDestroyed = true;
        }
    }
@@ -177,11 +177,10 @@ final class ALSProbe implements Probe {
        final float current = mLastAmbientLux;
        if (current > -1f) {
            nextConsumer.consume(current);
        } else if (mDestroyed) {
            nextConsumer.consume(-1f);
        } else if (mNextConsumer != null) {
            mNextConsumer.add(nextConsumer);
        } else {
            mDestroyed = false;
            mNextConsumer = nextConsumer;
            enableLightSensorLoggingLocked();
        }
@@ -199,12 +198,14 @@ final class ALSProbe implements Probe {
        resetTimerLocked(true /* start */);
    }

    private void disableLightSensorLoggingLocked() {
    private void disableLightSensorLoggingLocked(boolean destroying) {
        resetTimerLocked(false /* start */);

        if (mEnabled) {
            mEnabled = false;
            if (!destroying) {
                mLastAmbientLux = -1;
            }
            mSensorManager.unregisterListener(mLightSensorListener);
            Slog.v(TAG, "Disable ALS: " + mLightSensorListener.hashCode());
        }
+17 −8
Original line number Diff line number Diff line
@@ -125,12 +125,9 @@ public class ALSProbeTest {
        mProbe.destroy();
        mProbe.enable();

        AtomicInteger lux = new AtomicInteger(10);
        mProbe.awaitNextLux((v) -> lux.set(Math.round(v)), null /* handler */);

        verify(mSensorManager, never()).registerListener(any(), any(), anyInt());
        verifyNoMoreInteractions(mSensorManager);
        assertThat(lux.get()).isLessThan(0);
        assertThat(mProbe.getMostRecentLux()).isLessThan(0);
    }

    @Test
@@ -323,15 +320,27 @@ public class ALSProbeTest {
    }

    @Test
    public void testNoNextLuxWhenDestroyed() {
    public void testDestroyAllowsAwaitLuxExactlyOnce() {
        final float lastValue = 5.5f;
        mProbe.destroy();

        AtomicInteger lux = new AtomicInteger(-20);
        AtomicInteger lux = new AtomicInteger(10);
        mProbe.awaitNextLux((v) -> lux.set(Math.round(v)), null /* handler */);

        assertThat(lux.get()).isEqualTo(-1);
        verify(mSensorManager, never()).registerListener(
        verify(mSensorManager).registerListener(
                mSensorEventListenerCaptor.capture(), any(), anyInt());
        mSensorEventListenerCaptor.getValue().onSensorChanged(
                new SensorEvent(mLightSensor, 1, 1, new float[]{lastValue}));

        assertThat(lux.get()).isEqualTo(Math.round(lastValue));
        verify(mSensorManager).unregisterListener(eq(mSensorEventListenerCaptor.getValue()));

        lux.set(22);
        mProbe.enable();
        mProbe.awaitNextLux((v) -> lux.set(Math.round(v)), null /* handler */);
        mProbe.enable();

        assertThat(lux.get()).isEqualTo(Math.round(lastValue));
        verifyNoMoreInteractions(mSensorManager);
    }