Merge "Backup and restore IP and proxy settings" into honeycomb-mr1
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsBackupAgent.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsBackupAgent.java
index fa63edb..744798e 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsBackupAgent.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsBackupAgent.java
@@ -27,6 +27,7 @@
 import java.io.FileOutputStream;
 import java.io.FileReader;
 import java.io.FileWriter;
+import java.io.InputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.util.Arrays;
@@ -58,15 +59,20 @@
     private static final String KEY_SECURE = "secure";
     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
     // 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_SECURE = 1;
-    private static final int STATE_LOCALE = 2;
-    private static final int STATE_WIFI   = 3;
-    private static final int STATE_SIZE   = 4; // The number of state items
+    private static final int STATE_SYSTEM          = 0;
+    private static final int STATE_SECURE          = 1;
+    private static final int STATE_LOCALE          = 2;
+    private static final int STATE_WIFI_SUPPLICANT = 3;
+    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[] sortedSecureKeys = null;
@@ -91,12 +97,18 @@
     // 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.
     private static final String KEY_WIFI_SUPPLICANT = "\uffedWIFI";
+    private static final String KEY_WIFI_CONFIG = "\uffedCONFIG_WIFI";
 
     private SettingsHelper mSettingsHelper;
+    private WifiManager mWfm;
+    private static String mWifiConfigFile;
 
     public void onCreate() {
         mSettingsHelper = new SettingsHelper(this);
         super.onCreate();
+
+        WifiManager mWfm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
+        if (mWfm != null) mWifiConfigFile = mWfm.getConfigFile();
     }
 
     @Override
@@ -106,7 +118,8 @@
         byte[] systemSettingsData = getSystemSettings();
         byte[] secureSettingsData = getSecureSettings();
         byte[] locale = mSettingsHelper.getLocaleData();
-        byte[] wifiData = getWifiSupplicant(FILE_WIFI_SUPPLICANT);
+        byte[] wifiSupplicantData = getWifiSupplicant(FILE_WIFI_SUPPLICANT);
+        byte[] wifiConfigData = getFileData(mWifiConfigFile);
 
         long[] stateChecksums = readOldChecksums(oldState);
 
@@ -116,8 +129,12 @@
                 writeIfChanged(stateChecksums[STATE_SECURE], KEY_SECURE, secureSettingsData, data);
         stateChecksums[STATE_LOCALE] =
                 writeIfChanged(stateChecksums[STATE_LOCALE], KEY_LOCALE, locale, data);
-        stateChecksums[STATE_WIFI] =
-                writeIfChanged(stateChecksums[STATE_WIFI], KEY_WIFI_SUPPLICANT, wifiData, data);
+        stateChecksums[STATE_WIFI_SUPPLICANT] =
+                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);
     }
@@ -148,7 +165,9 @@
                 byte[] localeData = new byte[size];
                 data.readEntityData(localeData, 0, size);
                 mSettingsHelper.setLocaleData(localeData);
-            } else {
+            } else if (KEY_WIFI_CONFIG.equals(key)) {
+                restoreFileData(mWifiConfigFile, data);
+             } else {
                 data.skipEntityData();
             }
         }
@@ -162,7 +181,11 @@
 
         try {
             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++) {
                     stateChecksums[i] = dataInput.readLong();
                 }
@@ -353,6 +376,60 @@
         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) {
         BufferedReader br = null;
         try {
@@ -455,10 +532,9 @@
     }
 
     private int enableWifi(boolean enable) {
-        WifiManager wfm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
-        if (wfm != null) {
-            int state = wfm.getWifiState();
-            wfm.setWifiEnabled(enable);
+        if (mWfm != null) {
+            int state = mWfm.getWifiState();
+            mWfm.setWifiEnabled(enable);
             return state;
         }
         return WifiManager.WIFI_STATE_UNKNOWN;
diff --git a/services/java/com/android/server/WifiService.java b/services/java/com/android/server/WifiService.java
index 3c6c427..0000237 100644
--- a/services/java/com/android/server/WifiService.java
+++ b/services/java/com/android/server/WifiService.java
@@ -29,12 +29,12 @@
 import android.content.pm.PackageManager;
 import android.database.ContentObserver;
 import android.net.wifi.IWifiManager;
+import android.net.wifi.ScanResult;
+import android.net.wifi.SupplicantState;
 import android.net.wifi.WifiInfo;
 import android.net.wifi.WifiManager;
 import android.net.wifi.WifiStateMachine;
-import android.net.wifi.ScanResult;
 import android.net.wifi.WifiConfiguration;
-import android.net.wifi.SupplicantState;
 import android.net.wifi.WifiConfiguration.KeyMgmt;
 import android.net.wifi.WpsConfiguration;
 import android.net.wifi.WpsResult;
@@ -882,8 +882,6 @@
         mWifiStateMachine.clearBlacklist();
     }
 
-
-
     /**
      * Get a reference to handler. This is used by a client to establish
      * an AsyncChannel communication with WifiService
@@ -898,6 +896,14 @@
         return new Messenger(mAsyncServiceHandler);
     }
 
+    /**
+     * Get the IP and proxy configuration file
+     */
+    public String getConfigFile() {
+        enforceAccessPermission();
+        return mWifiStateMachine.getConfigFile();
+    }
+
     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
diff --git a/wifi/java/android/net/wifi/IWifiManager.aidl b/wifi/java/android/net/wifi/IWifiManager.aidl
index 1d115b1..16a61db 100644
--- a/wifi/java/android/net/wifi/IWifiManager.aidl
+++ b/wifi/java/android/net/wifi/IWifiManager.aidl
@@ -104,5 +104,7 @@
     void clearBlacklist();
 
     Messenger getMessenger();
+
+    String getConfigFile();
 }
 
diff --git a/wifi/java/android/net/wifi/WifiConfigStore.java b/wifi/java/android/net/wifi/WifiConfigStore.java
index e6decc88..6455d84 100644
--- a/wifi/java/android/net/wifi/WifiConfigStore.java
+++ b/wifi/java/android/net/wifi/WifiConfigStore.java
@@ -1347,4 +1347,8 @@
         }
         return sb.toString();
     }
+
+    public static String getConfigFile() {
+        return ipConfigFile;
+    }
 }
diff --git a/wifi/java/android/net/wifi/WifiManager.java b/wifi/java/android/net/wifi/WifiManager.java
index 5238899..2e49a77 100644
--- a/wifi/java/android/net/wifi/WifiManager.java
+++ b/wifi/java/android/net/wifi/WifiManager.java
@@ -1176,6 +1176,18 @@
     }
 
     /**
+     * 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.
      * Normally the Wi-Fi radio may turn off when the user has not used the device in a while.
      * Acquiring a WifiLock will keep the radio on until the lock is released.  Multiple
diff --git a/wifi/java/android/net/wifi/WifiStateMachine.java b/wifi/java/android/net/wifi/WifiStateMachine.java
index 717dcf0..4346b327 100644
--- a/wifi/java/android/net/wifi/WifiStateMachine.java
+++ b/wifi/java/android/net/wifi/WifiStateMachine.java
@@ -887,6 +887,13 @@
     }
 
     /**
+     * Returns the wifi configuration file
+     */
+    public String getConfigFile() {
+        return WifiConfigStore.getConfigFile();
+    }
+
+    /**
      * Send a message indicating bluetooth adapter connection state changed
      */
     public void sendBluetoothAdapterStateChange(int state) {