Add WPA support for soft Ap

Bug: 2771935

Change-Id: Ib16c1c3b9aaccbcdf372365d3c0825bdf0680207
diff --git a/services/java/com/android/server/NetworkManagementService.java b/services/java/com/android/server/NetworkManagementService.java
index ef3e89a..eaf68b0 100644
--- a/services/java/com/android/server/NetworkManagementService.java
+++ b/services/java/com/android/server/NetworkManagementService.java
@@ -610,11 +610,10 @@
                  * argv7 - Preamble
                  * argv8 - Max SCB
                  */
-                String str = String.format("softap set " + wlanIface + " " + softapIface +
-                                           " %s %s %s", convertQuotedString(wifiConfig.SSID),
-                                           wifiConfig.allowedKeyManagement.get(KeyMgmt.WPA_PSK) ?
-                                           "wpa2-psk" : "open",
-                                           convertQuotedString(wifiConfig.preSharedKey));
+                 String str = String.format("softap set " + wlanIface + " " + softapIface +
+                                       " %s %s %s", convertQuotedString(wifiConfig.SSID),
+                                       getSecurityType(wifiConfig),
+                                       convertQuotedString(wifiConfig.preSharedKey));
                 mConnector.doCommand(str);
             }
             mConnector.doCommand(String.format("softap startap"));
@@ -631,6 +630,17 @@
         return '"' + s.replaceAll("\\\\","\\\\\\\\").replaceAll("\"","\\\\\"") + '"';
     }
 
+    private String getSecurityType(WifiConfiguration wifiConfig) {
+        switch (wifiConfig.getAuthType()) {
+            case KeyMgmt.WPA_PSK:
+                return "wpa-psk";
+            case KeyMgmt.WPA2_PSK:
+                return "wpa2-psk";
+            default:
+                return "open";
+        }
+    }
+
     public void stopAccessPoint() throws IllegalStateException {
         mContext.enforceCallingOrSelfPermission(
                 android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService");
@@ -656,7 +666,7 @@
             } else {
                 String str = String.format("softap set " + wlanIface + " " + softapIface
                         + " %s %s %s", convertQuotedString(wifiConfig.SSID),
-                        wifiConfig.allowedKeyManagement.get(KeyMgmt.WPA_PSK) ? "wpa2-psk" : "open",
+                        getSecurityType(wifiConfig),
                         convertQuotedString(wifiConfig.preSharedKey));
                 mConnector.doCommand(str);
             }
diff --git a/services/java/com/android/server/WifiService.java b/services/java/com/android/server/WifiService.java
index 5e78353..56fae81 100644
--- a/services/java/com/android/server/WifiService.java
+++ b/services/java/com/android/server/WifiService.java
@@ -501,15 +501,12 @@
     public synchronized void setWifiApConfiguration(WifiConfiguration wifiConfig) {
         enforceChangePermission();
         final ContentResolver cr = mContext.getContentResolver();
-        boolean isWpa;
         if (wifiConfig == null)
             return;
+        int authType = wifiConfig.getAuthType();
         Settings.Secure.putString(cr, Settings.Secure.WIFI_AP_SSID, wifiConfig.SSID);
-        isWpa = wifiConfig.allowedKeyManagement.get(KeyMgmt.WPA_PSK);
-        Settings.Secure.putInt(cr,
-                               Settings.Secure.WIFI_AP_SECURITY,
-                               isWpa ? KeyMgmt.WPA_PSK : KeyMgmt.NONE);
-        if (isWpa)
+        Settings.Secure.putInt(cr, Settings.Secure.WIFI_AP_SECURITY, authType);
+        if (authType != KeyMgmt.NONE)
             Settings.Secure.putString(cr, Settings.Secure.WIFI_AP_PASSWD, wifiConfig.preSharedKey);
     }