/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.afwsamples.testdpc.policy.wifimanagement;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;

public class WifiConfigUtil {

    /**
     * Save or replace the wifi configuration.
     *
     * @param context
     * @param wifiConfiguration
     * @return success to add/replace the wifi configuration
     */
    public static boolean saveWifiConfiguration(Context context, WifiConfiguration
            wifiConfiguration) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (wifiConfiguration.networkId == -1) {
            // new wifi configuration, add it and then save it.
            int networkId = wifiManager.addNetwork(wifiConfiguration);
            if (networkId != -1) {
                // Added successfully, try to save it now.
                if (wifiManager.saveConfiguration()) {
                    return true;
                } else {
                    // Remove the added network that fail to save.
                    wifiManager.removeNetwork(networkId);
                }
            }
        } else {
            // existing wifi configuration, update it and then save it.
            int networkId = wifiManager.updateNetwork(wifiConfiguration);
            if (networkId != -1) {
                if (wifiManager.saveConfiguration()) {
                    return true;
                }
            }
        }
        return false;
    }

}

@@ -1210,16 +1210,46 @@
 RVICE);%0A
+        final int networkId;%0A%0A
         
@@ -1357,36 +1357,32 @@
 it.%0A            
-int 
 networkId = wifi
@@ -1432,88 +1432,87 @@
     
-    if (networkId != -1) %7B%0A                // Added successfully, try to
+%7D else %7B%0A            // existing wifi configuration, update it and then
  save it
  now
@@ -1511,12 +1511,8 @@
 e it
- now
 .%0A  
@@ -1525,32 +1525,50 @@
     
-    if (wifiManager.save
+networkId = wifiManager.updateNetwork(wifi
 Conf
@@ -1568,37 +1568,34 @@
 ifiConfiguration
-()) %7B
+);
 %0A               
@@ -1583,35 +1583,35 @@
 n);%0A        
-   
+%7D%0A%0A
         
  return true
@@ -1594,37 +1594,46 @@
  %7D%0A%0A        
- return true;
+if (networkId == -1) %7B
 %0A           
@@ -1629,36 +1629,37 @@
             
-    %7D else %7B
+return false;
 %0A           
@@ -1647,36 +1647,35 @@
  false;%0A        
-    
+%7D%0A%0A
         // Remov
@@ -1673,50 +1673,39 @@
  // 
-Remove the added network that fail
+Added successfully, try
  to save
 .%0A  
@@ -1700,30 +1700,25 @@
  to save
+ it now
 .%0A
-            
         
@@ -1733,13 +1733,13 @@
 ger.
-remov
+enabl
 eNet
@@ -1756,28 +1756,84 @@
 rkId
-);%0A                %7D
+, /* disableOthers */ false);%0A        if (wifiManager.saveConfiguration()) %7B
 %0A   
@@ -1833,33 +1833,44 @@
 ) %7B%0A            
-%7D
+return true;
 %0A        %7D else 
@@ -1890,63 +1890,50 @@
  // 
-existing wifi configuration, update it and then
+Remove the added network that fail to
  save
- it
 .%0A  
@@ -1945,24 +1945,8 @@
     
- int networkId =
  wif
@@ -1946,37 +1946,37 @@
     wifiManager.
-updat
+remov
 eNetwork(wifiCon
@@ -1972,214 +1972,55 @@
 ork(
-wifiConfiguration);%0A            if (networkId != -1) %7B%0A                if (wifiManager.saveConfiguration()) %7B%0A                    return true;%0A                %7D%0A            %7D%0A        %7D%0A        return false;
+networkId);%0A            return false;%0A        %7D
 %0A   

Enable wifi configs after adding/updating