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

Commit 7079b4d8 authored by Beverly's avatar Beverly
Browse files

SysUi ThresholdSensorImpl registers for wakeup variant

Test: atest ThresholdSensorImplTest, manual
Fixes: 189164569
Change-Id: Ib542f5839fd85ffd51ec337f6621cb0148eea4c2
parent 604b00db
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -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();
+17 −8
Original line number Diff line number Diff line
@@ -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,9 +327,11 @@ class ThresholdSensorImpl implements ThresholdSensor {
            for (Sensor s : sensorList) {
                if (sensorType.equals(s.getStringType())) {
                    sensor = s;
                    if (!requireWakeUp || sensor.isWakeUpSensor()) {
                        break;
                    }
                }
            }

            return sensor;
        }
+1 −1
Original line number Diff line number Diff line
@@ -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 =
+80 −0
Original line number Diff line number Diff line
@@ -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();