Add Keyguard session end UIEvents

Fixes: 271427785
Test: atest SessionTrackerTest
Change-Id: I7397566ff8dd4f892f1350620a147293a23a4403
This commit is contained in:
Beverly
2023-03-29 20:42:57 +00:00
parent 84fd6c7823
commit 1cdbdc9a80
2 changed files with 96 additions and 4 deletions

View File

@@ -28,6 +28,8 @@ import androidx.annotation.NonNull;
import com.android.internal.logging.InstanceId;
import com.android.internal.logging.InstanceIdSequence;
import com.android.internal.logging.UiEvent;
import com.android.internal.logging.UiEventLogger;
import com.android.internal.statusbar.IStatusBarService;
import com.android.keyguard.KeyguardUpdateMonitor;
import com.android.keyguard.KeyguardUpdateMonitorCallback;
@@ -60,6 +62,7 @@ public class SessionTracker implements CoreStartable {
private final AuthController mAuthController;
private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
private final KeyguardStateController mKeyguardStateController;
private final UiEventLogger mUiEventLogger;
private final Map<Integer, InstanceId> mSessionToInstanceId = new HashMap<>();
private boolean mKeyguardSessionStarted;
@@ -69,12 +72,14 @@ public class SessionTracker implements CoreStartable {
IStatusBarService statusBarService,
AuthController authController,
KeyguardUpdateMonitor keyguardUpdateMonitor,
KeyguardStateController keyguardStateController
KeyguardStateController keyguardStateController,
UiEventLogger uiEventLogger
) {
mStatusBarManagerService = statusBarService;
mAuthController = authController;
mKeyguardUpdateMonitor = keyguardUpdateMonitor;
mKeyguardStateController = keyguardStateController;
mUiEventLogger = uiEventLogger;
}
@Override
@@ -116,6 +121,10 @@ public class SessionTracker implements CoreStartable {
}
private void endSession(int type) {
endSession(type, null);
}
private void endSession(int type, @Nullable SessionUiEvent endSessionUiEvent) {
if (mSessionToInstanceId.getOrDefault(type, null) == null) {
Log.e(TAG, "session [" + getString(type) + "] was not started");
return;
@@ -127,6 +136,9 @@ public class SessionTracker implements CoreStartable {
if (DEBUG) {
Log.d(TAG, "Session end for [" + getString(type) + "] id=" + instanceId);
}
if (endSessionUiEvent != null) {
mUiEventLogger.log(endSessionUiEvent, instanceId);
}
mStatusBarManagerService.onSessionEnded(type, instanceId);
} catch (RemoteException e) {
Log.e(TAG, "Unable to send onSessionEnded for session="
@@ -139,7 +151,7 @@ public class SessionTracker implements CoreStartable {
@Override
public void onStartedGoingToSleep(int why) {
if (mKeyguardSessionStarted) {
endSession(SESSION_KEYGUARD);
endSession(SESSION_KEYGUARD, SessionUiEvent.KEYGUARD_SESSION_END_GOING_TO_SLEEP);
}
// Start a new session whenever the device goes to sleep
@@ -162,7 +174,8 @@ public class SessionTracker implements CoreStartable {
startSession(SESSION_KEYGUARD);
} else if (!keyguardShowing && wasSessionStarted) {
mKeyguardSessionStarted = false;
endSession(SESSION_KEYGUARD);
endSession(SESSION_KEYGUARD,
SessionUiEvent.KEYGUARD_SESSION_END_KEYGUARD_GOING_AWAY);
}
}
};
@@ -200,4 +213,22 @@ public class SessionTracker implements CoreStartable {
return "unknownType=" + sessionType;
}
enum SessionUiEvent implements UiEventLogger.UiEventEnum {
@UiEvent(doc = "A keyguard session ended due to the keyguard going away.")
KEYGUARD_SESSION_END_KEYGUARD_GOING_AWAY(1354),
@UiEvent(doc = "A keyguard session ended due to display going to sleep.")
KEYGUARD_SESSION_END_GOING_TO_SLEEP(1355);
private final int mId;
SessionUiEvent(int id) {
mId = id;
}
@Override
public int getId() {
return mId;
}
}
}

View File

@@ -26,6 +26,7 @@ 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.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -37,6 +38,7 @@ import android.testing.TestableLooper;
import androidx.test.filters.SmallTest;
import com.android.internal.logging.InstanceId;
import com.android.internal.logging.UiEventLogger;
import com.android.internal.statusbar.IStatusBarService;
import com.android.keyguard.KeyguardUpdateMonitor;
import com.android.keyguard.KeyguardUpdateMonitorCallback;
@@ -64,6 +66,8 @@ public class SessionTrackerTest extends SysuiTestCase {
private KeyguardUpdateMonitor mKeyguardUpdateMonitor;
@Mock
private KeyguardStateController mKeyguardStateController;
@Mock
private UiEventLogger mUiEventLogger;
@Captor
ArgumentCaptor<KeyguardUpdateMonitorCallback> mKeyguardUpdateMonitorCallbackCaptor;
@@ -87,7 +91,8 @@ public class SessionTrackerTest extends SysuiTestCase {
mStatusBarService,
mAuthController,
mKeyguardUpdateMonitor,
mKeyguardStateController
mKeyguardStateController,
mUiEventLogger
);
}
@@ -238,6 +243,62 @@ public class SessionTrackerTest extends SysuiTestCase {
eq(SESSION_KEYGUARD), any(InstanceId.class));
}
@Test
public void uiEventLoggedOnEndSessionWhenDeviceStartsSleeping() throws RemoteException {
// GIVEN session tracker start
mSessionTracker.start();
captureKeyguardUpdateMonitorCallback();
captureKeyguardStateControllerCallback();
// GIVEN keyguard becomes visible (ie: from lockdown), so there's a valid keyguard
// session running
when(mKeyguardStateController.isShowing()).thenReturn(true);
mKeyguardStateCallback.onKeyguardShowingChanged();
// WHEN device starts going to sleep
mKeyguardUpdateMonitorCallback.onStartedGoingToSleep(0);
// THEN UI event is logged
verify(mUiEventLogger).log(
eq(SessionTracker.SessionUiEvent.KEYGUARD_SESSION_END_GOING_TO_SLEEP),
any(InstanceId.class));
}
@Test
public void noUiEventLoggedOnEndSessionWhenDeviceStartsSleepingWithoutStartSession()
throws RemoteException {
// GIVEN session tracker start without any valid sessions
mSessionTracker.start();
captureKeyguardUpdateMonitorCallback();
// WHEN device starts going to sleep when there was no started sessions
mKeyguardUpdateMonitorCallback.onStartedGoingToSleep(0);
// THEN UI event is never logged
verify(mUiEventLogger, never()).log(
eq(SessionTracker.SessionUiEvent.KEYGUARD_SESSION_END_GOING_TO_SLEEP),
any(InstanceId.class));
}
@Test
public void uiEventLoggedOnEndSessionWhenKeyguardGoingAway() throws RemoteException {
// GIVEN session tracker started w/o any sessions
mSessionTracker.start();
captureKeyguardUpdateMonitorCallback();
captureKeyguardStateControllerCallback();
// WHEN keyguard was showing and now it's not
when(mKeyguardStateController.isShowing()).thenReturn(true);
mKeyguardStateCallback.onKeyguardShowingChanged();
when(mKeyguardStateController.isShowing()).thenReturn(false);
mKeyguardStateCallback.onKeyguardShowingChanged();
// THEN UI event is logged
verify(mUiEventLogger).log(
eq(SessionTracker.SessionUiEvent.KEYGUARD_SESSION_END_KEYGUARD_GOING_AWAY),
any(InstanceId.class));
}
void captureKeyguardUpdateMonitorCallback() {
verify(mKeyguardUpdateMonitor).registerCallback(
mKeyguardUpdateMonitorCallbackCaptor.capture());