From 7079b4d8d47673ba256c508ce82eb10e23a2a48d Mon Sep 17 00:00:00 2001 From: Beverly Date: Mon, 28 Jun 2021 16:43:54 -0400 Subject: [PATCH] SysUi ThresholdSensorImpl registers for wakeup variant Test: atest ThresholdSensorImplTest, manual Fixes: 189164569 Change-Id: Ib542f5839fd85ffd51ec337f6621cb0148eea4c2 --- .../systemui/util/sensors/SensorModule.java | 7 +- .../util/sensors/ThresholdSensorImpl.java | 25 ++++-- .../util/sensors/FakeSensorManager.java | 2 +- .../util/sensors/ThresholdSensorImplTest.java | 80 +++++++++++++++++++ 4 files changed, 102 insertions(+), 12 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/util/sensors/SensorModule.java b/packages/SystemUI/src/com/android/systemui/util/sensors/SensorModule.java index 7f37562446294..11e7df8bd85f8 100644 --- a/packages/SystemUI/src/com/android/systemui/util/sensors/SensorModule.java +++ b/packages/SystemUI/src/com/android/systemui/util/sensors/SensorModule.java @@ -36,12 +36,13 @@ public class SensorModule { try { return thresholdSensorBuilder .setSensorDelay(SensorManager.SENSOR_DELAY_NORMAL) - .setSensorResourceId(R.string.proximity_sensor_type) + .setSensorResourceId(R.string.proximity_sensor_type, true) .setThresholdResourceId(R.dimen.proximity_sensor_threshold) .setThresholdLatchResourceId(R.dimen.proximity_sensor_threshold_latch) .build(); } catch (IllegalStateException e) { - Sensor defaultSensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); + Sensor defaultSensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY, + true); return thresholdSensorBuilder .setSensor(defaultSensor) .setThresholdValue(defaultSensor != null ? defaultSensor.getMaximumRange() : 0) @@ -55,7 +56,7 @@ public class SensorModule { ThresholdSensorImpl.Builder thresholdSensorBuilder) { try { return thresholdSensorBuilder - .setSensorResourceId(R.string.proximity_sensor_secondary_type) + .setSensorResourceId(R.string.proximity_sensor_secondary_type, true) .setThresholdResourceId(R.dimen.proximity_sensor_secondary_threshold) .setThresholdLatchResourceId(R.dimen.proximity_sensor_secondary_threshold_latch) .build(); diff --git a/packages/SystemUI/src/com/android/systemui/util/sensors/ThresholdSensorImpl.java b/packages/SystemUI/src/com/android/systemui/util/sensors/ThresholdSensorImpl.java index 31c3072970664..d10cf9b180c31 100644 --- a/packages/SystemUI/src/com/android/systemui/util/sensors/ThresholdSensorImpl.java +++ b/packages/SystemUI/src/com/android/systemui/util/sensors/ThresholdSensorImpl.java @@ -230,14 +230,16 @@ class ThresholdSensorImpl implements ThresholdSensor { mExecution = execution; } - Builder setSensorDelay(int sensorDelay) { mSensorDelay = sensorDelay; return this; } - - Builder setSensorResourceId(int sensorResourceId) { - setSensorType(mResources.getString(sensorResourceId)); + /** + * If requiresWakeUp is false, the first sensor with sensorType (regardless of whether the + * sensor is a wakeup sensor or not) will be set. + */ + Builder setSensorResourceId(int sensorResourceId, boolean requireWakeUp) { + setSensorType(mResources.getString(sensorResourceId), requireWakeUp); return this; } @@ -259,8 +261,12 @@ class ThresholdSensorImpl implements ThresholdSensor { return this; } - Builder setSensorType(String sensorType) { - Sensor sensor = findSensorByType(sensorType); + /** + * If requiresWakeUp is false, the first sensor with sensorType (regardless of whether the + * sensor is a wakeup sensor or not) will be set. + */ + Builder setSensorType(String sensorType, boolean requireWakeUp) { + Sensor sensor = findSensorByType(sensorType, requireWakeUp); if (sensor != null) { setSensor(sensor); } @@ -310,7 +316,8 @@ class ThresholdSensorImpl implements ThresholdSensor { mThresholdValue, mThresholdLatchValue, mSensorDelay); } - private Sensor findSensorByType(String sensorType) { + @VisibleForTesting + Sensor findSensorByType(String sensorType, boolean requireWakeUp) { if (sensorType.isEmpty()) { return null; } @@ -320,7 +327,9 @@ class ThresholdSensorImpl implements ThresholdSensor { for (Sensor s : sensorList) { if (sensorType.equals(s.getStringType())) { sensor = s; - break; + if (!requireWakeUp || sensor.isWakeUpSensor()) { + break; + } } } diff --git a/packages/SystemUI/tests/src/com/android/systemui/util/sensors/FakeSensorManager.java b/packages/SystemUI/tests/src/com/android/systemui/util/sensors/FakeSensorManager.java index 27b225e3c4fac..6e73827fedfb9 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/util/sensors/FakeSensorManager.java +++ b/packages/SystemUI/tests/src/com/android/systemui/util/sensors/FakeSensorManager.java @@ -60,7 +60,7 @@ public class FakeSensorManager extends SensorManager { public FakeSensorManager(Context context) throws Exception { Sensor proxSensor = context.getSystemService(SensorManager.class) - .getDefaultSensor(Sensor.TYPE_PROXIMITY); + .getDefaultSensor(Sensor.TYPE_PROXIMITY, true); if (proxSensor == null) { // No prox? Let's create a fake one! proxSensor = diff --git a/packages/SystemUI/tests/src/com/android/systemui/util/sensors/ThresholdSensorImplTest.java b/packages/SystemUI/tests/src/com/android/systemui/util/sensors/ThresholdSensorImplTest.java index 12765679a7f36..125063a7adc44 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/util/sensors/ThresholdSensorImplTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/util/sensors/ThresholdSensorImplTest.java @@ -16,10 +16,15 @@ package com.android.systemui.util.sensors; +import static android.hardware.Sensor.TYPE_ALL; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import android.hardware.Sensor; import android.test.suitebuilder.annotation.SmallTest; import android.testing.AndroidTestingRunner; @@ -33,6 +38,8 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import java.util.List; + @SmallTest @RunWith(AndroidTestingRunner.class) public class ThresholdSensorImplTest extends SysuiTestCase { @@ -59,6 +66,79 @@ public class ThresholdSensorImplTest extends SysuiTestCase { .build(); } + @Test + public void testRegistersWakeUpProxSensor_givenWakeUpExistsAfterNonWakeup() { + // GIVEN sensor manager with two prox sensors (one non-wakeup, one wakeup) + final String sensorTypeProx = "prox"; + AsyncSensorManager mockSensorManager = mock(AsyncSensorManager.class); + + Sensor mockNonWakeupProx = mock(Sensor.class); + when(mockNonWakeupProx.isWakeUpSensor()).thenReturn(false); + when(mockNonWakeupProx.getStringType()).thenReturn(sensorTypeProx); + + Sensor mockWakeupProx = mock(Sensor.class); + when(mockWakeupProx.isWakeUpSensor()).thenReturn(true); + when(mockWakeupProx.getStringType()).thenReturn(sensorTypeProx); + + when(mockSensorManager.getSensorList(TYPE_ALL)).thenReturn( + List.of(mockNonWakeupProx, mockWakeupProx)); + + // WHEN we build a threshold sensor by type + ThresholdSensorImpl.Builder thresholdSensorBuilder = new ThresholdSensorImpl.Builder( + null, mockSensorManager, new FakeExecution()); + Sensor proxSensor = thresholdSensorBuilder.findSensorByType(sensorTypeProx, true); + + // THEN the prox sensor used is the wakeup sensor + assertEquals(mockWakeupProx, proxSensor); + } + + @Test + public void testRegistersWakeUpProxSensor_givenNonWakeUpExistsAfterWakeup() { + // GIVEN sensor manager with two prox sensors (one wakeup, one non-wakeup) + final String sensorTypeProx = "prox"; + AsyncSensorManager mockSensorManager = mock(AsyncSensorManager.class); + + Sensor mockNonWakeupProx = mock(Sensor.class); + when(mockNonWakeupProx.isWakeUpSensor()).thenReturn(false); + when(mockNonWakeupProx.getStringType()).thenReturn(sensorTypeProx); + + Sensor mockWakeupProx = mock(Sensor.class); + when(mockWakeupProx.isWakeUpSensor()).thenReturn(true); + when(mockWakeupProx.getStringType()).thenReturn(sensorTypeProx); + + when(mockSensorManager.getSensorList(TYPE_ALL)).thenReturn( + List.of(mockWakeupProx, mockNonWakeupProx)); + + // WHEN we build a threshold sensor by type + ThresholdSensorImpl.Builder thresholdSensorBuilder = new ThresholdSensorImpl.Builder( + null, mockSensorManager, new FakeExecution()); + Sensor proxSensor = thresholdSensorBuilder.findSensorByType(sensorTypeProx, true); + + // THEN the prox sensor used is the wakeup sensor + assertEquals(mockWakeupProx, proxSensor); + } + + @Test + public void testRegistersNonWakeUpProxSensor_givenNonWakeUpOnly() { + // GIVEN sensor manager with one non-wakeup prox sensor + final String sensorTypeProx = "prox"; + AsyncSensorManager mockSensorManager = mock(AsyncSensorManager.class); + + Sensor mockNonWakeupProx = mock(Sensor.class); + when(mockNonWakeupProx.isWakeUpSensor()).thenReturn(false); + when(mockNonWakeupProx.getStringType()).thenReturn(sensorTypeProx); + + when(mockSensorManager.getSensorList(TYPE_ALL)).thenReturn(List.of(mockNonWakeupProx)); + + // WHEN we build a threshold sensor by type + ThresholdSensorImpl.Builder thresholdSensorBuilder = new ThresholdSensorImpl.Builder( + null, mockSensorManager, new FakeExecution()); + Sensor proxSensor = thresholdSensorBuilder.findSensorByType(sensorTypeProx, true); + + // THEN the prox sensor used is the one available (non-wakeup) + assertEquals(mockNonWakeupProx, proxSensor); + } + @Test public void testSingleListener() { TestableListener listener = new TestableListener();