blob: f1fb208b1e10d2a2649e6218586d4b6d7d595d87 [file] [log] [blame]
Amith Yamasani220f4d62009-07-02 02:34:14 -07001/*
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
17package com.android.providers.settings;
18
Christopher Tate1d0fca32017-06-06 13:30:00 -070019import android.annotation.Nullable;
Bryan Mawhinneye483b562017-05-15 14:46:05 +010020import android.annotation.UserIdInt;
Svetoslav Ganova571a582011-09-20 18:32:20 -070021import android.app.backup.BackupAgentHelper;
22import android.app.backup.BackupDataInput;
23import android.app.backup.BackupDataOutput;
24import android.app.backup.FullBackupDataOutput;
Christopher Tate87bc7e72013-06-10 16:55:02 -070025import android.content.ContentResolver;
Svetoslav Ganova571a582011-09-20 18:32:20 -070026import android.content.ContentValues;
27import android.content.Context;
28import android.database.Cursor;
Ritesh Reddyadca34a2016-02-04 18:33:30 +000029import android.net.NetworkPolicy;
30import android.net.NetworkPolicyManager;
Svetoslav Ganova571a582011-09-20 18:32:20 -070031import android.net.Uri;
Christopher Tatecab915a2015-01-08 12:50:00 -080032import android.net.wifi.WifiConfiguration;
Svetoslav Ganova571a582011-09-20 18:32:20 -070033import android.net.wifi.WifiManager;
Svetoslav Ganova571a582011-09-20 18:32:20 -070034import android.os.ParcelFileDescriptor;
Amith Yamasani072543f2015-02-13 11:09:45 -080035import android.os.UserHandle;
Svetoslav Ganova571a582011-09-20 18:32:20 -070036import android.provider.Settings;
Svet Ganovaabc9632017-07-28 19:37:15 -070037import android.util.ArrayMap;
Ritesh Reddyaeb4c062016-01-26 19:40:48 +000038import android.util.BackupUtils;
Svetoslav Ganova571a582011-09-20 18:32:20 -070039import android.util.Log;
40
Amith Yamasani072543f2015-02-13 11:09:45 -080041import com.android.internal.widget.LockPatternUtils;
42
Brad Fitzpatrick70787892010-11-17 11:31:12 -080043import java.io.BufferedOutputStream;
Amith Yamasani072543f2015-02-13 11:09:45 -080044import java.io.ByteArrayInputStream;
Christopher Tatecab915a2015-01-08 12:50:00 -080045import java.io.ByteArrayOutputStream;
Amith Yamasanid1582142009-07-08 20:04:55 -070046import java.io.DataInputStream;
47import java.io.DataOutputStream;
Amith Yamasani2cfab842009-09-09 18:27:31 -070048import java.io.EOFException;
Amith Yamasani220f4d62009-07-02 02:34:14 -070049import java.io.File;
50import java.io.FileInputStream;
51import java.io.FileOutputStream;
52import java.io.IOException;
Svetoslav Ganova571a582011-09-20 18:32:20 -070053import java.util.HashMap;
Christopher Tate8dfe2b92012-05-15 15:05:04 -070054import java.util.HashSet;
Svetoslav Ganova571a582011-09-20 18:32:20 -070055import java.util.Map;
Amith Yamasanid1582142009-07-08 20:04:55 -070056import java.util.zip.CRC32;
Amith Yamasani220f4d62009-07-02 02:34:14 -070057
Amith Yamasani220f4d62009-07-02 02:34:14 -070058/**
59 * Performs backup and restore of the System and Secure settings.
60 * List of settings that are backed up are stored in the Settings.java file
61 */
Christopher Tatecc84c692010-03-29 14:54:02 -070062public class SettingsBackupAgent extends BackupAgentHelper {
Christopher Tate436344a2009-09-30 16:17:37 -070063 private static final boolean DEBUG = false;
Christopher Tate8dfe2b92012-05-15 15:05:04 -070064 private static final boolean DEBUG_BACKUP = DEBUG || false;
Amith Yamasani220f4d62009-07-02 02:34:14 -070065
Svet Ganovaabc9632017-07-28 19:37:15 -070066 private static final byte[] NULL_VALUE = new byte[0];
67 private static final int NULL_SIZE = -1;
68
Amith Yamasani220f4d62009-07-02 02:34:14 -070069 private static final String KEY_SYSTEM = "system";
70 private static final String KEY_SECURE = "secure";
Christopher Tate66488d62012-10-02 11:58:01 -070071 private static final String KEY_GLOBAL = "global";
Amith Yamasani8823c0a82009-07-07 14:30:17 -070072 private static final String KEY_LOCALE = "locale";
Amith Yamasani072543f2015-02-13 11:09:45 -080073 private static final String KEY_LOCK_SETTINGS = "lock_settings";
Ritesh Reddyaeb4c062016-01-26 19:40:48 +000074 private static final String KEY_SOFTAP_CONFIG = "softap_config";
Ritesh Reddyadca34a2016-02-04 18:33:30 +000075 private static final String KEY_NETWORK_POLICIES = "network_policies";
Roshan Pius7a2491f2016-06-02 09:22:55 -070076 private static final String KEY_WIFI_NEW_CONFIG = "wifi_new_config";
Amith Yamasani220f4d62009-07-02 02:34:14 -070077
Christopher Tatea286f412009-09-18 15:51:15 -070078 // Versioning of the state file. Increment this version
79 // number any time the set of state items is altered.
Roshan Pius7a2491f2016-06-02 09:22:55 -070080 private static final int STATE_VERSION = 7;
Ritesh Reddyadca34a2016-02-04 18:33:30 +000081
82 // Versioning of the Network Policies backup payload.
83 private static final int NETWORK_POLICIES_BACKUP_VERSION = 1;
84
Christopher Tatea286f412009-09-18 15:51:15 -070085
Christopher Tate66488d62012-10-02 11:58:01 -070086 // Slots in the checksum array. Never insert new items in the middle
87 // of this array; new slots must be appended.
Ritesh Reddyadca34a2016-02-04 18:33:30 +000088 private static final int STATE_SYSTEM = 0;
89 private static final int STATE_SECURE = 1;
90 private static final int STATE_LOCALE = 2;
91 private static final int STATE_WIFI_SUPPLICANT = 3;
92 private static final int STATE_WIFI_CONFIG = 4;
93 private static final int STATE_GLOBAL = 5;
94 private static final int STATE_LOCK_SETTINGS = 6;
95 private static final int STATE_SOFTAP_CONFIG = 7;
96 private static final int STATE_NETWORK_POLICIES = 8;
Roshan Pius7a2491f2016-06-02 09:22:55 -070097 private static final int STATE_WIFI_NEW_CONFIG = 9;
Christopher Tate66488d62012-10-02 11:58:01 -070098
Roshan Pius7a2491f2016-06-02 09:22:55 -070099 private static final int STATE_SIZE = 10; // The current number of state items
Christopher Tate66488d62012-10-02 11:58:01 -0700100
101 // Number of entries in the checksum array at various version numbers
102 private static final int STATE_SIZES[] = {
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000103 0,
104 4, // version 1
105 5, // version 2 added STATE_WIFI_CONFIG
106 6, // version 3 added STATE_GLOBAL
107 7, // version 4 added STATE_LOCK_SETTINGS
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000108 8, // version 5 added STATE_SOFTAP_CONFIG
Roshan Pius7a2491f2016-06-02 09:22:55 -0700109 9, // version 6 added STATE_NETWORK_POLICIES
110 STATE_SIZE // version 7 added STATE_WIFI_NEW_CONFIG
Christopher Tate66488d62012-10-02 11:58:01 -0700111 };
Amith Yamasanid1582142009-07-08 20:04:55 -0700112
Christopher Tate75a99702011-05-18 16:28:19 -0700113 // Versioning of the 'full backup' format
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000114 // Increment this version any time a new item is added
Roshan Pius7a2491f2016-06-02 09:22:55 -0700115 private static final int FULL_BACKUP_VERSION = 6;
Christopher Tate66488d62012-10-02 11:58:01 -0700116 private static final int FULL_BACKUP_ADDED_GLOBAL = 2; // added the "global" entry
Amith Yamasani072543f2015-02-13 11:09:45 -0800117 private static final int FULL_BACKUP_ADDED_LOCK_SETTINGS = 3; // added the "lock_settings" entry
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000118 private static final int FULL_BACKUP_ADDED_SOFTAP_CONF = 4; //added the "softap_config" entry
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000119 private static final int FULL_BACKUP_ADDED_NETWORK_POLICIES = 5; //added "network_policies"
Roshan Pius7a2491f2016-06-02 09:22:55 -0700120 private static final int FULL_BACKUP_ADDED_WIFI_NEW = 6; // added "wifi_new_config" entry
Christopher Tate75a99702011-05-18 16:28:19 -0700121
Svetoslav Ganova571a582011-09-20 18:32:20 -0700122 private static final int INTEGER_BYTE_COUNT = Integer.SIZE / Byte.SIZE;
Amith Yamasani220f4d62009-07-02 02:34:14 -0700123
124 private static final byte[] EMPTY_DATA = new byte[0];
125
126 private static final String TAG = "SettingsBackupAgent";
127
Amith Yamasani220f4d62009-07-02 02:34:14 -0700128 private static final String[] PROJECTION = {
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000129 Settings.NameValueTable.NAME,
130 Settings.NameValueTable.VALUE
Amith Yamasani220f4d62009-07-02 02:34:14 -0700131 };
132
Christian Sonntag92c17522009-08-07 15:16:17 -0700133 // the key to store the WIFI data under, should be sorted as last, so restore happens last.
134 // use very late unicode character to quasi-guarantee last sort position.
Amith Yamasani2cfab842009-09-09 18:27:31 -0700135 private static final String KEY_WIFI_SUPPLICANT = "\uffedWIFI";
Irfan Sheriff4aeca7c52011-03-10 16:53:33 -0800136 private static final String KEY_WIFI_CONFIG = "\uffedCONFIG_WIFI";
Amith Yamasani220f4d62009-07-02 02:34:14 -0700137
Amith Yamasani072543f2015-02-13 11:09:45 -0800138 // Keys within the lock settings section
139 private static final String KEY_LOCK_SETTINGS_OWNER_INFO_ENABLED = "owner_info_enabled";
140 private static final String KEY_LOCK_SETTINGS_OWNER_INFO = "owner_info";
Bryan Mawhinneye483b562017-05-15 14:46:05 +0100141 private static final String KEY_LOCK_SETTINGS_VISIBLE_PATTERN_ENABLED =
142 "visible_pattern_enabled";
143 private static final String KEY_LOCK_SETTINGS_POWER_BUTTON_INSTANTLY_LOCKS =
144 "power_button_instantly_locks";
Amith Yamasani072543f2015-02-13 11:09:45 -0800145
Christopher Tate4a627c72011-04-01 14:43:32 -0700146 // Name of the temporary file we use during full backup/restore. This is
147 // stored in the full-backup tarfile as well, so should not be changed.
148 private static final String STAGE_FILE = "flattened-data";
149
Amith Yamasani220f4d62009-07-02 02:34:14 -0700150 private SettingsHelper mSettingsHelper;
151
Roshan Pius7a2491f2016-06-02 09:22:55 -0700152 private WifiManager mWifiManager;
Christopher Tate8dfe2b92012-05-15 15:05:04 -0700153
Michal Karpinski6135a262017-08-11 10:45:58 +0100154 // Version of the SDK that com.android.providers.settings package has been restored from.
155 // Populated in onRestore().
156 private int mRestoredFromSdkInt;
157
Svetoslav Ganova571a582011-09-20 18:32:20 -0700158 @Override
Amith Yamasani220f4d62009-07-02 02:34:14 -0700159 public void onCreate() {
Christopher Tate75a99702011-05-18 16:28:19 -0700160 if (DEBUG_BACKUP) Log.d(TAG, "onCreate() invoked");
161
Amith Yamasani220f4d62009-07-02 02:34:14 -0700162 mSettingsHelper = new SettingsHelper(this);
Roshan Pius7a2491f2016-06-02 09:22:55 -0700163 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
Amith Yamasani220f4d62009-07-02 02:34:14 -0700164 super.onCreate();
165 }
166
167 @Override
168 public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
Ritesh Reddy33117fe2016-02-01 13:45:33 +0000169 ParcelFileDescriptor newState) throws IOException {
Amith Yamasani220f4d62009-07-02 02:34:14 -0700170
171 byte[] systemSettingsData = getSystemSettings();
172 byte[] secureSettingsData = getSecureSettings();
Christopher Tate66488d62012-10-02 11:58:01 -0700173 byte[] globalSettingsData = getGlobalSettings();
Bryan Mawhinneye483b562017-05-15 14:46:05 +0100174 byte[] lockSettingsData = getLockSettings(UserHandle.myUserId());
Amith Yamasani8823c0a82009-07-07 14:30:17 -0700175 byte[] locale = mSettingsHelper.getLocaleData();
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000176 byte[] softApConfigData = getSoftAPConfiguration();
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000177 byte[] netPoliciesData = getNetworkPolicies();
Roshan Pius7a2491f2016-06-02 09:22:55 -0700178 byte[] wifiFullConfigData = getNewWifiConfigData();
Amith Yamasani220f4d62009-07-02 02:34:14 -0700179
Christopher Tate79ec80d2011-06-24 14:58:49 -0700180 long[] stateChecksums = readOldChecksums(oldState);
Amith Yamasani220f4d62009-07-02 02:34:14 -0700181
Christopher Tate79ec80d2011-06-24 14:58:49 -0700182 stateChecksums[STATE_SYSTEM] =
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000183 writeIfChanged(stateChecksums[STATE_SYSTEM], KEY_SYSTEM, systemSettingsData, data);
Christopher Tate79ec80d2011-06-24 14:58:49 -0700184 stateChecksums[STATE_SECURE] =
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000185 writeIfChanged(stateChecksums[STATE_SECURE], KEY_SECURE, secureSettingsData, data);
Christopher Tate66488d62012-10-02 11:58:01 -0700186 stateChecksums[STATE_GLOBAL] =
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000187 writeIfChanged(stateChecksums[STATE_GLOBAL], KEY_GLOBAL, globalSettingsData, data);
Christopher Tate79ec80d2011-06-24 14:58:49 -0700188 stateChecksums[STATE_LOCALE] =
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000189 writeIfChanged(stateChecksums[STATE_LOCALE], KEY_LOCALE, locale, data);
Roshan Pius7a2491f2016-06-02 09:22:55 -0700190 stateChecksums[STATE_WIFI_SUPPLICANT] = 0;
191 stateChecksums[STATE_WIFI_CONFIG] = 0;
Amith Yamasani072543f2015-02-13 11:09:45 -0800192 stateChecksums[STATE_LOCK_SETTINGS] =
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000193 writeIfChanged(stateChecksums[STATE_LOCK_SETTINGS], KEY_LOCK_SETTINGS,
194 lockSettingsData, data);
195 stateChecksums[STATE_SOFTAP_CONFIG] =
196 writeIfChanged(stateChecksums[STATE_SOFTAP_CONFIG], KEY_SOFTAP_CONFIG,
197 softApConfigData, data);
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000198 stateChecksums[STATE_NETWORK_POLICIES] =
199 writeIfChanged(stateChecksums[STATE_NETWORK_POLICIES], KEY_NETWORK_POLICIES,
200 netPoliciesData, data);
Roshan Pius7a2491f2016-06-02 09:22:55 -0700201 stateChecksums[STATE_WIFI_NEW_CONFIG] =
202 writeIfChanged(stateChecksums[STATE_WIFI_NEW_CONFIG], KEY_WIFI_NEW_CONFIG,
203 wifiFullConfigData, data);
Amith Yamasani8823c0a82009-07-07 14:30:17 -0700204
Christopher Tate79ec80d2011-06-24 14:58:49 -0700205 writeNewChecksums(stateChecksums, newState);
Amith Yamasani220f4d62009-07-02 02:34:14 -0700206 }
207
208 @Override
209 public void onRestore(BackupDataInput data, int appVersionCode,
Ritesh Reddy33117fe2016-02-01 13:45:33 +0000210 ParcelFileDescriptor newState) throws IOException {
Amith Yamasani220f4d62009-07-02 02:34:14 -0700211
Michal Karpinski6135a262017-08-11 10:45:58 +0100212 // versionCode of com.android.providers.settings corresponds to SDK_INT
213 mRestoredFromSdkInt = appVersionCode;
214
Christopher Tate66488d62012-10-02 11:58:01 -0700215 HashSet<String> movedToGlobal = new HashSet<String>();
Svetoslav683914b2015-01-15 14:22:26 -0800216 Settings.System.getMovedToGlobalSettings(movedToGlobal);
217 Settings.Secure.getMovedToGlobalSettings(movedToGlobal);
Roshan Pius7a2491f2016-06-02 09:22:55 -0700218 byte[] restoredWifiSupplicantData = null;
219 byte[] restoredWifiIpConfigData = null;
Christopher Tate66488d62012-10-02 11:58:01 -0700220
Amith Yamasani220f4d62009-07-02 02:34:14 -0700221 while (data.readNextHeader()) {
222 final String key = data.getKey();
Amith Yamasani8823c0a82009-07-07 14:30:17 -0700223 final int size = data.getDataSize();
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000224 switch (key) {
225 case KEY_SYSTEM :
226 restoreSettings(data, Settings.System.CONTENT_URI, movedToGlobal);
227 mSettingsHelper.applyAudioSettings();
228 break;
229
230 case KEY_SECURE :
231 restoreSettings(data, Settings.Secure.CONTENT_URI, movedToGlobal);
232 break;
233
234 case KEY_GLOBAL :
235 restoreSettings(data, Settings.Global.CONTENT_URI, null);
236 break;
237
238 case KEY_WIFI_SUPPLICANT :
Roshan Pius7a2491f2016-06-02 09:22:55 -0700239 restoredWifiSupplicantData = new byte[size];
240 data.readEntityData(restoredWifiSupplicantData, 0, size);
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000241 break;
242
243 case KEY_LOCALE :
244 byte[] localeData = new byte[size];
245 data.readEntityData(localeData, 0, size);
246 mSettingsHelper.setLocaleData(localeData, size);
247 break;
248
249 case KEY_WIFI_CONFIG :
Roshan Pius7a2491f2016-06-02 09:22:55 -0700250 restoredWifiIpConfigData = new byte[size];
251 data.readEntityData(restoredWifiIpConfigData, 0, size);
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000252 break;
253
254 case KEY_LOCK_SETTINGS :
Bryan Mawhinneye483b562017-05-15 14:46:05 +0100255 restoreLockSettings(UserHandle.myUserId(), data);
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000256 break;
257
258 case KEY_SOFTAP_CONFIG :
259 byte[] softapData = new byte[size];
260 data.readEntityData(softapData, 0, size);
261 restoreSoftApConfiguration(softapData);
262 break;
263
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000264 case KEY_NETWORK_POLICIES:
265 byte[] netPoliciesData = new byte[size];
266 data.readEntityData(netPoliciesData, 0, size);
267 restoreNetworkPolicies(netPoliciesData);
268 break;
269
Roshan Pius7a2491f2016-06-02 09:22:55 -0700270 case KEY_WIFI_NEW_CONFIG:
271 byte[] restoredWifiNewConfigData = new byte[size];
272 data.readEntityData(restoredWifiNewConfigData, 0, size);
Roshan Pius5db739c2016-06-17 16:51:36 -0700273 restoreNewWifiConfigData(restoredWifiNewConfigData);
Roshan Pius7a2491f2016-06-02 09:22:55 -0700274 break;
275
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000276 default :
277 data.skipEntityData();
278
Amith Yamasani220f4d62009-07-02 02:34:14 -0700279 }
280 }
Christopher Tated488bc02012-10-09 14:06:30 -0700281
Roshan Pius7a2491f2016-06-02 09:22:55 -0700282 // Do this at the end so that we also pull in the ipconfig data.
283 if (restoredWifiSupplicantData != null) {
284 restoreSupplicantWifiConfigData(
Roshan Pius5db739c2016-06-17 16:51:36 -0700285 restoredWifiSupplicantData, restoredWifiIpConfigData);
Christopher Tated488bc02012-10-09 14:06:30 -0700286 }
Amith Yamasani220f4d62009-07-02 02:34:14 -0700287 }
288
Christopher Tate75a99702011-05-18 16:28:19 -0700289 @Override
Christopher Tate79ec80d2011-06-24 14:58:49 -0700290 public void onFullBackup(FullBackupDataOutput data) throws IOException {
291 byte[] systemSettingsData = getSystemSettings();
292 byte[] secureSettingsData = getSecureSettings();
Christopher Tate66488d62012-10-02 11:58:01 -0700293 byte[] globalSettingsData = getGlobalSettings();
Bryan Mawhinneye483b562017-05-15 14:46:05 +0100294 byte[] lockSettingsData = getLockSettings(UserHandle.myUserId());
Christopher Tate79ec80d2011-06-24 14:58:49 -0700295 byte[] locale = mSettingsHelper.getLocaleData();
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000296 byte[] softApConfigData = getSoftAPConfiguration();
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000297 byte[] netPoliciesData = getNetworkPolicies();
Roshan Pius7a2491f2016-06-02 09:22:55 -0700298 byte[] wifiFullConfigData = getNewWifiConfigData();
Christopher Tate79ec80d2011-06-24 14:58:49 -0700299
300 // Write the data to the staging file, then emit that as our tarfile
301 // representation of the backed-up settings.
302 String root = getFilesDir().getAbsolutePath();
303 File stage = new File(root, STAGE_FILE);
304 try {
305 FileOutputStream filestream = new FileOutputStream(stage);
306 BufferedOutputStream bufstream = new BufferedOutputStream(filestream);
307 DataOutputStream out = new DataOutputStream(bufstream);
308
Christopher Tate2efd2db2011-07-19 16:32:49 -0700309 if (DEBUG_BACKUP) Log.d(TAG, "Writing flattened data version " + FULL_BACKUP_VERSION);
Christopher Tate79ec80d2011-06-24 14:58:49 -0700310 out.writeInt(FULL_BACKUP_VERSION);
311
Christopher Tate2efd2db2011-07-19 16:32:49 -0700312 if (DEBUG_BACKUP) Log.d(TAG, systemSettingsData.length + " bytes of settings data");
Christopher Tate79ec80d2011-06-24 14:58:49 -0700313 out.writeInt(systemSettingsData.length);
314 out.write(systemSettingsData);
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000315 if (DEBUG_BACKUP) {
316 Log.d(TAG, secureSettingsData.length + " bytes of secure settings data");
317 }
Christopher Tate79ec80d2011-06-24 14:58:49 -0700318 out.writeInt(secureSettingsData.length);
319 out.write(secureSettingsData);
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000320 if (DEBUG_BACKUP) {
321 Log.d(TAG, globalSettingsData.length + " bytes of global settings data");
322 }
Christopher Tate66488d62012-10-02 11:58:01 -0700323 out.writeInt(globalSettingsData.length);
324 out.write(globalSettingsData);
Christopher Tate2efd2db2011-07-19 16:32:49 -0700325 if (DEBUG_BACKUP) Log.d(TAG, locale.length + " bytes of locale data");
Christopher Tate79ec80d2011-06-24 14:58:49 -0700326 out.writeInt(locale.length);
327 out.write(locale);
Amith Yamasani072543f2015-02-13 11:09:45 -0800328 if (DEBUG_BACKUP) Log.d(TAG, lockSettingsData.length + " bytes of lock settings data");
329 out.writeInt(lockSettingsData.length);
330 out.write(lockSettingsData);
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000331 if (DEBUG_BACKUP) Log.d(TAG, softApConfigData.length + " bytes of softap config data");
332 out.writeInt(softApConfigData.length);
333 out.write(softApConfigData);
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000334 if (DEBUG_BACKUP) Log.d(TAG, netPoliciesData.length + " bytes of net policies data");
335 out.writeInt(netPoliciesData.length);
336 out.write(netPoliciesData);
Roshan Pius7a2491f2016-06-02 09:22:55 -0700337 if (DEBUG_BACKUP) {
338 Log.d(TAG, wifiFullConfigData.length + " bytes of wifi config data");
339 }
340 out.writeInt(wifiFullConfigData.length);
341 out.write(wifiFullConfigData);
Christopher Tate79ec80d2011-06-24 14:58:49 -0700342
343 out.flush(); // also flushes downstream
344
345 // now we're set to emit the tar stream
346 fullBackupFile(stage, data);
347 } finally {
348 stage.delete();
349 }
350 }
351
352 @Override
Christopher Tate75a99702011-05-18 16:28:19 -0700353 public void onRestoreFile(ParcelFileDescriptor data, long size,
Ritesh Reddy33117fe2016-02-01 13:45:33 +0000354 int type, String domain, String relpath, long mode, long mtime)
Christopher Tate75a99702011-05-18 16:28:19 -0700355 throws IOException {
356 if (DEBUG_BACKUP) Log.d(TAG, "onRestoreFile() invoked");
357 // Our data is actually a blob of flattened settings data identical to that
358 // produced during incremental backups. Just unpack and apply it all in
359 // turn.
360 FileInputStream instream = new FileInputStream(data.getFileDescriptor());
361 DataInputStream in = new DataInputStream(instream);
362
363 int version = in.readInt();
364 if (DEBUG_BACKUP) Log.d(TAG, "Flattened data version " + version);
Christopher Tate66488d62012-10-02 11:58:01 -0700365 if (version <= FULL_BACKUP_VERSION) {
366 // Generate the moved-to-global lookup table
367 HashSet<String> movedToGlobal = new HashSet<String>();
Svetoslav683914b2015-01-15 14:22:26 -0800368 Settings.System.getMovedToGlobalSettings(movedToGlobal);
369 Settings.Secure.getMovedToGlobalSettings(movedToGlobal);
Christopher Tate66488d62012-10-02 11:58:01 -0700370
Christopher Tate75a99702011-05-18 16:28:19 -0700371 // system settings data first
372 int nBytes = in.readInt();
373 if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of settings data");
374 byte[] buffer = new byte[nBytes];
Christopher Tate2efd2db2011-07-19 16:32:49 -0700375 in.readFully(buffer, 0, nBytes);
Christopher Tate66488d62012-10-02 11:58:01 -0700376 restoreSettings(buffer, nBytes, Settings.System.CONTENT_URI, movedToGlobal);
Christopher Tate75a99702011-05-18 16:28:19 -0700377
378 // secure settings
379 nBytes = in.readInt();
380 if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of secure settings data");
381 if (nBytes > buffer.length) buffer = new byte[nBytes];
Christopher Tate2efd2db2011-07-19 16:32:49 -0700382 in.readFully(buffer, 0, nBytes);
Christopher Tate66488d62012-10-02 11:58:01 -0700383 restoreSettings(buffer, nBytes, Settings.Secure.CONTENT_URI, movedToGlobal);
384
385 // Global only if sufficiently new
386 if (version >= FULL_BACKUP_ADDED_GLOBAL) {
387 nBytes = in.readInt();
388 if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of global settings data");
389 if (nBytes > buffer.length) buffer = new byte[nBytes];
390 in.readFully(buffer, 0, nBytes);
391 movedToGlobal.clear(); // no redirection; this *is* the global namespace
392 restoreSettings(buffer, nBytes, Settings.Global.CONTENT_URI, movedToGlobal);
393 }
Christopher Tate75a99702011-05-18 16:28:19 -0700394
395 // locale
396 nBytes = in.readInt();
397 if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of locale data");
398 if (nBytes > buffer.length) buffer = new byte[nBytes];
Christopher Tate2efd2db2011-07-19 16:32:49 -0700399 in.readFully(buffer, 0, nBytes);
Christopher Tate75a99702011-05-18 16:28:19 -0700400 mSettingsHelper.setLocaleData(buffer, nBytes);
401
Roshan Pius7a2491f2016-06-02 09:22:55 -0700402 // Restore older backups performing the necessary migrations.
403 if (version < FULL_BACKUP_ADDED_WIFI_NEW) {
404 // wifi supplicant
405 int supplicant_size = in.readInt();
406 if (DEBUG_BACKUP) Log.d(TAG, supplicant_size + " bytes of wifi supplicant data");
407 byte[] supplicant_buffer = new byte[supplicant_size];
408 in.readFully(supplicant_buffer, 0, supplicant_size);
Christopher Tate75a99702011-05-18 16:28:19 -0700409
Roshan Pius7a2491f2016-06-02 09:22:55 -0700410 // ip config
411 int ipconfig_size = in.readInt();
412 if (DEBUG_BACKUP) Log.d(TAG, ipconfig_size + " bytes of ip config data");
413 byte[] ipconfig_buffer = new byte[ipconfig_size];
414 in.readFully(ipconfig_buffer, 0, nBytes);
Roshan Pius5db739c2016-06-17 16:51:36 -0700415 restoreSupplicantWifiConfigData(supplicant_buffer, ipconfig_buffer);
Roshan Pius7a2491f2016-06-02 09:22:55 -0700416 }
Christopher Tate75a99702011-05-18 16:28:19 -0700417
Amith Yamasani072543f2015-02-13 11:09:45 -0800418 if (version >= FULL_BACKUP_ADDED_LOCK_SETTINGS) {
419 nBytes = in.readInt();
420 if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of lock settings data");
421 if (nBytes > buffer.length) buffer = new byte[nBytes];
422 if (nBytes > 0) {
423 in.readFully(buffer, 0, nBytes);
Bryan Mawhinneye483b562017-05-15 14:46:05 +0100424 restoreLockSettings(UserHandle.myUserId(), buffer, nBytes);
Amith Yamasani072543f2015-02-13 11:09:45 -0800425 }
426 }
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000427 // softap config
428 if (version >= FULL_BACKUP_ADDED_SOFTAP_CONF) {
429 nBytes = in.readInt();
430 if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of softap config data");
431 if (nBytes > buffer.length) buffer = new byte[nBytes];
432 if (nBytes > 0) {
433 in.readFully(buffer, 0, nBytes);
434 restoreSoftApConfiguration(buffer);
435 }
436 }
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000437 // network policies
438 if (version >= FULL_BACKUP_ADDED_NETWORK_POLICIES) {
439 nBytes = in.readInt();
440 if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of network policies data");
441 if (nBytes > buffer.length) buffer = new byte[nBytes];
442 if (nBytes > 0) {
443 in.readFully(buffer, 0, nBytes);
444 restoreNetworkPolicies(buffer);
445 }
446 }
Roshan Pius7a2491f2016-06-02 09:22:55 -0700447 // Restore full wifi config data
448 if (version >= FULL_BACKUP_ADDED_WIFI_NEW) {
449 nBytes = in.readInt();
450 if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of full wifi config data");
451 if (nBytes > buffer.length) buffer = new byte[nBytes];
452 in.readFully(buffer, 0, nBytes);
Roshan Pius5db739c2016-06-17 16:51:36 -0700453 restoreNewWifiConfigData(buffer);
Roshan Pius7a2491f2016-06-02 09:22:55 -0700454 }
455
Christopher Tate75a99702011-05-18 16:28:19 -0700456 if (DEBUG_BACKUP) Log.d(TAG, "Full restore complete.");
457 } else {
458 data.close();
459 throw new IOException("Invalid file schema");
460 }
461 }
462
Amith Yamasanid1582142009-07-08 20:04:55 -0700463 private long[] readOldChecksums(ParcelFileDescriptor oldState) throws IOException {
464 long[] stateChecksums = new long[STATE_SIZE];
465
466 DataInputStream dataInput = new DataInputStream(
467 new FileInputStream(oldState.getFileDescriptor()));
Christopher Tatea286f412009-09-18 15:51:15 -0700468
469 try {
470 int stateVersion = dataInput.readInt();
Ritesh Reddy2400d272016-02-02 11:42:26 +0000471 if (stateVersion > STATE_VERSION) {
472 // Constrain the maximum state version this backup agent
473 // can handle in case a newer or corrupt backup set existed
474 stateVersion = STATE_VERSION;
475 }
Christopher Tate66488d62012-10-02 11:58:01 -0700476 for (int i = 0; i < STATE_SIZES[stateVersion]; i++) {
477 stateChecksums[i] = dataInput.readLong();
Amith Yamasanid1582142009-07-08 20:04:55 -0700478 }
Christopher Tatea286f412009-09-18 15:51:15 -0700479 } catch (EOFException eof) {
480 // With the default 0 checksum we'll wind up forcing a backup of
481 // any unhandled data sets, which is appropriate.
Amith Yamasanid1582142009-07-08 20:04:55 -0700482 }
483 dataInput.close();
484 return stateChecksums;
485 }
486
487 private void writeNewChecksums(long[] checksums, ParcelFileDescriptor newState)
488 throws IOException {
489 DataOutputStream dataOutput = new DataOutputStream(
Marvin Paul68795552014-12-16 14:12:33 -0800490 new BufferedOutputStream(new FileOutputStream(newState.getFileDescriptor())));
Christopher Tatea286f412009-09-18 15:51:15 -0700491
492 dataOutput.writeInt(STATE_VERSION);
Amith Yamasanid1582142009-07-08 20:04:55 -0700493 for (int i = 0; i < STATE_SIZE; i++) {
494 dataOutput.writeLong(checksums[i]);
495 }
496 dataOutput.close();
497 }
498
499 private long writeIfChanged(long oldChecksum, String key, byte[] data,
Ritesh Reddy33117fe2016-02-01 13:45:33 +0000500 BackupDataOutput output) {
Amith Yamasanid1582142009-07-08 20:04:55 -0700501 CRC32 checkSummer = new CRC32();
502 checkSummer.update(data);
503 long newChecksum = checkSummer.getValue();
504 if (oldChecksum == newChecksum) {
505 return oldChecksum;
506 }
507 try {
Amith Yamasani072543f2015-02-13 11:09:45 -0800508 if (DEBUG_BACKUP) {
509 Log.v(TAG, "Writing entity " + key + " of size " + data.length);
510 }
Amith Yamasanid1582142009-07-08 20:04:55 -0700511 output.writeEntityHeader(key, data.length);
512 output.writeEntityData(data, data.length);
513 } catch (IOException ioe) {
514 // Bail
515 }
516 return newChecksum;
517 }
518
Amith Yamasani220f4d62009-07-02 02:34:14 -0700519 private byte[] getSystemSettings() {
Svetoslav Ganova571a582011-09-20 18:32:20 -0700520 Cursor cursor = getContentResolver().query(Settings.System.CONTENT_URI, PROJECTION, null,
521 null, null);
Jeff Brown1d8e7d62011-10-09 15:24:53 -0700522 try {
523 return extractRelevantValues(cursor, Settings.System.SETTINGS_TO_BACKUP);
524 } finally {
525 cursor.close();
526 }
Amith Yamasani220f4d62009-07-02 02:34:14 -0700527 }
528
529 private byte[] getSecureSettings() {
Svetoslav Ganova571a582011-09-20 18:32:20 -0700530 Cursor cursor = getContentResolver().query(Settings.Secure.CONTENT_URI, PROJECTION, null,
531 null, null);
Jeff Brown1d8e7d62011-10-09 15:24:53 -0700532 try {
533 return extractRelevantValues(cursor, Settings.Secure.SETTINGS_TO_BACKUP);
534 } finally {
535 cursor.close();
536 }
Amith Yamasani220f4d62009-07-02 02:34:14 -0700537 }
538
Christopher Tate66488d62012-10-02 11:58:01 -0700539 private byte[] getGlobalSettings() {
540 Cursor cursor = getContentResolver().query(Settings.Global.CONTENT_URI, PROJECTION, null,
541 null, null);
542 try {
543 return extractRelevantValues(cursor, Settings.Global.SETTINGS_TO_BACKUP);
544 } finally {
545 cursor.close();
546 }
547 }
548
Amith Yamasani072543f2015-02-13 11:09:45 -0800549 /**
Bryan Mawhinneye483b562017-05-15 14:46:05 +0100550 * Serialize the owner info and other lock settings
Amith Yamasani072543f2015-02-13 11:09:45 -0800551 */
Bryan Mawhinneye483b562017-05-15 14:46:05 +0100552 private byte[] getLockSettings(@UserIdInt int userId) {
Amith Yamasani072543f2015-02-13 11:09:45 -0800553 final LockPatternUtils lockPatternUtils = new LockPatternUtils(this);
Bryan Mawhinneye483b562017-05-15 14:46:05 +0100554 final boolean ownerInfoEnabled = lockPatternUtils.isOwnerInfoEnabled(userId);
555 final String ownerInfo = lockPatternUtils.getOwnerInfo(userId);
556 final boolean lockPatternEnabled = lockPatternUtils.isLockPatternEnabled(userId);
557 final boolean visiblePatternEnabled = lockPatternUtils.isVisiblePatternEnabled(userId);
558 final boolean powerButtonInstantlyLocks =
559 lockPatternUtils.getPowerButtonInstantlyLocks(userId);
Amith Yamasani072543f2015-02-13 11:09:45 -0800560
561 ByteArrayOutputStream baos = new ByteArrayOutputStream();
562 DataOutputStream out = new DataOutputStream(baos);
563 try {
564 out.writeUTF(KEY_LOCK_SETTINGS_OWNER_INFO_ENABLED);
565 out.writeUTF(ownerInfoEnabled ? "1" : "0");
566 if (ownerInfo != null) {
567 out.writeUTF(KEY_LOCK_SETTINGS_OWNER_INFO);
568 out.writeUTF(ownerInfo != null ? ownerInfo : "");
569 }
Bryan Mawhinneye483b562017-05-15 14:46:05 +0100570 if (lockPatternUtils.isVisiblePatternEverChosen(userId)) {
571 out.writeUTF(KEY_LOCK_SETTINGS_VISIBLE_PATTERN_ENABLED);
572 out.writeUTF(visiblePatternEnabled ? "1" : "0");
573 }
574 if (lockPatternUtils.isPowerButtonInstantlyLocksEverChosen(userId)) {
575 out.writeUTF(KEY_LOCK_SETTINGS_POWER_BUTTON_INSTANTLY_LOCKS);
576 out.writeUTF(powerButtonInstantlyLocks ? "1" : "0");
577 }
Amith Yamasani072543f2015-02-13 11:09:45 -0800578 // End marker
579 out.writeUTF("");
580 out.flush();
581 } catch (IOException ioe) {
582 }
583 return baos.toByteArray();
584 }
585
Christopher Tate66488d62012-10-02 11:58:01 -0700586 private void restoreSettings(BackupDataInput data, Uri contentUri,
Ritesh Reddy33117fe2016-02-01 13:45:33 +0000587 HashSet<String> movedToGlobal) {
Christopher Tate75a99702011-05-18 16:28:19 -0700588 byte[] settings = new byte[data.getDataSize()];
589 try {
590 data.readEntityData(settings, 0, settings.length);
591 } catch (IOException ioe) {
592 Log.e(TAG, "Couldn't read entity data");
593 return;
594 }
Christopher Tate66488d62012-10-02 11:58:01 -0700595 restoreSettings(settings, settings.length, contentUri, movedToGlobal);
Christopher Tate75a99702011-05-18 16:28:19 -0700596 }
597
Christopher Tate66488d62012-10-02 11:58:01 -0700598 private void restoreSettings(byte[] settings, int bytes, Uri contentUri,
Ritesh Reddy33117fe2016-02-01 13:45:33 +0000599 HashSet<String> movedToGlobal) {
Svetoslav Ganova571a582011-09-20 18:32:20 -0700600 if (DEBUG) {
601 Log.i(TAG, "restoreSettings: " + contentUri);
602 }
603
Christopher Tate1d0fca32017-06-06 13:30:00 -0700604 // Figure out the white list and redirects to the global table. We restore anything
605 // in either the backup whitelist or the legacy-restore whitelist for this table.
Christopher Tate6597e342015-02-17 12:15:25 -0800606 final String[] whitelist;
Christopher Tate796e0f02009-09-22 11:57:58 -0700607 if (contentUri.equals(Settings.Secure.CONTENT_URI)) {
Christopher Tate1d0fca32017-06-06 13:30:00 -0700608 whitelist = concat(Settings.Secure.SETTINGS_TO_BACKUP,
609 Settings.Secure.LEGACY_RESTORE_SETTINGS);
Christopher Tate796e0f02009-09-22 11:57:58 -0700610 } else if (contentUri.equals(Settings.System.CONTENT_URI)) {
Christopher Tate1d0fca32017-06-06 13:30:00 -0700611 whitelist = concat(Settings.System.SETTINGS_TO_BACKUP,
612 Settings.System.LEGACY_RESTORE_SETTINGS);
Christopher Tate66488d62012-10-02 11:58:01 -0700613 } else if (contentUri.equals(Settings.Global.CONTENT_URI)) {
Christopher Tate1d0fca32017-06-06 13:30:00 -0700614 whitelist = concat(Settings.Global.SETTINGS_TO_BACKUP,
615 Settings.Global.LEGACY_RESTORE_SETTINGS);
Svetoslav Ganova571a582011-09-20 18:32:20 -0700616 } else {
617 throw new IllegalArgumentException("Unknown URI: " + contentUri);
Christopher Tate796e0f02009-09-22 11:57:58 -0700618 }
619
Svetoslav Ganova571a582011-09-20 18:32:20 -0700620 // Restore only the white list data.
Amith Yamasani220f4d62009-07-02 02:34:14 -0700621 int pos = 0;
Svet Ganovaabc9632017-07-28 19:37:15 -0700622 final ArrayMap<String, String> cachedEntries = new ArrayMap<>();
Svetoslav Ganova571a582011-09-20 18:32:20 -0700623 ContentValues contentValues = new ContentValues(2);
624 SettingsHelper settingsHelper = mSettingsHelper;
Christopher Tate6597e342015-02-17 12:15:25 -0800625 ContentResolver cr = getContentResolver();
Christopher Tate0738e882009-09-11 16:35:39 -0700626
Svetoslav Ganova571a582011-09-20 18:32:20 -0700627 final int whiteListSize = whitelist.length;
628 for (int i = 0; i < whiteListSize; i++) {
629 String key = whitelist[i];
Christopher Tate0738e882009-09-11 16:35:39 -0700630
Svet Ganovaabc9632017-07-28 19:37:15 -0700631 String value = null;
632 boolean hasValueToRestore = false;
633 if (cachedEntries.indexOfKey(key) >= 0) {
634 value = cachedEntries.remove(key);
635 hasValueToRestore = true;
636 } else {
637 // If the value not cached, let us look it up.
Svetoslav Ganova571a582011-09-20 18:32:20 -0700638 while (pos < bytes) {
639 int length = readInt(settings, pos);
640 pos += INTEGER_BYTE_COUNT;
Svet Ganovaabc9632017-07-28 19:37:15 -0700641 String dataKey = length >= 0 ? new String(settings, pos, length) : null;
Svetoslav Ganova571a582011-09-20 18:32:20 -0700642 pos += length;
643 length = readInt(settings, pos);
644 pos += INTEGER_BYTE_COUNT;
Svet Ganovaabc9632017-07-28 19:37:15 -0700645 String dataValue = null;
646 if (length >= 0) {
647 dataValue = new String(settings, pos, length);
648 pos += length;
649 }
Svetoslav Ganova571a582011-09-20 18:32:20 -0700650 if (key.equals(dataKey)) {
651 value = dataValue;
Svet Ganovaabc9632017-07-28 19:37:15 -0700652 hasValueToRestore = true;
Svetoslav Ganova571a582011-09-20 18:32:20 -0700653 break;
654 }
655 cachedEntries.put(dataKey, dataValue);
Amith Yamasani70c874b2009-07-06 14:53:25 -0700656 }
Amith Yamasani220f4d62009-07-02 02:34:14 -0700657 }
Amith Yamasani220f4d62009-07-02 02:34:14 -0700658
Svet Ganovaabc9632017-07-28 19:37:15 -0700659 if (!hasValueToRestore) {
Svetoslav Ganova571a582011-09-20 18:32:20 -0700660 continue;
661 }
Christopher Tate796e0f02009-09-22 11:57:58 -0700662
Christopher Tate3543beb2012-10-05 13:35:12 -0700663 final Uri destination = (movedToGlobal != null && movedToGlobal.contains(key))
Christopher Tate66488d62012-10-02 11:58:01 -0700664 ? Settings.Global.CONTENT_URI
665 : contentUri;
Michal Karpinski6135a262017-08-11 10:45:58 +0100666 settingsHelper.restoreValue(this, cr, contentValues, destination, key, value,
667 mRestoredFromSdkInt);
Svetoslav Ganova571a582011-09-20 18:32:20 -0700668
Christopher Tated488bc02012-10-09 14:06:30 -0700669 if (DEBUG) {
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000670 Log.d(TAG, "Restored setting: " + destination + " : " + key + "=" + value);
Christopher Tate0738e882009-09-11 16:35:39 -0700671 }
672 }
Amith Yamasani220f4d62009-07-02 02:34:14 -0700673 }
674
Christopher Tate1d0fca32017-06-06 13:30:00 -0700675 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 Yamasani220f4d62009-07-02 02:34:14 -0700687 /**
Bryan Mawhinneye483b562017-05-15 14:46:05 +0100688 * Restores the owner info enabled and other settings in LockSettings.
Amith Yamasani072543f2015-02-13 11:09:45 -0800689 *
690 * @param buffer
691 * @param nBytes
692 */
Bryan Mawhinneye483b562017-05-15 14:46:05 +0100693 private void restoreLockSettings(@UserIdInt int userId, byte[] buffer, int nBytes) {
Amith Yamasani072543f2015-02-13 11:09:45 -0800694 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 Mawhinneye483b562017-05-15 14:46:05 +0100708 lockPatternUtils.setOwnerInfoEnabled("1".equals(value), userId);
Amith Yamasani072543f2015-02-13 11:09:45 -0800709 break;
710 case KEY_LOCK_SETTINGS_OWNER_INFO:
Bryan Mawhinneye483b562017-05-15 14:46:05 +0100711 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 Yamasani072543f2015-02-13 11:09:45 -0800719 break;
720 }
721 }
722 in.close();
723 } catch (IOException ioe) {
724 }
725 }
726
Bryan Mawhinneye483b562017-05-15 14:46:05 +0100727 private void restoreLockSettings(@UserIdInt int userId, BackupDataInput data) {
Amith Yamasani072543f2015-02-13 11:09:45 -0800728 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 Mawhinneye483b562017-05-15 14:46:05 +0100735 restoreLockSettings(userId, settings, settings.length);
Amith Yamasani072543f2015-02-13 11:09:45 -0800736 }
737
738 /**
Svetoslav Ganova571a582011-09-20 18:32:20 -0700739 * 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 Yamasani220f4d62009-07-02 02:34:14 -0700745 */
Svetoslav Ganova571a582011-09-20 18:32:20 -0700746 private byte[] extractRelevantValues(Cursor cursor, String[] settings) {
Svetoslav Ganova571a582011-09-20 18:32:20 -0700747 if (!cursor.moveToFirst()) {
Amith Yamasani220f4d62009-07-02 02:34:14 -0700748 Log.e(TAG, "Couldn't read from the cursor");
749 return new byte[0];
750 }
Svetoslav Ganova571a582011-09-20 18:32:20 -0700751
Svet Ganovaabc9632017-07-28 19:37:15 -0700752 final int nameColumnIndex = cursor.getColumnIndex(Settings.NameValueTable.NAME);
753 final int valueColumnIndex = cursor.getColumnIndex(Settings.NameValueTable.VALUE);
754
Svetoslav Ganova571a582011-09-20 18:32:20 -0700755 // Obtain the relevant data in a temporary array.
Amith Yamasani220f4d62009-07-02 02:34:14 -0700756 int totalSize = 0;
Svetoslav Ganova571a582011-09-20 18:32:20 -0700757 int backedUpSettingIndex = 0;
Svet Ganovaabc9632017-07-28 19:37:15 -0700758 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 Ganova571a582011-09-20 18:32:20 -0700761 for (int i = 0; i < settingsCount; i++) {
Svet Ganovaabc9632017-07-28 19:37:15 -0700762 final String key = settings[i];
Svetoslav683914b2015-01-15 14:22:26 -0800763
Svetoslav Ganova571a582011-09-20 18:32:20 -0700764 // If the value not cached, let us look it up.
Svet Ganovaabc9632017-07-28 19:37:15 -0700765 String value = null;
766 boolean hasValueToBackup = false;
767 if (cachedEntries.indexOfKey(key) >= 0) {
768 value = cachedEntries.remove(key);
769 hasValueToBackup = true;
770 } else {
Svetoslav Ganova571a582011-09-20 18:32:20 -0700771 while (!cursor.isAfterLast()) {
Svet Ganovaabc9632017-07-28 19:37:15 -0700772 final String cursorKey = cursor.getString(nameColumnIndex);
773 final String cursorValue = cursor.getString(valueColumnIndex);
Svetoslav Ganova571a582011-09-20 18:32:20 -0700774 cursor.moveToNext();
775 if (key.equals(cursorKey)) {
776 value = cursorValue;
Svet Ganovaabc9632017-07-28 19:37:15 -0700777 hasValueToBackup = true;
Svetoslav Ganova571a582011-09-20 18:32:20 -0700778 break;
779 }
780 cachedEntries.put(cursorKey, cursorValue);
Amith Yamasani220f4d62009-07-02 02:34:14 -0700781 }
Amith Yamasani220f4d62009-07-02 02:34:14 -0700782 }
Svetoslav Ganova571a582011-09-20 18:32:20 -0700783
Svet Ganovaabc9632017-07-28 19:37:15 -0700784 if (!hasValueToBackup) {
785 continue;
786 }
787
Amith Yamasani622bf2222013-09-06 13:54:28 -0700788 // Intercept the keys and see if they need special handling
789 value = mSettingsHelper.onBackupValue(key, value);
790
Svetoslav Ganova571a582011-09-20 18:32:20 -0700791 // Write the key and value in the intermediary array.
Svet Ganovaabc9632017-07-28 19:37:15 -0700792 final byte[] keyBytes = key.getBytes();
Svetoslav Ganova571a582011-09-20 18:32:20 -0700793 totalSize += INTEGER_BYTE_COUNT + keyBytes.length;
794 values[backedUpSettingIndex * 2] = keyBytes;
795
Svet Ganovaabc9632017-07-28 19:37:15 -0700796 final byte[] valueBytes = (value != null) ? value.getBytes() : NULL_VALUE;
Svetoslav Ganova571a582011-09-20 18:32:20 -0700797 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 Yamasani220f4d62009-07-02 02:34:14 -0700804 }
805 }
806
Svetoslav Ganova571a582011-09-20 18:32:20 -0700807 // Aggregate the result.
Amith Yamasani220f4d62009-07-02 02:34:14 -0700808 byte[] result = new byte[totalSize];
809 int pos = 0;
Svetoslav Ganova571a582011-09-20 18:32:20 -0700810 final int keyValuePairCount = backedUpSettingIndex * 2;
811 for (int i = 0; i < keyValuePairCount; i++) {
Svet Ganovaabc9632017-07-28 19:37:15 -0700812 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 Yamasani220f4d62009-07-02 02:34:14 -0700819 }
820 return result;
821 }
822
Roshan Pius5db739c2016-06-17 16:51:36 -0700823 private void restoreSupplicantWifiConfigData(byte[] supplicant_bytes, byte[] ipconfig_bytes) {
Roshan Pius7a2491f2016-06-02 09:22:55 -0700824 if (DEBUG_BACKUP) {
825 Log.v(TAG, "Applying restored supplicant wifi data");
Irfan Sheriff4aeca7c52011-03-10 16:53:33 -0800826 }
Roshan Pius7a2491f2016-06-02 09:22:55 -0700827 mWifiManager.restoreSupplicantBackupData(supplicant_bytes, ipconfig_bytes);
Amith Yamasani2cfab842009-09-09 18:27:31 -0700828 }
829
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000830 private byte[] getSoftAPConfiguration() {
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000831 try {
Roshan Pius7a2491f2016-06-02 09:22:55 -0700832 return mWifiManager.getWifiApConfiguration().getBytesForBackup();
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000833 } 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 Reddyaeb4c062016-01-26 19:40:48 +0000840 try {
841 WifiConfiguration config = WifiConfiguration
842 .getWifiConfigFromBackup(new DataInputStream(new ByteArrayInputStream(data)));
843 if (DEBUG) Log.d(TAG, "Successfully unMarshaled WifiConfiguration ");
Roshan Pius7a2491f2016-06-02 09:22:55 -0700844 mWifiManager.setWifiApConfiguration(config);
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000845 } catch (IOException | BackupUtils.BadVersionException e) {
846 Log.e(TAG, "Failed to unMarshal SoftAPConfiguration " + e.getMessage());
847 }
848 }
849
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000850 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) {
861 if (policy != null) {
862 byte[] marshaledPolicy = policy.getBytesForBackup();
863 out.writeByte(BackupUtils.NOT_NULL);
864 out.writeInt(marshaledPolicy.length);
865 out.write(marshaledPolicy);
866 } else {
867 out.writeByte(BackupUtils.NULL);
868 }
869 }
870 } catch (IOException ioe) {
871 Log.e(TAG, "Failed to convert NetworkPolicies to byte array " + ioe.getMessage());
872 baos.reset();
873 }
874 }
875 return baos.toByteArray();
876 }
877
Roshan Pius7a2491f2016-06-02 09:22:55 -0700878 private byte[] getNewWifiConfigData() {
879 return mWifiManager.retrieveBackupData();
880 }
881
Roshan Pius5db739c2016-06-17 16:51:36 -0700882 private void restoreNewWifiConfigData(byte[] bytes) {
Roshan Pius7a2491f2016-06-02 09:22:55 -0700883 if (DEBUG_BACKUP) {
884 Log.v(TAG, "Applying restored wifi data");
885 }
886 mWifiManager.restoreBackupData(bytes);
887 }
888
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000889 private void restoreNetworkPolicies(byte[] data) {
890 NetworkPolicyManager networkPolicyManager =
891 (NetworkPolicyManager) getSystemService(NETWORK_POLICY_SERVICE);
892 if (data != null && data.length != 0) {
893 DataInputStream in = new DataInputStream(new ByteArrayInputStream(data));
894 try {
895 int version = in.readInt();
896 if (version < 1 || version > NETWORK_POLICIES_BACKUP_VERSION) {
897 throw new BackupUtils.BadVersionException(
898 "Unknown Backup Serialization Version");
899 }
900 int length = in.readInt();
901 NetworkPolicy[] policies = new NetworkPolicy[length];
902 for (int i = 0; i < length; i++) {
903 byte isNull = in.readByte();
904 if (isNull == BackupUtils.NULL) continue;
905 int byteLength = in.readInt();
906 byte[] policyData = new byte[byteLength];
907 in.read(policyData, 0, byteLength);
908 policies[i] = NetworkPolicy.getNetworkPolicyFromBackup(
909 new DataInputStream(new ByteArrayInputStream(policyData)));
910 }
911 // Only set the policies if there was no error in the restore operation
912 networkPolicyManager.setNetworkPolicies(policies);
913 } catch (NullPointerException | IOException | BackupUtils.BadVersionException e) {
914 // NPE can be thrown when trying to instantiate a NetworkPolicy
915 Log.e(TAG, "Failed to convert byte array to NetworkPolicies " + e.getMessage());
916 }
917 }
918 }
919
Amith Yamasani220f4d62009-07-02 02:34:14 -0700920 /**
921 * Write an int in BigEndian into the byte array.
922 * @param out byte array
923 * @param pos current pos in array
924 * @param value integer to write
Svetoslav Ganova571a582011-09-20 18:32:20 -0700925 * @return the index after adding the size of an int (4) in bytes.
Amith Yamasani220f4d62009-07-02 02:34:14 -0700926 */
927 private int writeInt(byte[] out, int pos, int value) {
928 out[pos + 0] = (byte) ((value >> 24) & 0xFF);
929 out[pos + 1] = (byte) ((value >> 16) & 0xFF);
930 out[pos + 2] = (byte) ((value >> 8) & 0xFF);
931 out[pos + 3] = (byte) ((value >> 0) & 0xFF);
Svetoslav Ganova571a582011-09-20 18:32:20 -0700932 return pos + INTEGER_BYTE_COUNT;
Amith Yamasani220f4d62009-07-02 02:34:14 -0700933 }
934
935 private int writeBytes(byte[] out, int pos, byte[] value) {
936 System.arraycopy(value, 0, out, pos, value.length);
937 return pos + value.length;
938 }
939
940 private int readInt(byte[] in, int pos) {
Ritesh Reddyaeb4c062016-01-26 19:40:48 +0000941 int result = ((in[pos] & 0xFF) << 24)
942 | ((in[pos + 1] & 0xFF) << 16)
943 | ((in[pos + 2] & 0xFF) << 8)
944 | ((in[pos + 3] & 0xFF) << 0);
Amith Yamasani220f4d62009-07-02 02:34:14 -0700945 return result;
946 }
Paul Stewart45e6fec2016-05-20 08:14:30 -0700947}