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

Commit 78774b57 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Register prox secondary sensor when safe." into sc-dev

parents 1b3424aa c323089e
Loading
Loading
Loading
Loading
+30 −23
Original line number Diff line number Diff line
@@ -21,8 +21,8 @@ import android.util.Log;

import com.android.internal.annotations.VisibleForTesting;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.util.Assert;
import com.android.systemui.util.concurrency.DelayableExecutor;
import com.android.systemui.util.concurrency.Execution;

import java.util.ArrayList;
import java.util.List;
@@ -62,6 +62,7 @@ public class ProximitySensor implements ThresholdSensor {
    private final ThresholdSensor mPrimaryThresholdSensor;
    private final ThresholdSensor mSecondaryThresholdSensor;
    private final DelayableExecutor mDelayableExecutor;
    private final Execution mExecution;
    private final List<ThresholdSensor.Listener> mListeners = new ArrayList<>();
    private String mTag = null;
    @VisibleForTesting protected boolean mPaused;
@@ -74,14 +75,10 @@ public class ProximitySensor implements ThresholdSensor {
    private boolean mInitializedListeners = false;
    private boolean mSecondarySafe = false;

    private ThresholdSensor.Listener mPrimaryEventListener = new ThresholdSensor.Listener() {
        @Override
        public void onThresholdCrossed(ThresholdSensorEvent event) {
            onPrimarySensorEvent(event);
        }
    };
    private final ThresholdSensor.Listener mPrimaryEventListener = this::onPrimarySensorEvent;

    private ThresholdSensor.Listener mSecondaryEventListener = new ThresholdSensor.Listener() {
    private final ThresholdSensor.Listener mSecondaryEventListener =
            new ThresholdSensor.Listener() {
        @Override
        public void onThresholdCrossed(ThresholdSensorEvent event) {
            // If we no longer have a "below" signal and the secondary sensor is not
@@ -110,12 +107,15 @@ public class ProximitySensor implements ThresholdSensor {
    };

    @Inject
    public ProximitySensor(@PrimaryProxSensor ThresholdSensor primary,
    public ProximitySensor(
            @PrimaryProxSensor ThresholdSensor primary,
            @SecondaryProxSensor ThresholdSensor  secondary,
            @Main DelayableExecutor delayableExecutor) {
            @Main DelayableExecutor delayableExecutor,
            Execution execution) {
        mPrimaryThresholdSensor = primary;
        mSecondaryThresholdSensor = secondary;
        mDelayableExecutor = delayableExecutor;
        mExecution = execution;
    }

    @Override
@@ -127,7 +127,7 @@ public class ProximitySensor implements ThresholdSensor {

    @Override
    public void setDelay(int delay) {
        Assert.isMainThread();
        mExecution.assertIsMainThread();
        mPrimaryThresholdSensor.setDelay(delay);
        mSecondaryThresholdSensor.setDelay(delay);
    }
@@ -137,7 +137,7 @@ public class ProximitySensor implements ThresholdSensor {
     */
    @Override
    public void pause() {
        Assert.isMainThread();
        mExecution.assertIsMainThread();
        mPaused = true;
        unregisterInternal();
    }
@@ -147,18 +147,23 @@ public class ProximitySensor implements ThresholdSensor {
     */
    @Override
    public void resume() {
        Assert.isMainThread();
        mExecution.assertIsMainThread();
        mPaused = false;
        registerInternal();
    }

    /**
     * Sets that it is safe to leave the secondary sensor on indefinitely.
     *
     * The secondary sensor will be turned on if there are any registered listeners, regardless
     * of what is reported by the primary sensor.
     */
    public void setSecondarySafe(boolean safe) {
        mSecondarySafe = safe;
        if (!mSecondarySafe) {
            mSecondaryThresholdSensor.pause();
        } else {
            mSecondaryThresholdSensor.resume();
        }
    }

@@ -185,7 +190,7 @@ public class ProximitySensor implements ThresholdSensor {
     */
    @Override
    public void register(ThresholdSensor.Listener listener) {
        Assert.isMainThread();
        mExecution.assertIsMainThread();
        if (!isLoaded()) {
            return;
        }
@@ -199,13 +204,15 @@ public class ProximitySensor implements ThresholdSensor {
    }

    protected void registerInternal() {
        Assert.isMainThread();
        mExecution.assertIsMainThread();
        if (mRegistered || mPaused || mListeners.isEmpty()) {
            return;
        }
        if (!mInitializedListeners) {
            mPrimaryThresholdSensor.register(mPrimaryEventListener);
            if (!mSecondarySafe) {
                mSecondaryThresholdSensor.pause();
            }
            mSecondaryThresholdSensor.register(mSecondaryEventListener);
            mInitializedListeners = true;
        }
@@ -222,7 +229,7 @@ public class ProximitySensor implements ThresholdSensor {
     */
    @Override
    public void unregister(ThresholdSensor.Listener listener) {
        Assert.isMainThread();
        mExecution.assertIsMainThread();
        mListeners.remove(listener);
        if (mListeners.size() == 0) {
            unregisterInternal();
@@ -230,7 +237,7 @@ public class ProximitySensor implements ThresholdSensor {
    }

    protected void unregisterInternal() {
        Assert.isMainThread();
        mExecution.assertIsMainThread();
        if (!mRegistered) {
            return;
        }
@@ -252,7 +259,7 @@ public class ProximitySensor implements ThresholdSensor {

    /** Update all listeners with the last value this class received from the sensor. */
    public void alertListeners() {
        Assert.isMainThread();
        mExecution.assertIsMainThread();
        if (mAlerting.getAndSet(true)) {
            return;
        }
@@ -267,7 +274,7 @@ public class ProximitySensor implements ThresholdSensor {
    }

    private void onPrimarySensorEvent(ThresholdSensorEvent event) {
        Assert.isMainThread();
        mExecution.assertIsMainThread();
        if (mLastPrimaryEvent != null && event.getBelow() == mLastPrimaryEvent.getBelow()) {
            return;
        }
@@ -290,7 +297,7 @@ public class ProximitySensor implements ThresholdSensor {
    }

    private void onSensorEvent(ThresholdSensorEvent event) {
        Assert.isMainThread();
        mExecution.assertIsMainThread();
        if (mLastEvent != null && event.getBelow() == mLastEvent.getBelow()) {
            return;
        }
@@ -306,9 +313,9 @@ public class ProximitySensor implements ThresholdSensor {
    @Override
    public String toString() {
        return String.format("{registered=%s, paused=%s, near=%s, primarySensor=%s, "
                + "secondarySensor=%s}",
                + "secondarySensor=%s secondarySafe=%s}",
                isRegistered(), mPaused, isNear(), mPrimaryThresholdSensor,
                mSecondaryThresholdSensor);
                mSecondaryThresholdSensor, mSecondarySafe);
    }

    /**
+17 −12
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ import android.util.Log;

import com.android.internal.annotations.VisibleForTesting;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.util.Assert;
import com.android.systemui.util.concurrency.Execution;

import java.util.ArrayList;
import java.util.List;
@@ -37,6 +37,7 @@ class ThresholdSensorImpl implements ThresholdSensor {
    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);

    private final AsyncSensorManager mSensorManager;
    private final Execution mExecution;
    private final Sensor mSensor;
    private final float mThreshold;
    private boolean mRegistered;
@@ -61,9 +62,10 @@ class ThresholdSensorImpl implements ThresholdSensor {
        }
    };

    private ThresholdSensorImpl(AsyncSensorManager sensorManager,
            Sensor sensor, float threshold, float thresholdLatch, int sensorDelay) {
    private ThresholdSensorImpl(AsyncSensorManager sensorManager, Sensor sensor,
            Execution execution,  float threshold, float thresholdLatch, int sensorDelay) {
        mSensorManager = sensorManager;
        mExecution = execution;
        mSensor = sensor;
        mThreshold = threshold;
        mThresholdLatch = thresholdLatch;
@@ -107,7 +109,7 @@ class ThresholdSensorImpl implements ThresholdSensor {
     */
    @Override
    public void register(Listener listener) {
        Assert.isMainThread();
        mExecution.assertIsMainThread();
        if (!mListeners.contains(listener)) {
            mListeners.add(listener);
        }
@@ -116,7 +118,7 @@ class ThresholdSensorImpl implements ThresholdSensor {

    @Override
    public void unregister(Listener listener) {
        Assert.isMainThread();
        mExecution.assertIsMainThread();
        mListeners.remove(listener);
        unregisterInternal();
    }
@@ -126,7 +128,7 @@ class ThresholdSensorImpl implements ThresholdSensor {
     */
    @Override
    public void pause() {
        Assert.isMainThread();
        mExecution.assertIsMainThread();
        mPaused = true;
        unregisterInternal();
    }
@@ -136,7 +138,7 @@ class ThresholdSensorImpl implements ThresholdSensor {
     */
    @Override
    public void resume() {
        Assert.isMainThread();
        mExecution.assertIsMainThread();
        mPaused = false;
        registerInternal();
    }
@@ -148,7 +150,7 @@ class ThresholdSensorImpl implements ThresholdSensor {
    }

    private void registerInternal() {
        Assert.isMainThread();
        mExecution.assertIsMainThread();
        if (mRegistered || mPaused || mListeners.isEmpty()) {
            return;
        }
@@ -158,7 +160,7 @@ class ThresholdSensorImpl implements ThresholdSensor {
    }

    private void unregisterInternal() {
        Assert.isMainThread();
        mExecution.assertIsMainThread();
        if (!mRegistered) {
            return;
        }
@@ -177,7 +179,7 @@ class ThresholdSensorImpl implements ThresholdSensor {
     * still appears entirely binary.
     */
    private void onSensorEvent(boolean belowThreshold, boolean aboveThreshold, long timestampNs) {
        Assert.isMainThread();
        mExecution.assertIsMainThread();
        if (!mRegistered) {
            return;
        }
@@ -212,6 +214,7 @@ class ThresholdSensorImpl implements ThresholdSensor {
    static class Builder {
        private final Resources mResources;
        private final AsyncSensorManager mSensorManager;
        private final Execution mExecution;
        private int mSensorDelay = SensorManager.SENSOR_DELAY_NORMAL;;
        private float mThresholdValue;
        private float mThresholdLatchValue;
@@ -221,9 +224,10 @@ class ThresholdSensorImpl implements ThresholdSensor {
        private boolean mThresholdLatchValueSet;

        @Inject
        Builder(@Main Resources resources, AsyncSensorManager sensorManager) {
        Builder(@Main Resources resources, AsyncSensorManager sensorManager, Execution execution) {
            mResources = resources;
            mSensorManager = sensorManager;
            mExecution = execution;
        }


@@ -302,7 +306,8 @@ class ThresholdSensorImpl implements ThresholdSensor {
            }

            return new ThresholdSensorImpl(
                    mSensorManager, mSensor, mThresholdValue, mThresholdLatchValue, mSensorDelay);
                    mSensorManager, mSensor, mExecution,
                    mThresholdValue, mThresholdLatchValue, mSensorDelay);
        }

        private Sensor findSensorByType(String sensorType) {
+2 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.systemui.util.sensors;

import com.android.systemui.util.concurrency.DelayableExecutor;
import com.android.systemui.util.concurrency.FakeExecution;

public class FakeProximitySensor extends ProximitySensor {
    private boolean mAvailable;
@@ -25,7 +26,7 @@ public class FakeProximitySensor extends ProximitySensor {
    public FakeProximitySensor(ThresholdSensor primary, ThresholdSensor secondary,
            DelayableExecutor delayableExecutor) {
        super(primary, secondary == null ? new FakeThresholdSensor() : secondary,
                delayableExecutor);
                delayableExecutor, new FakeExecution());
        mAvailable = true;
    }

+5 −2
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.testing.TestableLooper;
import androidx.test.filters.SmallTest;

import com.android.systemui.SysuiTestCase;
import com.android.systemui.util.concurrency.FakeExecution;
import com.android.systemui.util.concurrency.FakeExecutor;
import com.android.systemui.util.time.FakeSystemClock;

@@ -54,7 +55,8 @@ public class ProximitySensorDualTest extends SysuiTestCase {
        mThresholdSensorSecondary.setLoaded(true);

        mProximitySensor = new ProximitySensor(
                mThresholdSensorPrimary, mThresholdSensorSecondary, mFakeExecutor);
                mThresholdSensorPrimary, mThresholdSensorSecondary, mFakeExecutor,
                new FakeExecution());
    }

    @Test
@@ -324,9 +326,10 @@ public class ProximitySensorDualTest extends SysuiTestCase {

        TestableListener listener = new TestableListener();

        // WE immediately register the secondary sensor.
        mProximitySensor.register(listener);
        assertFalse(mThresholdSensorPrimary.isPaused());
        assertTrue(mThresholdSensorSecondary.isPaused());
        assertFalse(mThresholdSensorSecondary.isPaused());
        assertNull(listener.mLastEvent);
        assertEquals(0, listener.mCallCount);

+2 −1
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.testing.TestableLooper;
import androidx.test.filters.SmallTest;

import com.android.systemui.SysuiTestCase;
import com.android.systemui.util.concurrency.FakeExecution;
import com.android.systemui.util.concurrency.FakeExecutor;
import com.android.systemui.util.time.FakeSystemClock;

@@ -54,7 +55,7 @@ public class ProximitySensorSingleTest extends SysuiTestCase {
        mThresholdSensor.setLoaded(true);

        mProximitySensor = new ProximitySensor(
                mThresholdSensor, new FakeThresholdSensor(), mFakeExecutor);
                mThresholdSensor, new FakeThresholdSensor(), mFakeExecutor, new FakeExecution());
    }

    @Test
Loading