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

Commit bb356384 authored by Daniel Perez's avatar Daniel Perez
Browse files

Move keys out of SettingsBackupAgent so that they can be reused.

With all the logging that I'm adding in SettingsHelper, I think it'd be useful to have these constants defined out of the agent so that they can be reused.

Bug: 379861078
Change-Id: Icbd0b926eba981c0492bf4e72e9e5cf6f40104b9
Flag: com.android.server.backup.enable_metrics_settings_backup_agents
Tested: SettingsBackupRestoreKeysTest
parent 81e545fa
Loading
Loading
Loading
Loading
+15 −19
Original line number Diff line number Diff line
@@ -16,6 +16,19 @@

package com.android.providers.settings;

import static com.android.providers.settings.SettingsBackupRestoreKeys.KEY_DEVICE_SPECIFIC_CONFIG;
import static com.android.providers.settings.SettingsBackupRestoreKeys.KEY_GLOBAL;
import static com.android.providers.settings.SettingsBackupRestoreKeys.KEY_LOCALE;
import static com.android.providers.settings.SettingsBackupRestoreKeys.KEY_LOCK_SETTINGS;
import static com.android.providers.settings.SettingsBackupRestoreKeys.KEY_NETWORK_POLICIES;
import static com.android.providers.settings.SettingsBackupRestoreKeys.KEY_SECURE;
import static com.android.providers.settings.SettingsBackupRestoreKeys.KEY_SIM_SPECIFIC_SETTINGS;
import static com.android.providers.settings.SettingsBackupRestoreKeys.KEY_SIM_SPECIFIC_SETTINGS_2;
import static com.android.providers.settings.SettingsBackupRestoreKeys.KEY_SOFTAP_CONFIG;
import static com.android.providers.settings.SettingsBackupRestoreKeys.KEY_SYSTEM;
import static com.android.providers.settings.SettingsBackupRestoreKeys.KEY_WIFI_NEW_CONFIG;
import static com.android.providers.settings.SettingsBackupRestoreKeys.KEY_WIFI_SETTINGS_BACKUP_DATA;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
@@ -99,22 +112,6 @@ public class SettingsBackupAgent extends BackupAgentHelper {
    private static final int NULL_SIZE = -1;
    private static final float FONT_SCALE_DEF_VALUE = 1.0f;

    private static final String KEY_SYSTEM = "system";
    private static final String KEY_SECURE = "secure";
    private static final String KEY_GLOBAL = "global";
    private static final String KEY_LOCALE = "locale";
    private static final String KEY_LOCK_SETTINGS = "lock_settings";
    private static final String KEY_SOFTAP_CONFIG = "softap_config";
    private static final String KEY_NETWORK_POLICIES = "network_policies";
    private static final String KEY_WIFI_NEW_CONFIG = "wifi_new_config";
    private static final String KEY_DEVICE_SPECIFIC_CONFIG = "device_specific_config";
    private static final String KEY_SIM_SPECIFIC_SETTINGS = "sim_specific_settings";
    // Restoring sim-specific data backed up from newer Android version to Android 12 was causing a
    // fatal crash. Creating a backup with a different key will prevent Android 12 versions from
    // restoring this data.
    private static final String KEY_SIM_SPECIFIC_SETTINGS_2 = "sim_specific_settings_2";
    private static final String KEY_WIFI_SETTINGS_BACKUP_DATA = "wifi_settings_backup_data";

    // Versioning of the state file.  Increment this version
    // number any time the set of state items is altered.
    private static final int STATE_VERSION = 9;
@@ -413,8 +410,7 @@ public class SettingsBackupAgent extends BackupAgentHelper {
                        .setLocaleData(
                            localeData,
                            size,
                            mBackupRestoreEventLogger,
                            KEY_LOCALE);
                            mBackupRestoreEventLogger);
                    break;

                case KEY_WIFI_CONFIG :
@@ -553,7 +549,7 @@ public class SettingsBackupAgent extends BackupAgentHelper {
            in.readFully(buffer, 0, nBytes);
            mSettingsHelper
                .setLocaleData(
                    buffer, nBytes, mBackupRestoreEventLogger, KEY_LOCALE);
                    buffer, nBytes, mBackupRestoreEventLogger);

            // Restore older backups performing the necessary migrations.
            if (version < FULL_BACKUP_ADDED_WIFI_NEW) {
+61 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.providers.settings;

import android.net.Uri;
import android.provider.Settings;

/**
 * Class to store the keys used for backup and restore.
 */
final class SettingsBackupRestoreKeys {
    static final String KEY_UNKNOWN = "unknown";
    static final String KEY_SYSTEM = "system";
    static final String KEY_SECURE = "secure";
    static final String KEY_GLOBAL = "global";
    static final String KEY_LOCALE = "locale";
    static final String KEY_LOCK_SETTINGS = "lock_settings";
    static final String KEY_SOFTAP_CONFIG = "softap_config";
    static final String KEY_NETWORK_POLICIES = "network_policies";
    static final String KEY_WIFI_NEW_CONFIG = "wifi_new_config";
    static final String KEY_DEVICE_SPECIFIC_CONFIG = "device_specific_config";
    static final String KEY_SIM_SPECIFIC_SETTINGS = "sim_specific_settings";
    // Restoring sim-specific data backed up from newer Android version to Android 12 was causing a
    // fatal crash. Creating a backup with a different key will prevent Android 12 versions from
    // restoring this data.
    static final String KEY_SIM_SPECIFIC_SETTINGS_2 = "sim_specific_settings_2";
    static final String KEY_WIFI_SETTINGS_BACKUP_DATA = "wifi_settings_backup_data";

    /**
     * Returns the key corresponding to the given URI.
     *
     * @param uri The URI of the setting's destination.
     * @return The key corresponding to the given URI, or KEY_UNKNOWN if the URI is not recognized.
     */
    static String getKeyFromUri(Uri uri) {
      if (uri.equals(Settings.Secure.CONTENT_URI)) {
        return KEY_SECURE;
      } else if (uri.equals(Settings.System.CONTENT_URI)) {
        return KEY_SYSTEM;
      } else if (uri.equals(Settings.Global.CONTENT_URI)) {
        return KEY_GLOBAL;
      } else {
        return KEY_UNKNOWN;
      }
    }

}
 No newline at end of file
+3 −4
Original line number Diff line number Diff line
@@ -742,10 +742,9 @@ public class SettingsHelper {
     * @param data the comma separated BCP-47 language tags in bytes.
     * @param size the size of the data in bytes.
     * @param backupRestoreEventLogger the logger to log the restore event.
     * @param dataType the data type of the setting for logging purposes.
     */
    /* package */ void setLocaleData(
        byte[] data, int size, BackupRestoreEventLogger backupRestoreEventLogger, String dataType) {
        byte[] data, int size, BackupRestoreEventLogger backupRestoreEventLogger) {
        final Configuration conf = mContext.getResources().getConfiguration();

        // Replace "_" with "-" to deal with older backups.
@@ -774,13 +773,13 @@ public class SettingsHelper {
                    mContext.getAttributionTag());
            if (Flags.enableMetricsSettingsBackupAgents()) {
                backupRestoreEventLogger
                    .logItemsRestored(dataType, localeList.size());
                    .logItemsRestored(SettingsBackupRestoreKeys.KEY_LOCALE, localeList.size());
            }
        } catch (RemoteException e) {
            if (Flags.enableMetricsSettingsBackupAgents()) {
                backupRestoreEventLogger
                    .logItemsRestoreFailed(
                        dataType,
                        SettingsBackupRestoreKeys.KEY_LOCALE,
                        localeList.size(),
                        ERROR_REMOTE_EXCEPTION_SETTING_LOCALE_DATA);
            }
+58 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.providers.settings;

import static com.google.common.truth.Truth.assertThat;

import android.net.Uri;
import android.provider.Settings;

import androidx.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Tests for {@link SettingsBackupRestoreKeys}.
 */
@RunWith(AndroidJUnit4.class)
public class SettingsBackupRestoreKeysTest {

    @Test
    public void getKeyFromUri_secureUri_returnsSecureKey() {
        assertThat(SettingsBackupRestoreKeys.getKeyFromUri(Settings.Secure.CONTENT_URI))
                .isEqualTo(SettingsBackupRestoreKeys.KEY_SECURE);
    }

    @Test
    public void getKeyFromUri_systemUri_returnsSystemKey() {
        assertThat(SettingsBackupRestoreKeys.getKeyFromUri(Settings.System.CONTENT_URI))
                .isEqualTo(SettingsBackupRestoreKeys.KEY_SYSTEM);
    }

    @Test
    public void getKeyFromUri_globalUri_returnsGlobalKey() {
        assertThat(SettingsBackupRestoreKeys.getKeyFromUri(Settings.Global.CONTENT_URI))
                .isEqualTo(SettingsBackupRestoreKeys.KEY_GLOBAL);
    }

    @Test
    public void getKeyFromUri_unknownUri_returnsUnknownKey() {
        assertThat(SettingsBackupRestoreKeys.getKeyFromUri(Uri.parse("content://unknown")))
                .isEqualTo(SettingsBackupRestoreKeys.KEY_UNKNOWN);
    }
}
 No newline at end of file