diff options
| author | 2017-07-28 19:37:15 -0700 | |
|---|---|---|
| committer | 2017-08-01 16:54:08 +0000 | |
| commit | aabc9638601f59eb2404265005e90d0820a408ba (patch) | |
| tree | 188604006ca8045ce65ab13b1de68b73a1656684 | |
| parent | 567fe364d43579f5d807b18facdcc4e2d2e896bf (diff) | |
Backup all backupable settings
We were backing up only settings that are backupable and not
null. However, for some settings null is a valid value meaning
nothing. For example, the currently selected autofill service.
Generally, if the setting is present in the settings database,
we need to backup whatever the value is there as it was set
by either the user or the system explicitly, otherwise the
setting would not be present in the database, hence no attempt
to back it up would be made.
Test: manual
bug:63631627
Change-Id: Iedffd7696fbcea9a2b211c9313b668ff3809a292
| -rw-r--r-- | packages/SettingsProvider/src/com/android/providers/settings/SettingsBackupAgent.java | 75 |
1 files changed, 49 insertions, 26 deletions
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsBackupAgent.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsBackupAgent.java index 96f51c16796f..3d0147dbd644 100644 --- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsBackupAgent.java +++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsBackupAgent.java @@ -34,6 +34,7 @@ import android.net.wifi.WifiManager; import android.os.ParcelFileDescriptor; import android.os.UserHandle; import android.provider.Settings; +import android.util.ArrayMap; import android.util.BackupUtils; import android.util.Log; @@ -62,6 +63,9 @@ public class SettingsBackupAgent extends BackupAgentHelper { private static final boolean DEBUG = false; private static final boolean DEBUG_BACKUP = DEBUG || false; + private static final byte[] NULL_VALUE = new byte[0]; + private static final int NULL_SIZE = -1; + private static final String KEY_SYSTEM = "system"; private static final String KEY_SECURE = "secure"; private static final String KEY_GLOBAL = "global"; @@ -608,7 +612,7 @@ public class SettingsBackupAgent extends BackupAgentHelper { // Restore only the white list data. int pos = 0; - Map<String, String> cachedEntries = new HashMap<String, String>(); + final ArrayMap<String, String> cachedEntries = new ArrayMap<>(); ContentValues contentValues = new ContentValues(2); SettingsHelper settingsHelper = mSettingsHelper; ContentResolver cr = getContentResolver(); @@ -616,28 +620,36 @@ public class SettingsBackupAgent extends BackupAgentHelper { final int whiteListSize = whitelist.length; for (int i = 0; i < whiteListSize; i++) { String key = whitelist[i]; - String value = cachedEntries.remove(key); - // If the value not cached, let us look it up. - if (value == null) { + String value = null; + boolean hasValueToRestore = false; + if (cachedEntries.indexOfKey(key) >= 0) { + value = cachedEntries.remove(key); + hasValueToRestore = true; + } else { + // If the value not cached, let us look it up. while (pos < bytes) { int length = readInt(settings, pos); pos += INTEGER_BYTE_COUNT; - String dataKey = length > 0 ? new String(settings, pos, length) : null; + String dataKey = length >= 0 ? new String(settings, pos, length) : null; pos += length; length = readInt(settings, pos); pos += INTEGER_BYTE_COUNT; - String dataValue = length > 0 ? new String(settings, pos, length) : null; - pos += length; + String dataValue = null; + if (length >= 0) { + dataValue = new String(settings, pos, length); + pos += length; + } if (key.equals(dataKey)) { value = dataValue; + hasValueToRestore = true; break; } cachedEntries.put(dataKey, dataValue); } } - if (value == null) { + if (!hasValueToRestore) { continue; } @@ -724,50 +736,56 @@ public class SettingsBackupAgent extends BackupAgentHelper { * @return The byte array of extracted values. */ private byte[] extractRelevantValues(Cursor cursor, String[] settings) { - final int settingsCount = settings.length; - byte[][] values = new byte[settingsCount * 2][]; // keys and values if (!cursor.moveToFirst()) { Log.e(TAG, "Couldn't read from the cursor"); return new byte[0]; } + final int nameColumnIndex = cursor.getColumnIndex(Settings.NameValueTable.NAME); + final int valueColumnIndex = cursor.getColumnIndex(Settings.NameValueTable.VALUE); + // Obtain the relevant data in a temporary array. int totalSize = 0; int backedUpSettingIndex = 0; - Map<String, String> cachedEntries = new HashMap<String, String>(); + final int settingsCount = settings.length; + final byte[][] values = new byte[settingsCount * 2][]; // keys and values + final ArrayMap<String, String> cachedEntries = new ArrayMap<>(); for (int i = 0; i < settingsCount; i++) { - String key = settings[i]; - String value = cachedEntries.remove(key); - - final int nameColumnIndex = cursor.getColumnIndex(Settings.NameValueTable.NAME); - final int valueColumnIndex = cursor.getColumnIndex(Settings.NameValueTable.VALUE); + final String key = settings[i]; // If the value not cached, let us look it up. - if (value == null) { + String value = null; + boolean hasValueToBackup = false; + if (cachedEntries.indexOfKey(key) >= 0) { + value = cachedEntries.remove(key); + hasValueToBackup = true; + } else { while (!cursor.isAfterLast()) { - String cursorKey = cursor.getString(nameColumnIndex); - String cursorValue = cursor.getString(valueColumnIndex); + final String cursorKey = cursor.getString(nameColumnIndex); + final String cursorValue = cursor.getString(valueColumnIndex); cursor.moveToNext(); if (key.equals(cursorKey)) { value = cursorValue; + hasValueToBackup = true; break; } cachedEntries.put(cursorKey, cursorValue); } } + if (!hasValueToBackup) { + continue; + } + // Intercept the keys and see if they need special handling value = mSettingsHelper.onBackupValue(key, value); - if (value == null) { - continue; - } // Write the key and value in the intermediary array. - byte[] keyBytes = key.getBytes(); + final byte[] keyBytes = key.getBytes(); totalSize += INTEGER_BYTE_COUNT + keyBytes.length; values[backedUpSettingIndex * 2] = keyBytes; - byte[] valueBytes = value.getBytes(); + final byte[] valueBytes = (value != null) ? value.getBytes() : NULL_VALUE; totalSize += INTEGER_BYTE_COUNT + valueBytes.length; values[backedUpSettingIndex * 2 + 1] = valueBytes; @@ -783,8 +801,13 @@ public class SettingsBackupAgent extends BackupAgentHelper { int pos = 0; final int keyValuePairCount = backedUpSettingIndex * 2; for (int i = 0; i < keyValuePairCount; i++) { - pos = writeInt(result, pos, values[i].length); - pos = writeBytes(result, pos, values[i]); + final byte[] value = values[i]; + if (value != NULL_VALUE) { + pos = writeInt(result, pos, value.length); + pos = writeBytes(result, pos, value); + } else { + pos = writeInt(result, pos, NULL_SIZE); + } } return result; } |