Merge "Special-case backup/restore of replaced settings" into rvc-dev am: c3a77a143c

Change-Id: I9fe0184c6e832499dd44d09dea1a4a43b99cc2f0
This commit is contained in:
Ruslan Tkhakokhov
2020-04-29 00:10:46 +00:00
committed by Automerger Merge Worker
3 changed files with 94 additions and 1 deletions

View File

@@ -34,6 +34,7 @@ android_test {
], ],
static_libs: [ static_libs: [
"androidx.test.rules", "androidx.test.rules",
"mockito-target-minus-junit4",
"SettingsLibDisplayDensityUtils", "SettingsLibDisplayDensityUtils",
"platform-test-annotations", "platform-test-annotations",
"truth-prebuilt", "truth-prebuilt",

View File

@@ -48,6 +48,8 @@ import java.util.Locale;
public class SettingsHelper { public class SettingsHelper {
private static final String TAG = "SettingsHelper"; private static final String TAG = "SettingsHelper";
private static final String SILENT_RINGTONE = "_silent"; private static final String SILENT_RINGTONE = "_silent";
private static final String SETTINGS_REPLACED_KEY = "backup_skip_user_facing_data";
private static final String SETTING_ORIGINAL_KEY_SUFFIX = "_original";
private static final float FLOAT_TOLERANCE = 0.01f; private static final float FLOAT_TOLERANCE = 0.01f;
private Context mContext; private Context mContext;
@@ -121,6 +123,10 @@ public class SettingsHelper {
*/ */
public void restoreValue(Context context, ContentResolver cr, ContentValues contentValues, 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) {
if (isReplacedSystemSetting(name)) {
return;
}
// Will we need a post-restore broadcast for this element? // Will we need a post-restore broadcast for this element?
String oldValue = null; String oldValue = null;
boolean sendBroadcast = false; boolean sendBroadcast = false;
@@ -203,7 +209,32 @@ public class SettingsHelper {
} }
} }
// Return the original value // Return the original value
return value; return isReplacedSystemSetting(name) ? getRealValueForSystemSetting(name) : value;
}
/**
* The setting value might have been replaced temporarily. If that's the case, return the real
* value instead of the temporary one.
*/
@VisibleForTesting
public String getRealValueForSystemSetting(String setting) {
return Settings.System.getString(mContext.getContentResolver(),
setting + SETTING_ORIGINAL_KEY_SUFFIX);
}
@VisibleForTesting
public boolean isReplacedSystemSetting(String setting) {
// This list should not be modified.
if (!Settings.System.MASTER_MONO.equals(setting)
&& !Settings.System.SCREEN_OFF_TIMEOUT.equals(setting)) {
return false;
}
// If this flag is set, values for the system settings from the list above have been
// temporarily replaced. We don't want to back up the temporary value or run restore for
// such settings.
// TODO(154822946): Remove this logic in the next release.
return Settings.Secure.getInt(mContext.getContentResolver(), SETTINGS_REPLACED_KEY,
/* def */ 0) != 0;
} }
/** /**

View File

@@ -18,18 +18,79 @@ package com.android.providers.settings;
import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.media.AudioManager;
import android.net.Uri;
import android.os.LocaleList; import android.os.LocaleList;
import android.telephony.TelephonyManager;
import androidx.test.runner.AndroidJUnit4; import androidx.test.runner.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
/** /**
* Tests for the SettingsHelperTest * Tests for the SettingsHelperTest
*/ */
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public class SettingsHelperTest { public class SettingsHelperTest {
private static final String SETTING_KEY = "setting_key";
private static final String SETTING_VALUE = "setting_value";
private static final String SETTING_REAL_VALUE = "setting_real_value";
private SettingsHelper mSettingsHelper;
@Mock private Context mContext;
@Mock private ContentResolver mContentResolver;
@Mock private AudioManager mAudioManager;
@Mock private TelephonyManager mTelephonyManager;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mContext.getSystemService(eq(Context.AUDIO_SERVICE))).thenReturn(mAudioManager);
when(mContext.getSystemService(eq(Context.TELEPHONY_SERVICE))).thenReturn(
mTelephonyManager);
mSettingsHelper = spy(new SettingsHelper(mContext));
}
@Test
public void testOnBackupValue_settingReplaced_returnsRealValue() {
when(mSettingsHelper.isReplacedSystemSetting(eq(SETTING_KEY))).thenReturn(true);
doReturn(SETTING_REAL_VALUE).when(mSettingsHelper).getRealValueForSystemSetting(
eq(SETTING_KEY));
assertEquals(SETTING_REAL_VALUE, mSettingsHelper.onBackupValue(SETTING_KEY, SETTING_VALUE));
}
@Test
public void testGetRealValue_settingNotReplaced_returnsSameValue() {
when(mSettingsHelper.isReplacedSystemSetting(eq(SETTING_KEY))).thenReturn(false);
assertEquals(SETTING_VALUE, mSettingsHelper.onBackupValue(SETTING_KEY, SETTING_VALUE));
}
@Test
public void testRestoreValue_settingReplaced_doesNotRestore() {
when(mSettingsHelper.isReplacedSystemSetting(eq(SETTING_KEY))).thenReturn(true);
mSettingsHelper.restoreValue(mContext, mContentResolver, new ContentValues(), Uri.EMPTY,
SETTING_KEY, SETTING_VALUE, /* restoredFromSdkInt */ 0);
verifyZeroInteractions(mContentResolver);
}
@Test @Test
public void testResolveLocales() throws Exception { public void testResolveLocales() throws Exception {
// Empty string from backup server // Empty string from backup server