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

Commit 12b4ecdb authored by Darryl L Johnson's avatar Darryl L Johnson
Browse files

Ensure DeviceStateProviderImpl considers first state before others.

DeviceStateProviderImpl#notifyDeviceStateChangedIfNeeded() would skip
the first state and consider other states, before falling back to the
first state if no others match. This fails to handle the case where a
state is declared in the config file without any conditions to allow the
state to be requested externally (for ex, TENT mode).

Fixes: 178387437
Test: atest DeviceStateProviderImplTest
Change-Id: I7240c412b98978028c3d86257723cf00dba28ea0
parent 9e5ec68e
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"