Merge "Backup and restore IP and proxy settings" into honeycomb-mr1

This commit is contained in:
Irfan Sheriff
2011-03-14 11:48:35 -07:00
committed by Android (Google) Code Review
6 changed files with 126 additions and 19 deletions

View File

@ -27,6 +27,7 @@ import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.InputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Arrays; import java.util.Arrays;
@ -58,15 +59,20 @@ public class SettingsBackupAgent extends BackupAgentHelper {
private static final String KEY_SECURE = "secure"; private static final String KEY_SECURE = "secure";
private static final String KEY_LOCALE = "locale"; private static final String KEY_LOCALE = "locale";
//Version 2 adds STATE_WIFI_CONFIG
private static final int STATE_VERSION_1 = 1;
private static final int STATE_VERSION_1_SIZE = 4;
// Versioning of the state file. Increment this version // Versioning of the state file. Increment this version
// number any time the set of state items is altered. // number any time the set of state items is altered.
private static final int STATE_VERSION = 1; private static final int STATE_VERSION = 2;
private static final int STATE_SYSTEM = 0; private static final int STATE_SYSTEM = 0;
private static final int STATE_SECURE = 1; private static final int STATE_SECURE = 1;
private static final int STATE_LOCALE = 2; private static final int STATE_LOCALE = 2;
private static final int STATE_WIFI = 3; private static final int STATE_WIFI_SUPPLICANT = 3;
private static final int STATE_SIZE = 4; // The number of state items private static final int STATE_WIFI_CONFIG = 4;
private static final int STATE_SIZE = 5; // The number of state items
private static String[] sortedSystemKeys = null; private static String[] sortedSystemKeys = null;
private static String[] sortedSecureKeys = null; private static String[] sortedSecureKeys = null;
@ -91,12 +97,18 @@ public class SettingsBackupAgent extends BackupAgentHelper {
// the key to store the WIFI data under, should be sorted as last, so restore happens last. // the key to store the WIFI data under, should be sorted as last, so restore happens last.
// use very late unicode character to quasi-guarantee last sort position. // use very late unicode character to quasi-guarantee last sort position.
private static final String KEY_WIFI_SUPPLICANT = "\uffedWIFI"; private static final String KEY_WIFI_SUPPLICANT = "\uffedWIFI";
private static final String KEY_WIFI_CONFIG = "\uffedCONFIG_WIFI";
private SettingsHelper mSettingsHelper; private SettingsHelper mSettingsHelper;
private WifiManager mWfm;
private static String mWifiConfigFile;
public void onCreate() { public void onCreate() {
mSettingsHelper = new SettingsHelper(this); mSettingsHelper = new SettingsHelper(this);
super.onCreate(); super.onCreate();
WifiManager mWfm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (mWfm != null) mWifiConfigFile = mWfm.getConfigFile();
} }
@Override @Override
@ -106,7 +118,8 @@ public class SettingsBackupAgent extends BackupAgentHelper {
byte[] systemSettingsData = getSystemSettings(); byte[] systemSettingsData = getSystemSettings();
byte[] secureSettingsData = getSecureSettings(); byte[] secureSettingsData = getSecureSettings();
byte[] locale = mSettingsHelper.getLocaleData(); byte[] locale = mSettingsHelper.getLocaleData();
byte[] wifiData = getWifiSupplicant(FILE_WIFI_SUPPLICANT); byte[] wifiSupplicantData = getWifiSupplicant(FILE_WIFI_SUPPLICANT);
byte[] wifiConfigData = getFileData(mWifiConfigFile);
long[] stateChecksums = readOldChecksums(oldState); long[] stateChecksums = readOldChecksums(oldState);
@ -116,8 +129,12 @@ public class SettingsBackupAgent extends BackupAgentHelper {
writeIfChanged(stateChecksums[STATE_SECURE], KEY_SECURE, secureSettingsData, data); writeIfChanged(stateChecksums[STATE_SECURE], KEY_SECURE, secureSettingsData, data);
stateChecksums[STATE_LOCALE] = stateChecksums[STATE_LOCALE] =
writeIfChanged(stateChecksums[STATE_LOCALE], KEY_LOCALE, locale, data); writeIfChanged(stateChecksums[STATE_LOCALE], KEY_LOCALE, locale, data);
stateChecksums[STATE_WIFI] = stateChecksums[STATE_WIFI_SUPPLICANT] =
writeIfChanged(stateChecksums[STATE_WIFI], KEY_WIFI_SUPPLICANT, wifiData, data); writeIfChanged(stateChecksums[STATE_WIFI_SUPPLICANT], KEY_WIFI_SUPPLICANT,
wifiSupplicantData, data);
stateChecksums[STATE_WIFI_CONFIG] =
writeIfChanged(stateChecksums[STATE_WIFI_CONFIG], KEY_WIFI_CONFIG, wifiConfigData,
data);
writeNewChecksums(stateChecksums, newState); writeNewChecksums(stateChecksums, newState);
} }
@ -148,6 +165,8 @@ public class SettingsBackupAgent extends BackupAgentHelper {
byte[] localeData = new byte[size]; byte[] localeData = new byte[size];
data.readEntityData(localeData, 0, size); data.readEntityData(localeData, 0, size);
mSettingsHelper.setLocaleData(localeData); mSettingsHelper.setLocaleData(localeData);
} else if (KEY_WIFI_CONFIG.equals(key)) {
restoreFileData(mWifiConfigFile, data);
} else { } else {
data.skipEntityData(); data.skipEntityData();
} }
@ -162,7 +181,11 @@ public class SettingsBackupAgent extends BackupAgentHelper {
try { try {
int stateVersion = dataInput.readInt(); int stateVersion = dataInput.readInt();
if (stateVersion == STATE_VERSION) { if (stateVersion == STATE_VERSION_1) {
for (int i = 0; i < STATE_VERSION_1_SIZE; i++) {
stateChecksums[i] = dataInput.readLong();
}
} else if (stateVersion == STATE_VERSION) {
for (int i = 0; i < STATE_SIZE; i++) { for (int i = 0; i < STATE_SIZE; i++) {
stateChecksums[i] = dataInput.readLong(); stateChecksums[i] = dataInput.readLong();
} }
@ -353,6 +376,60 @@ public class SettingsBackupAgent extends BackupAgentHelper {
return result; return result;
} }
private byte[] getFileData(String filename) {
InputStream is = null;
try {
File file = new File(filename);
is = new FileInputStream(file);
//Will truncate read on a very long file,
//should not happen for a config file
byte[] bytes = new byte[(int)file.length()];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
//read failure
if (offset < bytes.length) {
Log.w(TAG, "Couldn't backup " + filename);
return EMPTY_DATA;
}
return bytes;
} catch (IOException ioe) {
Log.w(TAG, "Couldn't backup " + filename);
return EMPTY_DATA;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}
}
}
private void restoreFileData(String filename, BackupDataInput data) {
byte[] bytes = new byte[data.getDataSize()];
if (bytes.length <= 0) return;
try {
data.readEntityData(bytes, 0, bytes.length);
File file = new File(filename);
if (file.exists()) file.delete();
OutputStream os = new BufferedOutputStream(new FileOutputStream(filename, true));
os.write(bytes);
os.close();
} catch (IOException ioe) {
Log.w(TAG, "Couldn't restore " + filename);
}
}
private byte[] getWifiSupplicant(String filename) { private byte[] getWifiSupplicant(String filename) {
BufferedReader br = null; BufferedReader br = null;
try { try {
@ -455,10 +532,9 @@ public class SettingsBackupAgent extends BackupAgentHelper {
} }
private int enableWifi(boolean enable) { private int enableWifi(boolean enable) {
WifiManager wfm = (WifiManager) getSystemService(Context.WIFI_SERVICE); if (mWfm != null) {
if (wfm != null) { int state = mWfm.getWifiState();
int state = wfm.getWifiState(); mWfm.setWifiEnabled(enable);
wfm.setWifiEnabled(enable);
return state; return state;
} }
return WifiManager.WIFI_STATE_UNKNOWN; return WifiManager.WIFI_STATE_UNKNOWN;

View File

@ -29,12 +29,12 @@ import android.content.IntentFilter;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.database.ContentObserver; import android.database.ContentObserver;
import android.net.wifi.IWifiManager; import android.net.wifi.IWifiManager;
import android.net.wifi.ScanResult;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiInfo; import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.net.wifi.WifiStateMachine; import android.net.wifi.WifiStateMachine;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiConfiguration;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiConfiguration.KeyMgmt; import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WpsConfiguration; import android.net.wifi.WpsConfiguration;
import android.net.wifi.WpsResult; import android.net.wifi.WpsResult;
@ -882,8 +882,6 @@ public class WifiService extends IWifiManager.Stub {
mWifiStateMachine.clearBlacklist(); mWifiStateMachine.clearBlacklist();
} }
/** /**
* Get a reference to handler. This is used by a client to establish * Get a reference to handler. This is used by a client to establish
* an AsyncChannel communication with WifiService * an AsyncChannel communication with WifiService
@ -898,6 +896,14 @@ public class WifiService extends IWifiManager.Stub {
return new Messenger(mAsyncServiceHandler); return new Messenger(mAsyncServiceHandler);
} }
/**
* Get the IP and proxy configuration file
*/
public String getConfigFile() {
enforceAccessPermission();
return mWifiStateMachine.getConfigFile();
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {

View File

@ -104,5 +104,7 @@ interface IWifiManager
void clearBlacklist(); void clearBlacklist();
Messenger getMessenger(); Messenger getMessenger();
String getConfigFile();
} }

View File

@ -1347,4 +1347,8 @@ class WifiConfigStore {
} }
return sb.toString(); return sb.toString();
} }
public static String getConfigFile() {
return ipConfigFile;
}
} }

View File

@ -1175,6 +1175,18 @@ public class WifiManager {
} }
} }
/**
* Returns the file in which IP and proxy configuration data is stored
* @hide
*/
public String getConfigFile() {
try {
return mService.getConfigFile();
} catch (RemoteException e) {
return null;
}
}
/** /**
* Allows an application to keep the Wi-Fi radio awake. * Allows an application to keep the Wi-Fi radio awake.
* Normally the Wi-Fi radio may turn off when the user has not used the device in a while. * Normally the Wi-Fi radio may turn off when the user has not used the device in a while.

View File

@ -886,6 +886,13 @@ public class WifiStateMachine extends HierarchicalStateMachine {
return mFrequencyBand.get(); return mFrequencyBand.get();
} }
/**
* Returns the wifi configuration file
*/
public String getConfigFile() {
return WifiConfigStore.getConfigFile();
}
/** /**
* Send a message indicating bluetooth adapter connection state changed * Send a message indicating bluetooth adapter connection state changed
*/ */