OSDN Git Service

AOD: Pass through pulse reason to DozeUi
authorAdrian Roos <roosa@google.com>
Fri, 28 Apr 2017 22:42:58 +0000 (15:42 -0700)
committerAdrian Roos <roosa@google.com>
Fri, 28 Apr 2017 22:42:58 +0000 (15:42 -0700)
Fixes an issue where the Ambient Display transition
is too slow in response to double tap / lift, because
we are applying the animation for the notification case.

Bug: 34716110
Test: runtest systemuig
Change-Id: I84f2c59205d157d89baac1688dd6cd97a170a489

packages/SystemUI/src/com/android/systemui/doze/DozeLog.java
packages/SystemUI/src/com/android/systemui/doze/DozeMachine.java
packages/SystemUI/src/com/android/systemui/doze/DozeTriggers.java
packages/SystemUI/src/com/android/systemui/doze/DozeUi.java
packages/SystemUI/tests/src/com/android/systemui/doze/DozeMachineTest.java
packages/SystemUI/tests/src/com/android/systemui/doze/DozeTriggersTest.java

index 7139d59..af02e5b 100644 (file)
@@ -37,6 +37,7 @@ public class DozeLog {
 
     private static final int PULSE_REASONS = 5;
 
+    public static final int PULSE_REASON_NONE = -1;
     public static final int PULSE_REASON_INTENT = 0;
     public static final int PULSE_REASON_NOTIFICATION = 1;
     public static final int PULSE_REASON_SENSOR_SIGMOTION = 2;
index 38b32e9..44bb33a 100644 (file)
@@ -106,6 +106,7 @@ public class DozeMachine {
 
     private final ArrayList<State> mQueuedRequests = new ArrayList<>();
     private State mState = State.UNINITIALIZED;
+    private int mPulseReason;
     private boolean mWakeLockHeldForCurrentState = false;
 
     public DozeMachine(Service service, AmbientDisplayConfiguration config,
@@ -133,6 +134,20 @@ public class DozeMachine {
      */
     @MainThread
     public void requestState(State requestedState) {
+        Preconditions.checkArgument(requestedState != State.DOZE_REQUEST_PULSE);
+        requestState(requestedState, DozeLog.PULSE_REASON_NONE);
+    }
+
+    @MainThread
+    public void requestPulse(int pulseReason) {
+        // Must not be called during a transition. There's no inherent problem with that,
+        // but there's currently no need to execute from a transition and it simplifies the
+        // code to not have to worry about keeping the pulseReason in mQueuedRequests.
+        Preconditions.checkState(!isExecutingTransition());
+        requestState(State.DOZE_REQUEST_PULSE, pulseReason);
+    }
+
+    private void requestState(State requestedState, int pulseReason) {
         Assert.isMainThread();
         if (DEBUG) {
             Log.i(TAG, "request: current=" + mState + " req=" + requestedState,
@@ -146,7 +161,7 @@ public class DozeMachine {
             for (int i = 0; i < mQueuedRequests.size(); i++) {
                 // Transitions in Parts can call back into requestState, which will
                 // cause mQueuedRequests to grow.
-                transitionTo(mQueuedRequests.get(i));
+                transitionTo(mQueuedRequests.get(i), pulseReason);
             }
             mQueuedRequests.clear();
             mWakeLock.release();
@@ -165,6 +180,20 @@ public class DozeMachine {
         return mState;
     }
 
+    /**
+     * @return the current pulse reason.
+     *
+     * This is only valid if the machine is currently in one of the pulse states.
+     */
+    @MainThread
+    public int getPulseReason() {
+        Assert.isMainThread();
+        Preconditions.checkState(mState == State.DOZE_REQUEST_PULSE
+                || mState == State.DOZE_PULSING
+                || mState == State.DOZE_PULSE_DONE, "must be in pulsing state, but is " + mState);
+        return mPulseReason;
+    }
+
     /** Requests the PowerManager to wake up now. */
     public void wakeUp() {
         mDozeService.requestWakeUp();
@@ -174,7 +203,7 @@ public class DozeMachine {
         return !mQueuedRequests.isEmpty();
     }
 
-    private void transitionTo(State requestedState) {
+    private void transitionTo(State requestedState, int pulseReason) {
         State newState = transitionPolicy(requestedState);
 
         if (DEBUG) {
@@ -190,6 +219,7 @@ public class DozeMachine {
         State oldState = mState;
         mState = newState;
 
+        updatePulseReason(newState, oldState, pulseReason);
         performTransitionOnComponents(oldState, newState);
         updateScreenState(newState);
         updateWakeLockState(newState);
@@ -197,6 +227,14 @@ public class DozeMachine {
         resolveIntermediateState(newState);
     }
 
+    private void updatePulseReason(State newState, State oldState, int pulseReason) {
+        if (newState == State.DOZE_REQUEST_PULSE) {
+            mPulseReason = pulseReason;
+        } else if (oldState == State.DOZE_PULSE_DONE) {
+            mPulseReason = DozeLog.PULSE_REASON_NONE;
+        }
+    }
+
     private void performTransitionOnComponents(State oldState, State newState) {
         for (Part p : mParts) {
             p.transitionTo(oldState, newState);
@@ -280,7 +318,8 @@ public class DozeMachine {
             case INITIALIZED:
             case DOZE_PULSE_DONE:
                 transitionTo(mConfig.alwaysOnEnabled(UserHandle.USER_CURRENT)
-                        ? DozeMachine.State.DOZE_AOD : DozeMachine.State.DOZE);
+                        ? DozeMachine.State.DOZE_AOD : DozeMachine.State.DOZE,
+                        DozeLog.PULSE_REASON_NONE);
                 break;
             default:
                 break;
index ea55c5f..47eeb57 100644 (file)
@@ -231,7 +231,7 @@ public class DozeTriggers implements DozeMachine.Part {
                     mDozeHost.isPulsingBlocked());
             return;
         }
-        mMachine.requestState(DozeMachine.State.DOZE_REQUEST_PULSE);
+        mMachine.requestPulse(reason);
     }
 
     @Override
index 03076cc..64a152e 100644 (file)
@@ -83,7 +83,7 @@ public class DozeUi implements DozeMachine.Part {
                 unscheduleTimeTick();
                 break;
             case DOZE_REQUEST_PULSE:
-                pulseWhileDozing(DozeLog.PULSE_REASON_NOTIFICATION /* TODO */);
+                pulseWhileDozing(mMachine.getPulseReason());
                 break;
             case DOZE_PULSE_DONE:
                 mHost.abortPulsing();
index cdbde5e..d203602 100644 (file)
@@ -106,7 +106,7 @@ public class DozeMachineTest {
     public void testPulseDone_goesToDoze() {
         when(mConfigMock.alwaysOnEnabled(anyInt())).thenReturn(false);
         mMachine.requestState(INITIALIZED);
-        mMachine.requestState(DOZE_REQUEST_PULSE);
+        mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
         mMachine.requestState(DOZE_PULSING);
 
         mMachine.requestState(DOZE_PULSE_DONE);
@@ -119,7 +119,7 @@ public class DozeMachineTest {
     public void testPulseDone_goesToAoD() {
         when(mConfigMock.alwaysOnEnabled(anyInt())).thenReturn(true);
         mMachine.requestState(INITIALIZED);
-        mMachine.requestState(DOZE_REQUEST_PULSE);
+        mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
         mMachine.requestState(DOZE_PULSING);
 
         mMachine.requestState(DOZE_PULSE_DONE);
@@ -163,7 +163,7 @@ public class DozeMachineTest {
     public void testWakeLock_heldInPulseStates() {
         mMachine.requestState(INITIALIZED);
 
-        mMachine.requestState(DOZE_REQUEST_PULSE);
+        mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
         assertTrue(mWakeLockFake.isHeld());
 
         mMachine.requestState(DOZE_PULSING);
@@ -186,7 +186,7 @@ public class DozeMachineTest {
         mMachine.requestState(INITIALIZED);
 
         mMachine.requestState(DOZE);
-        mMachine.requestState(DOZE_REQUEST_PULSE);
+        mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
         mMachine.requestState(DOZE_PULSING);
         mMachine.requestState(DOZE_PULSE_DONE);
 
@@ -198,9 +198,9 @@ public class DozeMachineTest {
         mMachine.requestState(INITIALIZED);
 
         mMachine.requestState(DOZE);
-        mMachine.requestState(DOZE_REQUEST_PULSE);
+        mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
         mMachine.requestState(DOZE_PULSING);
-        mMachine.requestState(DOZE_REQUEST_PULSE);
+        mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
         mMachine.requestState(DOZE_PULSE_DONE);
     }
 
@@ -209,7 +209,7 @@ public class DozeMachineTest {
         mMachine.requestState(INITIALIZED);
 
         mMachine.requestState(DOZE);
-        mMachine.requestState(DOZE_REQUEST_PULSE);
+        mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
         mMachine.requestState(DOZE_PULSE_DONE);
     }
 
@@ -235,7 +235,7 @@ public class DozeMachineTest {
     public void testScreen_onInPulse() {
         mMachine.requestState(INITIALIZED);
 
-        mMachine.requestState(DOZE_REQUEST_PULSE);
+        mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
         mMachine.requestState(DOZE_PULSING);
 
         assertEquals(Display.STATE_DOZE, mServiceFake.screenState);
@@ -246,7 +246,7 @@ public class DozeMachineTest {
         mMachine.requestState(INITIALIZED);
 
         mMachine.requestState(DOZE);
-        mMachine.requestState(DOZE_REQUEST_PULSE);
+        mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
 
         assertEquals(Display.STATE_OFF, mServiceFake.screenState);
     }
@@ -256,7 +256,7 @@ public class DozeMachineTest {
         mMachine.requestState(INITIALIZED);
 
         mMachine.requestState(DOZE_AOD);
-        mMachine.requestState(DOZE_REQUEST_PULSE);
+        mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
 
         assertEquals(Display.STATE_DOZE, mServiceFake.screenState);
     }
@@ -270,12 +270,43 @@ public class DozeMachineTest {
             return null;
         }).when(mPartMock).transitionTo(any(), eq(DOZE_REQUEST_PULSE));
 
-        mMachine.requestState(DOZE_REQUEST_PULSE);
+        mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
 
         assertEquals(DOZE_PULSING, mMachine.getState());
     }
 
     @Test
+    public void testPulseReason_getMatchesRequest() {
+        mMachine.requestState(INITIALIZED);
+        mMachine.requestState(DOZE);
+        mMachine.requestPulse(DozeLog.PULSE_REASON_SENSOR_DOUBLE_TAP);
+
+        assertEquals(DozeLog.PULSE_REASON_SENSOR_DOUBLE_TAP, mMachine.getPulseReason());
+    }
+
+    @Test
+    public void testPulseReason_getFromTransition() {
+        mMachine.requestState(INITIALIZED);
+        mMachine.requestState(DOZE);
+        doAnswer(inv -> {
+            DozeMachine.State newState = inv.getArgument(1);
+            if (newState == DOZE_REQUEST_PULSE
+                    || newState == DOZE_PULSING
+                    || newState == DOZE_PULSE_DONE) {
+                assertEquals(DozeLog.PULSE_REASON_NOTIFICATION, mMachine.getPulseReason());
+            } else {
+                assertTrue("unexpected state " + newState,
+                        newState == DOZE || newState == DOZE_AOD);
+            }
+            return null;
+        }).when(mPartMock).transitionTo(any(), any());
+
+        mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
+        mMachine.requestState(DOZE_PULSING);
+        mMachine.requestState(DOZE_PULSE_DONE);
+    }
+
+    @Test
     public void testWakeUp_wakesUp() {
         mMachine.wakeUp();
 
index 12e75a1..d57f813 100644 (file)
@@ -22,7 +22,6 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
-import static org.mockito.Mockito.withSettings;
 
 import android.app.Instrumentation;
 import android.content.Context;
@@ -39,8 +38,6 @@ import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Ignore;
 import org.junit.Test;
-import org.mockito.Answers;
-import org.mockito.MockSettings;
 
 public class DozeTriggersTest {
     private Context mContext;
@@ -104,7 +101,7 @@ public class DozeTriggersTest {
             mSensors.PROXIMITY.sendProximityResult(true); /* Far */
         });
 
-        verify(mMachine).requestState(DozeMachine.State.DOZE_REQUEST_PULSE);
+        verify(mMachine).requestPulse(anyInt());
     }
 
 }
\ No newline at end of file