softAp UI changes

Remove channel settings
Add subtext error handling
Remove WEP, keep WPA2 only for now

Bug: 2413908
Change-Id: Ie867e84a0705f0d2185eeb3a4c86a8227446a338
diff --git a/src/com/android/settings/TetherSettings.java b/src/com/android/settings/TetherSettings.java
index eea45c4..5770482 100644
--- a/src/com/android/settings/TetherSettings.java
+++ b/src/com/android/settings/TetherSettings.java
@@ -26,6 +26,8 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.net.ConnectivityManager;
+import android.net.wifi.WifiManager;
+import android.net.wifi.WifiConfiguration;
 import android.os.Environment;
 import android.preference.CheckBoxPreference;
 import android.preference.Preference;
@@ -54,6 +56,7 @@
     private PreferenceScreen mWifiApSettings;
     private WifiApEnabler mWifiApEnabler;
     private PreferenceScreen mTetherHelp;
+    private WifiManager mWifiManager;
 
     private BroadcastReceiver mTetherChangeReceiver;
 
@@ -76,6 +79,8 @@
 
         ConnectivityManager cm =
                 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
+        mWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
+
         mUsbRegexs = cm.getTetherableUsbRegexs();
         if (mUsbRegexs.length == 0) {
             getPreferenceScreen().removePreference(mUsbTether);
@@ -221,6 +226,17 @@
             mUsbTether.setEnabled(false);
             mUsbTether.setChecked(false);
         }
+
+        if (wifiTethered) {
+            WifiConfiguration mWifiConfig = mWifiManager.getWifiApConfiguration();
+            String s = getString(com.android.internal.R.string.wifi_tether_configure_ssid_default);
+            mEnableWifiAp.setSummary(String.format(getString(R.string.wifi_tether_enabled_subtext),
+                                                   (mWifiConfig == null) ? s : mWifiConfig.SSID));
+        }
+
+        if (wifiErrored) {
+            mEnableWifiAp.setSummary(R.string.wifi_error);
+        }
     }
 
     @Override
diff --git a/src/com/android/settings/wifi/WifiApDialog.java b/src/com/android/settings/wifi/WifiApDialog.java
index 7053d72..b8d4782 100644
--- a/src/com/android/settings/wifi/WifiApDialog.java
+++ b/src/com/android/settings/wifi/WifiApDialog.java
@@ -49,6 +49,9 @@
 
     private final DialogInterface.OnClickListener mListener;
 
+    private static final int OPEN_INDEX = 0;
+    private static final int WPA_INDEX = 1;
+
     private View mView;
     private TextView mSsid;
     private int mSecurityType = AccessPoint.SECURITY_NONE;
@@ -76,22 +79,6 @@
                 config.allowedKeyManagement.set(KeyMgmt.NONE);
                 return config;
 
-            case AccessPoint.SECURITY_WEP:
-                config.allowedKeyManagement.set(KeyMgmt.NONE);
-                config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
-                if (mPassword.length() != 0) {
-                    int length = mPassword.length();
-                    String password = mPassword.getText().toString();
-                    // WEP-40, WEP-104, and 256-bit WEP (WEP-232?)
-                    if ((length == 10 || length == 26 || length == 58) &&
-                            password.matches("[0-9A-Fa-f]*")) {
-                        config.wepKeys[0] = password;
-                    } else {
-                        config.wepKeys[0] = '"' + password + '"';
-                    }
-                }
-                return config;
-
             case AccessPoint.SECURITY_PSK:
                 config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
                 config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
@@ -130,14 +117,18 @@
         if (mWifiConfig != null) {
             mSsid.setText(mWifiConfig.SSID);
             switch (mSecurityType) {
-              case AccessPoint.SECURITY_WEP:
-                  mPassword.setText(mWifiConfig.wepKeys[0]);
+              case AccessPoint.SECURITY_NONE:
+                  mSecurity.setSelection(OPEN_INDEX);
                   break;
               case AccessPoint.SECURITY_PSK:
-                  mPassword.setText(mWifiConfig.preSharedKey);
+                  String str = mWifiConfig.preSharedKey;
+                  if (!str.matches("[0-9A-Fa-f]{64}")) {
+                     str = str.substring(1,str.length()-1);
+                  }
+                  mPassword.setText(str);
+                  mSecurity.setSelection(WPA_INDEX);
                   break;
             }
-            mSecurity.setSelection(mSecurityType);
         }
 
         mSsid.addTextChangedListener(this);
