Loading packages/SettingsProvider/src/com/android/providers/settings/SettingsBackupAgent.java +0 −4 Original line number Diff line number Diff line Loading @@ -1053,14 +1053,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); Log.d(TAG, "Restored setting: " + destination + " : " + key + "=" + value); if (areAgentMetricsEnabled) { mBackupRestoreEventLogger.logItemsRestored(finalSettingsKey, /* count= */ 1); } } } Loading packages/SettingsProvider/src/com/android/providers/settings/SettingsHelper.java +14 −0 Original line number Diff line number Diff line Loading @@ -81,6 +81,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; Loading Loading @@ -206,6 +207,12 @@ public class SettingsHelper { table = sGlobalLookup; } // Get datatype for B&R metrics logging. String datatype = ""; if (Flags.enableMetricsSettingsBackupAgents()) { datatype = SettingsBackupRestoreKeys.getKeyFromUri(destination); } sendBroadcast = sBroadcastOnRestore.contains(name); sendBroadcastSystemUI = sBroadcastOnRestoreSystemUI.contains(name); sendBroadcastAccessibility = sBroadcastOnRestoreAccessibility.contains(name); Loading Loading @@ -292,12 +299,19 @@ public class SettingsHelper { contentValues.put(Settings.NameValueTable.NAME, name); contentValues.put(Settings.NameValueTable.VALUE, value); cr.insert(destination, contentValues); if (Flags.enableMetricsSettingsBackupAgents()) { mBackupRestoreEventLogger.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()) { mBackupRestoreEventLogger.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 Loading packages/SettingsProvider/test/src/com/android/providers/settings/SettingsBackupAgentTest.java +66 −108 Original line number Diff line number Diff line Loading @@ -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() Loading Loading @@ -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() { Loading Loading @@ -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 = Loading @@ -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); Loading @@ -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 Loading @@ -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 = Loading @@ -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 Loading @@ -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); Loading @@ -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(), Loading @@ -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); Loading @@ -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)); } Loading packages/SettingsProvider/test/src/com/android/providers/settings/SettingsHelperTest.java +114 −0 Original line number Diff line number Diff line Loading @@ -28,6 +28,9 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; import android.app.backup.BackupAnnotations.OperationType; import android.app.backup.BackupRestoreEventLogger; import android.app.backup.BackupRestoreEventLogger.DataTypeResult; import android.content.ContentProvider; import android.content.ContentValues; import android.content.Context; Loading @@ -42,6 +45,7 @@ import android.media.Utils; import android.net.Uri; import android.os.Bundle; import android.os.LocaleList; import android.platform.test.annotations.DisableFlags; import android.platform.test.annotations.EnableFlags; import android.platform.test.flag.junit.SetFlagsRule; import android.provider.BaseColumns; Loading @@ -65,6 +69,8 @@ import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoRule; /** * Tests for the SettingsHelperTest Loading @@ -91,6 +97,8 @@ public class SettingsHelperTest { @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); @Rule public MockitoRule mockitoRule = MockitoJUnit.rule(); @Mock private Context mContext; @Mock private Resources mResources; Loading @@ -100,6 +108,8 @@ public class SettingsHelperTest { @Mock private MockContentResolver mContentResolver; private MockSettingsProvider mSettingsProvider; private BackupRestoreEventLogger mBackupRestoreEventLogger; @Before public void setUp() { MockitoAnnotations.initMocks(this); Loading @@ -115,6 +125,8 @@ public class SettingsHelperTest { when(mContext.getContentResolver()).thenReturn(mContentResolver); mSettingsProvider = new MockSettingsProvider(mContext); mContentResolver.addProvider(Settings.AUTHORITY, mSettingsProvider); mBackupRestoreEventLogger = new BackupRestoreEventLogger(OperationType.RESTORE); mSettingsHelper.setBackupRestoreEventLogger(mBackupRestoreEventLogger); } @After Loading Loading @@ -791,6 +803,99 @@ public class SettingsHelperTest { assertThat(mSettingsHelper.getLocaleList()).isEqualTo(LOCALE_LIST); } @Test @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS) public void restoreValue_metricsFlagIsEnabled_restoresSetting_secureUri_logsSuccessWithSecureDatatype() { mSettingsHelper.restoreValue( mContext, mContentResolver, new ContentValues(), Settings.Secure.CONTENT_URI, SETTING_KEY, SETTING_VALUE, /* restoredFromSdkInt */ 0); DataTypeResult loggingResult = getLoggingResultForDatatype(SettingsBackupRestoreKeys.KEY_SECURE); assertThat(loggingResult).isNotNull(); assertThat(loggingResult.getSuccessCount()).isEqualTo(1); } @Test @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS) public void restoreValue_metricsFlagIsEnabled_restoresSetting_systemUri_logsSuccessWithSystemDatatype() { mSettingsHelper.restoreValue( mContext, mContentResolver, new ContentValues(), Settings.System.CONTENT_URI, SETTING_KEY, SETTING_VALUE, /* restoredFromSdkInt */ 0); DataTypeResult loggingResult = getLoggingResultForDatatype(SettingsBackupRestoreKeys.KEY_SYSTEM); assertThat(loggingResult).isNotNull(); assertThat(loggingResult.getSuccessCount()).isEqualTo(1); } @Test @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS) public void restoreValue_metricsFlagIsEnabled_restoresSetting_globalUri_logsSuccessWithGlobalDatatype() { mSettingsHelper.restoreValue( mContext, mContentResolver, new ContentValues(), Settings.Global.CONTENT_URI, SETTING_KEY, SETTING_VALUE, /* restoredFromSdkInt */ 0); DataTypeResult loggingResult = getLoggingResultForDatatype(SettingsBackupRestoreKeys.KEY_GLOBAL); assertThat(loggingResult).isNotNull(); assertThat(loggingResult.getSuccessCount()).isEqualTo(1); } @Test @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS) public void restoreValue_metricsFlagIsEnabled_doesNotRestoreSetting_logsFailure() { mSettingsHelper.restoreValue( mContext, mContentResolver, new ContentValues(), Uri.EMPTY, SETTING_KEY, SETTING_VALUE, /* restoredFromSdkInt */ 0); DataTypeResult loggingResult = getLoggingResultForDatatype(SettingsBackupRestoreKeys.KEY_UNKNOWN); assertThat(loggingResult).isNotNull(); assertThat(loggingResult.getFailCount()).isEqualTo(1); } @Test @DisableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS) public void restoreValue_metricsFlagIsDisabled_doesNotLogMetrics() { mSettingsHelper.restoreValue( mContext, mContentResolver, new ContentValues(), Uri.EMPTY, SETTING_KEY, SETTING_VALUE, /* restoredFromSdkInt */ 0); assertThat(getLoggingResultForDatatype(SettingsBackupRestoreKeys.KEY_UNKNOWN)).isNull(); } private int getAutoRotationSettingValue() { return Settings.System.getInt(mContentResolver, Settings.System.ACCELEROMETER_ROTATION, Loading Loading @@ -851,4 +956,13 @@ public class SettingsHelperTest { assertThat(Settings.System.getString(mContentResolver, settings)) .isEqualTo(testRingtoneSettingsValue); } private DataTypeResult getLoggingResultForDatatype(String dataType) { for (DataTypeResult result : mBackupRestoreEventLogger.getLoggingResults()) { if (result.getDataType().equals(dataType)) { return result; } } return null; } } Loading
packages/SettingsProvider/src/com/android/providers/settings/SettingsBackupAgent.java +0 −4 Original line number Diff line number Diff line Loading @@ -1053,14 +1053,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); Log.d(TAG, "Restored setting: " + destination + " : " + key + "=" + value); if (areAgentMetricsEnabled) { mBackupRestoreEventLogger.logItemsRestored(finalSettingsKey, /* count= */ 1); } } } Loading
packages/SettingsProvider/src/com/android/providers/settings/SettingsHelper.java +14 −0 Original line number Diff line number Diff line Loading @@ -81,6 +81,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; Loading Loading @@ -206,6 +207,12 @@ public class SettingsHelper { table = sGlobalLookup; } // Get datatype for B&R metrics logging. String datatype = ""; if (Flags.enableMetricsSettingsBackupAgents()) { datatype = SettingsBackupRestoreKeys.getKeyFromUri(destination); } sendBroadcast = sBroadcastOnRestore.contains(name); sendBroadcastSystemUI = sBroadcastOnRestoreSystemUI.contains(name); sendBroadcastAccessibility = sBroadcastOnRestoreAccessibility.contains(name); Loading Loading @@ -292,12 +299,19 @@ public class SettingsHelper { contentValues.put(Settings.NameValueTable.NAME, name); contentValues.put(Settings.NameValueTable.VALUE, value); cr.insert(destination, contentValues); if (Flags.enableMetricsSettingsBackupAgents()) { mBackupRestoreEventLogger.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()) { mBackupRestoreEventLogger.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 Loading
packages/SettingsProvider/test/src/com/android/providers/settings/SettingsBackupAgentTest.java +66 −108 Original line number Diff line number Diff line Loading @@ -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() Loading Loading @@ -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() { Loading Loading @@ -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 = Loading @@ -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); Loading @@ -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 Loading @@ -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 = Loading @@ -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 Loading @@ -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); Loading @@ -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(), Loading @@ -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); Loading @@ -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)); } Loading
packages/SettingsProvider/test/src/com/android/providers/settings/SettingsHelperTest.java +114 −0 Original line number Diff line number Diff line Loading @@ -28,6 +28,9 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; import android.app.backup.BackupAnnotations.OperationType; import android.app.backup.BackupRestoreEventLogger; import android.app.backup.BackupRestoreEventLogger.DataTypeResult; import android.content.ContentProvider; import android.content.ContentValues; import android.content.Context; Loading @@ -42,6 +45,7 @@ import android.media.Utils; import android.net.Uri; import android.os.Bundle; import android.os.LocaleList; import android.platform.test.annotations.DisableFlags; import android.platform.test.annotations.EnableFlags; import android.platform.test.flag.junit.SetFlagsRule; import android.provider.BaseColumns; Loading @@ -65,6 +69,8 @@ import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoRule; /** * Tests for the SettingsHelperTest Loading @@ -91,6 +97,8 @@ public class SettingsHelperTest { @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); @Rule public MockitoRule mockitoRule = MockitoJUnit.rule(); @Mock private Context mContext; @Mock private Resources mResources; Loading @@ -100,6 +108,8 @@ public class SettingsHelperTest { @Mock private MockContentResolver mContentResolver; private MockSettingsProvider mSettingsProvider; private BackupRestoreEventLogger mBackupRestoreEventLogger; @Before public void setUp() { MockitoAnnotations.initMocks(this); Loading @@ -115,6 +125,8 @@ public class SettingsHelperTest { when(mContext.getContentResolver()).thenReturn(mContentResolver); mSettingsProvider = new MockSettingsProvider(mContext); mContentResolver.addProvider(Settings.AUTHORITY, mSettingsProvider); mBackupRestoreEventLogger = new BackupRestoreEventLogger(OperationType.RESTORE); mSettingsHelper.setBackupRestoreEventLogger(mBackupRestoreEventLogger); } @After Loading Loading @@ -791,6 +803,99 @@ public class SettingsHelperTest { assertThat(mSettingsHelper.getLocaleList()).isEqualTo(LOCALE_LIST); } @Test @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS) public void restoreValue_metricsFlagIsEnabled_restoresSetting_secureUri_logsSuccessWithSecureDatatype() { mSettingsHelper.restoreValue( mContext, mContentResolver, new ContentValues(), Settings.Secure.CONTENT_URI, SETTING_KEY, SETTING_VALUE, /* restoredFromSdkInt */ 0); DataTypeResult loggingResult = getLoggingResultForDatatype(SettingsBackupRestoreKeys.KEY_SECURE); assertThat(loggingResult).isNotNull(); assertThat(loggingResult.getSuccessCount()).isEqualTo(1); } @Test @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS) public void restoreValue_metricsFlagIsEnabled_restoresSetting_systemUri_logsSuccessWithSystemDatatype() { mSettingsHelper.restoreValue( mContext, mContentResolver, new ContentValues(), Settings.System.CONTENT_URI, SETTING_KEY, SETTING_VALUE, /* restoredFromSdkInt */ 0); DataTypeResult loggingResult = getLoggingResultForDatatype(SettingsBackupRestoreKeys.KEY_SYSTEM); assertThat(loggingResult).isNotNull(); assertThat(loggingResult.getSuccessCount()).isEqualTo(1); } @Test @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS) public void restoreValue_metricsFlagIsEnabled_restoresSetting_globalUri_logsSuccessWithGlobalDatatype() { mSettingsHelper.restoreValue( mContext, mContentResolver, new ContentValues(), Settings.Global.CONTENT_URI, SETTING_KEY, SETTING_VALUE, /* restoredFromSdkInt */ 0); DataTypeResult loggingResult = getLoggingResultForDatatype(SettingsBackupRestoreKeys.KEY_GLOBAL); assertThat(loggingResult).isNotNull(); assertThat(loggingResult.getSuccessCount()).isEqualTo(1); } @Test @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS) public void restoreValue_metricsFlagIsEnabled_doesNotRestoreSetting_logsFailure() { mSettingsHelper.restoreValue( mContext, mContentResolver, new ContentValues(), Uri.EMPTY, SETTING_KEY, SETTING_VALUE, /* restoredFromSdkInt */ 0); DataTypeResult loggingResult = getLoggingResultForDatatype(SettingsBackupRestoreKeys.KEY_UNKNOWN); assertThat(loggingResult).isNotNull(); assertThat(loggingResult.getFailCount()).isEqualTo(1); } @Test @DisableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS) public void restoreValue_metricsFlagIsDisabled_doesNotLogMetrics() { mSettingsHelper.restoreValue( mContext, mContentResolver, new ContentValues(), Uri.EMPTY, SETTING_KEY, SETTING_VALUE, /* restoredFromSdkInt */ 0); assertThat(getLoggingResultForDatatype(SettingsBackupRestoreKeys.KEY_UNKNOWN)).isNull(); } private int getAutoRotationSettingValue() { return Settings.System.getInt(mContentResolver, Settings.System.ACCELEROMETER_ROTATION, Loading Loading @@ -851,4 +956,13 @@ public class SettingsHelperTest { assertThat(Settings.System.getString(mContentResolver, settings)) .isEqualTo(testRingtoneSettingsValue); } private DataTypeResult getLoggingResultForDatatype(String dataType) { for (DataTypeResult result : mBackupRestoreEventLogger.getLoggingResults()) { if (result.getDataType().equals(dataType)) { return result; } } return null; } }