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 | |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 19 | import android.app.backup.BackupAgentHelper; |
| 20 | import android.app.backup.BackupDataInput; |
| 21 | import android.app.backup.BackupDataOutput; |
| 22 | import android.app.backup.FullBackupDataOutput; |
| 23 | import android.content.ContentValues; |
| 24 | import android.content.Context; |
| 25 | import android.database.Cursor; |
| 26 | import android.net.Uri; |
| 27 | import android.net.wifi.WifiManager; |
| 28 | import android.os.FileUtils; |
| 29 | import android.os.ParcelFileDescriptor; |
| 30 | import android.os.Process; |
| 31 | import android.provider.Settings; |
| 32 | import android.util.Log; |
| 33 | |
Brad Fitzpatrick | 7078789 | 2010-11-17 11:31:12 -0800 | [diff] [blame] | 34 | import java.io.BufferedOutputStream; |
Amith Yamasani | 2cfab84 | 2009-09-09 18:27:31 -0700 | [diff] [blame] | 35 | import java.io.BufferedReader; |
| 36 | import java.io.BufferedWriter; |
Christopher Tate | 8dfe2b9 | 2012-05-15 15:05:04 -0700 | [diff] [blame] | 37 | import java.io.CharArrayReader; |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 38 | import java.io.DataInputStream; |
| 39 | import java.io.DataOutputStream; |
Amith Yamasani | 2cfab84 | 2009-09-09 18:27:31 -0700 | [diff] [blame] | 40 | import java.io.EOFException; |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 41 | import java.io.File; |
| 42 | import java.io.FileInputStream; |
| 43 | import java.io.FileOutputStream; |
Amith Yamasani | 2cfab84 | 2009-09-09 18:27:31 -0700 | [diff] [blame] | 44 | import java.io.FileReader; |
| 45 | import java.io.FileWriter; |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 46 | import java.io.IOException; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 47 | import java.io.InputStream; |
Brad Fitzpatrick | 7078789 | 2010-11-17 11:31:12 -0800 | [diff] [blame] | 48 | import java.io.OutputStream; |
Christopher Tate | 8dfe2b9 | 2012-05-15 15:05:04 -0700 | [diff] [blame] | 49 | import java.io.Writer; |
| 50 | import java.util.ArrayList; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 51 | import java.util.HashMap; |
Christopher Tate | 8dfe2b9 | 2012-05-15 15:05:04 -0700 | [diff] [blame] | 52 | import java.util.HashSet; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 53 | import java.util.Map; |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 54 | import java.util.zip.CRC32; |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 55 | |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Performs backup and restore of the System and Secure settings. |
| 58 | * List of settings that are backed up are stored in the Settings.java file |
| 59 | */ |
Christopher Tate | cc84c69 | 2010-03-29 14:54:02 -0700 | [diff] [blame] | 60 | public class SettingsBackupAgent extends BackupAgentHelper { |
Christopher Tate | 436344a | 2009-09-30 16:17:37 -0700 | [diff] [blame] | 61 | private static final boolean DEBUG = false; |
Christopher Tate | 8dfe2b9 | 2012-05-15 15:05:04 -0700 | [diff] [blame] | 62 | private static final boolean DEBUG_BACKUP = DEBUG || false; |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 63 | |
Christopher Tate | 5067685 | 2012-10-04 19:10:11 -0700 | [diff] [blame] | 64 | /* Don't restore wifi config until we have new logic for parsing the |
| 65 | * saved wifi config and configuring the new APs without having to |
| 66 | * disable and re-enable wifi |
| 67 | */ |
| 68 | private static final boolean NAIVE_WIFI_RESTORE = false; |
| 69 | |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 70 | private static final String KEY_SYSTEM = "system"; |
| 71 | private static final String KEY_SECURE = "secure"; |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 72 | private static final String KEY_GLOBAL = "global"; |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 73 | private static final String KEY_LOCALE = "locale"; |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 74 | |
Christopher Tate | a286f41 | 2009-09-18 15:51:15 -0700 | [diff] [blame] | 75 | // Versioning of the state file. Increment this version |
| 76 | // number any time the set of state items is altered. |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 77 | private static final int STATE_VERSION = 3; |
Christopher Tate | a286f41 | 2009-09-18 15:51:15 -0700 | [diff] [blame] | 78 | |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 79 | // Slots in the checksum array. Never insert new items in the middle |
| 80 | // of this array; new slots must be appended. |
Irfan Sheriff | 4aeca7c5 | 2011-03-10 16:53:33 -0800 | [diff] [blame] | 81 | private static final int STATE_SYSTEM = 0; |
| 82 | private static final int STATE_SECURE = 1; |
| 83 | private static final int STATE_LOCALE = 2; |
| 84 | private static final int STATE_WIFI_SUPPLICANT = 3; |
| 85 | private static final int STATE_WIFI_CONFIG = 4; |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 86 | private static final int STATE_GLOBAL = 5; |
| 87 | |
| 88 | private static final int STATE_SIZE = 6; // The current number of state items |
| 89 | |
| 90 | // Number of entries in the checksum array at various version numbers |
| 91 | private static final int STATE_SIZES[] = { |
| 92 | 0, |
| 93 | 4, // version 1 |
| 94 | 5, // version 2 added STATE_WIFI_CONFIG |
| 95 | STATE_SIZE // version 3 added STATE_GLOBAL |
| 96 | }; |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 97 | |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 98 | // Versioning of the 'full backup' format |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 99 | private static final int FULL_BACKUP_VERSION = 2; |
| 100 | private static final int FULL_BACKUP_ADDED_GLOBAL = 2; // added the "global" entry |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 101 | |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 102 | private static final int INTEGER_BYTE_COUNT = Integer.SIZE / Byte.SIZE; |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 103 | |
| 104 | private static final byte[] EMPTY_DATA = new byte[0]; |
| 105 | |
| 106 | private static final String TAG = "SettingsBackupAgent"; |
| 107 | |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 108 | private static final int COLUMN_NAME = 1; |
| 109 | private static final int COLUMN_VALUE = 2; |
| 110 | |
| 111 | private static final String[] PROJECTION = { |
| 112 | Settings.NameValueTable._ID, |
| 113 | Settings.NameValueTable.NAME, |
| 114 | Settings.NameValueTable.VALUE |
| 115 | }; |
| 116 | |
| 117 | private static final String FILE_WIFI_SUPPLICANT = "/data/misc/wifi/wpa_supplicant.conf"; |
Amith Yamasani | 2cfab84 | 2009-09-09 18:27:31 -0700 | [diff] [blame] | 118 | private static final String FILE_WIFI_SUPPLICANT_TEMPLATE = |
| 119 | "/system/etc/wifi/wpa_supplicant.conf"; |
Christian Sonntag | 92c1752 | 2009-08-07 15:16:17 -0700 | [diff] [blame] | 120 | |
| 121 | // the key to store the WIFI data under, should be sorted as last, so restore happens last. |
| 122 | // use very late unicode character to quasi-guarantee last sort position. |
Amith Yamasani | 2cfab84 | 2009-09-09 18:27:31 -0700 | [diff] [blame] | 123 | private static final String KEY_WIFI_SUPPLICANT = "\uffedWIFI"; |
Irfan Sheriff | 4aeca7c5 | 2011-03-10 16:53:33 -0800 | [diff] [blame] | 124 | private static final String KEY_WIFI_CONFIG = "\uffedCONFIG_WIFI"; |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 125 | |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 126 | // Name of the temporary file we use during full backup/restore. This is |
| 127 | // stored in the full-backup tarfile as well, so should not be changed. |
| 128 | private static final String STAGE_FILE = "flattened-data"; |
| 129 | |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 130 | private SettingsHelper mSettingsHelper; |
Irfan Sheriff | 4aeca7c5 | 2011-03-10 16:53:33 -0800 | [diff] [blame] | 131 | private WifiManager mWfm; |
| 132 | private static String mWifiConfigFile; |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 133 | |
Christopher Tate | 8dfe2b9 | 2012-05-15 15:05:04 -0700 | [diff] [blame] | 134 | // Class for capturing a network definition from the wifi supplicant config file |
| 135 | static class Network { |
| 136 | String ssid = ""; // equals() and hashCode() need these to be non-null |
| 137 | String key_mgmt = ""; |
| 138 | final ArrayList<String> rawLines = new ArrayList<String>(); |
| 139 | |
| 140 | public static Network readFromStream(BufferedReader in) { |
| 141 | final Network n = new Network(); |
| 142 | String line; |
| 143 | try { |
| 144 | while (in.ready()) { |
| 145 | line = in.readLine(); |
| 146 | if (line == null || line.startsWith("}")) { |
| 147 | break; |
| 148 | } |
| 149 | n.rememberLine(line); |
| 150 | } |
| 151 | } catch (IOException e) { |
| 152 | return null; |
| 153 | } |
| 154 | return n; |
| 155 | } |
| 156 | |
| 157 | void rememberLine(String line) { |
| 158 | // can't rely on particular whitespace patterns so strip leading/trailing |
| 159 | line = line.trim(); |
| 160 | if (line.isEmpty()) return; // only whitespace; drop the line |
| 161 | rawLines.add(line); |
| 162 | |
| 163 | // remember the ssid and key_mgmt lines for duplicate culling |
| 164 | if (line.startsWith("ssid")) { |
| 165 | ssid = line; |
| 166 | } else if (line.startsWith("key_mgmt")) { |
| 167 | key_mgmt = line; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | public void write(Writer w) throws IOException { |
| 172 | w.write("\nnetwork={\n"); |
| 173 | for (String line : rawLines) { |
| 174 | w.write("\t" + line + "\n"); |
| 175 | } |
| 176 | w.write("}\n"); |
| 177 | } |
| 178 | |
| 179 | public void dump() { |
| 180 | Log.v(TAG, "network={"); |
| 181 | for (String line : rawLines) { |
| 182 | Log.v(TAG, " " + line); |
| 183 | } |
| 184 | Log.v(TAG, "}"); |
| 185 | } |
| 186 | |
| 187 | // Same approach as Pair.equals() and Pair.hashCode() |
| 188 | @Override |
| 189 | public boolean equals(Object o) { |
| 190 | if (o == this) return true; |
| 191 | if (!(o instanceof Network)) return false; |
| 192 | final Network other; |
| 193 | try { |
| 194 | other = (Network) o; |
| 195 | } catch (ClassCastException e) { |
| 196 | return false; |
| 197 | } |
| 198 | return ssid.equals(other.ssid) && key_mgmt.equals(other.key_mgmt); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public int hashCode() { |
| 203 | int result = 17; |
| 204 | result = 31 * result + ssid.hashCode(); |
| 205 | result = 31 * result + key_mgmt.hashCode(); |
| 206 | return result; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // Ingest multiple wifi config file fragments, looking for network={} blocks |
| 211 | // and eliminating duplicates |
| 212 | class WifiNetworkSettings { |
| 213 | // One for fast lookup, one for maintaining ordering |
| 214 | final HashSet<Network> mKnownNetworks = new HashSet<Network>(); |
| 215 | final ArrayList<Network> mNetworks = new ArrayList<Network>(8); |
| 216 | |
| 217 | public void readNetworks(BufferedReader in) { |
| 218 | try { |
| 219 | String line; |
| 220 | while (in.ready()) { |
| 221 | line = in.readLine(); |
| 222 | if (line != null) { |
| 223 | // Parse out 'network=' decls so we can ignore duplicates |
| 224 | if (line.startsWith("network")) { |
| 225 | Network net = Network.readFromStream(in); |
| 226 | if (! mKnownNetworks.contains(net)) { |
| 227 | if (DEBUG_BACKUP) { |
| 228 | Log.v(TAG, "Adding " + net.ssid + " / " + net.key_mgmt); |
| 229 | } |
| 230 | mKnownNetworks.add(net); |
| 231 | mNetworks.add(net); |
| 232 | } else { |
| 233 | if (DEBUG_BACKUP) { |
| 234 | Log.v(TAG, "Dupe; skipped " + net.ssid + " / " + net.key_mgmt); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | } catch (IOException e) { |
| 241 | // whatever happened, we're done now |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | public void write(Writer w) throws IOException { |
| 246 | for (Network net : mNetworks) { |
| 247 | net.write(w); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | public void dump() { |
| 252 | for (Network net : mNetworks) { |
| 253 | net.dump(); |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 258 | @Override |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 259 | public void onCreate() { |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 260 | if (DEBUG_BACKUP) Log.d(TAG, "onCreate() invoked"); |
| 261 | |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 262 | mSettingsHelper = new SettingsHelper(this); |
| 263 | super.onCreate(); |
Irfan Sheriff | 4aeca7c5 | 2011-03-10 16:53:33 -0800 | [diff] [blame] | 264 | |
| 265 | WifiManager mWfm = (WifiManager) getSystemService(Context.WIFI_SERVICE); |
| 266 | if (mWfm != null) mWifiConfigFile = mWfm.getConfigFile(); |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, |
| 271 | ParcelFileDescriptor newState) throws IOException { |
| 272 | |
| 273 | byte[] systemSettingsData = getSystemSettings(); |
| 274 | byte[] secureSettingsData = getSecureSettings(); |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 275 | byte[] globalSettingsData = getGlobalSettings(); |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 276 | byte[] locale = mSettingsHelper.getLocaleData(); |
Irfan Sheriff | 4aeca7c5 | 2011-03-10 16:53:33 -0800 | [diff] [blame] | 277 | byte[] wifiSupplicantData = getWifiSupplicant(FILE_WIFI_SUPPLICANT); |
| 278 | byte[] wifiConfigData = getFileData(mWifiConfigFile); |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 279 | |
Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 280 | long[] stateChecksums = readOldChecksums(oldState); |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 281 | |
Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 282 | stateChecksums[STATE_SYSTEM] = |
| 283 | writeIfChanged(stateChecksums[STATE_SYSTEM], KEY_SYSTEM, systemSettingsData, data); |
| 284 | stateChecksums[STATE_SECURE] = |
| 285 | writeIfChanged(stateChecksums[STATE_SECURE], KEY_SECURE, secureSettingsData, data); |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 286 | stateChecksums[STATE_GLOBAL] = |
Christopher Tate | 3543beb | 2012-10-05 13:35:12 -0700 | [diff] [blame] | 287 | writeIfChanged(stateChecksums[STATE_GLOBAL], KEY_GLOBAL, globalSettingsData, data); |
Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 288 | stateChecksums[STATE_LOCALE] = |
| 289 | writeIfChanged(stateChecksums[STATE_LOCALE], KEY_LOCALE, locale, data); |
| 290 | stateChecksums[STATE_WIFI_SUPPLICANT] = |
| 291 | writeIfChanged(stateChecksums[STATE_WIFI_SUPPLICANT], KEY_WIFI_SUPPLICANT, |
| 292 | wifiSupplicantData, data); |
| 293 | stateChecksums[STATE_WIFI_CONFIG] = |
| 294 | writeIfChanged(stateChecksums[STATE_WIFI_CONFIG], KEY_WIFI_CONFIG, wifiConfigData, |
| 295 | data); |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 296 | |
Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 297 | writeNewChecksums(stateChecksums, newState); |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | @Override |
| 301 | public void onRestore(BackupDataInput data, int appVersionCode, |
| 302 | ParcelFileDescriptor newState) throws IOException { |
| 303 | |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 304 | HashSet<String> movedToGlobal = new HashSet<String>(); |
| 305 | Settings.System.getMovedKeys(movedToGlobal); |
| 306 | Settings.Secure.getMovedKeys(movedToGlobal); |
| 307 | |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 308 | while (data.readNextHeader()) { |
| 309 | final String key = data.getKey(); |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 310 | final int size = data.getDataSize(); |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 311 | if (KEY_SYSTEM.equals(key)) { |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 312 | restoreSettings(data, Settings.System.CONTENT_URI, movedToGlobal); |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 313 | mSettingsHelper.applyAudioSettings(); |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 314 | } else if (KEY_SECURE.equals(key)) { |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 315 | restoreSettings(data, Settings.Secure.CONTENT_URI, movedToGlobal); |
Christopher Tate | 3543beb | 2012-10-05 13:35:12 -0700 | [diff] [blame] | 316 | } else if (KEY_GLOBAL.equals(key)) { |
| 317 | restoreSettings(data, Settings.Global.CONTENT_URI, null); |
Christopher Tate | 5067685 | 2012-10-04 19:10:11 -0700 | [diff] [blame] | 318 | } else if (NAIVE_WIFI_RESTORE && KEY_WIFI_SUPPLICANT.equals(key)) { |
Christian Sonntag | c5b5b0f | 2009-08-07 10:00:21 -0700 | [diff] [blame] | 319 | int retainedWifiState = enableWifi(false); |
Amith Yamasani | 2cfab84 | 2009-09-09 18:27:31 -0700 | [diff] [blame] | 320 | restoreWifiSupplicant(FILE_WIFI_SUPPLICANT, data); |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 321 | FileUtils.setPermissions(FILE_WIFI_SUPPLICANT, |
| 322 | FileUtils.S_IRUSR | FileUtils.S_IWUSR | |
| 323 | FileUtils.S_IRGRP | FileUtils.S_IWGRP, |
| 324 | Process.myUid(), Process.WIFI_UID); |
Christian Sonntag | c5b5b0f | 2009-08-07 10:00:21 -0700 | [diff] [blame] | 325 | // retain the previous WIFI state. |
| 326 | enableWifi(retainedWifiState == WifiManager.WIFI_STATE_ENABLED || |
| 327 | retainedWifiState == WifiManager.WIFI_STATE_ENABLING); |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 328 | } else if (KEY_LOCALE.equals(key)) { |
| 329 | byte[] localeData = new byte[size]; |
| 330 | data.readEntityData(localeData, 0, size); |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 331 | mSettingsHelper.setLocaleData(localeData, size); |
Christopher Tate | 5067685 | 2012-10-04 19:10:11 -0700 | [diff] [blame] | 332 | } else if (NAIVE_WIFI_RESTORE && KEY_WIFI_CONFIG.equals(key)) { |
Irfan Sheriff | 4aeca7c5 | 2011-03-10 16:53:33 -0800 | [diff] [blame] | 333 | restoreFileData(mWifiConfigFile, data); |
| 334 | } else { |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 335 | data.skipEntityData(); |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 340 | @Override |
Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 341 | public void onFullBackup(FullBackupDataOutput data) throws IOException { |
| 342 | byte[] systemSettingsData = getSystemSettings(); |
| 343 | byte[] secureSettingsData = getSecureSettings(); |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 344 | byte[] globalSettingsData = getGlobalSettings(); |
Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 345 | byte[] locale = mSettingsHelper.getLocaleData(); |
| 346 | byte[] wifiSupplicantData = getWifiSupplicant(FILE_WIFI_SUPPLICANT); |
| 347 | byte[] wifiConfigData = getFileData(mWifiConfigFile); |
| 348 | |
| 349 | // Write the data to the staging file, then emit that as our tarfile |
| 350 | // representation of the backed-up settings. |
| 351 | String root = getFilesDir().getAbsolutePath(); |
| 352 | File stage = new File(root, STAGE_FILE); |
| 353 | try { |
| 354 | FileOutputStream filestream = new FileOutputStream(stage); |
| 355 | BufferedOutputStream bufstream = new BufferedOutputStream(filestream); |
| 356 | DataOutputStream out = new DataOutputStream(bufstream); |
| 357 | |
Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 358 | if (DEBUG_BACKUP) Log.d(TAG, "Writing flattened data version " + FULL_BACKUP_VERSION); |
Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 359 | out.writeInt(FULL_BACKUP_VERSION); |
| 360 | |
Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 361 | if (DEBUG_BACKUP) Log.d(TAG, systemSettingsData.length + " bytes of settings data"); |
Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 362 | out.writeInt(systemSettingsData.length); |
| 363 | out.write(systemSettingsData); |
Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 364 | if (DEBUG_BACKUP) Log.d(TAG, secureSettingsData.length + " bytes of secure settings data"); |
Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 365 | out.writeInt(secureSettingsData.length); |
| 366 | out.write(secureSettingsData); |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 367 | if (DEBUG_BACKUP) Log.d(TAG, globalSettingsData.length + " bytes of global settings data"); |
| 368 | out.writeInt(globalSettingsData.length); |
| 369 | out.write(globalSettingsData); |
Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 370 | if (DEBUG_BACKUP) Log.d(TAG, locale.length + " bytes of locale data"); |
Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 371 | out.writeInt(locale.length); |
| 372 | out.write(locale); |
Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 373 | if (DEBUG_BACKUP) Log.d(TAG, wifiSupplicantData.length + " bytes of wifi supplicant data"); |
Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 374 | out.writeInt(wifiSupplicantData.length); |
| 375 | out.write(wifiSupplicantData); |
Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 376 | if (DEBUG_BACKUP) Log.d(TAG, wifiConfigData.length + " bytes of wifi config data"); |
Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 377 | out.writeInt(wifiConfigData.length); |
| 378 | out.write(wifiConfigData); |
| 379 | |
| 380 | out.flush(); // also flushes downstream |
| 381 | |
| 382 | // now we're set to emit the tar stream |
| 383 | fullBackupFile(stage, data); |
| 384 | } finally { |
| 385 | stage.delete(); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | @Override |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 390 | public void onRestoreFile(ParcelFileDescriptor data, long size, |
| 391 | int type, String domain, String relpath, long mode, long mtime) |
| 392 | throws IOException { |
| 393 | if (DEBUG_BACKUP) Log.d(TAG, "onRestoreFile() invoked"); |
| 394 | // Our data is actually a blob of flattened settings data identical to that |
| 395 | // produced during incremental backups. Just unpack and apply it all in |
| 396 | // turn. |
| 397 | FileInputStream instream = new FileInputStream(data.getFileDescriptor()); |
| 398 | DataInputStream in = new DataInputStream(instream); |
| 399 | |
| 400 | int version = in.readInt(); |
| 401 | if (DEBUG_BACKUP) Log.d(TAG, "Flattened data version " + version); |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 402 | if (version <= FULL_BACKUP_VERSION) { |
| 403 | // Generate the moved-to-global lookup table |
| 404 | HashSet<String> movedToGlobal = new HashSet<String>(); |
| 405 | Settings.System.getMovedKeys(movedToGlobal); |
| 406 | Settings.Secure.getMovedKeys(movedToGlobal); |
| 407 | |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 408 | // system settings data first |
| 409 | int nBytes = in.readInt(); |
| 410 | if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of settings data"); |
| 411 | byte[] buffer = new byte[nBytes]; |
Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 412 | in.readFully(buffer, 0, nBytes); |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 413 | restoreSettings(buffer, nBytes, Settings.System.CONTENT_URI, movedToGlobal); |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 414 | |
| 415 | // secure settings |
| 416 | nBytes = in.readInt(); |
| 417 | if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of secure settings data"); |
| 418 | if (nBytes > buffer.length) buffer = new byte[nBytes]; |
Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 419 | in.readFully(buffer, 0, nBytes); |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 420 | restoreSettings(buffer, nBytes, Settings.Secure.CONTENT_URI, movedToGlobal); |
| 421 | |
| 422 | // Global only if sufficiently new |
| 423 | if (version >= FULL_BACKUP_ADDED_GLOBAL) { |
| 424 | nBytes = in.readInt(); |
| 425 | if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of global settings data"); |
| 426 | if (nBytes > buffer.length) buffer = new byte[nBytes]; |
| 427 | in.readFully(buffer, 0, nBytes); |
| 428 | movedToGlobal.clear(); // no redirection; this *is* the global namespace |
| 429 | restoreSettings(buffer, nBytes, Settings.Global.CONTENT_URI, movedToGlobal); |
| 430 | } |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 431 | |
| 432 | // locale |
| 433 | nBytes = in.readInt(); |
| 434 | if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of locale data"); |
| 435 | if (nBytes > buffer.length) buffer = new byte[nBytes]; |
Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 436 | in.readFully(buffer, 0, nBytes); |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 437 | mSettingsHelper.setLocaleData(buffer, nBytes); |
| 438 | |
| 439 | // wifi supplicant |
| 440 | nBytes = in.readInt(); |
| 441 | if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of wifi supplicant data"); |
| 442 | if (nBytes > buffer.length) buffer = new byte[nBytes]; |
Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 443 | in.readFully(buffer, 0, nBytes); |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 444 | int retainedWifiState = enableWifi(false); |
| 445 | restoreWifiSupplicant(FILE_WIFI_SUPPLICANT, buffer, nBytes); |
| 446 | FileUtils.setPermissions(FILE_WIFI_SUPPLICANT, |
| 447 | FileUtils.S_IRUSR | FileUtils.S_IWUSR | |
| 448 | FileUtils.S_IRGRP | FileUtils.S_IWGRP, |
| 449 | Process.myUid(), Process.WIFI_UID); |
| 450 | // retain the previous WIFI state. |
| 451 | enableWifi(retainedWifiState == WifiManager.WIFI_STATE_ENABLED || |
| 452 | retainedWifiState == WifiManager.WIFI_STATE_ENABLING); |
| 453 | |
| 454 | // wifi config |
| 455 | nBytes = in.readInt(); |
| 456 | if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of wifi config data"); |
| 457 | if (nBytes > buffer.length) buffer = new byte[nBytes]; |
Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 458 | in.readFully(buffer, 0, nBytes); |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 459 | restoreFileData(mWifiConfigFile, buffer, nBytes); |
| 460 | |
| 461 | if (DEBUG_BACKUP) Log.d(TAG, "Full restore complete."); |
| 462 | } else { |
| 463 | data.close(); |
| 464 | throw new IOException("Invalid file schema"); |
| 465 | } |
| 466 | } |
| 467 | |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 468 | private long[] readOldChecksums(ParcelFileDescriptor oldState) throws IOException { |
| 469 | long[] stateChecksums = new long[STATE_SIZE]; |
| 470 | |
| 471 | DataInputStream dataInput = new DataInputStream( |
| 472 | new FileInputStream(oldState.getFileDescriptor())); |
Christopher Tate | a286f41 | 2009-09-18 15:51:15 -0700 | [diff] [blame] | 473 | |
| 474 | try { |
| 475 | int stateVersion = dataInput.readInt(); |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 476 | for (int i = 0; i < STATE_SIZES[stateVersion]; i++) { |
| 477 | stateChecksums[i] = dataInput.readLong(); |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 478 | } |
Christopher Tate | a286f41 | 2009-09-18 15:51:15 -0700 | [diff] [blame] | 479 | } 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 Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 482 | } |
| 483 | dataInput.close(); |
| 484 | return stateChecksums; |
| 485 | } |
| 486 | |
| 487 | private void writeNewChecksums(long[] checksums, ParcelFileDescriptor newState) |
| 488 | throws IOException { |
| 489 | DataOutputStream dataOutput = new DataOutputStream( |
| 490 | new FileOutputStream(newState.getFileDescriptor())); |
Christopher Tate | a286f41 | 2009-09-18 15:51:15 -0700 | [diff] [blame] | 491 | |
| 492 | dataOutput.writeInt(STATE_VERSION); |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 493 | 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, |
| 500 | BackupDataOutput output) { |
| 501 | CRC32 checkSummer = new CRC32(); |
| 502 | checkSummer.update(data); |
| 503 | long newChecksum = checkSummer.getValue(); |
| 504 | if (oldChecksum == newChecksum) { |
| 505 | return oldChecksum; |
| 506 | } |
| 507 | try { |
| 508 | output.writeEntityHeader(key, data.length); |
| 509 | output.writeEntityData(data, data.length); |
| 510 | } catch (IOException ioe) { |
| 511 | // Bail |
| 512 | } |
| 513 | return newChecksum; |
| 514 | } |
| 515 | |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 516 | private byte[] getSystemSettings() { |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 517 | Cursor cursor = getContentResolver().query(Settings.System.CONTENT_URI, PROJECTION, null, |
| 518 | null, null); |
Jeff Brown | 1d8e7d6 | 2011-10-09 15:24:53 -0700 | [diff] [blame] | 519 | try { |
| 520 | return extractRelevantValues(cursor, Settings.System.SETTINGS_TO_BACKUP); |
| 521 | } finally { |
| 522 | cursor.close(); |
| 523 | } |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | private byte[] getSecureSettings() { |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 527 | Cursor cursor = getContentResolver().query(Settings.Secure.CONTENT_URI, PROJECTION, null, |
| 528 | null, null); |
Jeff Brown | 1d8e7d6 | 2011-10-09 15:24:53 -0700 | [diff] [blame] | 529 | try { |
| 530 | return extractRelevantValues(cursor, Settings.Secure.SETTINGS_TO_BACKUP); |
| 531 | } finally { |
| 532 | cursor.close(); |
| 533 | } |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 536 | private byte[] getGlobalSettings() { |
| 537 | Cursor cursor = getContentResolver().query(Settings.Global.CONTENT_URI, PROJECTION, null, |
| 538 | null, null); |
| 539 | try { |
| 540 | return extractRelevantValues(cursor, Settings.Global.SETTINGS_TO_BACKUP); |
| 541 | } finally { |
| 542 | cursor.close(); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | private void restoreSettings(BackupDataInput data, Uri contentUri, |
| 547 | HashSet<String> movedToGlobal) { |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 548 | byte[] settings = new byte[data.getDataSize()]; |
| 549 | try { |
| 550 | data.readEntityData(settings, 0, settings.length); |
| 551 | } catch (IOException ioe) { |
| 552 | Log.e(TAG, "Couldn't read entity data"); |
| 553 | return; |
| 554 | } |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 555 | restoreSettings(settings, settings.length, contentUri, movedToGlobal); |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 558 | private void restoreSettings(byte[] settings, int bytes, Uri contentUri, |
| 559 | HashSet<String> movedToGlobal) { |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 560 | if (DEBUG) { |
| 561 | Log.i(TAG, "restoreSettings: " + contentUri); |
| 562 | } |
| 563 | |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 564 | // Figure out the white list and redirects to the global table. |
Christopher Tate | 796e0f0 | 2009-09-22 11:57:58 -0700 | [diff] [blame] | 565 | String[] whitelist = null; |
| 566 | if (contentUri.equals(Settings.Secure.CONTENT_URI)) { |
| 567 | whitelist = Settings.Secure.SETTINGS_TO_BACKUP; |
| 568 | } else if (contentUri.equals(Settings.System.CONTENT_URI)) { |
| 569 | whitelist = Settings.System.SETTINGS_TO_BACKUP; |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 570 | } else if (contentUri.equals(Settings.Global.CONTENT_URI)) { |
| 571 | whitelist = Settings.Global.SETTINGS_TO_BACKUP; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 572 | } else { |
| 573 | throw new IllegalArgumentException("Unknown URI: " + contentUri); |
Christopher Tate | 796e0f0 | 2009-09-22 11:57:58 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 576 | // Restore only the white list data. |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 577 | int pos = 0; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 578 | Map<String, String> cachedEntries = new HashMap<String, String>(); |
| 579 | ContentValues contentValues = new ContentValues(2); |
| 580 | SettingsHelper settingsHelper = mSettingsHelper; |
Christopher Tate | 0738e88 | 2009-09-11 16:35:39 -0700 | [diff] [blame] | 581 | |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 582 | final int whiteListSize = whitelist.length; |
| 583 | for (int i = 0; i < whiteListSize; i++) { |
| 584 | String key = whitelist[i]; |
| 585 | String value = cachedEntries.remove(key); |
Christopher Tate | 0738e88 | 2009-09-11 16:35:39 -0700 | [diff] [blame] | 586 | |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 587 | // If the value not cached, let us look it up. |
| 588 | if (value == null) { |
| 589 | while (pos < bytes) { |
| 590 | int length = readInt(settings, pos); |
| 591 | pos += INTEGER_BYTE_COUNT; |
| 592 | String dataKey = length > 0 ? new String(settings, pos, length) : null; |
| 593 | pos += length; |
| 594 | length = readInt(settings, pos); |
| 595 | pos += INTEGER_BYTE_COUNT; |
| 596 | String dataValue = length > 0 ? new String(settings, pos, length) : null; |
| 597 | pos += length; |
| 598 | if (key.equals(dataKey)) { |
| 599 | value = dataValue; |
| 600 | break; |
| 601 | } |
| 602 | cachedEntries.put(dataKey, dataValue); |
Amith Yamasani | 70c874b | 2009-07-06 14:53:25 -0700 | [diff] [blame] | 603 | } |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 604 | } |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 605 | |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 606 | if (value == null) { |
| 607 | continue; |
| 608 | } |
Christopher Tate | 796e0f0 | 2009-09-22 11:57:58 -0700 | [diff] [blame] | 609 | |
Christopher Tate | 3543beb | 2012-10-05 13:35:12 -0700 | [diff] [blame] | 610 | final Uri destination = (movedToGlobal != null && movedToGlobal.contains(key)) |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 611 | ? Settings.Global.CONTENT_URI |
| 612 | : contentUri; |
| 613 | |
| 614 | // The helper doesn't care what namespace the keys are in |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 615 | if (settingsHelper.restoreValue(key, value)) { |
| 616 | contentValues.clear(); |
| 617 | contentValues.put(Settings.NameValueTable.NAME, key); |
| 618 | contentValues.put(Settings.NameValueTable.VALUE, value); |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 619 | getContentResolver().insert(destination, contentValues); |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Christopher Tate | d717788 | 2012-09-07 18:09:03 -0700 | [diff] [blame] | 622 | if (DEBUG || true) { |
Christopher Tate | 66488d6 | 2012-10-02 11:58:01 -0700 | [diff] [blame] | 623 | Log.d(TAG, "Restored setting: " + destination + " : "+ key + "=" + value); |
Christopher Tate | 0738e88 | 2009-09-11 16:35:39 -0700 | [diff] [blame] | 624 | } |
| 625 | } |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | /** |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 629 | * Given a cursor and a set of keys, extract the required keys and |
| 630 | * values and write them to a byte array. |
| 631 | * |
| 632 | * @param cursor A cursor with settings data. |
| 633 | * @param settings The settings to extract. |
| 634 | * @return The byte array of extracted values. |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 635 | */ |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 636 | private byte[] extractRelevantValues(Cursor cursor, String[] settings) { |
| 637 | final int settingsCount = settings.length; |
| 638 | byte[][] values = new byte[settingsCount * 2][]; // keys and values |
| 639 | if (!cursor.moveToFirst()) { |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 640 | Log.e(TAG, "Couldn't read from the cursor"); |
| 641 | return new byte[0]; |
| 642 | } |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 643 | |
| 644 | // Obtain the relevant data in a temporary array. |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 645 | int totalSize = 0; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 646 | int backedUpSettingIndex = 0; |
| 647 | Map<String, String> cachedEntries = new HashMap<String, String>(); |
| 648 | for (int i = 0; i < settingsCount; i++) { |
| 649 | String key = settings[i]; |
| 650 | String value = cachedEntries.remove(key); |
| 651 | |
| 652 | // If the value not cached, let us look it up. |
| 653 | if (value == null) { |
| 654 | while (!cursor.isAfterLast()) { |
| 655 | String cursorKey = cursor.getString(COLUMN_NAME); |
| 656 | String cursorValue = cursor.getString(COLUMN_VALUE); |
| 657 | cursor.moveToNext(); |
| 658 | if (key.equals(cursorKey)) { |
| 659 | value = cursorValue; |
| 660 | break; |
| 661 | } |
| 662 | cachedEntries.put(cursorKey, cursorValue); |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 663 | } |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 664 | } |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 665 | |
| 666 | if (value == null) { |
| 667 | continue; |
| 668 | } |
| 669 | |
| 670 | // Write the key and value in the intermediary array. |
| 671 | byte[] keyBytes = key.getBytes(); |
| 672 | totalSize += INTEGER_BYTE_COUNT + keyBytes.length; |
| 673 | values[backedUpSettingIndex * 2] = keyBytes; |
| 674 | |
| 675 | byte[] valueBytes = value.getBytes(); |
| 676 | totalSize += INTEGER_BYTE_COUNT + valueBytes.length; |
| 677 | values[backedUpSettingIndex * 2 + 1] = valueBytes; |
| 678 | |
| 679 | backedUpSettingIndex++; |
| 680 | |
| 681 | if (DEBUG) { |
| 682 | Log.d(TAG, "Backed up setting: " + key + "=" + value); |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 686 | // Aggregate the result. |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 687 | byte[] result = new byte[totalSize]; |
| 688 | int pos = 0; |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 689 | final int keyValuePairCount = backedUpSettingIndex * 2; |
| 690 | for (int i = 0; i < keyValuePairCount; i++) { |
| 691 | pos = writeInt(result, pos, values[i].length); |
| 692 | pos = writeBytes(result, pos, values[i]); |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 693 | } |
| 694 | return result; |
| 695 | } |
| 696 | |
Irfan Sheriff | 4aeca7c5 | 2011-03-10 16:53:33 -0800 | [diff] [blame] | 697 | private byte[] getFileData(String filename) { |
| 698 | InputStream is = null; |
| 699 | try { |
| 700 | File file = new File(filename); |
| 701 | is = new FileInputStream(file); |
| 702 | |
| 703 | //Will truncate read on a very long file, |
| 704 | //should not happen for a config file |
| 705 | byte[] bytes = new byte[(int)file.length()]; |
| 706 | |
| 707 | int offset = 0; |
| 708 | int numRead = 0; |
| 709 | while (offset < bytes.length |
| 710 | && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { |
| 711 | offset += numRead; |
| 712 | } |
| 713 | |
| 714 | //read failure |
| 715 | if (offset < bytes.length) { |
| 716 | Log.w(TAG, "Couldn't backup " + filename); |
| 717 | return EMPTY_DATA; |
| 718 | } |
| 719 | return bytes; |
| 720 | } catch (IOException ioe) { |
| 721 | Log.w(TAG, "Couldn't backup " + filename); |
| 722 | return EMPTY_DATA; |
| 723 | } finally { |
| 724 | if (is != null) { |
| 725 | try { |
| 726 | is.close(); |
| 727 | } catch (IOException e) { |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | } |
| 733 | |
| 734 | private void restoreFileData(String filename, BackupDataInput data) { |
| 735 | byte[] bytes = new byte[data.getDataSize()]; |
| 736 | if (bytes.length <= 0) return; |
Christopher Tate | f12fbcd | 2011-06-28 16:16:42 -0700 | [diff] [blame] | 737 | try { |
| 738 | data.readEntityData(bytes, 0, data.getDataSize()); |
| 739 | restoreFileData(filename, bytes, bytes.length); |
| 740 | } catch (IOException e) { |
| 741 | Log.w(TAG, "Unable to read file data for " + filename); |
| 742 | } |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | private void restoreFileData(String filename, byte[] bytes, int size) { |
Irfan Sheriff | 4aeca7c5 | 2011-03-10 16:53:33 -0800 | [diff] [blame] | 746 | try { |
Irfan Sheriff | 4aeca7c5 | 2011-03-10 16:53:33 -0800 | [diff] [blame] | 747 | File file = new File(filename); |
| 748 | if (file.exists()) file.delete(); |
| 749 | |
| 750 | OutputStream os = new BufferedOutputStream(new FileOutputStream(filename, true)); |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 751 | os.write(bytes, 0, size); |
Irfan Sheriff | 4aeca7c5 | 2011-03-10 16:53:33 -0800 | [diff] [blame] | 752 | os.close(); |
| 753 | } catch (IOException ioe) { |
| 754 | Log.w(TAG, "Couldn't restore " + filename); |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | |
Amith Yamasani | 2cfab84 | 2009-09-09 18:27:31 -0700 | [diff] [blame] | 759 | private byte[] getWifiSupplicant(String filename) { |
Brad Fitzpatrick | 7078789 | 2010-11-17 11:31:12 -0800 | [diff] [blame] | 760 | BufferedReader br = null; |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 761 | try { |
| 762 | File file = new File(filename); |
| 763 | if (file.exists()) { |
Brad Fitzpatrick | 7078789 | 2010-11-17 11:31:12 -0800 | [diff] [blame] | 764 | br = new BufferedReader(new FileReader(file)); |
Amith Yamasani | 2cfab84 | 2009-09-09 18:27:31 -0700 | [diff] [blame] | 765 | StringBuffer relevantLines = new StringBuffer(); |
| 766 | boolean started = false; |
| 767 | String line; |
| 768 | while ((line = br.readLine()) != null) { |
| 769 | if (!started && line.startsWith("network")) { |
| 770 | started = true; |
| 771 | } |
| 772 | if (started) { |
| 773 | relevantLines.append(line).append("\n"); |
| 774 | } |
| 775 | } |
| 776 | if (relevantLines.length() > 0) { |
| 777 | return relevantLines.toString().getBytes(); |
| 778 | } else { |
| 779 | return EMPTY_DATA; |
| 780 | } |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 781 | } else { |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 782 | return EMPTY_DATA; |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 783 | } |
| 784 | } catch (IOException ioe) { |
| 785 | Log.w(TAG, "Couldn't backup " + filename); |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 786 | return EMPTY_DATA; |
Brad Fitzpatrick | 7078789 | 2010-11-17 11:31:12 -0800 | [diff] [blame] | 787 | } finally { |
| 788 | if (br != null) { |
| 789 | try { |
| 790 | br.close(); |
| 791 | } catch (IOException e) { |
| 792 | } |
| 793 | } |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 794 | } |
| 795 | } |
| 796 | |
Amith Yamasani | 2cfab84 | 2009-09-09 18:27:31 -0700 | [diff] [blame] | 797 | private void restoreWifiSupplicant(String filename, BackupDataInput data) { |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 798 | byte[] bytes = new byte[data.getDataSize()]; |
| 799 | if (bytes.length <= 0) return; |
Christopher Tate | 28cdb9e | 2011-06-24 15:06:48 -0700 | [diff] [blame] | 800 | try { |
| 801 | data.readEntityData(bytes, 0, data.getDataSize()); |
| 802 | restoreWifiSupplicant(filename, bytes, bytes.length); |
| 803 | } catch (IOException e) { |
| 804 | Log.w(TAG, "Unable to read supplicant data"); |
| 805 | } |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | private void restoreWifiSupplicant(String filename, byte[] bytes, int size) { |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 809 | try { |
Christopher Tate | 8dfe2b9 | 2012-05-15 15:05:04 -0700 | [diff] [blame] | 810 | WifiNetworkSettings supplicantImage = new WifiNetworkSettings(); |
Amith Yamasani | 2cfab84 | 2009-09-09 18:27:31 -0700 | [diff] [blame] | 811 | |
Christopher Tate | 8dfe2b9 | 2012-05-15 15:05:04 -0700 | [diff] [blame] | 812 | File supplicantFile = new File(FILE_WIFI_SUPPLICANT); |
| 813 | if (supplicantFile.exists()) { |
| 814 | // Retain the existing APs; we'll append the restored ones to them |
| 815 | BufferedReader in = new BufferedReader(new FileReader(FILE_WIFI_SUPPLICANT)); |
| 816 | supplicantImage.readNetworks(in); |
| 817 | in.close(); |
| 818 | |
| 819 | supplicantFile.delete(); |
| 820 | } |
| 821 | |
| 822 | // Incorporate the restore AP information |
| 823 | if (size > 0) { |
| 824 | char[] restoredAsBytes = new char[size]; |
| 825 | for (int i = 0; i < size; i++) restoredAsBytes[i] = (char) bytes[i]; |
| 826 | BufferedReader in = new BufferedReader(new CharArrayReader(restoredAsBytes)); |
| 827 | supplicantImage.readNetworks(in); |
| 828 | |
| 829 | if (DEBUG_BACKUP) { |
| 830 | Log.v(TAG, "Final AP list:"); |
| 831 | supplicantImage.dump(); |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | // Install the correct default template |
| 836 | BufferedWriter bw = new BufferedWriter(new FileWriter(FILE_WIFI_SUPPLICANT)); |
| 837 | copyWifiSupplicantTemplate(bw); |
| 838 | |
| 839 | // Write the restored supplicant config and we're done |
| 840 | supplicantImage.write(bw); |
| 841 | bw.close(); |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 842 | } catch (IOException ioe) { |
| 843 | Log.w(TAG, "Couldn't restore " + filename); |
| 844 | } |
| 845 | } |
| 846 | |
Christopher Tate | 8dfe2b9 | 2012-05-15 15:05:04 -0700 | [diff] [blame] | 847 | private void copyWifiSupplicantTemplate(BufferedWriter bw) { |
Amith Yamasani | 2cfab84 | 2009-09-09 18:27:31 -0700 | [diff] [blame] | 848 | try { |
| 849 | BufferedReader br = new BufferedReader(new FileReader(FILE_WIFI_SUPPLICANT_TEMPLATE)); |
Amith Yamasani | 2cfab84 | 2009-09-09 18:27:31 -0700 | [diff] [blame] | 850 | char[] temp = new char[1024]; |
| 851 | int size; |
| 852 | while ((size = br.read(temp)) > 0) { |
| 853 | bw.write(temp, 0, size); |
| 854 | } |
Amith Yamasani | 2cfab84 | 2009-09-09 18:27:31 -0700 | [diff] [blame] | 855 | br.close(); |
| 856 | } catch (IOException ioe) { |
| 857 | Log.w(TAG, "Couldn't copy wpa_supplicant file"); |
| 858 | } |
| 859 | } |
| 860 | |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 861 | /** |
| 862 | * Write an int in BigEndian into the byte array. |
| 863 | * @param out byte array |
| 864 | * @param pos current pos in array |
| 865 | * @param value integer to write |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 866 | * @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] | 867 | */ |
| 868 | private int writeInt(byte[] out, int pos, int value) { |
| 869 | out[pos + 0] = (byte) ((value >> 24) & 0xFF); |
| 870 | out[pos + 1] = (byte) ((value >> 16) & 0xFF); |
| 871 | out[pos + 2] = (byte) ((value >> 8) & 0xFF); |
| 872 | out[pos + 3] = (byte) ((value >> 0) & 0xFF); |
Svetoslav Ganov | a571a58 | 2011-09-20 18:32:20 -0700 | [diff] [blame] | 873 | return pos + INTEGER_BYTE_COUNT; |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | private int writeBytes(byte[] out, int pos, byte[] value) { |
| 877 | System.arraycopy(value, 0, out, pos, value.length); |
| 878 | return pos + value.length; |
| 879 | } |
| 880 | |
| 881 | private int readInt(byte[] in, int pos) { |
| 882 | int result = |
| 883 | ((in[pos ] & 0xFF) << 24) | |
| 884 | ((in[pos + 1] & 0xFF) << 16) | |
| 885 | ((in[pos + 2] & 0xFF) << 8) | |
| 886 | ((in[pos + 3] & 0xFF) << 0); |
| 887 | return result; |
| 888 | } |
| 889 | |
Christian Sonntag | c5b5b0f | 2009-08-07 10:00:21 -0700 | [diff] [blame] | 890 | private int enableWifi(boolean enable) { |
Irfan Sheriff | 4f4f516 | 2012-04-27 14:42:42 -0700 | [diff] [blame] | 891 | if (mWfm == null) { |
| 892 | mWfm = (WifiManager) getSystemService(Context.WIFI_SERVICE); |
| 893 | } |
Irfan Sheriff | 4aeca7c5 | 2011-03-10 16:53:33 -0800 | [diff] [blame] | 894 | if (mWfm != null) { |
| 895 | int state = mWfm.getWifiState(); |
| 896 | mWfm.setWifiEnabled(enable); |
Christian Sonntag | c5b5b0f | 2009-08-07 10:00:21 -0700 | [diff] [blame] | 897 | return state; |
Irfan Sheriff | 4f4f516 | 2012-04-27 14:42:42 -0700 | [diff] [blame] | 898 | } else { |
| 899 | Log.e(TAG, "Failed to fetch WifiManager instance"); |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 900 | } |
Christian Sonntag | c5b5b0f | 2009-08-07 10:00:21 -0700 | [diff] [blame] | 901 | return WifiManager.WIFI_STATE_UNKNOWN; |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 902 | } |
Amith Yamasani | 220f4d6 | 2009-07-02 02:34:14 -0700 | [diff] [blame] | 903 | } |