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

Commit 2449f0e1 authored by Darryl Johnson's avatar Darryl Johnson Committed by Android (Google) Code Review
Browse files

Merge "Ensure DeviceStateProviderImpl considers first state before others."

parents 26832720 12b4ecdb
Loading
Loading
Loading
Loading
+12 −3
Original line number Diff line number Diff line
@@ -63,8 +63,17 @@ import javax.xml.datatype.DatatypeConfigurationException;
/**
 * Implementation of {@link DeviceStateProvider} that reads the set of supported device states
 * from a configuration file provided at either /vendor/etc/devicestate or
 * /data/system/devicestate/. By default, the provider supports {@link #DEFAULT_DEVICE_STATE} when
 * no configuration is provided.
 * /data/system/devicestate/.
 * <p>
 * When a device state configuration file is present this provider will consider the provided
 * {@link Conditions} block for each declared state, halting and returning when the first set of
 * conditions for a device state match the current system state. If there are multiple states whose
 * conditions match the current system state the matching state with the smallest integer identifier
 * will be returned. When no declared state matches the current system state, the device state with
 * the smallest integer identifier will be returned.
 * <p>
 * By default, the provider reports {@link #DEFAULT_DEVICE_STATE} when no configuration file is
 * provided.
 */
public final class DeviceStateProviderImpl implements DeviceStateProvider,
        InputManagerInternal.LidSwitchCallback, SensorEventListener {
@@ -273,7 +282,7 @@ public final class DeviceStateProviderImpl implements DeviceStateProvider,
            }

            int newState = mOrderedStates[0];
            for (int i = 1; i < mOrderedStates.length; i++) {
            for (int i = 0; i < mOrderedStates.length; i++) {
                int state = mOrderedStates[i];
                if (mStateConditions.get(state).getAsBoolean()) {
                    newState = state;
+26 −0
Original line number Diff line number Diff line
@@ -127,6 +127,32 @@ public final class DeviceStateProviderImplTest {
        assertEquals(DEFAULT_DEVICE_STATE, mIntegerCaptor.getValue().intValue());
    }

    @Test
    public void create_multipleMatchingStatesDefaultsToLowestIdentifier() {
        String configString = "<device-state-config>\n"
                + "    <device-state>\n"
                + "        <identifier>1</identifier>\n"
                + "        <conditions/>\n"
                + "    </device-state>\n"
                + "    <device-state>\n"
                + "        <identifier>2</identifier>\n"
                + "        <conditions/>\n"
                + "    </device-state>\n"
                + "</device-state-config>\n";
        DeviceStateProviderImpl.ReadableConfig config = new TestReadableConfig(configString);
        DeviceStateProviderImpl provider = DeviceStateProviderImpl.createFromConfig(mContext,
                config);

        DeviceStateProvider.Listener listener = mock(DeviceStateProvider.Listener.class);
        provider.setListener(listener);

        verify(listener).onSupportedDeviceStatesChanged(mIntArrayCaptor.capture());
        assertArrayEquals(new int[] { 1, 2 }, mIntArrayCaptor.getValue());

        verify(listener).onStateChanged(mIntegerCaptor.capture());
        assertEquals(1, mIntegerCaptor.getValue().intValue());
    }

    @Test
    public void create_lidSwitch() {
        String configString = "<device-state-config>\n"