diff options
3 files changed, 36 insertions, 4 deletions
diff --git a/services/core/java/com/android/server/biometrics/AuthenticationStatsCollector.java b/services/core/java/com/android/server/biometrics/AuthenticationStatsCollector.java index 2a3302c119a9..e8a20def02cb 100644 --- a/services/core/java/com/android/server/biometrics/AuthenticationStatsCollector.java +++ b/services/core/java/com/android/server/biometrics/AuthenticationStatsCollector.java @@ -68,8 +68,10 @@ public class AuthenticationStatsCollector { @Override public void onReceive(@NonNull Context context, @NonNull Intent intent) { final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); + if (userId != UserHandle.USER_NULL && intent.getAction().equals(Intent.ACTION_USER_REMOVED)) { + Slog.d(TAG, "Removing data for user: " + userId); onUserRemoved(userId); } } @@ -84,7 +86,9 @@ public class AuthenticationStatsCollector { mModality = modality; mBiometricNotification = biometricNotification; - context.registerReceiver(mBroadcastReceiver, new IntentFilter(Intent.ACTION_USER_REMOVED)); + IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction(Intent.ACTION_USER_REMOVED); + context.registerReceiver(mBroadcastReceiver, intentFilter); } private void initializeUserAuthenticationStatsMap() { diff --git a/services/core/java/com/android/server/biometrics/AuthenticationStatsPersister.java b/services/core/java/com/android/server/biometrics/AuthenticationStatsPersister.java index 74e1410dba00..8122b1d131f8 100644 --- a/services/core/java/com/android/server/biometrics/AuthenticationStatsPersister.java +++ b/services/core/java/com/android/server/biometrics/AuthenticationStatsPersister.java @@ -59,7 +59,7 @@ public class AuthenticationStatsPersister { // The package info in the context isn't initialized in the way it is for normal apps, // so the standard, name-based context.getSharedPreferences doesn't work. Instead, we // build the path manually below using the same policy that appears in ContextImpl. - final File prefsFile = new File(Environment.getDataSystemDeDirectory(), FILE_NAME); + final File prefsFile = new File(Environment.getDataSystemDirectory(), FILE_NAME); mSharedPreferences = context.getSharedPreferences(prefsFile, Context.MODE_PRIVATE); } @@ -137,16 +137,19 @@ public class AuthenticationStatsPersister { iterator.remove(); break; } + // Reset frrStatJson when user doesn't exist. + frrStatJson = null; } - // If there's existing frr stats in the file, we want to update the stats for the given - // modality and keep the stats for other modalities. + // Checks if this is a new user and there's no JSON for this user in the storage. if (frrStatJson == null) { frrStatJson = new JSONObject().put(USER_ID, userId); } frrStatsSet.add(buildFrrStats(frrStatJson, totalAttempts, rejectedAttempts, enrollmentNotifications, modality)); + Slog.d(TAG, "frrStatsSet to persist: " + frrStatsSet); + mSharedPreferences.edit().putStringSet(KEY, frrStatsSet).apply(); } catch (JSONException e) { diff --git a/services/tests/servicestests/src/com/android/server/biometrics/AuthenticationStatsPersisterTest.java b/services/tests/servicestests/src/com/android/server/biometrics/AuthenticationStatsPersisterTest.java index dde2a3c2cfd7..0c0d47a6b165 100644 --- a/services/tests/servicestests/src/com/android/server/biometrics/AuthenticationStatsPersisterTest.java +++ b/services/tests/servicestests/src/com/android/server/biometrics/AuthenticationStatsPersisterTest.java @@ -217,6 +217,31 @@ public class AuthenticationStatsPersisterTest { } @Test + public void persistFrrStats_multiUser_newUser_shouldUpdateRecord() throws JSONException { + AuthenticationStats authenticationStats1 = new AuthenticationStats(USER_ID_1, + 300 /* totalAttempts */, 10 /* rejectedAttempts */, + 0 /* enrollmentNotifications */, BiometricsProtoEnums.MODALITY_FACE); + AuthenticationStats authenticationStats2 = new AuthenticationStats(USER_ID_2, + 100 /* totalAttempts */, 5 /* rejectedAttempts */, + 1 /* enrollmentNotifications */, BiometricsProtoEnums.MODALITY_FINGERPRINT); + + // Sets up the shared preference with user 1 only. + when(mSharedPreferences.getStringSet(eq(KEY), anySet())).thenReturn( + Set.of(buildFrrStats(authenticationStats1))); + + // Add data for user 2. + mAuthenticationStatsPersister.persistFrrStats(authenticationStats2.getUserId(), + authenticationStats2.getTotalAttempts(), + authenticationStats2.getRejectedAttempts(), + authenticationStats2.getEnrollmentNotifications(), + authenticationStats2.getModality()); + + verify(mEditor).putStringSet(eq(KEY), mStringSetArgumentCaptor.capture()); + assertThat(mStringSetArgumentCaptor.getValue()) + .contains(buildFrrStats(authenticationStats2)); + } + + @Test public void removeFrrStats_existingUser_shouldUpdateRecord() throws JSONException { AuthenticationStats authenticationStats = new AuthenticationStats(USER_ID_1, 300 /* totalAttempts */, 10 /* rejectedAttempts */, |