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

This commit is contained in:
TreeHugger Robot
2021-06-01 14:51:16 +00:00
committed by Android (Google) Code Review
6 changed files with 59 additions and 50 deletions

View File

@@ -21,8 +21,8 @@ import android.util.Log;
import com.android.internal.annotations.VisibleForTesting; import com.android.internal.annotations.VisibleForTesting;
import com.android.systemui.dagger.qualifiers.Main; 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.DelayableExecutor;
import com.android.systemui.util.concurrency.Execution;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -62,6 +62,7 @@ public class ProximitySensor implements ThresholdSensor {
private final ThresholdSensor mPrimaryThresholdSensor; private final ThresholdSensor mPrimaryThresholdSensor;
private final ThresholdSensor mSecondaryThresholdSensor; private final ThresholdSensor mSecondaryThresholdSensor;
private final DelayableExecutor mDelayableExecutor; private final DelayableExecutor mDelayableExecutor;
private final Execution mExecution;
private final List<ThresholdSensor.Listener> mListeners = new ArrayList<>(); private final List<ThresholdSensor.Listener> mListeners = new ArrayList<>();
private String mTag = null; private String mTag = null;
@VisibleForTesting protected boolean mPaused; @VisibleForTesting protected boolean mPaused;
@@ -74,14 +75,10 @@ public class ProximitySensor implements ThresholdSensor {
private boolean mInitializedListeners = false; private boolean mInitializedListeners = false;
private boolean mSecondarySafe = false; private boolean mSecondarySafe = false;
private ThresholdSensor.Listener mPrimaryEventListener = new ThresholdSensor.Listener() { private final ThresholdSensor.Listener mPrimaryEventListener = this::onPrimarySensorEvent;
@Override
public void onThresholdCrossed(ThresholdSensorEvent event) {
onPrimarySensorEvent(event);
}
};
private ThresholdSensor.Listener mSecondaryEventListener = new ThresholdSensor.Listener() { private final ThresholdSensor.Listener mSecondaryEventListener =
new ThresholdSensor.Listener() {
@Override @Override
public void onThresholdCrossed(ThresholdSensorEvent event) { public void onThresholdCrossed(ThresholdSensorEvent event) {
// If we no longer have a "below" signal and the secondary sensor is not // If we no longer have a "below" signal and the secondary sensor is not
@@ -110,12 +107,15 @@ public class ProximitySensor implements ThresholdSensor {
}; };
@Inject @Inject
public ProximitySensor(@PrimaryProxSensor ThresholdSensor primary, public ProximitySensor(
@PrimaryProxSensor ThresholdSensor primary,
@SecondaryProxSensor ThresholdSensor secondary, @SecondaryProxSensor ThresholdSensor secondary,
@Main DelayableExecutor delayableExecutor) { @Main DelayableExecutor delayableExecutor,
Execution execution) {
mPrimaryThresholdSensor = primary; mPrimaryThresholdSensor = primary;
mSecondaryThresholdSensor = secondary; mSecondaryThresholdSensor = secondary;
mDelayableExecutor = delayableExecutor; mDelayableExecutor = delayableExecutor;
mExecution = execution;
} }
@Override @Override
@@ -127,7 +127,7 @@ public class ProximitySensor implements ThresholdSensor {
@Override @Override
public void setDelay(int delay) { public void setDelay(int delay) {
Assert.isMainThread(); mExecution.assertIsMainThread();
mPrimaryThresholdSensor.setDelay(delay); mPrimaryThresholdSensor.setDelay(delay);
mSecondaryThresholdSensor.setDelay(delay); mSecondaryThresholdSensor.setDelay(delay);
} }
@@ -137,7 +137,7 @@ public class ProximitySensor implements ThresholdSensor {
*/ */
@Override @Override
public void pause() { public void pause() {
Assert.isMainThread(); mExecution.assertIsMainThread();
mPaused = true; mPaused = true;
unregisterInternal(); unregisterInternal();
} }
@@ -147,18 +147,23 @@ public class ProximitySensor implements ThresholdSensor {
*/ */
@Override @Override
public void resume() { public void resume() {
Assert.isMainThread(); mExecution.assertIsMainThread();
mPaused = false; mPaused = false;
registerInternal(); registerInternal();
} }
/** /**
* Sets that it is safe to leave the secondary sensor on indefinitely. * 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) { public void setSecondarySafe(boolean safe) {
mSecondarySafe = safe; mSecondarySafe = safe;
if (!mSecondarySafe) { if (!mSecondarySafe) {
mSecondaryThresholdSensor.pause(); mSecondaryThresholdSensor.pause();
} else {
mSecondaryThresholdSensor.resume();
} }
} }
@@ -185,7 +190,7 @@ public class ProximitySensor implements ThresholdSensor {
*/ */
@Override @Override
public void register(ThresholdSensor.Listener listener) { public void register(ThresholdSensor.Listener listener) {
Assert.isMainThread(); mExecution.assertIsMainThread();
if (!isLoaded()) { if (!isLoaded()) {
return; return;
} }
@@ -199,13 +204,15 @@ public class ProximitySensor implements ThresholdSensor {
} }
protected void registerInternal() { protected void registerInternal() {
Assert.isMainThread(); mExecution.assertIsMainThread();
if (mRegistered || mPaused || mListeners.isEmpty()) { if (mRegistered || mPaused || mListeners.isEmpty()) {
return; return;
} }
if (!mInitializedListeners) { if (!mInitializedListeners) {
mPrimaryThresholdSensor.register(mPrimaryEventListener); mPrimaryThresholdSensor.register(mPrimaryEventListener);
mSecondaryThresholdSensor.pause(); if (!mSecondarySafe) {
mSecondaryThresholdSensor.pause();
}
mSecondaryThresholdSensor.register(mSecondaryEventListener); mSecondaryThresholdSensor.register(mSecondaryEventListener);
mInitializedListeners = true; mInitializedListeners = true;
} }
@@ -222,7 +229,7 @@ public class ProximitySensor implements ThresholdSensor {
*/ */
@Override @Override
public void unregister(ThresholdSensor.Listener listener) { public void unregister(ThresholdSensor.Listener listener) {
Assert.isMainThread(); mExecution.assertIsMainThread();
mListeners.remove(listener); mListeners.remove(listener);
if (mListeners.size() == 0) { if (mListeners.size() == 0) {
unregisterInternal(); unregisterInternal();
@@ -230,7 +237,7 @@ public class ProximitySensor implements ThresholdSensor {
} }
protected void unregisterInternal() { protected void unregisterInternal() {
Assert.isMainThread(); mExecution.assertIsMainThread();
if (!mRegistered) { if (!mRegistered) {
return; return;
} }
@@ -252,7 +259,7 @@ public class ProximitySensor implements ThresholdSensor {
/** Update all listeners with the last value this class received from the sensor. */ /** Update all listeners with the last value this class received from the sensor. */
public void alertListeners() { public void alertListeners() {
Assert.isMainThread(); mExecution.assertIsMainThread();
if (mAlerting.getAndSet(true)) { if (mAlerting.getAndSet(true)) {
return; return;
} }
@@ -267,7 +274,7 @@ public class ProximitySensor implements ThresholdSensor {
} }
private void onPrimarySensorEvent(ThresholdSensorEvent event) { private void onPrimarySensorEvent(ThresholdSensorEvent event) {
Assert.isMainThread(); mExecution.assertIsMainThread();
if (mLastPrimaryEvent != null && event.getBelow() == mLastPrimaryEvent.getBelow()) { if (mLastPrimaryEvent != null && event.getBelow() == mLastPrimaryEvent.getBelow()) {
return; return;
} }
@@ -290,7 +297,7 @@ public class ProximitySensor implements ThresholdSensor {
} }
private void onSensorEvent(ThresholdSensorEvent event) { private void onSensorEvent(ThresholdSensorEvent event) {
Assert.isMainThread(); mExecution.assertIsMainThread();
if (mLastEvent != null && event.getBelow() == mLastEvent.getBelow()) { if (mLastEvent != null && event.getBelow() == mLastEvent.getBelow()) {
return; return;
} }
@@ -306,9 +313,9 @@ public class ProximitySensor implements ThresholdSensor {
@Override @Override
public String toString() { public String toString() {
return String.format("{registered=%s, paused=%s, near=%s, primarySensor=%s, " return String.format("{registered=%s, paused=%s, near=%s, primarySensor=%s, "
+ "secondarySensor=%s}", + "secondarySensor=%s secondarySafe=%s}",
isRegistered(), mPaused, isNear(), mPrimaryThresholdSensor, isRegistered(), mPaused, isNear(), mPrimaryThresholdSensor,
mSecondaryThresholdSensor); mSecondaryThresholdSensor, mSecondarySafe);
} }
/** /**

View File

@@ -25,7 +25,7 @@ import android.util.Log;
import com.android.internal.annotations.VisibleForTesting; import com.android.internal.annotations.VisibleForTesting;
import com.android.systemui.dagger.qualifiers.Main; 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.ArrayList;
import java.util.List; import java.util.List;
@@ -37,6 +37,7 @@ class ThresholdSensorImpl implements ThresholdSensor {
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
private final AsyncSensorManager mSensorManager; private final AsyncSensorManager mSensorManager;
private final Execution mExecution;
private final Sensor mSensor; private final Sensor mSensor;
private final float mThreshold; private final float mThreshold;
private boolean mRegistered; private boolean mRegistered;
@@ -61,9 +62,10 @@ class ThresholdSensorImpl implements ThresholdSensor {
} }
}; };
private ThresholdSensorImpl(AsyncSensorManager sensorManager, private ThresholdSensorImpl(AsyncSensorManager sensorManager, Sensor sensor,
Sensor sensor, float threshold, float thresholdLatch, int sensorDelay) { Execution execution, float threshold, float thresholdLatch, int sensorDelay) {
mSensorManager = sensorManager; mSensorManager = sensorManager;
mExecution = execution;
mSensor = sensor; mSensor = sensor;
mThreshold = threshold; mThreshold = threshold;
mThresholdLatch = thresholdLatch; mThresholdLatch = thresholdLatch;
@@ -107,7 +109,7 @@ class ThresholdSensorImpl implements ThresholdSensor {
*/ */
@Override @Override
public void register(Listener listener) { public void register(Listener listener) {
Assert.isMainThread(); mExecution.assertIsMainThread();
if (!mListeners.contains(listener)) { if (!mListeners.contains(listener)) {
mListeners.add(listener); mListeners.add(listener);
} }
@@ -116,7 +118,7 @@ class ThresholdSensorImpl implements ThresholdSensor {
@Override @Override
public void unregister(Listener listener) { public void unregister(Listener listener) {
Assert.isMainThread(); mExecution.assertIsMainThread();
mListeners.remove(listener); mListeners.remove(listener);
unregisterInternal(); unregisterInternal();
} }
@@ -126,7 +128,7 @@ class ThresholdSensorImpl implements ThresholdSensor {
*/ */
@Override @Override
public void pause() { public void pause() {
Assert.isMainThread(); mExecution.assertIsMainThread();
mPaused = true; mPaused = true;
unregisterInternal(); unregisterInternal();
} }
@@ -136,7 +138,7 @@ class ThresholdSensorImpl implements ThresholdSensor {
*/ */
@Override @Override
public void resume() { public void resume() {
Assert.isMainThread(); mExecution.assertIsMainThread();
mPaused = false; mPaused = false;
registerInternal(); registerInternal();
} }
@@ -148,7 +150,7 @@ class ThresholdSensorImpl implements ThresholdSensor {
} }
private void registerInternal() { private void registerInternal() {
Assert.isMainThread(); mExecution.assertIsMainThread();
if (mRegistered || mPaused || mListeners.isEmpty()) { if (mRegistered || mPaused || mListeners.isEmpty()) {
return; return;
} }
@@ -158,7 +160,7 @@ class ThresholdSensorImpl implements ThresholdSensor {
} }
private void unregisterInternal() { private void unregisterInternal() {
Assert.isMainThread(); mExecution.assertIsMainThread();
if (!mRegistered) { if (!mRegistered) {
return; return;
} }
@@ -177,7 +179,7 @@ class ThresholdSensorImpl implements ThresholdSensor {
* still appears entirely binary. * still appears entirely binary.
*/ */
private void onSensorEvent(boolean belowThreshold, boolean aboveThreshold, long timestampNs) { private void onSensorEvent(boolean belowThreshold, boolean aboveThreshold, long timestampNs) {
Assert.isMainThread(); mExecution.assertIsMainThread();
if (!mRegistered) { if (!mRegistered) {
return; return;
} }
@@ -212,6 +214,7 @@ class ThresholdSensorImpl implements ThresholdSensor {
static class Builder { static class Builder {
private final Resources mResources; private final Resources mResources;
private final AsyncSensorManager mSensorManager; private final AsyncSensorManager mSensorManager;
private final Execution mExecution;
private int mSensorDelay = SensorManager.SENSOR_DELAY_NORMAL;; private int mSensorDelay = SensorManager.SENSOR_DELAY_NORMAL;;
private float mThresholdValue; private float mThresholdValue;
private float mThresholdLatchValue; private float mThresholdLatchValue;
@@ -221,9 +224,10 @@ class ThresholdSensorImpl implements ThresholdSensor {
private boolean mThresholdLatchValueSet; private boolean mThresholdLatchValueSet;
@Inject @Inject
Builder(@Main Resources resources, AsyncSensorManager sensorManager) { Builder(@Main Resources resources, AsyncSensorManager sensorManager, Execution execution) {
mResources = resources; mResources = resources;
mSensorManager = sensorManager; mSensorManager = sensorManager;
mExecution = execution;
} }
@@ -302,7 +306,8 @@ class ThresholdSensorImpl implements ThresholdSensor {
} }
return new ThresholdSensorImpl( return new ThresholdSensorImpl(
mSensorManager, mSensor, mThresholdValue, mThresholdLatchValue, mSensorDelay); mSensorManager, mSensor, mExecution,
mThresholdValue, mThresholdLatchValue, mSensorDelay);
} }
private Sensor findSensorByType(String sensorType) { private Sensor findSensorByType(String sensorType) {

View File

@@ -17,6 +17,7 @@
package com.android.systemui.util.sensors; package com.android.systemui.util.sensors;
import com.android.systemui.util.concurrency.DelayableExecutor; import com.android.systemui.util.concurrency.DelayableExecutor;
import com.android.systemui.util.concurrency.FakeExecution;
public class FakeProximitySensor extends ProximitySensor { public class FakeProximitySensor extends ProximitySensor {
private boolean mAvailable; private boolean mAvailable;
@@ -25,7 +26,7 @@ public class FakeProximitySensor extends ProximitySensor {
public FakeProximitySensor(ThresholdSensor primary, ThresholdSensor secondary, public FakeProximitySensor(ThresholdSensor primary, ThresholdSensor secondary,
DelayableExecutor delayableExecutor) { DelayableExecutor delayableExecutor) {
super(primary, secondary == null ? new FakeThresholdSensor() : secondary, super(primary, secondary == null ? new FakeThresholdSensor() : secondary,
delayableExecutor); delayableExecutor, new FakeExecution());
mAvailable = true; mAvailable = true;
} }

View File

@@ -27,6 +27,7 @@ import android.testing.TestableLooper;
import androidx.test.filters.SmallTest; import androidx.test.filters.SmallTest;
import com.android.systemui.SysuiTestCase; import com.android.systemui.SysuiTestCase;
import com.android.systemui.util.concurrency.FakeExecution;
import com.android.systemui.util.concurrency.FakeExecutor; import com.android.systemui.util.concurrency.FakeExecutor;
import com.android.systemui.util.time.FakeSystemClock; import com.android.systemui.util.time.FakeSystemClock;
@@ -54,7 +55,8 @@ public class ProximitySensorDualTest extends SysuiTestCase {
mThresholdSensorSecondary.setLoaded(true); mThresholdSensorSecondary.setLoaded(true);
mProximitySensor = new ProximitySensor( mProximitySensor = new ProximitySensor(
mThresholdSensorPrimary, mThresholdSensorSecondary, mFakeExecutor); mThresholdSensorPrimary, mThresholdSensorSecondary, mFakeExecutor,
new FakeExecution());
} }
@Test @Test
@@ -324,9 +326,10 @@ public class ProximitySensorDualTest extends SysuiTestCase {
TestableListener listener = new TestableListener(); TestableListener listener = new TestableListener();
// WE immediately register the secondary sensor.
mProximitySensor.register(listener); mProximitySensor.register(listener);
assertFalse(mThresholdSensorPrimary.isPaused()); assertFalse(mThresholdSensorPrimary.isPaused());
assertTrue(mThresholdSensorSecondary.isPaused()); assertFalse(mThresholdSensorSecondary.isPaused());
assertNull(listener.mLastEvent); assertNull(listener.mLastEvent);
assertEquals(0, listener.mCallCount); assertEquals(0, listener.mCallCount);

View File

@@ -27,6 +27,7 @@ import android.testing.TestableLooper;
import androidx.test.filters.SmallTest; import androidx.test.filters.SmallTest;
import com.android.systemui.SysuiTestCase; import com.android.systemui.SysuiTestCase;
import com.android.systemui.util.concurrency.FakeExecution;
import com.android.systemui.util.concurrency.FakeExecutor; import com.android.systemui.util.concurrency.FakeExecutor;
import com.android.systemui.util.time.FakeSystemClock; import com.android.systemui.util.time.FakeSystemClock;
@@ -54,7 +55,7 @@ public class ProximitySensorSingleTest extends SysuiTestCase {
mThresholdSensor.setLoaded(true); mThresholdSensor.setLoaded(true);
mProximitySensor = new ProximitySensor( mProximitySensor = new ProximitySensor(
mThresholdSensor, new FakeThresholdSensor(), mFakeExecutor); mThresholdSensor, new FakeThresholdSensor(), mFakeExecutor, new FakeExecution());
} }
@Test @Test

View File

@@ -24,7 +24,7 @@ import android.test.suitebuilder.annotation.SmallTest;
import android.testing.AndroidTestingRunner; import android.testing.AndroidTestingRunner;
import com.android.systemui.SysuiTestCase; import com.android.systemui.SysuiTestCase;
import com.android.systemui.util.Assert; import com.android.systemui.util.concurrency.FakeExecution;
import com.android.systemui.util.concurrency.FakeExecutor; import com.android.systemui.util.concurrency.FakeExecutor;
import com.android.systemui.util.concurrency.FakeThreadFactory; import com.android.systemui.util.concurrency.FakeThreadFactory;
import com.android.systemui.util.time.FakeSystemClock; import com.android.systemui.util.time.FakeSystemClock;
@@ -52,7 +52,7 @@ public class ThresholdSensorImplTest extends SysuiTestCase {
mFakeProximitySensor = mSensorManager.getFakeProximitySensor(); mFakeProximitySensor = mSensorManager.getFakeProximitySensor();
ThresholdSensorImpl.Builder thresholdSensorBuilder = new ThresholdSensorImpl.Builder( ThresholdSensorImpl.Builder thresholdSensorBuilder = new ThresholdSensorImpl.Builder(
null, mAsyncSensorManager); null, mAsyncSensorManager, new FakeExecution());
mThresholdSensor = (ThresholdSensorImpl) thresholdSensorBuilder mThresholdSensor = (ThresholdSensorImpl) thresholdSensorBuilder
.setSensor(mFakeProximitySensor.getSensor()) .setSensor(mFakeProximitySensor.getSensor())
.setThresholdValue(mFakeProximitySensor.getSensor().getMaximumRange()) .setThresholdValue(mFakeProximitySensor.getSensor().getMaximumRange())
@@ -61,7 +61,6 @@ public class ThresholdSensorImplTest extends SysuiTestCase {
@Test @Test
public void testSingleListener() { public void testSingleListener() {
Assert.setTestThread(Thread.currentThread());
TestableListener listener = new TestableListener(); TestableListener listener = new TestableListener();
assertFalse(mThresholdSensor.isRegistered()); assertFalse(mThresholdSensor.isRegistered());
@@ -83,7 +82,6 @@ public class ThresholdSensorImplTest extends SysuiTestCase {
@Test @Test
public void testMultiListener() { public void testMultiListener() {
Assert.setTestThread(Thread.currentThread());
TestableListener listenerA = new TestableListener(); TestableListener listenerA = new TestableListener();
TestableListener listenerB = new TestableListener(); TestableListener listenerB = new TestableListener();
@@ -117,7 +115,6 @@ public class ThresholdSensorImplTest extends SysuiTestCase {
@Test @Test
public void testDuplicateListener() { public void testDuplicateListener() {
Assert.setTestThread(Thread.currentThread());
TestableListener listenerA = new TestableListener(); TestableListener listenerA = new TestableListener();
assertFalse(mThresholdSensor.isRegistered()); assertFalse(mThresholdSensor.isRegistered());
@@ -142,7 +139,6 @@ public class ThresholdSensorImplTest extends SysuiTestCase {
} }
@Test @Test
public void testUnregister() { public void testUnregister() {
Assert.setTestThread(Thread.currentThread());
TestableListener listener = new TestableListener(); TestableListener listener = new TestableListener();
assertFalse(mThresholdSensor.isRegistered()); assertFalse(mThresholdSensor.isRegistered());
@@ -162,7 +158,6 @@ public class ThresholdSensorImplTest extends SysuiTestCase {
@Test @Test
public void testPauseAndResume() { public void testPauseAndResume() {
Assert.setTestThread(Thread.currentThread());
TestableListener listener = new TestableListener(); TestableListener listener = new TestableListener();
assertFalse(mThresholdSensor.isRegistered()); assertFalse(mThresholdSensor.isRegistered());
@@ -205,7 +200,6 @@ public class ThresholdSensorImplTest extends SysuiTestCase {
@Test @Test
public void testAlertListeners() { public void testAlertListeners() {
Assert.setTestThread(Thread.currentThread());
TestableListener listenerA = new TestableListener(); TestableListener listenerA = new TestableListener();
TestableListener listenerB = new TestableListener(); TestableListener listenerB = new TestableListener();
@@ -237,12 +231,11 @@ public class ThresholdSensorImplTest extends SysuiTestCase {
@Test @Test
public void testHysteresis() { public void testHysteresis() {
Assert.setTestThread(Thread.currentThread());
float lowValue = 10f; float lowValue = 10f;
float highValue = 100f; float highValue = 100f;
FakeSensorManager.FakeGenericSensor sensor = mSensorManager.getFakeLightSensor(); FakeSensorManager.FakeGenericSensor sensor = mSensorManager.getFakeLightSensor();
ThresholdSensorImpl.Builder thresholdSensorBuilder = new ThresholdSensorImpl.Builder( ThresholdSensorImpl.Builder thresholdSensorBuilder = new ThresholdSensorImpl.Builder(
null, mAsyncSensorManager); null, mAsyncSensorManager, new FakeExecution());
ThresholdSensorImpl thresholdSensor = (ThresholdSensorImpl) thresholdSensorBuilder ThresholdSensorImpl thresholdSensor = (ThresholdSensorImpl) thresholdSensorBuilder
.setSensor(sensor.getSensor()) .setSensor(sensor.getSensor())
.setThresholdValue(lowValue) .setThresholdValue(lowValue)
@@ -286,7 +279,6 @@ public class ThresholdSensorImplTest extends SysuiTestCase {
@Test @Test
public void testAlertAfterPause() { public void testAlertAfterPause() {
Assert.setTestThread(Thread.currentThread());
TestableListener listener = new TestableListener(); TestableListener listener = new TestableListener();
mThresholdSensor.register(listener); mThresholdSensor.register(listener);