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