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

Commit 355f9bac authored by Daniel Perez's avatar Daniel Perez
Browse files

Move Settings backup and restore metric logging from SettingsProvider to SettingsHelper

This moves the logging of backup and restore related metrics to SettingsHelper where the database restore actually happens. For now, I added logging when the values are written into the database. I'll handle other cases in future CLs.

Change-Id: I2752aa3b84a431c26005a8796c1d79ab5d54d8d7
Bug: 379861078
Flag: com.android.server.backup.enable_metrics_settings_backup_agents
Tested: SettingsHelperTest
parent b1304210
Loading
Loading
Loading
Loading
+1 −5
Original line number Diff line number Diff line
@@ -1058,14 +1058,10 @@ public class SettingsBackupAgent extends BackupAgentHelper {
                Log.d(TAG, "Restored font scale from: " + toRestore + " to " + value);
            }

            // TODO(b/379861078): Log metrics inside this method.
            settingsHelper.restoreValue(this, cr, contentValues, destination, key, value,
                    mRestoredFromSdkInt);
                    mRestoredFromSdkInt, mBackupRestoreEventLogger, finalSettingsKey);

            Log.d(TAG, "Restored setting: " + destination + " : " + key + "=" + value);
            if (areAgentMetricsEnabled) {
                mBackupRestoreEventLogger.logItemsRestored(finalSettingsKey, /* count= */ 1);
            }
        }

    }
