Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit dffe5f05 authored by Wenhui Yang's avatar Wenhui Yang Committed by Automerger Merge Worker
Browse files

Merge "FRR follow-up: change the data directory" into udc-qpr-dev am: 84bdf480

parents aa432416 84bdf480
Loading
Loading
Loading
Loading
+5 −1
Original line number Original line Diff line number Diff line
@@ -68,8 +68,10 @@ public class AuthenticationStatsCollector {
        @Override
        @Override
        public void onReceive(@NonNull Context context, @NonNull Intent intent) {
        public void onReceive(@NonNull Context context, @NonNull Intent intent) {
            final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
            final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);

            if (userId != UserHandle.USER_NULL
            if (userId != UserHandle.USER_NULL
                    && intent.getAction().equals(Intent.ACTION_USER_REMOVED)) {
                    && intent.getAction().equals(Intent.ACTION_USER_REMOVED)) {
                Slog.d(TAG, "Removing data for user: " + userId);
                onUserRemoved(userId);
                onUserRemoved(userId);
            }
            }
        }
        }
@@ -84,7 +86,9 @@ public class AuthenticationStatsCollector {
        mModality = modality;
        mModality = modality;
        mBiometricNotification = biometricNotification;
        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() {
    private void initializeUserAuthenticationStatsMap() {
+6 −3
Original line number Original line Diff line number Diff line
@@ -59,7 +59,7 @@ public class AuthenticationStatsPersister {
        // The package info in the context isn't initialized in the way it is for normal apps,
        // 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
        // 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.
        // 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);
        mSharedPreferences = context.getSharedPreferences(prefsFile, Context.MODE_PRIVATE);
    }
    }


@@ -137,16 +137,19 @@ public class AuthenticationStatsPersister {
                    iterator.remove();
                    iterator.remove();
                    break;
                    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
            // Checks if this is a new user and there's no JSON for this user in the storage.
            // modality and keep the stats for other modalities.
            if (frrStatJson == null) {
            if (frrStatJson == null) {
                frrStatJson = new JSONObject().put(USER_ID, userId);
                frrStatJson = new JSONObject().put(USER_ID, userId);
            }
            }
            frrStatsSet.add(buildFrrStats(frrStatJson, totalAttempts, rejectedAttempts,
            frrStatsSet.add(buildFrrStats(frrStatJson, totalAttempts, rejectedAttempts,
                    enrollmentNotifications, modality));
                    enrollmentNotifications, modality));


            Slog.d(TAG, "frrStatsSet to persist: " + frrStatsSet);

            mSharedPreferences.edit().putStringSet(KEY, frrStatsSet).apply();
            mSharedPreferences.edit().putStringSet(KEY, frrStatsSet).apply();


        } catch (JSONException e) {
        } catch (JSONException e) {
+25 −0
Original line number Original line Diff line number Diff line
@@ -216,6 +216,31 @@ public class AuthenticationStatsPersisterTest {
        assertThat(mStringSetArgumentCaptor.getValue()).contains(expectedFrrStats);
        assertThat(mStringSetArgumentCaptor.getValue()).contains(expectedFrrStats);
    }
    }


    @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
    @Test
    public void removeFrrStats_existingUser_shouldUpdateRecord() throws JSONException {
    public void removeFrrStats_existingUser_shouldUpdateRecord() throws JSONException {
        AuthenticationStats authenticationStats = new AuthenticationStats(USER_ID_1,
        AuthenticationStats authenticationStats = new AuthenticationStats(USER_ID_1,