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

Commit b52575c5 authored by Michal Karpinski's avatar Michal Karpinski
Browse files

Add restore of Settings keys to

SettingsBackupAgent.RESTORE_FROM_HIGHER_SDK_INT_SUPPORTED_KEYS

Now that we have validators in place for all settings that are
backed up.

Also, add a setting to override restoreAnyVersion, which can be
modified by Phenotype.

Bug: 64988620
Bug: 72162887
Test: manual (P->P - master without changes to master with changes)
Test: manual (Q->P = API29 -> API28)
Test: a lot of other scenarios covered by our end-to-end tests
Test: a GTS test with both settings that don't have validators
      and have values that wouldn't pass validation will be added
      in vendor/xts
Change-Id: Ifaf94306984b5204c79648d48fd4056ad403196e
parent b5253b65
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -11174,6 +11174,15 @@ public final class Settings {
        public static final String LOCATION_GLOBAL_KILL_SWITCH =
                "location_global_kill_switch";

        /**
         * If set to 1, SettingsProvider's restoreAnyVersion="true" attribute will be ignored
         * and restoring to lower version of platform API will be skipped.
         *
         * @hide
         */
        public static final String OVERRIDE_SETTINGS_PROVIDER_RESTORE_ANY_VERSION =
                "override_settings_provider_restore_any_version";

        /**
         * Settings to backup. This is here so that it's in the same place as the settings
         * keys and easy to update.
+2 −1
Original line number Diff line number Diff line
@@ -426,7 +426,8 @@ public class SettingsBackupTest {
                    Settings.Global.ZEN_MODE,
                    Settings.Global.ZEN_MODE_CONFIG_ETAG,
                    Settings.Global.ZEN_MODE_RINGER_LEVEL,
                    Settings.Global.ZRAM_ENABLED);
                    Settings.Global.ZRAM_ENABLED,
                    Settings.Global.OVERRIDE_SETTINGS_PROVIDER_RESTORE_ANY_VERSION);

    private static final Set<String> BACKUP_BLACKLISTED_SECURE_SETTINGS =
             newHashSet(
+33 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.os.UserHandle;
import android.provider.Settings;
import android.provider.SettingsValidators.Validator;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.BackupUtils;
@@ -155,6 +156,9 @@ public class SettingsBackupAgent extends BackupAgentHelper {
            new ArraySet<String>(Arrays.asList(new String[] {
                KEY_NETWORK_POLICIES,
                KEY_WIFI_NEW_CONFIG,
                KEY_SYSTEM,
                KEY_SECURE,
                KEY_GLOBAL,
            }));

    private SettingsHelper mSettingsHelper;
@@ -223,6 +227,15 @@ public class SettingsBackupAgent extends BackupAgentHelper {
            Log.d(TAG, "onRestore(): appVersionCode: " + appVersionCode
                    + "; Build.VERSION.SDK_INT: " + Build.VERSION.SDK_INT);
        }

        boolean overrideRestoreAnyVersion = Settings.Global.getInt(getContentResolver(),
                Settings.Global.OVERRIDE_SETTINGS_PROVIDER_RESTORE_ANY_VERSION, 0) == 1;
        if ((appVersionCode > Build.VERSION.SDK_INT) && overrideRestoreAnyVersion) {
            Log.w(TAG, "Ignoring restore from API" + appVersionCode + " to API"
                    + Build.VERSION.SDK_INT + " due to settings flag override.");
            return;
        }

        // versionCode of com.android.providers.settings corresponds to SDK_INT
        mRestoredFromSdkInt = appVersionCode;

@@ -571,15 +584,19 @@ public class SettingsBackupAgent extends BackupAgentHelper {
        // Figure out the white list and redirects to the global table.  We restore anything
        // in either the backup whitelist or the legacy-restore whitelist for this table.
        final String[] whitelist;
        Map<String, Validator> validators = null;
        if (contentUri.equals(Settings.Secure.CONTENT_URI)) {
            whitelist = concat(Settings.Secure.SETTINGS_TO_BACKUP,
                    Settings.Secure.LEGACY_RESTORE_SETTINGS);
            validators = Settings.Secure.VALIDATORS;
        } else if (contentUri.equals(Settings.System.CONTENT_URI)) {
            whitelist = concat(Settings.System.SETTINGS_TO_BACKUP,
                    Settings.System.LEGACY_RESTORE_SETTINGS);
            validators = Settings.System.VALIDATORS;
        } else if (contentUri.equals(Settings.Global.CONTENT_URI)) {
            whitelist = concat(Settings.Global.SETTINGS_TO_BACKUP,
                    Settings.Global.LEGACY_RESTORE_SETTINGS);
            validators = Settings.Global.VALIDATORS;
        } else {
            throw new IllegalArgumentException("Unknown URI: " + contentUri);
        }
@@ -627,6 +644,13 @@ public class SettingsBackupAgent extends BackupAgentHelper {
                continue;
            }

            // only restore the settings that have valid values
            if (!isValidSettingValue(key, value, validators)) {
                Log.w(TAG, "Attempted restore of " + key + " setting, but its value didn't pass"
                        + " validation, value: " + value);
                continue;
            }

            final Uri destination = (movedToGlobal != null && movedToGlobal.contains(key))
                    ? Settings.Global.CONTENT_URI
                    : contentUri;
@@ -639,6 +663,15 @@ public class SettingsBackupAgent extends BackupAgentHelper {
        }
    }

    private boolean isValidSettingValue(String key, String value,
            Map<String, Validator> validators) {
        if (key == null || validators == null) {
            return false;
        }
        Validator validator = validators.get(key);
        return (validator != null) && validator.validate(value);
    }

    private final String[] concat(String[] first, @Nullable String[] second) {
        if (second == null || second.length == 0) {
            return first;