Merge "Start a new keyguard session on device sleep" into tm-qpr-dev

This commit is contained in:
Beverly Tai
2022-12-07 21:23:17 +00:00
committed by Android (Google) Code Review
3 changed files with 44 additions and 7 deletions

View File

@@ -234,7 +234,10 @@ public class StatusBarManager {
/** /**
* Session flag for {@link #registerSessionListener} indicating the listener * 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 * @hide
*/ */
public static final int SESSION_KEYGUARD = 1 << 0; public static final int SESSION_KEYGUARD = 1 << 0;

View File

@@ -49,7 +49,9 @@ import javax.inject.Inject;
@SysUISingleton @SysUISingleton
public class SessionTracker implements CoreStartable { public class SessionTracker implements CoreStartable {
private static final String TAG = "SessionTracker"; 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 // At most 20 bits: ~1m possibilities, ~0.5% probability of collision in 100 values
private final InstanceIdSequence mInstanceIdGenerator = new InstanceIdSequence(1 << 20); private final InstanceIdSequence mInstanceIdGenerator = new InstanceIdSequence(1 << 20);
@@ -81,8 +83,8 @@ public class SessionTracker implements CoreStartable {
mKeyguardUpdateMonitor.registerCallback(mKeyguardUpdateMonitorCallback); mKeyguardUpdateMonitor.registerCallback(mKeyguardUpdateMonitorCallback);
mKeyguardStateController.addCallback(mKeyguardStateCallback); mKeyguardStateController.addCallback(mKeyguardStateCallback);
mKeyguardSessionStarted = mKeyguardStateController.isShowing(); if (mKeyguardStateController.isShowing()) {
if (mKeyguardSessionStarted) { mKeyguardSessionStarted = true;
startSession(SESSION_KEYGUARD); startSession(SESSION_KEYGUARD);
} }
} }
@@ -136,12 +138,11 @@ public class SessionTracker implements CoreStartable {
new KeyguardUpdateMonitorCallback() { new KeyguardUpdateMonitorCallback() {
@Override @Override
public void onStartedGoingToSleep(int why) { public void onStartedGoingToSleep(int why) {
// we need to register to the KeyguardUpdateMonitor lifecycle b/c it gets called
// before the WakefulnessLifecycle
if (mKeyguardSessionStarted) { if (mKeyguardSessionStarted) {
return; endSession(SESSION_KEYGUARD);
} }
// Start a new session whenever the device goes to sleep
mKeyguardSessionStarted = true; mKeyguardSessionStarted = true;
startSession(SESSION_KEYGUARD); startSession(SESSION_KEYGUARD);
} }
@@ -154,6 +155,9 @@ public class SessionTracker implements CoreStartable {
boolean wasSessionStarted = mKeyguardSessionStarted; boolean wasSessionStarted = mKeyguardSessionStarted;
boolean keyguardShowing = mKeyguardStateController.isShowing(); boolean keyguardShowing = mKeyguardStateController.isShowing();
if (keyguardShowing && !wasSessionStarted) { 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; mKeyguardSessionStarted = true;
startSession(SESSION_KEYGUARD); startSession(SESSION_KEYGUARD);
} else if (!keyguardShowing && wasSessionStarted) { } else if (!keyguardShowing && wasSessionStarted) {

View File

@@ -23,8 +23,10 @@ import static android.app.StatusBarManager.SESSION_KEYGUARD;
import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertNull;
import static org.junit.Assert.assertNotEquals;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -170,6 +172,34 @@ public class SessionTrackerTest extends SysuiTestCase {
eq(SESSION_KEYGUARD), any(InstanceId.class)); eq(SESSION_KEYGUARD), any(InstanceId.class));
} }
@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 @Test
public void testKeyguardSessionOnKeyguardShowingChange() throws RemoteException { public void testKeyguardSessionOnKeyguardShowingChange() throws RemoteException {
// GIVEN session tracker started w/o any sessions // GIVEN session tracker started w/o any sessions