@@ -153,7 +144,6 @@
 
     private void validate() {
         if ((mSsid != null && mSsid.length() == 0) ||
-                (mSecurityType == AccessPoint.SECURITY_WEP && mPassword.length() == 0) ||
                    (mSecurityType == AccessPoint.SECURITY_PSK && mPassword.length() < 8)) {
             getButton(BUTTON_SUBMIT).setEnabled(false);
         } else {
@@ -179,7 +169,10 @@
     }
 
     public void onItemSelected(AdapterView parent, View view, int position, long id) {
-        mSecurityType = position;
+        if(position == OPEN_INDEX)
+            mSecurityType = AccessPoint.SECURITY_NONE;
+        else
+            mSecurityType = AccessPoint.SECURITY_PSK;
         showSecurityFields();
         validate();
     }
diff --git a/src/com/android/settings/wifi/WifiApEnabler.java b/src/com/android/settings/wifi/WifiApEnabler.java
index 922b3b2..fc52a7a 100644
--- a/src/com/android/settings/wifi/WifiApEnabler.java
+++ b/src/com/android/settings/wifi/WifiApEnabler.java
@@ -22,7 +22,6 @@
 import android.app.AlertDialog;
 import android.content.BroadcastReceiver;
 import android.content.Context;
-import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.net.NetworkInfo;
@@ -36,15 +35,13 @@
 import android.util.Log;
 import android.widget.Toast;
 
-public class WifiApEnabler implements Preference.OnPreferenceChangeListener,
-                                      DialogInterface.OnClickListener {
+public class WifiApEnabler implements Preference.OnPreferenceChangeListener {
     private final Context mContext;
     private final CheckBoxPreference mCheckBox;
     private final CharSequence mOriginalSummary;
 
-    private final WifiManager mWifiManager;
+    private WifiManager mWifiManager;
     private final IntentFilter mIntentFilter;
-    private AlertDialog mAlertDialog = null;
 
     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
         @Override
@@ -75,50 +72,20 @@
     public void pause() {
         mContext.unregisterReceiver(mReceiver);
         mCheckBox.setOnPreferenceChangeListener(null);
-        if (mAlertDialog != null) {
-            mAlertDialog.dismiss();
-            mAlertDialog = null;
-        }
     }
 
-    public boolean onPreferenceChange(Preference preference, Object value) {
-        boolean enable = (Boolean) value;
+    public boolean onPreferenceChange(Preference preference, Object enable) {
 
-        if (enable && mWifiManager.isWifiEnabled()) {
-            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
-            /**
-             * TODO: this alert will go away.
-             */
-            builder.setMessage("Turning off Wifi client. Enabling Wifi tethering")
-                   .setCancelable(false)
-                   .setPositiveButton(android.R.string.ok, this)
-                   .setNegativeButton(android.R.string.cancel, this);
-
-            mAlertDialog = builder.create();
-            mAlertDialog.show();
+        if (mWifiManager.setWifiApEnabled(null, (Boolean)enable)) {
+            /* Disable here, enabled on receiving success broadcast */
+            mCheckBox.setEnabled(false);
         } else {
-            setUpAccessPoint(enable);
+            mCheckBox.setSummary(R.string.wifi_error);
         }
 
         return false;
     }
 
-    public void onClick(DialogInterface dialog, int id) {
-        if(id == DialogInterface.BUTTON_POSITIVE ) {
-            setUpAccessPoint(true);
-        } else if (id == DialogInterface.BUTTON_NEGATIVE) {
-            dialog.dismiss();
-            mAlertDialog = null;
-        }
-    }
-
-    private void setUpAccessPoint(boolean enable) {
-        if (mWifiManager.setWifiApEnabled(null, enable)) {
-            mCheckBox.setEnabled(false);
-        } else {
-            mCheckBox.setSummary(R.string.wifi_error);
-        }
-    }
 
     private void handleWifiApStateChanged(int state) {
         switch (state) {
@@ -127,8 +94,11 @@
                 mCheckBox.setEnabled(false);
                 break;
             case WifiManager.WIFI_AP_STATE_ENABLED:
+                /**
+                 * Summary on enable is handled by tether
+                 * broadcast notice
+                 */
                 mCheckBox.setChecked(true);
-                mCheckBox.setSummary(null);
                 mCheckBox.setEnabled(true);
                 break;
             case WifiManager.WIFI_AP_STATE_DISABLING:
diff --git a/src/com/android/settings/wifi/WifiApSettings.java b/src/com/android/settings/wifi/WifiApSettings.java
index 17900fe..b8fb904 100644
--- a/src/com/android/settings/wifi/WifiApSettings.java
+++ b/src/com/android/settings/wifi/WifiApSettings.java
@@ -30,6 +30,8 @@
 import android.provider.Settings;
 import android.util.Log;
 import android.net.wifi.WifiConfiguration;
+import android.net.wifi.WifiConfiguration.AuthAlgorithm;
+import android.net.wifi.WifiConfiguration.KeyMgmt;
 import android.net.wifi.WifiManager;
 import android.os.Bundle;
 
@@ -37,14 +39,17 @@
  * Displays preferences for Tethering.
  */
 public class WifiApSettings extends PreferenceActivity
-        implements DialogInterface.OnClickListener, Preference.OnPreferenceChangeListener {
+                            implements DialogInterface.OnClickListener {
 
     private static final String WIFI_AP_SSID_AND_SECURITY = "wifi_ap_ssid_and_security";
-    private static final String WIFI_AP_CHANNEL = "wifi_ap_channel";
     private static final String ENABLE_WIFI_AP = "enable_wifi_ap";
+    private static final int CONFIG_SUBTEXT = R.string.wifi_tether_configure_subtext;
 
+    private static final int OPEN_INDEX = 0;
+    private static final int WPA_INDEX = 1;
+
+    private String[] mSecurityType;
     private Preference mCreateNetwork;
-    private ListPreference mChannel;
     private CheckBoxPreference mEnableWifiAp;
 
     private WifiApDialog mDialog;
@@ -57,38 +62,26 @@
         super.onCreate(savedInstanceState);
 
         mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
+        mWifiConfig = mWifiManager.getWifiApConfiguration();
+        mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
 
         addPreferencesFromResource(R.xml.wifi_ap_settings);
 
         mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
-        mChannel = (ListPreference) findPreference(WIFI_AP_CHANNEL);
         mEnableWifiAp = (CheckBoxPreference) findPreference(ENABLE_WIFI_AP);
 
         mWifiApEnabler = new WifiApEnabler(this, mEnableWifiAp);
 
-        initChannels();
-    }
-
-    public void initChannels() {
-        mChannel.setOnPreferenceChangeListener(this);
-
-        int numChannels = mWifiManager.getNumAllowedChannels();
-
-        String[] entries = new String[numChannels];
-        String[] entryValues = new String[numChannels];
-
-        for (int i = 1; i <= numChannels; i++) {
-            entries[i-1] = "Channel "+i;
-            entryValues[i-1] = i+"";
+        if(mWifiConfig == null) {
+            String s = getString(com.android.internal.R.string.wifi_tether_configure_ssid_default);
+            mCreateNetwork.setSummary(String.format(getString(CONFIG_SUBTEXT),
+                                                    s, mSecurityType[OPEN_INDEX]));
+        } else {
+            mCreateNetwork.setSummary(String.format(getString(CONFIG_SUBTEXT),
+                                      mWifiConfig.SSID,
+                                      mWifiConfig.allowedKeyManagement.get(KeyMgmt.WPA_PSK) ?
+                                      mSecurityType[WPA_INDEX] : mSecurityType[OPEN_INDEX]));
         }
-
-        mChannel.setEntries(entries);
-        mChannel.setEntryValues(entryValues);
-        mChannel.setEnabled(true);
-        /**
-         * TODO: randomize initial channel chosen
-         */
-        mChannel.setValue("2");
     }
 
     @Override
@@ -120,32 +113,15 @@
     }
 
     public void onClick(DialogInterface dialogInterface, int button) {
-        /**
-         * TODO: Needs work
-         */
+
         mWifiConfig = mDialog.getConfig();
 
-        if(mWifiConfig.SSID != null)
-            mCreateNetwork.setSummary(mWifiConfig.SSID);
-
-        /**
-         * TODO: set SSID and security
-         */
-    }
-
-    public boolean onPreferenceChange(Preference preference, Object newValue) {
-        /**
-         * TODO: Needs work
-         */
-
-        String key = preference.getKey();
-        if (key == null) return true;
-
-        if (key.equals(WIFI_AP_CHANNEL)) {
-            int chosenChannel = Integer.parseInt((String) newValue);
-            if(newValue != null)
-                mChannel.setSummary(newValue.toString());
+        if (button == DialogInterface.BUTTON_POSITIVE && mWifiConfig != null) {
+            mWifiManager.setWifiApEnabled(mWifiConfig, true);
+            mCreateNetwork.setSummary(String.format(getString(CONFIG_SUBTEXT),
+                                      mWifiConfig.SSID,
+                                      mWifiConfig.allowedKeyManagement.get(KeyMgmt.WPA_PSK) ?
+                                      mSecurityType[WPA_INDEX] : mSecurityType[OPEN_INDEX]));
         }
-        return true;
     }
 }