diff options
3 files changed, 44 insertions, 7 deletions
diff --git a/core/java/android/app/StatusBarManager.java b/core/java/android/app/StatusBarManager.java index 719b5b64bbff..13fc2e6977ea 100644 --- a/core/java/android/app/StatusBarManager.java +++ b/core/java/android/app/StatusBarManager.java @@ -234,7 +234,10 @@ public class StatusBarManager { /** * Session flag for {@link #registerSessionListener} indicating the listener - * is interested in sessions on the keygaurd + * is interested in sessions on the keygaurd. + * Keyguard Session Boundaries: + * START_SESSION: device starts going to sleep OR the keyguard is newly shown + * END_SESSION: device starts going to sleep OR keyguard is no longer showing * @hide */ public static final int SESSION_KEYGUARD = 1 << 0; diff --git a/packages/SystemUI/src/com/android/systemui/log/SessionTracker.java b/packages/SystemUI/src/com/android/systemui/log/SessionTracker.java index c7e4c5e93090..b98a92ff8600 100644 --- a/packages/SystemUI/src/com/android/systemui/log/SessionTracker.java +++ b/packages/SystemUI/src/com/android/systemui/log/SessionTracker.java @@ -49,7 +49,9 @@ import javax.inject.Inject; @SysUISingleton public class SessionTracker implements CoreStartable { private static final String TAG = "SessionTracker"; - private static final boolean DEBUG = false; + + // To enable logs: `adb shell setprop log.tag.SessionTracker DEBUG` & restart sysui + private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); // At most 20 bits: ~1m possibilities, ~0.5% probability of collision in 100 values private final InstanceIdSequence mInstanceIdGenerator = new InstanceIdSequence(1 << 20); @@ -81,8 +83,8 @@ public class SessionTracker implements CoreStartable { mKeyguardUpdateMonitor.registerCallback(mKeyguardUpdateMonitorCallback); mKeyguardStateController.addCallback(mKeyguardStateCallback); - mKeyguardSessionStarted = mKeyguardStateController.isShowing(); - if (mKeyguardSessionStarted) { + if (mKeyguardStateController.isShowing()) { + mKeyguardSessionStarted = true; startSession(SESSION_KEYGUARD); } } @@ -136,12 +138,11 @@ public class SessionTracker implements CoreStartable { new KeyguardUpdateMonitorCallback() { @Override public void onStartedGoingToSleep(int why) { - // we need to register to the KeyguardUpdateMonitor lifecycle b/c it gets called - // before the WakefulnessLifecycle if (mKeyguardSessionStarted) { - return; + endSession(SESSION_KEYGUARD); } + // Start a new session whenever the device goes to sleep mKeyguardSessionStarted = true; startSession(SESSION_KEYGUARD); } @@ -154,6 +155,9 @@ public class SessionTracker implements CoreStartable { boolean wasSessionStarted = mKeyguardSessionStarted; boolean keyguardShowing = mKeyguardStateController.isShowing(); if (keyguardShowing && !wasSessionStarted) { + // the keyguard can start showing without the device going to sleep (ie: lockdown + // from the power button), so we start a new keyguard session when the keyguard is + // newly shown in addition to when the device starts going to sleep mKeyguardSessionStarted = true; startSession(SESSION_KEYGUARD); } else if (!keyguardShowing && wasSessionStarted) { diff --git a/packages/SystemUI/tests/src/com/android/systemui/log/SessionTrackerTest.java b/packages/SystemUI/tests/src/com/android/systemui/log/SessionTrackerTest.java index dc5522efe406..aa54a1cdf579 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/log/SessionTrackerTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/log/SessionTrackerTest.java @@ -23,8 +23,10 @@ import static android.app.StatusBarManager.SESSION_KEYGUARD; import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertNull; +import static org.junit.Assert.assertNotEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -171,6 +173,34 @@ public class SessionTrackerTest extends SysuiTestCase { } @Test + public void testKeyguardSessionOnDeviceStartsSleepingTwiceInARow_startsNewKeyguardSession() + throws RemoteException { + // GIVEN session tracker started w/o any sessions + mSessionTracker.start(); + captureKeyguardUpdateMonitorCallback(); + + // WHEN device starts going to sleep + mKeyguardUpdateMonitorCallback.onStartedGoingToSleep(0); + + // THEN the keyguard session has a session id + final InstanceId firstSessionId = mSessionTracker.getSessionId(SESSION_KEYGUARD); + assertNotNull(firstSessionId); + + // WHEN device starts going to sleep a second time + mKeyguardUpdateMonitorCallback.onStartedGoingToSleep(0); + + // THEN there's a new keyguard session with a unique session id + final InstanceId secondSessionId = mSessionTracker.getSessionId(SESSION_KEYGUARD); + assertNotNull(secondSessionId); + assertNotEquals(firstSessionId, secondSessionId); + + // THEN session start event gets sent to status bar service twice (once per going to + // sleep signal) + verify(mStatusBarService, times(2)).onSessionStarted( + eq(SESSION_KEYGUARD), any(InstanceId.class)); + } + + @Test public void testKeyguardSessionOnKeyguardShowingChange() throws RemoteException { // GIVEN session tracker started w/o any sessions mSessionTracker.start(); |