From 37dc9ce32d85718ae2e29b42545d498988bd6c0f Mon Sep 17 00:00:00 2001 From: Wenhui Yang Date: Wed, 13 Sep 2023 08:28:57 +0000 Subject: FRR follow-up: make persister non-null Bug: 295582896 Test: atest AuthenticationStatsCollectorTest Change-Id: I26829df6ccad850d5b9335b2719a3b375db7fc9f --- .../biometrics/AuthenticationStatsCollector.java | 37 +++++++--------------- .../AuthenticationStatsCollectorTest.java | 1 + 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/services/core/java/com/android/server/biometrics/AuthenticationStatsCollector.java b/services/core/java/com/android/server/biometrics/AuthenticationStatsCollector.java index 64691e0b062b..6edbfb7752a1 100644 --- a/services/core/java/com/android/server/biometrics/AuthenticationStatsCollector.java +++ b/services/core/java/com/android/server/biometrics/AuthenticationStatsCollector.java @@ -59,12 +59,9 @@ public class AuthenticationStatsCollector { private final float mThreshold; private final int mModality; - private boolean mPersisterInitialized = false; @NonNull private final Map mUserAuthenticationStatsMap; - - // TODO(b/295582896): Find a way to make this NonNull - @Nullable private AuthenticationStatsPersister mAuthenticationStatsPersister; + @NonNull private AuthenticationStatsPersister mAuthenticationStatsPersister; @NonNull private BiometricNotification mBiometricNotification; private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @@ -93,23 +90,20 @@ public class AuthenticationStatsCollector { mFaceManager = mContext.getSystemService(FaceManager.class); mFingerprintManager = mContext.getSystemService(FingerprintManager.class); + mAuthenticationStatsPersister = new AuthenticationStatsPersister(mContext); + + initializeUserAuthenticationStatsMap(); + mAuthenticationStatsPersister.persistFrrThreshold(mThreshold); + IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(Intent.ACTION_USER_REMOVED); context.registerReceiver(mBroadcastReceiver, intentFilter); } private void initializeUserAuthenticationStatsMap() { - try { - mAuthenticationStatsPersister = new AuthenticationStatsPersister(mContext); - for (AuthenticationStats stats : - mAuthenticationStatsPersister.getAllFrrStats(mModality)) { - mUserAuthenticationStatsMap.put(stats.getUserId(), stats); - } - mAuthenticationStatsPersister.persistFrrThreshold(mThreshold); - - mPersisterInitialized = true; - } catch (IllegalStateException e) { - Slog.w(TAG, "Failed to initialize AuthenticationStatsPersister.", e); + for (AuthenticationStats stats : + mAuthenticationStatsPersister.getAllFrrStats(mModality)) { + mUserAuthenticationStatsMap.put(stats.getUserId(), stats); } } @@ -143,9 +137,7 @@ public class AuthenticationStatsCollector { sendNotificationIfNeeded(userId); - if (mPersisterInitialized) { - persistDataIfNeeded(userId); - } + persistDataIfNeeded(userId); } /** Check if a notification should be sent after a calculation cycle. */ @@ -188,13 +180,8 @@ public class AuthenticationStatsCollector { } private void onUserRemoved(final int userId) { - if (!mPersisterInitialized) { - initializeUserAuthenticationStatsMap(); - } - if (mPersisterInitialized) { - mUserAuthenticationStatsMap.remove(userId); - mAuthenticationStatsPersister.removeFrrStats(userId); - } + mUserAuthenticationStatsMap.remove(userId); + mAuthenticationStatsPersister.removeFrrStats(userId); } private boolean isSingleModalityDevice() { diff --git a/services/tests/servicestests/src/com/android/server/biometrics/AuthenticationStatsCollectorTest.java b/services/tests/servicestests/src/com/android/server/biometrics/AuthenticationStatsCollectorTest.java index fa6e7f60c1b0..a11a8f53c2cb 100644 --- a/services/tests/servicestests/src/com/android/server/biometrics/AuthenticationStatsCollectorTest.java +++ b/services/tests/servicestests/src/com/android/server/biometrics/AuthenticationStatsCollectorTest.java @@ -104,6 +104,7 @@ public class AuthenticationStatsCollectorTest { when(mSharedPreferences.getStringSet(anyString(), anySet())).thenReturn(emptySet()); when(mSharedPreferences.edit()).thenReturn(mEditor); when(mEditor.putFloat(anyString(), anyFloat())).thenReturn(mEditor); + when(mEditor.putStringSet(anyString(), anySet())).thenReturn(mEditor); mAuthenticationStatsCollector = new AuthenticationStatsCollector(mContext, 0 /* modality */, mBiometricNotification); -- cgit v1.2.3-59-g8ed1b