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

Commit ecbffa5b authored by Jerry Chang's avatar Jerry Chang
Browse files

Prevent requesting invalid state after docked

Prevent requesting DOZE_AOD_DOCKED while doze pulsing and requesting
DOZE_PULSE_DOWN while DOZE_AOD_DOCKED.

Fix: 153168608
Test: atest DozeMachineTest DozeDockHandlerTest
Test: manual test that docked on paired dock while pulsing won't crash
Change-Id: Ieec969c58ac85f90a985ec714643feea51118787
parent b7b84697
Loading
Loading
Loading
Loading
+13 −3
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ public class DozeDockHandler implements DozeMachine.Part {

    private int mDockState = DockManager.STATE_NONE;

    public DozeDockHandler(AmbientDisplayConfiguration config, DozeMachine machine,
    DozeDockHandler(AmbientDisplayConfiguration config, DozeMachine machine,
            DockManager dockManager) {
        mMachine = machine;
        mConfig = config;
@@ -74,8 +74,13 @@ public class DozeDockHandler implements DozeMachine.Part {
        @Override
        public void onEvent(int dockState) {
            if (DEBUG) Log.d(TAG, "dock event = " + dockState);
            final DozeMachine.State nextState;

            mDockState = dockState;
            if (isPulsing()) {
                return;
            }

            DozeMachine.State nextState;
            switch (mDockState) {
                case DockManager.STATE_DOCKED:
                    nextState = State.DOZE_AOD_DOCKED;
@@ -90,10 +95,15 @@ public class DozeDockHandler implements DozeMachine.Part {
                default:
                    return;
            }

            mMachine.requestState(nextState);
        }

        private boolean isPulsing() {
            DozeMachine.State state = mMachine.getState();
            return state == State.DOZE_REQUEST_PULSE || state == State.DOZE_PULSING
                    || state == State.DOZE_PULSING_BRIGHT;
        }

        void register() {
            if (mRegistered) {
                return;
+2 −2
Original line number Diff line number Diff line
@@ -339,8 +339,8 @@ public class DozeMachine {
            return State.DOZE;
        }
        if ((mState == State.DOZE_AOD_PAUSED || mState == State.DOZE_AOD_PAUSING
                || mState == State.DOZE_AOD || mState == State.DOZE)
                && requestedState == State.DOZE_PULSE_DONE) {
                || mState == State.DOZE_AOD || mState == State.DOZE
                || mState == State.DOZE_AOD_DOCKED) && requestedState == State.DOZE_PULSE_DONE) {
            Log.i(TAG, "Dropping pulse done because current state is already done: " + mState);
            return mState;
        }
+30 −0
Original line number Diff line number Diff line
@@ -20,8 +20,10 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.hardware.display.AmbientDisplayConfiguration;
import android.testing.AndroidTestingRunner;
@@ -56,6 +58,7 @@ public class DozeDockHandlerTest extends SysuiTestCase {
        mDockManagerFake = spy(new DockManagerFake());
        mDockHandler = new DozeDockHandler(mConfig, mMachine, mDockManagerFake);

        when(mMachine.getState()).thenReturn(State.DOZE_AOD);
        doReturn(true).when(mConfig).alwaysOnEnabled(anyInt());
        mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
    }
@@ -101,4 +104,31 @@ public class DozeDockHandlerTest extends SysuiTestCase {

        verify(mMachine).requestState(eq(State.DOZE));
    }

    @Test
    public void onEvent_dockedWhilePulsing_wontRequestStateChange() {
        when(mMachine.getState()).thenReturn(State.DOZE_PULSING);

        mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED);

        verify(mMachine, never()).requestState(any(State.class));
    }

    @Test
    public void onEvent_noneWhilePulsing_wontRequestStateChange() {
        when(mMachine.getState()).thenReturn(State.DOZE_PULSING);

        mDockManagerFake.setDockEvent(DockManager.STATE_NONE);

        verify(mMachine, never()).requestState(any(State.class));
    }

    @Test
    public void onEvent_hideWhilePulsing_wontRequestStateChange() {
        when(mMachine.getState()).thenReturn(State.DOZE_PULSING);

        mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED_HIDE);

        verify(mMachine, never()).requestState(any(State.class));
    }
}
+11 −0
Original line number Diff line number Diff line
@@ -253,6 +253,17 @@ public class DozeMachineTest extends SysuiTestCase {
        assertEquals(DOZE_AOD_DOCKED, mMachine.getState());
    }

    @Test
    public void testPulseDone_whileDockedAoD_staysDockedAod() {
        when(mDockManager.isDocked()).thenReturn(true);
        mMachine.requestState(INITIALIZED);
        mMachine.requestState(DOZE_AOD_DOCKED);

        mMachine.requestState(DOZE_PULSE_DONE);

        verify(mPartMock, never()).transitionTo(DOZE_AOD_DOCKED, DOZE_PULSE_DONE);
    }

    @Test
    public void testPulseDone_dozeSuppressed_afterDocked_goesToDoze() {
        when(mHost.isDozeSuppressed()).thenReturn(true);