Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.providers.settings; |
| 18 | |
Bryan Mawhinney | e483b56 | 2017-05-15 14:46:05 +0100 | [diff] [blame] | 19 | import android.annotation.UserIdInt; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 20 | import android.app.backup.BackupAgentHelper; |
| 21 | import android.app.backup.BackupDataInput; |
| 22 | import android.app.backup.BackupDataOutput; |
| 23 | import android.app.backup.FullBackupDataOutput; |
Christopher Tate | 87bc7e7 | 2013-06-10 16:55:02 -0700 | [diff] [blame] | 24 | import android.content.ContentResolver; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 25 | import android.content.ContentValues; |
| 26 | import android.content.Context; |
| 27 | import android.database.Cursor; |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 28 | import android.net.NetworkPolicy; |
| 29 | import android.net.NetworkPolicyManager; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 30 | import android.net.Uri; |
Christopher Tate | cab915a | 2015-01-08 12:50:00 -0800 | [diff] [blame] | 31 | import android.net.wifi.WifiConfiguration; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 32 | import android.net.wifi.WifiManager; |
Michal Karpinski | 6517d88 | 2018-01-08 15:23:03 +0000 | [diff] [blame] | 33 | import android.os.Build; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 34 | import android.os.ParcelFileDescriptor; |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 35 | import android.os.UserHandle; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 36 | import android.provider.Settings; |
Al Sutton | 66a3e01 | 2019-10-15 15:34:13 +0100 | [diff] [blame] | 37 | import android.provider.settings.backup.DeviceSpecificSettings; |
Al Sutton | 0833b2e | 2019-08-22 16:21:30 +0100 | [diff] [blame] | 38 | import android.provider.settings.backup.GlobalSettings; |
| 39 | import android.provider.settings.backup.SecureSettings; |
| 40 | import android.provider.settings.backup.SystemSettings; |
Al Sutton | 91f89d0 | 2019-08-16 12:56:57 +0100 | [diff] [blame] | 41 | import android.provider.settings.validators.GlobalSettingsValidators; |
| 42 | import android.provider.settings.validators.SecureSettingsValidators; |
| 43 | import android.provider.settings.validators.SystemSettingsValidators; |
Al Sutton | b0067fb | 2019-08-16 10:34:46 +0100 | [diff] [blame] | 44 | import android.provider.settings.validators.Validator; |
Svet Ganov | aabc963 | 2017-07-28 19:37:15 -0700 | [diff] [blame] | 45 | import android.util.ArrayMap; |
Michal Karpinski | 6517d88 | 2018-01-08 15:23:03 +0000 | [diff] [blame] | 46 | import android.util.ArraySet; |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 47 | import android.util.BackupUtils; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 48 | import android.util.Log; |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 49 | import android.util.Slog; |
| 50 | import android.view.Display; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 51 | |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 52 | import com.android.internal.annotations.VisibleForTesting; |
Al Sutton | 81be155 | 2019-07-17 11:19:13 +0100 | [diff] [blame] | 53 | import com.android.internal.util.ArrayUtils; |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 54 | import com.android.internal.widget.LockPatternUtils; |
Al Sutton | e5e7916 | 2019-08-19 13:42:16 +0100 | [diff] [blame] | 55 | import com.android.settingslib.display.DisplayDensityConfiguration; |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 56 | |
Brad Fitzpatrick | 7078789 | 2010-11-17 11:31:12 -0800 | [diff] [blame] | 57 | import java.io.BufferedOutputStream; |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 58 | import java.io.ByteArrayInputStream; |
Christopher Tate | cab915a | 2015-01-08 12:50:00 -0800 | [diff] [blame] | 59 | import java.io.ByteArrayOutputStream; |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 60 | import java.io.DataInputStream; |
| 61 | import java.io.DataOutputStream; |
Amith Yamasani | 2cfab84 | 2009-09-09 18:27:31 -0700 | [diff] [blame] | 62 | import java.io.EOFException; |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 63 | import java.io.FileInputStream; |
| 64 | import java.io.FileOutputStream; |
| 65 | import java.io.IOException; |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 66 | import java.io.OutputStream; |
Annie Meng | 47f5c9c | 2018-02-27 14:48:21 +0000 | [diff] [blame] | 67 | import java.time.DateTimeException; |
Michal Karpinski | 6517d88 | 2018-01-08 15:23:03 +0000 | [diff] [blame] | 68 | import java.util.Arrays; |
Christopher Tate | 8dfe2b9 | 2012-05-15 15:05:04 -0700 | [diff] [blame] | 69 | import java.util.HashSet; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 70 | import java.util.Map; |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 71 | import java.util.Objects; |
Ruslan Tkhakokhov | b8bf45f | 2019-07-24 21:13:35 +0100 | [diff] [blame] | 72 | import java.util.Set; |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 73 | import java.util.concurrent.atomic.AtomicInteger; |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 74 | import java.util.zip.CRC32; |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 75 | |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 76 | /** |
| 77 | * Performs backup and restore of the System and Secure settings. |
| 78 | * List of settings that are backed up are stored in the Settings.java file |
| 79 | */ |
Christopher Tate | cc84c69 | 2010-03-29 14:54:02 -0700 | [diff] [blame] | 80 | public class SettingsBackupAgent extends BackupAgentHelper { |
Christopher Tate | 436344a | 2009-09-30 16:17:37 -0700 | [diff] [blame] | 81 | private static final boolean DEBUG = false; |
Christopher Tate | 8dfe2b9 | 2012-05-15 15:05:04 -0700 | [diff] [blame] | 82 | private static final boolean DEBUG_BACKUP = DEBUG || false; |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 83 | |
Svet Ganov | aabc963 | 2017-07-28 19:37:15 -0700 | [diff] [blame] | 84 | private static final byte[] NULL_VALUE = new byte[0]; |
| 85 | private static final int NULL_SIZE = -1; |
| 86 | |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 87 | private static final String KEY_SYSTEM = "system"; |
| 88 | private static final String KEY_SECURE = "secure"; |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 89 | private static final String KEY_GLOBAL = "global"; |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 90 | private static final String KEY_LOCALE = "locale"; |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 91 | private static final String KEY_LOCK_SETTINGS = "lock_settings"; |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 92 | private static final String KEY_SOFTAP_CONFIG = "softap_config"; |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 93 | private static final String KEY_NETWORK_POLICIES = "network_policies"; |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 94 | private static final String KEY_WIFI_NEW_CONFIG = "wifi_new_config"; |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 95 | private static final String KEY_DEVICE_SPECIFIC_CONFIG = "device_specific_config"; |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 96 | |
Christopher Tate | a286f41 | 2009-09-18 15:51:15 -0700 | [diff] [blame] | 97 | // Versioning of the state file. Increment this version |
| 98 | // number any time the set of state items is altered. |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 99 | private static final int STATE_VERSION = 8; |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 100 | |
| 101 | // Versioning of the Network Policies backup payload. |
| 102 | private static final int NETWORK_POLICIES_BACKUP_VERSION = 1; |
| 103 | |
Christopher Tate | a286f41 | 2009-09-18 15:51:15 -0700 | [diff] [blame] | 104 | |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 105 | // Slots in the checksum array. Never insert new items in the middle |
| 106 | // of this array; new slots must be appended. |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 107 | private static final int STATE_SYSTEM = 0; |
| 108 | private static final int STATE_SECURE = 1; |
| 109 | private static final int STATE_LOCALE = 2; |
| 110 | private static final int STATE_WIFI_SUPPLICANT = 3; |
| 111 | private static final int STATE_WIFI_CONFIG = 4; |
| 112 | private static final int STATE_GLOBAL = 5; |
| 113 | private static final int STATE_LOCK_SETTINGS = 6; |
| 114 | private static final int STATE_SOFTAP_CONFIG = 7; |
| 115 | private static final int STATE_NETWORK_POLICIES = 8; |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 116 | private static final int STATE_WIFI_NEW_CONFIG = 9; |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 117 | private static final int STATE_DEVICE_CONFIG = 10; |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 118 | |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 119 | private static final int STATE_SIZE = 11; // The current number of state items |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 120 | |
| 121 | // Number of entries in the checksum array at various version numbers |
| 122 | private static final int STATE_SIZES[] = { |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 123 | 0, |
| 124 | 4, // version 1 |
| 125 | 5, // version 2 added STATE_WIFI_CONFIG |
| 126 | 6, // version 3 added STATE_GLOBAL |
| 127 | 7, // version 4 added STATE_LOCK_SETTINGS |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 128 | 8, // version 5 added STATE_SOFTAP_CONFIG |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 129 | 9, // version 6 added STATE_NETWORK_POLICIES |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 130 | 10, // version 7 added STATE_WIFI_NEW_CONFIG |
| 131 | STATE_SIZE // version 8 added STATE_DEVICE_CONFIG |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 132 | }; |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 133 | |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 134 | private static final int FULL_BACKUP_ADDED_GLOBAL = 2; // added the "global" entry |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 135 | private static final int FULL_BACKUP_ADDED_LOCK_SETTINGS = 3; // added the "lock_settings" entry |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 136 | private static final int FULL_BACKUP_ADDED_SOFTAP_CONF = 4; //added the "softap_config" entry |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 137 | private static final int FULL_BACKUP_ADDED_NETWORK_POLICIES = 5; //added "network_policies" |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 138 | private static final int FULL_BACKUP_ADDED_WIFI_NEW = 6; // added "wifi_new_config" entry |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 139 | private static final int FULL_BACKUP_ADDED_DEVICE_SPECIFIC = 7; // added "device specific" entry |
| 140 | // Versioning of the 'full backup' format |
| 141 | // Increment this version any time a new item is added |
| 142 | private static final int FULL_BACKUP_VERSION = FULL_BACKUP_ADDED_DEVICE_SPECIFIC; |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 143 | |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 144 | private static final int INTEGER_BYTE_COUNT = Integer.SIZE / Byte.SIZE; |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 145 | |
| 146 | private static final byte[] EMPTY_DATA = new byte[0]; |
| 147 | |
| 148 | private static final String TAG = "SettingsBackupAgent"; |
| 149 | |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 150 | @VisibleForTesting |
| 151 | static final String[] PROJECTION = { |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 152 | Settings.NameValueTable.NAME, |
| 153 | Settings.NameValueTable.VALUE |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 156 | // Versioning of the 'device specific' section of a backup |
| 157 | // Increment this any time the format is changed or data added. |
| 158 | @VisibleForTesting |
| 159 | static final int DEVICE_SPECIFIC_VERSION = 1; |
| 160 | |
Christian Sonntag | 92c1752 | 2009-08-07 15:16:17 -0700 | [diff] [blame] | 161 | // the key to store the WIFI data under, should be sorted as last, so restore happens last. |
| 162 | // use very late unicode character to quasi-guarantee last sort position. |
Amith Yamasani | 2cfab84 | 2009-09-09 18:27:31 -0700 | [diff] [blame] | 163 | private static final String KEY_WIFI_SUPPLICANT = "\uffedWIFI"; |
Irfan Sheriff | 4aeca7c5 | 2011-03-10 16:53:33 -0800 | [diff] [blame] | 164 | private static final String KEY_WIFI_CONFIG = "\uffedCONFIG_WIFI"; |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 165 | |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 166 | // Keys within the lock settings section |
| 167 | private static final String KEY_LOCK_SETTINGS_OWNER_INFO_ENABLED = "owner_info_enabled"; |
| 168 | private static final String KEY_LOCK_SETTINGS_OWNER_INFO = "owner_info"; |
Bryan Mawhinney | e483b56 | 2017-05-15 14:46:05 +0100 | [diff] [blame] | 169 | private static final String KEY_LOCK_SETTINGS_VISIBLE_PATTERN_ENABLED = |
| 170 | "visible_pattern_enabled"; |
| 171 | private static final String KEY_LOCK_SETTINGS_POWER_BUTTON_INSTANTLY_LOCKS = |
| 172 | "power_button_instantly_locks"; |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 173 | |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 174 | // Name of the temporary file we use during full backup/restore. This is |
| 175 | // stored in the full-backup tarfile as well, so should not be changed. |
| 176 | private static final String STAGE_FILE = "flattened-data"; |
| 177 | |
Michal Karpinski | 6517d88 | 2018-01-08 15:23:03 +0000 | [diff] [blame] | 178 | // List of keys that support restore to lower version of the SDK, introduced in Android P |
| 179 | private static final ArraySet<String> RESTORE_FROM_HIGHER_SDK_INT_SUPPORTED_KEYS = |
| 180 | new ArraySet<String>(Arrays.asList(new String[] { |
| 181 | KEY_NETWORK_POLICIES, |
| 182 | KEY_WIFI_NEW_CONFIG, |
Michal Karpinski | b52575c | 2018-01-19 17:38:45 +0000 | [diff] [blame] | 183 | KEY_SYSTEM, |
| 184 | KEY_SECURE, |
| 185 | KEY_GLOBAL, |
Michal Karpinski | 6517d88 | 2018-01-08 15:23:03 +0000 | [diff] [blame] | 186 | })); |
| 187 | |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 188 | @VisibleForTesting |
| 189 | SettingsHelper mSettingsHelper; |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 190 | |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 191 | private WifiManager mWifiManager; |
Christopher Tate | 8dfe2b9 | 2012-05-15 15:05:04 -0700 | [diff] [blame] | 192 | |
Michal Karpinski | 6135a26 | 2017-08-11 10:45:58 +0100 | [diff] [blame] | 193 | // Version of the SDK that com.android.providers.settings package has been restored from. |
| 194 | // Populated in onRestore(). |
| 195 | private int mRestoredFromSdkInt; |
| 196 | |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 197 | @Override |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 198 | public void onCreate() { |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 199 | if (DEBUG_BACKUP) Log.d(TAG, "onCreate() invoked"); |
| 200 | |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 201 | mSettingsHelper = new SettingsHelper(this); |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 202 | mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 203 | super.onCreate(); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, |
Ritesh Reddy | 33117fe | 2016-02-01 13:45:33 +0000 | [diff] [blame] | 208 | ParcelFileDescriptor newState) throws IOException { |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 209 | |
| 210 | byte[] systemSettingsData = getSystemSettings(); |
| 211 | byte[] secureSettingsData = getSecureSettings(); |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 212 | byte[] globalSettingsData = getGlobalSettings(); |
Bryan Mawhinney | e483b56 | 2017-05-15 14:46:05 +0100 | [diff] [blame] | 213 | byte[] lockSettingsData = getLockSettings(UserHandle.myUserId()); |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 214 | byte[] locale = mSettingsHelper.getLocaleData(); |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 215 | byte[] softApConfigData = getSoftAPConfiguration(); |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 216 | byte[] netPoliciesData = getNetworkPolicies(); |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 217 | byte[] wifiFullConfigData = getNewWifiConfigData(); |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 218 | byte[] deviceSpecificInformation = getDeviceSpecificConfiguration(); |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 219 | |
Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 220 | long[] stateChecksums = readOldChecksums(oldState); |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 221 | |
Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 222 | stateChecksums[STATE_SYSTEM] = |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 223 | writeIfChanged(stateChecksums[STATE_SYSTEM], KEY_SYSTEM, systemSettingsData, data); |
Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 224 | stateChecksums[STATE_SECURE] = |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 225 | writeIfChanged(stateChecksums[STATE_SECURE], KEY_SECURE, secureSettingsData, data); |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 226 | stateChecksums[STATE_GLOBAL] = |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 227 | writeIfChanged(stateChecksums[STATE_GLOBAL], KEY_GLOBAL, globalSettingsData, data); |
Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 228 | stateChecksums[STATE_LOCALE] = |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 229 | writeIfChanged(stateChecksums[STATE_LOCALE], KEY_LOCALE, locale, data); |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 230 | stateChecksums[STATE_WIFI_SUPPLICANT] = 0; |
| 231 | stateChecksums[STATE_WIFI_CONFIG] = 0; |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 232 | stateChecksums[STATE_LOCK_SETTINGS] = |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 233 | writeIfChanged(stateChecksums[STATE_LOCK_SETTINGS], KEY_LOCK_SETTINGS, |
| 234 | lockSettingsData, data); |
| 235 | stateChecksums[STATE_SOFTAP_CONFIG] = |
| 236 | writeIfChanged(stateChecksums[STATE_SOFTAP_CONFIG], KEY_SOFTAP_CONFIG, |
| 237 | softApConfigData, data); |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 238 | stateChecksums[STATE_NETWORK_POLICIES] = |
| 239 | writeIfChanged(stateChecksums[STATE_NETWORK_POLICIES], KEY_NETWORK_POLICIES, |
| 240 | netPoliciesData, data); |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 241 | stateChecksums[STATE_WIFI_NEW_CONFIG] = |
| 242 | writeIfChanged(stateChecksums[STATE_WIFI_NEW_CONFIG], KEY_WIFI_NEW_CONFIG, |
| 243 | wifiFullConfigData, data); |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 244 | stateChecksums[STATE_DEVICE_CONFIG] = |
| 245 | writeIfChanged(stateChecksums[STATE_DEVICE_CONFIG], KEY_DEVICE_SPECIFIC_CONFIG, |
| 246 | deviceSpecificInformation, data); |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 247 | |
Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 248 | writeNewChecksums(stateChecksums, newState); |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public void onRestore(BackupDataInput data, int appVersionCode, |
Ritesh Reddy | 33117fe | 2016-02-01 13:45:33 +0000 | [diff] [blame] | 253 | ParcelFileDescriptor newState) throws IOException { |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 254 | |
Michal Karpinski | 6517d88 | 2018-01-08 15:23:03 +0000 | [diff] [blame] | 255 | if (DEBUG) { |
| 256 | Log.d(TAG, "onRestore(): appVersionCode: " + appVersionCode |
| 257 | + "; Build.VERSION.SDK_INT: " + Build.VERSION.SDK_INT); |
| 258 | } |
Michal Karpinski | b52575c | 2018-01-19 17:38:45 +0000 | [diff] [blame] | 259 | |
| 260 | boolean overrideRestoreAnyVersion = Settings.Global.getInt(getContentResolver(), |
| 261 | Settings.Global.OVERRIDE_SETTINGS_PROVIDER_RESTORE_ANY_VERSION, 0) == 1; |
| 262 | if ((appVersionCode > Build.VERSION.SDK_INT) && overrideRestoreAnyVersion) { |
| 263 | Log.w(TAG, "Ignoring restore from API" + appVersionCode + " to API" |
| 264 | + Build.VERSION.SDK_INT + " due to settings flag override."); |
| 265 | return; |
| 266 | } |
| 267 | |
Michal Karpinski | 6135a26 | 2017-08-11 10:45:58 +0100 | [diff] [blame] | 268 | // versionCode of com.android.providers.settings corresponds to SDK_INT |
| 269 | mRestoredFromSdkInt = appVersionCode; |
| 270 | |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 271 | HashSet<String> movedToGlobal = new HashSet<String>(); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 272 | Settings.System.getMovedToGlobalSettings(movedToGlobal); |
| 273 | Settings.Secure.getMovedToGlobalSettings(movedToGlobal); |
Ruslan Tkhakokhov | b8bf45f | 2019-07-24 21:13:35 +0100 | [diff] [blame] | 274 | Set<String> movedToSecure = getMovedToSecureSettings(); |
| 275 | |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 276 | byte[] restoredWifiSupplicantData = null; |
| 277 | byte[] restoredWifiIpConfigData = null; |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 278 | |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 279 | while (data.readNextHeader()) { |
| 280 | final String key = data.getKey(); |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 281 | final int size = data.getDataSize(); |
Michal Karpinski | 6517d88 | 2018-01-08 15:23:03 +0000 | [diff] [blame] | 282 | |
| 283 | // bail out of restoring from higher SDK_INT version for unsupported keys |
| 284 | if (appVersionCode > Build.VERSION.SDK_INT |
| 285 | && !RESTORE_FROM_HIGHER_SDK_INT_SUPPORTED_KEYS.contains(key)) { |
| 286 | Log.w(TAG, "Not restoring unrecognized key '" |
| 287 | + key + "' from future version " + appVersionCode); |
Anton Philippov | 54c6d6c | 2018-04-10 22:52:28 +0100 | [diff] [blame] | 288 | data.skipEntityData(); |
Michal Karpinski | 6517d88 | 2018-01-08 15:23:03 +0000 | [diff] [blame] | 289 | continue; |
| 290 | } |
| 291 | |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 292 | switch (key) { |
| 293 | case KEY_SYSTEM : |
Ruslan Tkhakokhov | b8bf45f | 2019-07-24 21:13:35 +0100 | [diff] [blame] | 294 | restoreSettings(data, Settings.System.CONTENT_URI, movedToGlobal, |
| 295 | movedToSecure); |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 296 | mSettingsHelper.applyAudioSettings(); |
| 297 | break; |
| 298 | |
| 299 | case KEY_SECURE : |
Ruslan Tkhakokhov | b8bf45f | 2019-07-24 21:13:35 +0100 | [diff] [blame] | 300 | restoreSettings(data, Settings.Secure.CONTENT_URI, movedToGlobal, null); |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 301 | break; |
| 302 | |
| 303 | case KEY_GLOBAL : |
Ruslan Tkhakokhov | b8bf45f | 2019-07-24 21:13:35 +0100 | [diff] [blame] | 304 | restoreSettings(data, Settings.Global.CONTENT_URI, null, movedToSecure); |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 305 | break; |
| 306 | |
| 307 | case KEY_WIFI_SUPPLICANT : |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 308 | restoredWifiSupplicantData = new byte[size]; |
| 309 | data.readEntityData(restoredWifiSupplicantData, 0, size); |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 310 | break; |
| 311 | |
| 312 | case KEY_LOCALE : |
| 313 | byte[] localeData = new byte[size]; |
| 314 | data.readEntityData(localeData, 0, size); |
| 315 | mSettingsHelper.setLocaleData(localeData, size); |
| 316 | break; |
| 317 | |
| 318 | case KEY_WIFI_CONFIG : |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 319 | restoredWifiIpConfigData = new byte[size]; |
| 320 | data.readEntityData(restoredWifiIpConfigData, 0, size); |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 321 | break; |
| 322 | |
| 323 | case KEY_LOCK_SETTINGS : |
Bryan Mawhinney | e483b56 | 2017-05-15 14:46:05 +0100 | [diff] [blame] | 324 | restoreLockSettings(UserHandle.myUserId(), data); |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 325 | break; |
| 326 | |
| 327 | case KEY_SOFTAP_CONFIG : |
| 328 | byte[] softapData = new byte[size]; |
| 329 | data.readEntityData(softapData, 0, size); |
| 330 | restoreSoftApConfiguration(softapData); |
| 331 | break; |
| 332 | |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 333 | case KEY_NETWORK_POLICIES: |
| 334 | byte[] netPoliciesData = new byte[size]; |
| 335 | data.readEntityData(netPoliciesData, 0, size); |
| 336 | restoreNetworkPolicies(netPoliciesData); |
| 337 | break; |
| 338 | |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 339 | case KEY_WIFI_NEW_CONFIG: |
| 340 | byte[] restoredWifiNewConfigData = new byte[size]; |
| 341 | data.readEntityData(restoredWifiNewConfigData, 0, size); |
Roshan Pius | 5db739c | 2016-06-17 16:51:36 -0700 | [diff] [blame] | 342 | restoreNewWifiConfigData(restoredWifiNewConfigData); |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 343 | break; |
| 344 | |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 345 | case KEY_DEVICE_SPECIFIC_CONFIG: |
| 346 | byte[] restoredDeviceSpecificConfig = new byte[size]; |
| 347 | data.readEntityData(restoredDeviceSpecificConfig, 0, size); |
| 348 | restoreDeviceSpecificConfig(restoredDeviceSpecificConfig); |
| 349 | break; |
| 350 | |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 351 | default : |
| 352 | data.skipEntityData(); |
| 353 | |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 354 | } |
| 355 | } |
Christopher Tate | d488bc0 | 2012-10-09 14:06:30 -0700 | [diff] [blame] | 356 | |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 357 | // Do this at the end so that we also pull in the ipconfig data. |
| 358 | if (restoredWifiSupplicantData != null) { |
| 359 | restoreSupplicantWifiConfigData( |
Roshan Pius | 5db739c | 2016-06-17 16:51:36 -0700 | [diff] [blame] | 360 | restoredWifiSupplicantData, restoredWifiIpConfigData); |
Christopher Tate | d488bc0 | 2012-10-09 14:06:30 -0700 | [diff] [blame] | 361 | } |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 364 | @Override |
Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 365 | public void onFullBackup(FullBackupDataOutput data) throws IOException { |
Michal Karpinski | fbcb2458 | 2018-01-09 17:06:19 +0000 | [diff] [blame] | 366 | // Full backup of SettingsBackupAgent support was removed in Android P. If you want to adb |
| 367 | // backup com.android.providers.settings package use \"-keyvalue\" flag. |
| 368 | // Full restore of SettingsBackupAgent is still available for backwards compatibility. |
Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | @Override |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 372 | public void onRestoreFile(ParcelFileDescriptor data, long size, |
Ritesh Reddy | 33117fe | 2016-02-01 13:45:33 +0000 | [diff] [blame] | 373 | int type, String domain, String relpath, long mode, long mtime) |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 374 | throws IOException { |
| 375 | if (DEBUG_BACKUP) Log.d(TAG, "onRestoreFile() invoked"); |
| 376 | // Our data is actually a blob of flattened settings data identical to that |
| 377 | // produced during incremental backups. Just unpack and apply it all in |
| 378 | // turn. |
| 379 | FileInputStream instream = new FileInputStream(data.getFileDescriptor()); |
| 380 | DataInputStream in = new DataInputStream(instream); |
| 381 | |
| 382 | int version = in.readInt(); |
| 383 | if (DEBUG_BACKUP) Log.d(TAG, "Flattened data version " + version); |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 384 | if (version <= FULL_BACKUP_VERSION) { |
| 385 | // Generate the moved-to-global lookup table |
| 386 | HashSet<String> movedToGlobal = new HashSet<String>(); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 387 | Settings.System.getMovedToGlobalSettings(movedToGlobal); |
| 388 | Settings.Secure.getMovedToGlobalSettings(movedToGlobal); |
Ruslan Tkhakokhov | b8bf45f | 2019-07-24 21:13:35 +0100 | [diff] [blame] | 389 | Set<String> movedToSecure = getMovedToSecureSettings(); |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 390 | |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 391 | // system settings data first |
| 392 | int nBytes = in.readInt(); |
| 393 | if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of settings data"); |
| 394 | byte[] buffer = new byte[nBytes]; |
Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 395 | in.readFully(buffer, 0, nBytes); |
Ruslan Tkhakokhov | b8bf45f | 2019-07-24 21:13:35 +0100 | [diff] [blame] | 396 | restoreSettings(buffer, nBytes, Settings.System.CONTENT_URI, movedToGlobal, |
| 397 | movedToSecure); |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 398 | |
| 399 | // secure settings |
| 400 | nBytes = in.readInt(); |
| 401 | if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of secure settings data"); |
| 402 | if (nBytes > buffer.length) buffer = new byte[nBytes]; |
Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 403 | in.readFully(buffer, 0, nBytes); |
Ruslan Tkhakokhov | b8bf45f | 2019-07-24 21:13:35 +0100 | [diff] [blame] | 404 | restoreSettings(buffer, nBytes, Settings.Secure.CONTENT_URI, movedToGlobal, null); |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 405 | |
| 406 | // Global only if sufficiently new |
| 407 | if (version >= FULL_BACKUP_ADDED_GLOBAL) { |
| 408 | nBytes = in.readInt(); |
| 409 | if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of global settings data"); |
| 410 | if (nBytes > buffer.length) buffer = new byte[nBytes]; |
| 411 | in.readFully(buffer, 0, nBytes); |
| 412 | movedToGlobal.clear(); // no redirection; this *is* the global namespace |
Ruslan Tkhakokhov | b8bf45f | 2019-07-24 21:13:35 +0100 | [diff] [blame] | 413 | restoreSettings(buffer, nBytes, Settings.Global.CONTENT_URI, movedToGlobal, |
| 414 | movedToSecure); |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 415 | } |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 416 | |
| 417 | // locale |
| 418 | nBytes = in.readInt(); |
| 419 | if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of locale data"); |
| 420 | if (nBytes > buffer.length) buffer = new byte[nBytes]; |
Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 421 | in.readFully(buffer, 0, nBytes); |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 422 | mSettingsHelper.setLocaleData(buffer, nBytes); |
| 423 | |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 424 | // Restore older backups performing the necessary migrations. |
| 425 | if (version < FULL_BACKUP_ADDED_WIFI_NEW) { |
| 426 | // wifi supplicant |
| 427 | int supplicant_size = in.readInt(); |
| 428 | if (DEBUG_BACKUP) Log.d(TAG, supplicant_size + " bytes of wifi supplicant data"); |
| 429 | byte[] supplicant_buffer = new byte[supplicant_size]; |
| 430 | in.readFully(supplicant_buffer, 0, supplicant_size); |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 431 | |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 432 | // ip config |
| 433 | int ipconfig_size = in.readInt(); |
| 434 | if (DEBUG_BACKUP) Log.d(TAG, ipconfig_size + " bytes of ip config data"); |
| 435 | byte[] ipconfig_buffer = new byte[ipconfig_size]; |
| 436 | in.readFully(ipconfig_buffer, 0, nBytes); |
Roshan Pius | 5db739c | 2016-06-17 16:51:36 -0700 | [diff] [blame] | 437 | restoreSupplicantWifiConfigData(supplicant_buffer, ipconfig_buffer); |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 438 | } |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 439 | |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 440 | if (version >= FULL_BACKUP_ADDED_LOCK_SETTINGS) { |
| 441 | nBytes = in.readInt(); |
| 442 | if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of lock settings data"); |
| 443 | if (nBytes > buffer.length) buffer = new byte[nBytes]; |
| 444 | if (nBytes > 0) { |
| 445 | in.readFully(buffer, 0, nBytes); |
Bryan Mawhinney | e483b56 | 2017-05-15 14:46:05 +0100 | [diff] [blame] | 446 | restoreLockSettings(UserHandle.myUserId(), buffer, nBytes); |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 447 | } |
| 448 | } |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 449 | // softap config |
| 450 | if (version >= FULL_BACKUP_ADDED_SOFTAP_CONF) { |
| 451 | nBytes = in.readInt(); |
| 452 | if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of softap config data"); |
| 453 | if (nBytes > buffer.length) buffer = new byte[nBytes]; |
| 454 | if (nBytes > 0) { |
| 455 | in.readFully(buffer, 0, nBytes); |
| 456 | restoreSoftApConfiguration(buffer); |
| 457 | } |
| 458 | } |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 459 | // network policies |
| 460 | if (version >= FULL_BACKUP_ADDED_NETWORK_POLICIES) { |
| 461 | nBytes = in.readInt(); |
| 462 | if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of network policies data"); |
| 463 | if (nBytes > buffer.length) buffer = new byte[nBytes]; |
| 464 | if (nBytes > 0) { |
| 465 | in.readFully(buffer, 0, nBytes); |
| 466 | restoreNetworkPolicies(buffer); |
| 467 | } |
| 468 | } |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 469 | // Restore full wifi config data |
| 470 | if (version >= FULL_BACKUP_ADDED_WIFI_NEW) { |
| 471 | nBytes = in.readInt(); |
| 472 | if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of full wifi config data"); |
| 473 | if (nBytes > buffer.length) buffer = new byte[nBytes]; |
| 474 | in.readFully(buffer, 0, nBytes); |
Roshan Pius | 5db739c | 2016-06-17 16:51:36 -0700 | [diff] [blame] | 475 | restoreNewWifiConfigData(buffer); |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 478 | if (DEBUG_BACKUP) Log.d(TAG, "Full restore complete."); |
| 479 | } else { |
| 480 | data.close(); |
| 481 | throw new IOException("Invalid file schema"); |
| 482 | } |
| 483 | } |
| 484 | |
Ruslan Tkhakokhov | b8bf45f | 2019-07-24 21:13:35 +0100 | [diff] [blame] | 485 | private Set<String> getMovedToSecureSettings() { |
| 486 | Set<String> movedToSecureSettings = new HashSet<>(); |
| 487 | Settings.Global.getMovedToSecureSettings(movedToSecureSettings); |
| 488 | Settings.System.getMovedToSecureSettings(movedToSecureSettings); |
| 489 | return movedToSecureSettings; |
| 490 | } |
| 491 | |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 492 | private long[] readOldChecksums(ParcelFileDescriptor oldState) throws IOException { |
| 493 | long[] stateChecksums = new long[STATE_SIZE]; |
| 494 | |
| 495 | DataInputStream dataInput = new DataInputStream( |
| 496 | new FileInputStream(oldState.getFileDescriptor())); |
Christopher Tate | a286f41 | 2009-09-18 15:51:15 -0700 | [diff] [blame] | 497 | |
| 498 | try { |
| 499 | int stateVersion = dataInput.readInt(); |
Ritesh Reddy | 2400d27 | 2016-02-02 11:42:26 +0000 | [diff] [blame] | 500 | if (stateVersion > STATE_VERSION) { |
| 501 | // Constrain the maximum state version this backup agent |
| 502 | // can handle in case a newer or corrupt backup set existed |
| 503 | stateVersion = STATE_VERSION; |
| 504 | } |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 505 | for (int i = 0; i < STATE_SIZES[stateVersion]; i++) { |
| 506 | stateChecksums[i] = dataInput.readLong(); |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 507 | } |
Christopher Tate | a286f41 | 2009-09-18 15:51:15 -0700 | [diff] [blame] | 508 | } catch (EOFException eof) { |
| 509 | // With the default 0 checksum we'll wind up forcing a backup of |
| 510 | // any unhandled data sets, which is appropriate. |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 511 | } |
| 512 | dataInput.close(); |
| 513 | return stateChecksums; |
| 514 | } |
| 515 | |
| 516 | private void writeNewChecksums(long[] checksums, ParcelFileDescriptor newState) |
| 517 | throws IOException { |
| 518 | DataOutputStream dataOutput = new DataOutputStream( |
Marvin Paul | 6879555 | 2014-12-16 14:12:33 -0800 | [diff] [blame] | 519 | new BufferedOutputStream(new FileOutputStream(newState.getFileDescriptor()))); |
Christopher Tate | a286f41 | 2009-09-18 15:51:15 -0700 | [diff] [blame] | 520 | |
| 521 | dataOutput.writeInt(STATE_VERSION); |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 522 | for (int i = 0; i < STATE_SIZE; i++) { |
| 523 | dataOutput.writeLong(checksums[i]); |
| 524 | } |
| 525 | dataOutput.close(); |
| 526 | } |
| 527 | |
| 528 | private long writeIfChanged(long oldChecksum, String key, byte[] data, |
Ritesh Reddy | 33117fe | 2016-02-01 13:45:33 +0000 | [diff] [blame] | 529 | BackupDataOutput output) { |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 530 | CRC32 checkSummer = new CRC32(); |
| 531 | checkSummer.update(data); |
| 532 | long newChecksum = checkSummer.getValue(); |
| 533 | if (oldChecksum == newChecksum) { |
| 534 | return oldChecksum; |
| 535 | } |
| 536 | try { |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 537 | if (DEBUG_BACKUP) { |
| 538 | Log.v(TAG, "Writing entity " + key + " of size " + data.length); |
| 539 | } |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 540 | output.writeEntityHeader(key, data.length); |
| 541 | output.writeEntityData(data, data.length); |
| 542 | } catch (IOException ioe) { |
| 543 | // Bail |
| 544 | } |
| 545 | return newChecksum; |
| 546 | } |
| 547 | |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 548 | private byte[] getSystemSettings() { |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 549 | Cursor cursor = getContentResolver().query(Settings.System.CONTENT_URI, PROJECTION, null, |
| 550 | null, null); |
Jeff Brown | 1d8e7d6 | 2011-10-09 15:24:53 -0700 | [diff] [blame] | 551 | try { |
Al Sutton | 0833b2e | 2019-08-22 16:21:30 +0100 | [diff] [blame] | 552 | return extractRelevantValues(cursor, SystemSettings.SETTINGS_TO_BACKUP); |
Jeff Brown | 1d8e7d6 | 2011-10-09 15:24:53 -0700 | [diff] [blame] | 553 | } finally { |
| 554 | cursor.close(); |
| 555 | } |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | private byte[] getSecureSettings() { |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 559 | Cursor cursor = getContentResolver().query(Settings.Secure.CONTENT_URI, PROJECTION, null, |
| 560 | null, null); |
Jeff Brown | 1d8e7d6 | 2011-10-09 15:24:53 -0700 | [diff] [blame] | 561 | try { |
Al Sutton | 0833b2e | 2019-08-22 16:21:30 +0100 | [diff] [blame] | 562 | return extractRelevantValues(cursor, SecureSettings.SETTINGS_TO_BACKUP); |
Jeff Brown | 1d8e7d6 | 2011-10-09 15:24:53 -0700 | [diff] [blame] | 563 | } finally { |
| 564 | cursor.close(); |
| 565 | } |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 568 | private byte[] getGlobalSettings() { |
| 569 | Cursor cursor = getContentResolver().query(Settings.Global.CONTENT_URI, PROJECTION, null, |
| 570 | null, null); |
| 571 | try { |
Al Sutton | 0833b2e | 2019-08-22 16:21:30 +0100 | [diff] [blame] | 572 | return extractRelevantValues(cursor, GlobalSettings.SETTINGS_TO_BACKUP); |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 573 | } finally { |
| 574 | cursor.close(); |
| 575 | } |
| 576 | } |
| 577 | |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 578 | /** |
Bryan Mawhinney | e483b56 | 2017-05-15 14:46:05 +0100 | [diff] [blame] | 579 | * Serialize the owner info and other lock settings |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 580 | */ |
Bryan Mawhinney | e483b56 | 2017-05-15 14:46:05 +0100 | [diff] [blame] | 581 | private byte[] getLockSettings(@UserIdInt int userId) { |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 582 | final LockPatternUtils lockPatternUtils = new LockPatternUtils(this); |
Bryan Mawhinney | e483b56 | 2017-05-15 14:46:05 +0100 | [diff] [blame] | 583 | final boolean ownerInfoEnabled = lockPatternUtils.isOwnerInfoEnabled(userId); |
| 584 | final String ownerInfo = lockPatternUtils.getOwnerInfo(userId); |
| 585 | final boolean lockPatternEnabled = lockPatternUtils.isLockPatternEnabled(userId); |
| 586 | final boolean visiblePatternEnabled = lockPatternUtils.isVisiblePatternEnabled(userId); |
| 587 | final boolean powerButtonInstantlyLocks = |
| 588 | lockPatternUtils.getPowerButtonInstantlyLocks(userId); |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 589 | |
| 590 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 591 | DataOutputStream out = new DataOutputStream(baos); |
| 592 | try { |
| 593 | out.writeUTF(KEY_LOCK_SETTINGS_OWNER_INFO_ENABLED); |
| 594 | out.writeUTF(ownerInfoEnabled ? "1" : "0"); |
| 595 | if (ownerInfo != null) { |
| 596 | out.writeUTF(KEY_LOCK_SETTINGS_OWNER_INFO); |
| 597 | out.writeUTF(ownerInfo != null ? ownerInfo : ""); |
| 598 | } |
Bryan Mawhinney | e483b56 | 2017-05-15 14:46:05 +0100 | [diff] [blame] | 599 | if (lockPatternUtils.isVisiblePatternEverChosen(userId)) { |
| 600 | out.writeUTF(KEY_LOCK_SETTINGS_VISIBLE_PATTERN_ENABLED); |
| 601 | out.writeUTF(visiblePatternEnabled ? "1" : "0"); |
| 602 | } |
| 603 | if (lockPatternUtils.isPowerButtonInstantlyLocksEverChosen(userId)) { |
| 604 | out.writeUTF(KEY_LOCK_SETTINGS_POWER_BUTTON_INSTANTLY_LOCKS); |
| 605 | out.writeUTF(powerButtonInstantlyLocks ? "1" : "0"); |
| 606 | } |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 607 | // End marker |
| 608 | out.writeUTF(""); |
| 609 | out.flush(); |
| 610 | } catch (IOException ioe) { |
| 611 | } |
| 612 | return baos.toByteArray(); |
| 613 | } |
| 614 | |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 615 | private void restoreSettings(BackupDataInput data, Uri contentUri, |
Ruslan Tkhakokhov | b8bf45f | 2019-07-24 21:13:35 +0100 | [diff] [blame] | 616 | HashSet<String> movedToGlobal, Set<String> movedToSecure) { |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 617 | byte[] settings = new byte[data.getDataSize()]; |
| 618 | try { |
| 619 | data.readEntityData(settings, 0, settings.length); |
| 620 | } catch (IOException ioe) { |
| 621 | Log.e(TAG, "Couldn't read entity data"); |
| 622 | return; |
| 623 | } |
Ruslan Tkhakokhov | b8bf45f | 2019-07-24 21:13:35 +0100 | [diff] [blame] | 624 | restoreSettings(settings, settings.length, contentUri, movedToGlobal, movedToSecure); |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 627 | private void restoreSettings(byte[] settings, int bytes, Uri contentUri, |
Ruslan Tkhakokhov | b8bf45f | 2019-07-24 21:13:35 +0100 | [diff] [blame] | 628 | HashSet<String> movedToGlobal, Set<String> movedToSecure) { |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 629 | restoreSettings(settings, 0, bytes, contentUri, movedToGlobal, movedToSecure); |
| 630 | } |
| 631 | |
| 632 | private void restoreSettings(byte[] settings, int pos, int bytes, Uri contentUri, |
| 633 | HashSet<String> movedToGlobal, Set<String> movedToSecure) { |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 634 | if (DEBUG) { |
| 635 | Log.i(TAG, "restoreSettings: " + contentUri); |
| 636 | } |
| 637 | |
Christopher Tate | 1d0fca3 | 2017-06-06 13:30:00 -0700 | [diff] [blame] | 638 | // Figure out the white list and redirects to the global table. We restore anything |
| 639 | // in either the backup whitelist or the legacy-restore whitelist for this table. |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 640 | final String[] whitelist; |
Michal Karpinski | b52575c | 2018-01-19 17:38:45 +0000 | [diff] [blame] | 641 | Map<String, Validator> validators = null; |
Christopher Tate | 796e0f0 | 2009-09-22 11:57:58 -0700 | [diff] [blame] | 642 | if (contentUri.equals(Settings.Secure.CONTENT_URI)) { |
Al Sutton | 0833b2e | 2019-08-22 16:21:30 +0100 | [diff] [blame] | 643 | whitelist = ArrayUtils.concatElements(String.class, SecureSettings.SETTINGS_TO_BACKUP, |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 644 | Settings.Secure.LEGACY_RESTORE_SETTINGS, |
Al Sutton | 66a3e01 | 2019-10-15 15:34:13 +0100 | [diff] [blame] | 645 | DeviceSpecificSettings.DEVICE_SPECIFIC_SETTINGS_TO_BACKUP); |
Al Sutton | 91f89d0 | 2019-08-16 12:56:57 +0100 | [diff] [blame] | 646 | validators = SecureSettingsValidators.VALIDATORS; |
Christopher Tate | 796e0f0 | 2009-09-22 11:57:58 -0700 | [diff] [blame] | 647 | } else if (contentUri.equals(Settings.System.CONTENT_URI)) { |
Al Sutton | 0833b2e | 2019-08-22 16:21:30 +0100 | [diff] [blame] | 648 | whitelist = ArrayUtils.concatElements(String.class, SystemSettings.SETTINGS_TO_BACKUP, |
Christopher Tate | 1d0fca3 | 2017-06-06 13:30:00 -0700 | [diff] [blame] | 649 | Settings.System.LEGACY_RESTORE_SETTINGS); |
Al Sutton | 91f89d0 | 2019-08-16 12:56:57 +0100 | [diff] [blame] | 650 | validators = SystemSettingsValidators.VALIDATORS; |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 651 | } else if (contentUri.equals(Settings.Global.CONTENT_URI)) { |
Al Sutton | 0833b2e | 2019-08-22 16:21:30 +0100 | [diff] [blame] | 652 | whitelist = ArrayUtils.concatElements(String.class, GlobalSettings.SETTINGS_TO_BACKUP, |
Christopher Tate | 1d0fca3 | 2017-06-06 13:30:00 -0700 | [diff] [blame] | 653 | Settings.Global.LEGACY_RESTORE_SETTINGS); |
Al Sutton | 91f89d0 | 2019-08-16 12:56:57 +0100 | [diff] [blame] | 654 | validators = GlobalSettingsValidators.VALIDATORS; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 655 | } else { |
| 656 | throw new IllegalArgumentException("Unknown URI: " + contentUri); |
Christopher Tate | 796e0f0 | 2009-09-22 11:57:58 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 659 | // Restore only the white list data. |
Svet Ganov | aabc963 | 2017-07-28 19:37:15 -0700 | [diff] [blame] | 660 | final ArrayMap<String, String> cachedEntries = new ArrayMap<>(); |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 661 | ContentValues contentValues = new ContentValues(2); |
| 662 | SettingsHelper settingsHelper = mSettingsHelper; |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 663 | ContentResolver cr = getContentResolver(); |
Christopher Tate | 0738e88 | 2009-09-11 16:35:39 -0700 | [diff] [blame] | 664 | |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 665 | final int whiteListSize = whitelist.length; |
| 666 | for (int i = 0; i < whiteListSize; i++) { |
| 667 | String key = whitelist[i]; |
Christopher Tate | 0738e88 | 2009-09-11 16:35:39 -0700 | [diff] [blame] | 668 | |
Svet Ganov | aabc963 | 2017-07-28 19:37:15 -0700 | [diff] [blame] | 669 | String value = null; |
| 670 | boolean hasValueToRestore = false; |
| 671 | if (cachedEntries.indexOfKey(key) >= 0) { |
| 672 | value = cachedEntries.remove(key); |
| 673 | hasValueToRestore = true; |
| 674 | } else { |
| 675 | // If the value not cached, let us look it up. |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 676 | while (pos < bytes) { |
| 677 | int length = readInt(settings, pos); |
| 678 | pos += INTEGER_BYTE_COUNT; |
Svet Ganov | aabc963 | 2017-07-28 19:37:15 -0700 | [diff] [blame] | 679 | String dataKey = length >= 0 ? new String(settings, pos, length) : null; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 680 | pos += length; |
| 681 | length = readInt(settings, pos); |
| 682 | pos += INTEGER_BYTE_COUNT; |
Svet Ganov | aabc963 | 2017-07-28 19:37:15 -0700 | [diff] [blame] | 683 | String dataValue = null; |
| 684 | if (length >= 0) { |
| 685 | dataValue = new String(settings, pos, length); |
| 686 | pos += length; |
| 687 | } |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 688 | if (key.equals(dataKey)) { |
| 689 | value = dataValue; |
Svet Ganov | aabc963 | 2017-07-28 19:37:15 -0700 | [diff] [blame] | 690 | hasValueToRestore = true; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 691 | break; |
| 692 | } |
| 693 | cachedEntries.put(dataKey, dataValue); |
Amith Yamasani | 70c874b | 2009-07-06 14:53:25 -0700 | [diff] [blame] | 694 | } |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 695 | } |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 696 | |
Svet Ganov | aabc963 | 2017-07-28 19:37:15 -0700 | [diff] [blame] | 697 | if (!hasValueToRestore) { |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 698 | continue; |
| 699 | } |
Christopher Tate | 796e0f0 | 2009-09-22 11:57:58 -0700 | [diff] [blame] | 700 | |
Michal Karpinski | b52575c | 2018-01-19 17:38:45 +0000 | [diff] [blame] | 701 | // only restore the settings that have valid values |
| 702 | if (!isValidSettingValue(key, value, validators)) { |
| 703 | Log.w(TAG, "Attempted restore of " + key + " setting, but its value didn't pass" |
| 704 | + " validation, value: " + value); |
| 705 | continue; |
| 706 | } |
| 707 | |
Ruslan Tkhakokhov | b8bf45f | 2019-07-24 21:13:35 +0100 | [diff] [blame] | 708 | final Uri destination; |
| 709 | if (movedToGlobal != null && movedToGlobal.contains(key)) { |
| 710 | destination = Settings.Global.CONTENT_URI; |
| 711 | } else if (movedToSecure != null && movedToSecure.contains(key)) { |
| 712 | destination = Settings.Secure.CONTENT_URI; |
| 713 | } else { |
| 714 | destination = contentUri; |
| 715 | } |
Michal Karpinski | 6135a26 | 2017-08-11 10:45:58 +0100 | [diff] [blame] | 716 | settingsHelper.restoreValue(this, cr, contentValues, destination, key, value, |
| 717 | mRestoredFromSdkInt); |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 718 | |
Christopher Tate | d488bc0 | 2012-10-09 14:06:30 -0700 | [diff] [blame] | 719 | if (DEBUG) { |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 720 | Log.d(TAG, "Restored setting: " + destination + " : " + key + "=" + value); |
Christopher Tate | 0738e88 | 2009-09-11 16:35:39 -0700 | [diff] [blame] | 721 | } |
| 722 | } |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Michal Karpinski | b52575c | 2018-01-19 17:38:45 +0000 | [diff] [blame] | 725 | private boolean isValidSettingValue(String key, String value, |
| 726 | Map<String, Validator> validators) { |
| 727 | if (key == null || validators == null) { |
| 728 | return false; |
| 729 | } |
| 730 | Validator validator = validators.get(key); |
| 731 | return (validator != null) && validator.validate(value); |
| 732 | } |
| 733 | |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 734 | /** |
Bryan Mawhinney | e483b56 | 2017-05-15 14:46:05 +0100 | [diff] [blame] | 735 | * Restores the owner info enabled and other settings in LockSettings. |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 736 | * |
| 737 | * @param buffer |
| 738 | * @param nBytes |
| 739 | */ |
Bryan Mawhinney | e483b56 | 2017-05-15 14:46:05 +0100 | [diff] [blame] | 740 | private void restoreLockSettings(@UserIdInt int userId, byte[] buffer, int nBytes) { |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 741 | final LockPatternUtils lockPatternUtils = new LockPatternUtils(this); |
| 742 | |
| 743 | ByteArrayInputStream bais = new ByteArrayInputStream(buffer, 0, nBytes); |
| 744 | DataInputStream in = new DataInputStream(bais); |
| 745 | try { |
| 746 | String key; |
| 747 | // Read until empty string marker |
| 748 | while ((key = in.readUTF()).length() > 0) { |
| 749 | final String value = in.readUTF(); |
| 750 | if (DEBUG_BACKUP) { |
| 751 | Log.v(TAG, "Restoring lock_settings " + key + " = " + value); |
| 752 | } |
| 753 | switch (key) { |
| 754 | case KEY_LOCK_SETTINGS_OWNER_INFO_ENABLED: |
Bryan Mawhinney | e483b56 | 2017-05-15 14:46:05 +0100 | [diff] [blame] | 755 | lockPatternUtils.setOwnerInfoEnabled("1".equals(value), userId); |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 756 | break; |
| 757 | case KEY_LOCK_SETTINGS_OWNER_INFO: |
Bryan Mawhinney | e483b56 | 2017-05-15 14:46:05 +0100 | [diff] [blame] | 758 | lockPatternUtils.setOwnerInfo(value, userId); |
| 759 | break; |
| 760 | case KEY_LOCK_SETTINGS_VISIBLE_PATTERN_ENABLED: |
| 761 | lockPatternUtils.reportPatternWasChosen(userId); |
| 762 | lockPatternUtils.setVisiblePatternEnabled("1".equals(value), userId); |
| 763 | break; |
| 764 | case KEY_LOCK_SETTINGS_POWER_BUTTON_INSTANTLY_LOCKS: |
| 765 | lockPatternUtils.setPowerButtonInstantlyLocks("1".equals(value), userId); |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 766 | break; |
| 767 | } |
| 768 | } |
| 769 | in.close(); |
| 770 | } catch (IOException ioe) { |
| 771 | } |
| 772 | } |
| 773 | |
Bryan Mawhinney | e483b56 | 2017-05-15 14:46:05 +0100 | [diff] [blame] | 774 | private void restoreLockSettings(@UserIdInt int userId, BackupDataInput data) { |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 775 | final byte[] settings = new byte[data.getDataSize()]; |
| 776 | try { |
| 777 | data.readEntityData(settings, 0, settings.length); |
| 778 | } catch (IOException ioe) { |
| 779 | Log.e(TAG, "Couldn't read entity data"); |
| 780 | return; |
| 781 | } |
Bryan Mawhinney | e483b56 | 2017-05-15 14:46:05 +0100 | [diff] [blame] | 782 | restoreLockSettings(userId, settings, settings.length); |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | /** |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 786 | * Given a cursor and a set of keys, extract the required keys and |
| 787 | * values and write them to a byte array. |
| 788 | * |
| 789 | * @param cursor A cursor with settings data. |
| 790 | * @param settings The settings to extract. |
| 791 | * @return The byte array of extracted values. |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 792 | */ |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 793 | private byte[] extractRelevantValues(Cursor cursor, String[] settings) { |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 794 | if (!cursor.moveToFirst()) { |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 795 | Log.e(TAG, "Couldn't read from the cursor"); |
| 796 | return new byte[0]; |
| 797 | } |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 798 | |
Svet Ganov | aabc963 | 2017-07-28 19:37:15 -0700 | [diff] [blame] | 799 | final int nameColumnIndex = cursor.getColumnIndex(Settings.NameValueTable.NAME); |
| 800 | final int valueColumnIndex = cursor.getColumnIndex(Settings.NameValueTable.VALUE); |
| 801 | |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 802 | // Obtain the relevant data in a temporary array. |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 803 | int totalSize = 0; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 804 | int backedUpSettingIndex = 0; |
Svet Ganov | aabc963 | 2017-07-28 19:37:15 -0700 | [diff] [blame] | 805 | final int settingsCount = settings.length; |
| 806 | final byte[][] values = new byte[settingsCount * 2][]; // keys and values |
| 807 | final ArrayMap<String, String> cachedEntries = new ArrayMap<>(); |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 808 | for (int i = 0; i < settingsCount; i++) { |
Svet Ganov | aabc963 | 2017-07-28 19:37:15 -0700 | [diff] [blame] | 809 | final String key = settings[i]; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 810 | |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 811 | // If the value not cached, let us look it up. |
Svet Ganov | aabc963 | 2017-07-28 19:37:15 -0700 | [diff] [blame] | 812 | String value = null; |
| 813 | boolean hasValueToBackup = false; |
| 814 | if (cachedEntries.indexOfKey(key) >= 0) { |
| 815 | value = cachedEntries.remove(key); |
| 816 | hasValueToBackup = true; |
| 817 | } else { |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 818 | while (!cursor.isAfterLast()) { |
Svet Ganov | aabc963 | 2017-07-28 19:37:15 -0700 | [diff] [blame] | 819 | final String cursorKey = cursor.getString(nameColumnIndex); |
| 820 | final String cursorValue = cursor.getString(valueColumnIndex); |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 821 | cursor.moveToNext(); |
| 822 | if (key.equals(cursorKey)) { |
| 823 | value = cursorValue; |
Svet Ganov | aabc963 | 2017-07-28 19:37:15 -0700 | [diff] [blame] | 824 | hasValueToBackup = true; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 825 | break; |
| 826 | } |
| 827 | cachedEntries.put(cursorKey, cursorValue); |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 828 | } |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 829 | } |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 830 | |
Svet Ganov | aabc963 | 2017-07-28 19:37:15 -0700 | [diff] [blame] | 831 | if (!hasValueToBackup) { |
| 832 | continue; |
| 833 | } |
| 834 | |
Amith Yamasani | 622bf222 | 2013-09-06 13:54:28 -0700 | [diff] [blame] | 835 | // Intercept the keys and see if they need special handling |
| 836 | value = mSettingsHelper.onBackupValue(key, value); |
| 837 | |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 838 | // Write the key and value in the intermediary array. |
Svet Ganov | aabc963 | 2017-07-28 19:37:15 -0700 | [diff] [blame] | 839 | final byte[] keyBytes = key.getBytes(); |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 840 | totalSize += INTEGER_BYTE_COUNT + keyBytes.length; |
| 841 | values[backedUpSettingIndex * 2] = keyBytes; |
| 842 | |
Svet Ganov | aabc963 | 2017-07-28 19:37:15 -0700 | [diff] [blame] | 843 | final byte[] valueBytes = (value != null) ? value.getBytes() : NULL_VALUE; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 844 | totalSize += INTEGER_BYTE_COUNT + valueBytes.length; |
| 845 | values[backedUpSettingIndex * 2 + 1] = valueBytes; |
| 846 | |
| 847 | backedUpSettingIndex++; |
| 848 | |
| 849 | if (DEBUG) { |
| 850 | Log.d(TAG, "Backed up setting: " + key + "=" + value); |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 854 | // Aggregate the result. |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 855 | byte[] result = new byte[totalSize]; |
| 856 | int pos = 0; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 857 | final int keyValuePairCount = backedUpSettingIndex * 2; |
| 858 | for (int i = 0; i < keyValuePairCount; i++) { |
Svet Ganov | aabc963 | 2017-07-28 19:37:15 -0700 | [diff] [blame] | 859 | final byte[] value = values[i]; |
| 860 | if (value != NULL_VALUE) { |
| 861 | pos = writeInt(result, pos, value.length); |
| 862 | pos = writeBytes(result, pos, value); |
| 863 | } else { |
| 864 | pos = writeInt(result, pos, NULL_SIZE); |
| 865 | } |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 866 | } |
| 867 | return result; |
| 868 | } |
| 869 | |
Roshan Pius | 5db739c | 2016-06-17 16:51:36 -0700 | [diff] [blame] | 870 | private void restoreSupplicantWifiConfigData(byte[] supplicant_bytes, byte[] ipconfig_bytes) { |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 871 | if (DEBUG_BACKUP) { |
| 872 | Log.v(TAG, "Applying restored supplicant wifi data"); |
Irfan Sheriff | 4aeca7c5 | 2011-03-10 16:53:33 -0800 | [diff] [blame] | 873 | } |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 874 | mWifiManager.restoreSupplicantBackupData(supplicant_bytes, ipconfig_bytes); |
Amith Yamasani | 2cfab84 | 2009-09-09 18:27:31 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 877 | private byte[] getSoftAPConfiguration() { |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 878 | try { |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 879 | return mWifiManager.getWifiApConfiguration().getBytesForBackup(); |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 880 | } catch (IOException ioe) { |
| 881 | Log.e(TAG, "Failed to marshal SoftAPConfiguration" + ioe.getMessage()); |
| 882 | return new byte[0]; |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | private void restoreSoftApConfiguration(byte[] data) { |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 887 | try { |
| 888 | WifiConfiguration config = WifiConfiguration |
| 889 | .getWifiConfigFromBackup(new DataInputStream(new ByteArrayInputStream(data))); |
| 890 | if (DEBUG) Log.d(TAG, "Successfully unMarshaled WifiConfiguration "); |
Rebecca Silberstein | d41106c | 2018-06-01 17:31:40 -0700 | [diff] [blame] | 891 | int originalApBand = config.apBand; |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 892 | mWifiManager.setWifiApConfiguration(config); |
Rebecca Silberstein | d41106c | 2018-06-01 17:31:40 -0700 | [diff] [blame] | 893 | |
| 894 | // Depending on device hardware, we may need to notify the user of a setting change for |
| 895 | // the apBand preference |
| 896 | boolean dualMode = mWifiManager.isDualModeSupported(); |
| 897 | int storedApBand = mWifiManager.getWifiApConfiguration().apBand; |
David Su | 6c07c9f | 2019-11-20 18:16:56 -0800 | [diff] [blame] | 898 | if (dualMode && storedApBand != originalApBand) { |
| 899 | Log.d(TAG, "restored ap configuration requires a conversion, notify the user"); |
| 900 | WifiSoftApBandChangedNotifier.notifyUserOfApBandConversion(this); |
Rebecca Silberstein | d41106c | 2018-06-01 17:31:40 -0700 | [diff] [blame] | 901 | } |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 902 | } catch (IOException | BackupUtils.BadVersionException e) { |
| 903 | Log.e(TAG, "Failed to unMarshal SoftAPConfiguration " + e.getMessage()); |
| 904 | } |
| 905 | } |
| 906 | |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 907 | private byte[] getNetworkPolicies() { |
| 908 | NetworkPolicyManager networkPolicyManager = |
| 909 | (NetworkPolicyManager) getSystemService(NETWORK_POLICY_SERVICE); |
| 910 | NetworkPolicy[] policies = networkPolicyManager.getNetworkPolicies(); |
| 911 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 912 | if (policies != null && policies.length != 0) { |
| 913 | DataOutputStream out = new DataOutputStream(baos); |
| 914 | try { |
| 915 | out.writeInt(NETWORK_POLICIES_BACKUP_VERSION); |
| 916 | out.writeInt(policies.length); |
| 917 | for (NetworkPolicy policy : policies) { |
Jeff Sharkey | 55a3fe7 | 2018-02-24 19:09:20 -0700 | [diff] [blame] | 918 | // We purposefully only backup policies that the user has |
| 919 | // defined; any inferred policies might include |
| 920 | // carrier-protected data that we can't export. |
| 921 | if (policy != null && !policy.inferred) { |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 922 | byte[] marshaledPolicy = policy.getBytesForBackup(); |
| 923 | out.writeByte(BackupUtils.NOT_NULL); |
| 924 | out.writeInt(marshaledPolicy.length); |
| 925 | out.write(marshaledPolicy); |
| 926 | } else { |
| 927 | out.writeByte(BackupUtils.NULL); |
| 928 | } |
| 929 | } |
| 930 | } catch (IOException ioe) { |
| 931 | Log.e(TAG, "Failed to convert NetworkPolicies to byte array " + ioe.getMessage()); |
| 932 | baos.reset(); |
| 933 | } |
| 934 | } |
| 935 | return baos.toByteArray(); |
| 936 | } |
| 937 | |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 938 | private byte[] getNewWifiConfigData() { |
| 939 | return mWifiManager.retrieveBackupData(); |
| 940 | } |
| 941 | |
Roshan Pius | 5db739c | 2016-06-17 16:51:36 -0700 | [diff] [blame] | 942 | private void restoreNewWifiConfigData(byte[] bytes) { |
Roshan Pius | 7a2491f | 2016-06-02 09:22:55 -0700 | [diff] [blame] | 943 | if (DEBUG_BACKUP) { |
| 944 | Log.v(TAG, "Applying restored wifi data"); |
| 945 | } |
| 946 | mWifiManager.restoreBackupData(bytes); |
| 947 | } |
| 948 | |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 949 | private void restoreNetworkPolicies(byte[] data) { |
| 950 | NetworkPolicyManager networkPolicyManager = |
| 951 | (NetworkPolicyManager) getSystemService(NETWORK_POLICY_SERVICE); |
| 952 | if (data != null && data.length != 0) { |
| 953 | DataInputStream in = new DataInputStream(new ByteArrayInputStream(data)); |
| 954 | try { |
| 955 | int version = in.readInt(); |
| 956 | if (version < 1 || version > NETWORK_POLICIES_BACKUP_VERSION) { |
| 957 | throw new BackupUtils.BadVersionException( |
| 958 | "Unknown Backup Serialization Version"); |
| 959 | } |
| 960 | int length = in.readInt(); |
| 961 | NetworkPolicy[] policies = new NetworkPolicy[length]; |
| 962 | for (int i = 0; i < length; i++) { |
| 963 | byte isNull = in.readByte(); |
| 964 | if (isNull == BackupUtils.NULL) continue; |
| 965 | int byteLength = in.readInt(); |
| 966 | byte[] policyData = new byte[byteLength]; |
| 967 | in.read(policyData, 0, byteLength); |
| 968 | policies[i] = NetworkPolicy.getNetworkPolicyFromBackup( |
| 969 | new DataInputStream(new ByteArrayInputStream(policyData))); |
| 970 | } |
| 971 | // Only set the policies if there was no error in the restore operation |
| 972 | networkPolicyManager.setNetworkPolicies(policies); |
Annie Meng | 47f5c9c | 2018-02-27 14:48:21 +0000 | [diff] [blame] | 973 | } catch (NullPointerException | IOException | BackupUtils.BadVersionException |
| 974 | | DateTimeException e) { |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 975 | // NPE can be thrown when trying to instantiate a NetworkPolicy |
| 976 | Log.e(TAG, "Failed to convert byte array to NetworkPolicies " + e.getMessage()); |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 981 | @VisibleForTesting |
| 982 | byte[] getDeviceSpecificConfiguration() throws IOException { |
| 983 | try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { |
| 984 | writeHeader(os); |
| 985 | os.write(getDeviceSpecificSettings()); |
| 986 | return os.toByteArray(); |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | @VisibleForTesting |
| 991 | void writeHeader(OutputStream os) throws IOException { |
| 992 | os.write(toByteArray(DEVICE_SPECIFIC_VERSION)); |
| 993 | os.write(toByteArray(Build.MANUFACTURER)); |
| 994 | os.write(toByteArray(Build.PRODUCT)); |
| 995 | } |
| 996 | |
| 997 | private byte[] getDeviceSpecificSettings() { |
| 998 | try (Cursor cursor = |
| 999 | getContentResolver() |
| 1000 | .query(Settings.Secure.CONTENT_URI, PROJECTION, null, null, null)) { |
| 1001 | return extractRelevantValues( |
Al Sutton | 66a3e01 | 2019-10-15 15:34:13 +0100 | [diff] [blame] | 1002 | cursor, DeviceSpecificSettings.DEVICE_SPECIFIC_SETTINGS_TO_BACKUP); |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | /** |
| 1007 | * Restore the device specific settings. |
| 1008 | * |
| 1009 | * @param data The byte array holding a backed up version of another devices settings. |
| 1010 | * @return true if the restore succeeded, false if it was stopped. |
| 1011 | */ |
| 1012 | @VisibleForTesting |
| 1013 | boolean restoreDeviceSpecificConfig(byte[] data) { |
| 1014 | // We're using an AtomicInteger to wrap the position int and allow called methods to |
| 1015 | // modify it. |
| 1016 | AtomicInteger pos = new AtomicInteger(0); |
| 1017 | if (!isSourceAcceptable(data, pos)) { |
| 1018 | return false; |
| 1019 | } |
| 1020 | |
| 1021 | Integer originalDensity = getPreviousDensity(); |
| 1022 | |
| 1023 | int dataStart = pos.get(); |
| 1024 | restoreSettings( |
| 1025 | data, dataStart, data.length, Settings.Secure.CONTENT_URI, null, null); |
| 1026 | |
| 1027 | updateWindowManagerIfNeeded(originalDensity); |
| 1028 | |
| 1029 | return true; |
| 1030 | } |
| 1031 | |
| 1032 | private void updateWindowManagerIfNeeded(Integer previousDensity) { |
| 1033 | int newDensity; |
| 1034 | try { |
| 1035 | newDensity = getForcedDensity(); |
| 1036 | } catch (Settings.SettingNotFoundException e) { |
| 1037 | // If there's not density setting we can't perform a change. |
| 1038 | return; |
| 1039 | } |
| 1040 | |
| 1041 | if (previousDensity == null || previousDensity != newDensity) { |
| 1042 | // From nothing to something is a change. |
Al Sutton | e5e7916 | 2019-08-19 13:42:16 +0100 | [diff] [blame] | 1043 | DisplayDensityConfiguration.setForcedDisplayDensity( |
| 1044 | Display.DEFAULT_DISPLAY, newDensity); |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | private Integer getPreviousDensity() { |
| 1049 | try { |
| 1050 | return getForcedDensity(); |
| 1051 | } catch (Settings.SettingNotFoundException e) { |
| 1052 | return null; |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | private int getForcedDensity() throws Settings.SettingNotFoundException { |
| 1057 | return Settings.Secure.getInt(getContentResolver(), Settings.Secure.DISPLAY_DENSITY_FORCED); |
| 1058 | } |
| 1059 | |
| 1060 | @VisibleForTesting |
| 1061 | boolean isSourceAcceptable(byte[] data, AtomicInteger pos) { |
| 1062 | int version = readInt(data, pos); |
| 1063 | if (version > DEVICE_SPECIFIC_VERSION) { |
| 1064 | Slog.w(TAG, "Unable to restore device specific information; Backup is too new"); |
| 1065 | return false; |
| 1066 | } |
| 1067 | |
| 1068 | String sourceManufacturer = readString(data, pos); |
| 1069 | if (!Objects.equals(Build.MANUFACTURER, sourceManufacturer)) { |
| 1070 | Log.w( |
| 1071 | TAG, |
| 1072 | "Unable to restore device specific information; Manufacturer mismatch " |
| 1073 | + "(\'" |
| 1074 | + Build.MANUFACTURER |
| 1075 | + "\' and \'" |
| 1076 | + sourceManufacturer |
| 1077 | + "\')"); |
| 1078 | return false; |
| 1079 | } |
| 1080 | |
| 1081 | String sourceProduct = readString(data, pos); |
| 1082 | if (!Objects.equals(Build.PRODUCT, sourceProduct)) { |
| 1083 | Log.w( |
| 1084 | TAG, |
| 1085 | "Unable to restore device specific information; Product mismatch (\'" |
| 1086 | + Build.PRODUCT |
| 1087 | + "\' and \'" |
| 1088 | + sourceProduct |
| 1089 | + "\')"); |
| 1090 | return false; |
| 1091 | } |
| 1092 | |
| 1093 | return true; |
| 1094 | } |
| 1095 | |
| 1096 | @VisibleForTesting |
| 1097 | static byte[] toByteArray(String value) { |
| 1098 | if (value == null) { |
| 1099 | return toByteArray(NULL_SIZE); |
| 1100 | } |
| 1101 | |
| 1102 | byte[] stringBytes = value.getBytes(); |
| 1103 | byte[] sizeAndString = new byte[stringBytes.length + INTEGER_BYTE_COUNT]; |
| 1104 | writeInt(sizeAndString, 0, stringBytes.length); |
| 1105 | writeBytes(sizeAndString, INTEGER_BYTE_COUNT, stringBytes); |
| 1106 | return sizeAndString; |
| 1107 | } |
| 1108 | |
| 1109 | @VisibleForTesting |
| 1110 | static byte[] toByteArray(int value) { |
| 1111 | byte[] result = new byte[INTEGER_BYTE_COUNT]; |
| 1112 | writeInt(result, 0, value); |
| 1113 | return result; |
| 1114 | } |
| 1115 | |
| 1116 | private String readString(byte[] data, AtomicInteger pos) { |
| 1117 | int byteCount = readInt(data, pos); |
| 1118 | if (byteCount == NULL_SIZE) { |
| 1119 | return null; |
| 1120 | } |
| 1121 | |
| 1122 | int stringStart = pos.getAndAdd(byteCount); |
| 1123 | return new String(data, stringStart, byteCount); |
| 1124 | } |
| 1125 | |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 1126 | /** |
| 1127 | * Write an int in BigEndian into the byte array. |
| 1128 | * @param out byte array |
| 1129 | * @param pos current pos in array |
| 1130 | * @param value integer to write |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 1131 | * @return the index after adding the size of an int (4) in bytes. |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 1132 | */ |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 1133 | private static int writeInt(byte[] out, int pos, int value) { |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 1134 | out[pos + 0] = (byte) ((value >> 24) & 0xFF); |
| 1135 | out[pos + 1] = (byte) ((value >> 16) & 0xFF); |
| 1136 | out[pos + 2] = (byte) ((value >> 8) & 0xFF); |
| 1137 | out[pos + 3] = (byte) ((value >> 0) & 0xFF); |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 1138 | return pos + INTEGER_BYTE_COUNT; |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 1141 | private static int writeBytes(byte[] out, int pos, byte[] value) { |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 1142 | System.arraycopy(value, 0, out, pos, value.length); |
| 1143 | return pos + value.length; |
| 1144 | } |
| 1145 | |
Al Sutton | 71dc8ff | 2019-07-12 11:42:03 +0100 | [diff] [blame] | 1146 | private int readInt(byte[] in, AtomicInteger pos) { |
| 1147 | return readInt(in, pos.getAndAdd(INTEGER_BYTE_COUNT)); |
| 1148 | } |
| 1149 | |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 1150 | private int readInt(byte[] in, int pos) { |
Ritesh Reddy | aeb4c06 | 2016-01-26 19:40:48 +0000 | [diff] [blame] | 1151 | int result = ((in[pos] & 0xFF) << 24) |
| 1152 | | ((in[pos + 1] & 0xFF) << 16) |
| 1153 | | ((in[pos + 2] & 0xFF) << 8) |
| 1154 | | ((in[pos + 3] & 0xFF) << 0); |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 1155 | return result; |
| 1156 | } |
Paul Stewart | 45e6fec | 2016-05-20 08:14:30 -0700 | [diff] [blame] | 1157 | } |