+13 −1
Original line number Diff line number Diff line
@@ -80,6 +80,7 @@ public class SettingsHelper {
    // Error messages for logging metrics.
    private static final String ERROR_REMOTE_EXCEPTION_SETTING_LOCALE_DATA =
        "remote_exception_setting_locale_data";
    private static final String ERROR_FAILED_TO_RESTORE_SETTING = "failed_to_restore_setting";

    private Context mContext;
    private AudioManager mAudioManager;
@@ -179,12 +180,15 @@ public class SettingsHelper {
     * whether or not the setting should be saved to the database as well.
     * @param name the name of the setting
     * @param value the string value of the setting
     * @param backupRestoreEventLogger the logger to log metrics for backup and restore.
     * @param datatype the datatype for logging purposes.
     * @return whether to continue with writing the value to the database. In
     * some cases the data will be written by the call to the appropriate API,
     * and in some cases the property value needs to be modified before setting.
     */
    public void restoreValue(Context context, ContentResolver cr, ContentValues contentValues,
            Uri destination, String name, String value, int restoredFromSdkInt) {
            Uri destination, String name, String value, int restoredFromSdkInt,
            BackupRestoreEventLogger backupRestoreEventLogger, String datatype) {
        if (isReplacedSystemSetting(name)) {
            return;
        }
@@ -223,6 +227,7 @@ public class SettingsHelper {
        }

        try {
            // TODO(b/379861078): Log metrics for cases that fall in the if-else block.
            if (Settings.System.SOUND_EFFECTS_ENABLED.equals(name)) {
                setSoundEffects(Integer.parseInt(value) == 1);
                // fall through to the ordinary write to settings
@@ -290,12 +295,19 @@ public class SettingsHelper {
            contentValues.put(Settings.NameValueTable.NAME, name);
            contentValues.put(Settings.NameValueTable.VALUE, value);
            cr.insert(destination, contentValues);
            if (Flags.enableMetricsSettingsBackupAgents()) {
                backupRestoreEventLogger.logItemsRestored(datatype, /* count= */ 1);
            }
        } catch (Exception e) {
            // If we fail to apply the setting, by definition nothing happened
            sendBroadcast = false;
            sendBroadcastSystemUI = false;
            sendBroadcastAccessibility = false;
            Log.e(TAG, "Failed to restore setting name: " + name + " + value: " + value, e);
            if (Flags.enableMetricsSettingsBackupAgents()) {
                backupRestoreEventLogger.logItemsRestoreFailed(
                    datatype, /* count= */ 1, ERROR_FAILED_TO_RESTORE_SETTING);
            }
        } finally {
            // If this was an element of interest, send the "we just restored it"
            // broadcast with the historical value now that the new value has
+69 −109
Original line number Diff line number Diff line
@@ -420,75 +420,6 @@ public class SettingsBackupAgentTest extends BaseSettingsProviderTest {
        assertNull(getLoggingResultForDatatype(TEST_KEY, mAgentUnderTest));
    }

    @Test
    @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void restoreSettings_agentMetricsAreEnabled_agentMetricsAreLogged() {
        mAgentUnderTest.onCreate(
            UserHandle.SYSTEM, BackupDestination.CLOUD, OperationType.RESTORE);
        SettingsBackupAgent.SettingsBackupAllowlist allowlist =
                new SettingsBackupAgent.SettingsBackupAllowlist(
                        new String[] {OVERRIDDEN_TEST_SETTING},
                        TEST_VALUES_VALIDATORS);
        mAgentUnderTest.setSettingsAllowlist(allowlist);
        mAgentUnderTest.setBlockedSettings();
        TestSettingsHelper settingsHelper = new TestSettingsHelper(mContext);
        mAgentUnderTest.mSettingsHelper = settingsHelper;

        byte[] backupData = generateBackupData(TEST_VALUES);
        mAgentUnderTest
            .restoreSettings(
                backupData,
                /* pos= */ 0,
                backupData.length,
                TEST_URI,
                /* movedToGlobal= */ null,
                /* movedToSecure= */ null,
                /* movedToSystem= */ null,
                /* blockedSettingsArrayId= */ 0,
                /* dynamicBlockList= */ Collections.emptySet(),
                /* settingsToPreserve= */ Collections.emptySet(),
                TEST_KEY);

        DataTypeResult loggingResult =
            getLoggingResultForDatatype(TEST_KEY, mAgentUnderTest);
        assertNotNull(loggingResult);
        assertEquals(loggingResult.getSuccessCount(), 1);
    }

    @Test
    @DisableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void restoreSettings_agentMetricsAreDisabled_agentMetricsAreNotLogged() {
        mAgentUnderTest.onCreate(
            UserHandle.SYSTEM, BackupDestination.CLOUD, OperationType.RESTORE);
        SettingsBackupAgent.SettingsBackupAllowlist allowlist =
                new SettingsBackupAgent.SettingsBackupAllowlist(
                        new String[] {OVERRIDDEN_TEST_SETTING},
                        TEST_VALUES_VALIDATORS);
        mAgentUnderTest.setSettingsAllowlist(allowlist);
        mAgentUnderTest.setBlockedSettings();
        TestSettingsHelper settingsHelper = new TestSettingsHelper(mContext);
        mAgentUnderTest.mSettingsHelper = settingsHelper;

        byte[] backupData = generateBackupData(TEST_VALUES);
        mAgentUnderTest
            .restoreSettings(
                backupData,
                /* pos= */ 0,
                backupData.length,
                TEST_URI,
                /* movedToGlobal= */ null,
                /* movedToSecure= */ null,
                /* movedToSystem= */ null,
                /* blockedSettingsArrayId= */ 0,
                /* dynamicBlockList= */ Collections.emptySet(),
                /* settingsToPreserve= */ Collections.emptySet(),
                TEST_KEY);

        DataTypeResult loggingResult =
            getLoggingResultForDatatype(TEST_KEY, mAgentUnderTest);
        assertNull(loggingResult);
    }

    @Test
    @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void restoreSettings_agentMetricsAreEnabled_readEntityDataFails_failureIsLogged()
@@ -576,6 +507,40 @@ public class SettingsBackupAgentTest extends BaseSettingsProviderTest {
        assertTrue(loggingResult.getErrors().containsKey(ERROR_SKIPPED_BY_SYSTEM));
    }

    @Test
    @DisableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void
        restoreSettings_agentMetricsAreDisabled_settingIsSkippedBySystem_failureIsNotLogged() {
        mAgentUnderTest.onCreate(
            UserHandle.SYSTEM, BackupDestination.CLOUD, OperationType.RESTORE);
        String[] settingBlockedBySystem = new String[] {OVERRIDDEN_TEST_SETTING};
        SettingsBackupAgent.SettingsBackupAllowlist allowlist =
                new SettingsBackupAgent.SettingsBackupAllowlist(
                        settingBlockedBySystem,
                        TEST_VALUES_VALIDATORS);
        mAgentUnderTest.setSettingsAllowlist(allowlist);
        mAgentUnderTest.setBlockedSettings(settingBlockedBySystem);
        TestSettingsHelper settingsHelper = new TestSettingsHelper(mContext);
        mAgentUnderTest.mSettingsHelper = settingsHelper;

        byte[] backupData = generateBackupData(TEST_VALUES);
        mAgentUnderTest
            .restoreSettings(
                backupData,
                /* pos= */ 0,
                backupData.length,
                TEST_URI,
                /* movedToGlobal= */ null,
                /* movedToSecure= */ null,
                /* movedToSystem= */ null,
                /* blockedSettingsArrayId= */ 0,
                /* dynamicBlockList= */ Collections.emptySet(),
                /* settingsToPreserve= */ Collections.emptySet(),
                TEST_KEY);

        assertNull(getLoggingResultForDatatype(TEST_KEY, mAgentUnderTest));
    }

    @Test
    @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void restoreSettings_agentMetricsAreEnabled_settingIsSkippedByBlockList_failureIsLogged() {
@@ -615,8 +580,9 @@ public class SettingsBackupAgentTest extends BaseSettingsProviderTest {
    }

    @Test
    @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void restoreSettings_agentMetricsAreEnabled_settingIsPreserved_failureIsLogged() {
    @DisableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void
        restoreSettings_agentMetricsAreDisabled_settingIsSkippedByBlockList_failureIsNotLogged() {
        mAgentUnderTest.onCreate(
            UserHandle.SYSTEM, BackupDestination.CLOUD, OperationType.RESTORE);
        SettingsBackupAgent.SettingsBackupAllowlist allowlist =
@@ -627,7 +593,7 @@ public class SettingsBackupAgentTest extends BaseSettingsProviderTest {
        mAgentUnderTest.setBlockedSettings();
        TestSettingsHelper settingsHelper = new TestSettingsHelper(mContext);
        mAgentUnderTest.mSettingsHelper = settingsHelper;
        Set<String> preservedSettings =
        Set<String> dynamicBlockList =
            Set.of(Uri.withAppendedPath(TEST_URI, OVERRIDDEN_TEST_SETTING).toString());

        byte[] backupData = generateBackupData(TEST_VALUES);
@@ -641,30 +607,28 @@ public class SettingsBackupAgentTest extends BaseSettingsProviderTest {
                /* movedToSecure= */ null,
                /* movedToSystem= */ null,
                /* blockedSettingsArrayId= */ 0,
                /* dynamicBlockList = */ Collections.emptySet(),
                preservedSettings,
                dynamicBlockList,
                /* settingsToPreserve= */ Collections.emptySet(),
                TEST_KEY);

        DataTypeResult loggingResult =
            getLoggingResultForDatatype(TEST_KEY, mAgentUnderTest);
        assertNotNull(loggingResult);
        assertEquals(loggingResult.getFailCount(), 1);
        assertTrue(loggingResult.getErrors().containsKey(ERROR_SKIPPED_PRESERVED));
        assertNull(getLoggingResultForDatatype(TEST_KEY, mAgentUnderTest));
    }

    @Test
    @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void restoreSettings_agentMetricsAreEnabled_settingIsNotValid_failureIsLogged() {
    public void restoreSettings_agentMetricsAreEnabled_settingIsPreserved_failureIsLogged() {
        mAgentUnderTest.onCreate(
            UserHandle.SYSTEM, BackupDestination.CLOUD, OperationType.RESTORE);
        SettingsBackupAgent.SettingsBackupAllowlist allowlist =
                new SettingsBackupAgent.SettingsBackupAllowlist(
                        new String[] {OVERRIDDEN_TEST_SETTING},
                        /* settingsValidators= */ null);
                        TEST_VALUES_VALIDATORS);
        mAgentUnderTest.setSettingsAllowlist(allowlist);
        mAgentUnderTest.setBlockedSettings();
        TestSettingsHelper settingsHelper = new TestSettingsHelper(mContext);
        mAgentUnderTest.mSettingsHelper = settingsHelper;
        Set<String> preservedSettings =
            Set.of(Uri.withAppendedPath(TEST_URI, OVERRIDDEN_TEST_SETTING).toString());

        byte[] backupData = generateBackupData(TEST_VALUES);
        mAgentUnderTest
@@ -678,19 +642,19 @@ public class SettingsBackupAgentTest extends BaseSettingsProviderTest {
                /* movedToSystem= */ null,
                /* blockedSettingsArrayId= */ 0,
                /* dynamicBlockList = */ Collections.emptySet(),
                /* settingsToPreserve= */ Collections.emptySet(),
                preservedSettings,
                TEST_KEY);

        DataTypeResult loggingResult =
            getLoggingResultForDatatype(TEST_KEY, mAgentUnderTest);
        assertNotNull(loggingResult);
        assertEquals(loggingResult.getFailCount(), 1);
        assertTrue(loggingResult.getErrors().containsKey(ERROR_DID_NOT_PASS_VALIDATION));
        assertTrue(loggingResult.getErrors().containsKey(ERROR_SKIPPED_PRESERVED));
    }

    @Test
    @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void restoreSettings_agentMetricsAreEnabled_settingIsMarkedAsMovedToGlobal_agentMetricsAreLoggedWithGlobalKey() {
    @DisableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void restoreSettings_agentMetricsAreDisabled_settingIsPreserved_failureIsNotLogged() {
        mAgentUnderTest.onCreate(
            UserHandle.SYSTEM, BackupDestination.CLOUD, OperationType.RESTORE);
        SettingsBackupAgent.SettingsBackupAllowlist allowlist =
@@ -701,6 +665,8 @@ public class SettingsBackupAgentTest extends BaseSettingsProviderTest {
        mAgentUnderTest.setBlockedSettings();
        TestSettingsHelper settingsHelper = new TestSettingsHelper(mContext);
        mAgentUnderTest.mSettingsHelper = settingsHelper;
        Set<String> preservedSettings =
            Set.of(Uri.withAppendedPath(TEST_URI, OVERRIDDEN_TEST_SETTING).toString());

        byte[] backupData = generateBackupData(TEST_VALUES);
        mAgentUnderTest
@@ -709,30 +675,26 @@ public class SettingsBackupAgentTest extends BaseSettingsProviderTest {
                /* pos= */ 0,
                backupData.length,
                TEST_URI,
                /* movedToGlobal= */ Set.of(OVERRIDDEN_TEST_SETTING),
                /* movedToGlobal= */ null,
                /* movedToSecure= */ null,
                /* movedToSystem= */ null,
                /* blockedSettingsArrayId= */ 0,
                /* dynamicBlockList = */ Collections.emptySet(),
                /* settingsToPreserve= */ Collections.emptySet(),
                preservedSettings,
                TEST_KEY);

        DataTypeResult loggingResult =
            getLoggingResultForDatatype(KEY_GLOBAL, mAgentUnderTest);
        assertNotNull(loggingResult);
        assertEquals(loggingResult.getSuccessCount(), 1);
        assertNull(getLoggingResultForDatatype(TEST_KEY, mAgentUnderTest));
    }

    @Test
    @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void restoreSettings_agentMetricsAreEnabled_settingIsMarkedAsMovedToSecure_agentMetricsAreLoggedWithSecureKey() {
    public void restoreSettings_agentMetricsAreEnabled_settingIsNotValid_failureIsLogged() {
        mAgentUnderTest.onCreate(
            UserHandle.SYSTEM, BackupDestination.CLOUD, OperationType.RESTORE);
        SettingsBackupAgent.SettingsBackupAllowlist allowlist =
                new SettingsBackupAgent.SettingsBackupAllowlist(
                        new String[] {OVERRIDDEN_TEST_SETTING},
                        TEST_VALUES_VALIDATORS);
                        /* settingsValidators= */ null);
        mAgentUnderTest.setSettingsAllowlist(allowlist);
        mAgentUnderTest.setBlockedSettings();
        TestSettingsHelper settingsHelper = new TestSettingsHelper(mContext);
@@ -746,7 +708,7 @@ public class SettingsBackupAgentTest extends BaseSettingsProviderTest {
                backupData.length,
                TEST_URI,
                /* movedToGlobal= */ null,
                /* movedToSecure= */ Set.of(OVERRIDDEN_TEST_SETTING),
                /* movedToSecure= */ null,
                /* movedToSystem= */ null,
                /* blockedSettingsArrayId= */ 0,
                /* dynamicBlockList = */ Collections.emptySet(),
@@ -754,21 +716,21 @@ public class SettingsBackupAgentTest extends BaseSettingsProviderTest {
                TEST_KEY);

        DataTypeResult loggingResult =
            getLoggingResultForDatatype(KEY_SECURE, mAgentUnderTest);
            getLoggingResultForDatatype(TEST_KEY, mAgentUnderTest);
        assertNotNull(loggingResult);
        assertEquals(loggingResult.getSuccessCount(), 1);
        assertNull(getLoggingResultForDatatype(TEST_KEY, mAgentUnderTest));
        assertEquals(loggingResult.getFailCount(), 1);
        assertTrue(loggingResult.getErrors().containsKey(ERROR_DID_NOT_PASS_VALIDATION));
    }

    @Test
    @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void restoreSettings_agentMetricsAreEnabled_settingIsMarkedAsMovedToSystem_agentMetricsAreLoggedWithSystemKey() {
    @DisableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void restoreSettings_agentMetricsAreDisabled_settingIsNotValid_failureIsNotLogged() {
        mAgentUnderTest.onCreate(
            UserHandle.SYSTEM, BackupDestination.CLOUD, OperationType.RESTORE);
        SettingsBackupAgent.SettingsBackupAllowlist allowlist =
                new SettingsBackupAgent.SettingsBackupAllowlist(
                        new String[] {OVERRIDDEN_TEST_SETTING},
                        TEST_VALUES_VALIDATORS);
                        /* settingsValidators= */ null);
        mAgentUnderTest.setSettingsAllowlist(allowlist);
        mAgentUnderTest.setBlockedSettings();
        TestSettingsHelper settingsHelper = new TestSettingsHelper(mContext);
@@ -783,16 +745,12 @@ public class SettingsBackupAgentTest extends BaseSettingsProviderTest {
                TEST_URI,
                /* movedToGlobal= */ null,
                /* movedToSecure= */ null,
                /* movedToSystem= */ Set.of(OVERRIDDEN_TEST_SETTING),
                /* movedToSystem= */ null,
                /* blockedSettingsArrayId= */ 0,
                /* dynamicBlockList = */ Collections.emptySet(),
                /* settingsToPreserve= */ Collections.emptySet(),
                TEST_KEY);

        DataTypeResult loggingResult =
            getLoggingResultForDatatype(KEY_SYSTEM, mAgentUnderTest);
        assertNotNull(loggingResult);
        assertEquals(loggingResult.getSuccessCount(), 1);
        assertNull(getLoggingResultForDatatype(TEST_KEY, mAgentUnderTest));
    }

@@ -961,7 +919,9 @@ public class SettingsBackupAgentTest extends BaseSettingsProviderTest {
                Uri destination,
                String name,
                String value,
                int restoredFromSdkInt) {
                int restoredFromSdkInt,
                BackupRestoreEventLogger backupRestoreEventLogger,
                String datatype) {
            mWrittenValues.put(name, value);
        }
    }
+18 −6
Original line number Diff line number Diff line
@@ -168,7 +168,9 @@ public class SettingsHelperRestoreTest {
                Settings.Secure.getUriFor(settingName),
                settingName,
                String.valueOf(restoreSettingValue),
                Build.VERSION.SDK_INT);
                Build.VERSION.SDK_INT,
                null,
                "");

        assertEquals(
                configuredSettingValue,
@@ -191,7 +193,9 @@ public class SettingsHelperRestoreTest {
                Settings.Secure.getUriFor(settingName),
                settingName,
                String.valueOf(restoreSettingValue),
                Build.VERSION.SDK_INT);
                Build.VERSION.SDK_INT,
                null,
                "");

        assertEquals(
                restoreSettingValue,
@@ -234,7 +238,9 @@ public class SettingsHelperRestoreTest {
                Settings.Secure.getUriFor(settingName),
                settingName,
                String.valueOf(0),
                Build.VERSION.SDK_INT);
                Build.VERSION.SDK_INT,
                null,
                "");

        assertEquals(configuredSettingValue, Settings.Secure.getInt(mContentResolver, settingName));
    }
@@ -256,7 +262,9 @@ public class SettingsHelperRestoreTest {
                Settings.Secure.getUriFor(settingName),
                settingName,
                String.valueOf(restoreSettingValue),
                Build.VERSION.SDK_INT);
                Build.VERSION.SDK_INT,
                null,
                "");

        assertEquals(restoreSettingValue, Settings.Secure.getInt(mContentResolver, settingName));
    }
@@ -278,7 +286,9 @@ public class SettingsHelperRestoreTest {
                Settings.Secure.getUriFor(settingName),
                settingName,
                restoreSettingValue,
                Build.VERSION.SDK_INT);
                Build.VERSION.SDK_INT,
                null,
                "");

        Intent intentReceived = futureIntent.get();
        assertThat(intentReceived.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE))
@@ -303,7 +313,9 @@ public class SettingsHelperRestoreTest {
                Settings.Secure.getUriFor(settingName),
                settingName,
                restoredValue,
                Build.VERSION.SDK_INT);
                Build.VERSION.SDK_INT,
                null,
                "");

        Intent intentReceived = futureIntent.get();
        assertThat(intentReceived.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE))
+115 −14

File changed.

Preview size limit exceeded, changes collapsed.