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