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

Commit ad0b38bd authored by Darryl L Johnson's avatar Darryl L Johnson
Browse files

Convert device state sensor type from int to String.

This converts the sensor type within the device state config file from
a raw int type to its fully qualified Sensor string type. For ex, a hinge
sensor that was previously identified with "36" would now be identified
with "android.sensor.hinge_angle".

Bug: 159401801
Test: atest DeviceStateProviderImplTest
Change-Id: I32ff9db11f68d602f7084d5308f0435bf418e522
parent fcdc5c4c
Loading
Loading
Loading
Loading
+23 −12
Original line number Diff line number Diff line
@@ -158,7 +158,6 @@ public final class DeviceStateProviderImpl implements DeviceStateProvider,
        // switch there is no need to register for a callback.
        boolean shouldListenToLidSwitch = false;

        final SensorManager sensorManager = mContext.getSystemService(SensorManager.class);
        // The set of Sensor(s) that this instance should register to receive SensorEvent(s) from.
        final ArraySet<Sensor> sensorsToListenTo = new ArraySet<>();

@@ -182,19 +181,10 @@ public final class DeviceStateProviderImpl implements DeviceStateProvider,
            List<SensorCondition> sensorConditions = conditions.getSensor();
            for (int j = 0; j < sensorConditions.size(); j++) {
                SensorCondition sensorCondition = sensorConditions.get(j);
                final int expectedSensorType = sensorCondition.getType().intValue();
                final String expectedSensorType = sensorCondition.getType();
                final String expectedSensorName = sensorCondition.getName();

                List<Sensor> sensors = sensorManager.getSensorList(expectedSensorType);
                Sensor foundSensor = null;
                for (int sensorIndex = 0; sensorIndex < sensors.size(); sensorIndex++) {
                    Sensor sensor = sensors.get(sensorIndex);
                    if (sensor.getName().equals(expectedSensorName)) {
                        foundSensor = sensor;
                        break;
                    }
                }

                final Sensor foundSensor = findSensor(expectedSensorType, expectedSensorName);
                if (foundSensor == null) {
                    throw new IllegalStateException("Failed to find Sensor with type: "
                            + expectedSensorType + " and name: " + expectedSensorName);
@@ -221,12 +211,33 @@ public final class DeviceStateProviderImpl implements DeviceStateProvider,
            inputManager.registerLidSwitchCallback(this);
        }

        final SensorManager sensorManager = mContext.getSystemService(SensorManager.class);
        for (int i = 0; i < sensorsToListenTo.size(); i++) {
            Sensor sensor = sensorsToListenTo.valueAt(i);
            sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_FASTEST);
        }
    }

    @Nullable
    private Sensor findSensor(String type, String name) {
        final SensorManager sensorManager = mContext.getSystemService(SensorManager.class);
        final List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
        for (int sensorIndex = 0; sensorIndex < sensors.size(); sensorIndex++) {
            final Sensor sensor = sensors.get(sensorIndex);
            final String sensorType = sensor.getStringType();
            final String sensorName = sensor.getName();

            if (sensorType == null || sensorName == null) {
                continue;
            }

            if (sensorType.equals(type) && sensorName.equals(name)) {
                return sensor;
            }
        }
        return null;
    }

    @Override
    public void setListener(Listener listener) {
        synchronized (mLock) {
+1 −1
Original line number Diff line number Diff line
@@ -57,8 +57,8 @@

    <xs:complexType name="sensorCondition">
        <xs:sequence>
            <xs:element name="type" type="xs:string" />
            <xs:element name="name" type="xs:string" />
            <xs:element name="type" type="xs:positiveInteger" />
            <xs:element name="value" type="numericRange" maxOccurs="unbounded" />
        </xs:sequence>
    </xs:complexType>
+2 −2
Original line number Diff line number Diff line
@@ -44,10 +44,10 @@ package com.android.server.policy.devicestate.config {
  public class SensorCondition {
    ctor public SensorCondition();
    method public String getName();
    method public java.math.BigInteger getType();
    method public String getType();
    method public java.util.List<com.android.server.policy.devicestate.config.NumericRange> getValue();
    method public void setName(String);
    method public void setType(java.math.BigInteger);
    method public void setType(String);
  }

  public class XmlParser {
+8 −7
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import static com.android.server.policy.DeviceStateProviderImpl.DEFAULT_DEVICE_S

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@@ -170,16 +171,16 @@ public final class DeviceStateProviderImplTest {

    @Test
    public void create_sensor() throws Exception {
        Sensor sensor = newSensor("sensor", Sensor.TYPE_HINGE_ANGLE);
        when(mSensorManager.getSensorList(eq(sensor.getType()))).thenReturn(List.of(sensor));
        Sensor sensor = newSensor("sensor", Sensor.STRING_TYPE_HINGE_ANGLE);
        when(mSensorManager.getSensorList(anyInt())).thenReturn(List.of(sensor));

        String configString = "<device-state-config>\n"
                + "    <device-state>\n"
                + "        <identifier>1</identifier>\n"
                + "        <conditions>\n"
                + "            <sensor>\n"
                + "                <type>" + sensor.getStringType() + "</type>\n"
                + "                <name>" + sensor.getName() + "</name>\n"
                + "                <type>" + sensor.getType() + "</type>\n"
                + "                <value>\n"
                + "                    <max>90</max>\n"
                + "                </value>\n"
@@ -190,8 +191,8 @@ public final class DeviceStateProviderImplTest {
                + "        <identifier>2</identifier>\n"
                + "        <conditions>\n"
                + "            <sensor>\n"
                + "                <type>" + sensor.getStringType() + "</type>\n"
                + "                <name>" + sensor.getName() + "</name>\n"
                + "                <type>" + sensor.getType() + "</type>\n"
                + "                <value>\n"
                + "                    <min-inclusive>90</min-inclusive>\n"
                + "                    <max>180</max>\n"
@@ -203,8 +204,8 @@ public final class DeviceStateProviderImplTest {
                + "        <identifier>3</identifier>\n"
                + "        <conditions>\n"
                + "            <sensor>\n"
                + "                <type>" + sensor.getStringType() + "</type>\n"
                + "                <name>" + sensor.getName() + "</name>\n"
                + "                <type>" + sensor.getType() + "</type>\n"
                + "                <value>\n"
                + "                    <min-inclusive>180</min-inclusive>\n"
                + "                </value>\n"
@@ -262,13 +263,13 @@ public final class DeviceStateProviderImplTest {
        assertEquals(1, mIntegerCaptor.getValue().intValue());
    }

    private static Sensor newSensor(String name, int type) throws Exception {
    private static Sensor newSensor(String name, String type) throws Exception {
        Constructor<Sensor> constructor = Sensor.class.getDeclaredConstructor();
        constructor.setAccessible(true);

        Sensor sensor = constructor.newInstance();
        FieldSetter.setField(sensor, Sensor.class.getDeclaredField("mName"), name);
        FieldSetter.setField(sensor, Sensor.class.getDeclaredField("mType"), type);
        FieldSetter.setField(sensor, Sensor.class.getDeclaredField("mStringType"), type);
        return sensor;
    }