/**
  * Generate and save a default wifiConfiguration with common values. Can only be called for
  * unsecured networks.
  *
  * @hide
  */
 protected void generateOpenNetworkConfig() {
   if (security != SECURITY_NONE) throw new IllegalStateException();
   if (mConfig != null) return;
   mConfig = new WifiConfiguration();
   mConfig.SSID = AccessPoint.convertToQuotedString(ssid);
   mConfig.allowedKeyManagement.set(KeyMgmt.NONE);
 }
  /* package */ WifiConfiguration getConfig() {
    if (mAccessPoint != null && mAccessPoint.networkId != INVALID_NETWORK_ID && !mEdit) {
      return null;
    }

    WifiConfiguration config = new WifiConfiguration();

    if (mAccessPoint == null) {
      config.SSID = AccessPoint.convertToQuotedString(mSsidView.getText().toString());
      // If the user adds a network manually, assume that it is hidden.
      config.hiddenSSID = true;
    } else if (mAccessPoint.networkId == INVALID_NETWORK_ID) {
      config.SSID = AccessPoint.convertToQuotedString(mAccessPoint.ssid);
    } else {
      config.networkId = mAccessPoint.networkId;
    }
    config.mode = mAccessPoint.mode;

    switch (mAccessPointSecurity) {
      case AccessPoint.SECURITY_NONE:
        config.allowedKeyManagement.set(KeyMgmt.NONE);
        break;

      case AccessPoint.SECURITY_WEP:
        config.allowedKeyManagement.set(KeyMgmt.NONE);
        config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
        config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
        if (mPasswordView.length() != 0) {
          int length = mPasswordView.length();
          String password = mPasswordView.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 + '"';
          }
        }
        break;

      case AccessPoint.SECURITY_PSK:
        config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
        if (mPasswordView.length() != 0) {
          String password = mPasswordView.getText().toString();
          if (password.matches("[0-9A-Fa-f]{64}")) {
            config.preSharedKey = password;
          } else {
            config.preSharedKey = '"' + password + '"';
          }
        }
        break;

      case AccessPoint.SECURITY_EAP:
        config.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
        config.allowedKeyManagement.set(KeyMgmt.IEEE8021X);
        config.eap.setValue((String) mEapMethodSpinner.getSelectedItem());

        config.phase2.setValue(
            (mPhase2Spinner.getSelectedItemPosition() == 0)
                ? ""
                : "auth=" + mPhase2Spinner.getSelectedItem());
        config.ca_cert.setValue(
            (mEapCaCertSpinner.getSelectedItemPosition() == 0)
                ? ""
                : KEYSTORE_SPACE
                    + Credentials.CA_CERTIFICATE
                    + (String) mEapCaCertSpinner.getSelectedItem());
        config.client_cert.setValue(
            (mEapUserCertSpinner.getSelectedItemPosition() == 0)
                ? ""
                : KEYSTORE_SPACE
                    + Credentials.USER_CERTIFICATE
                    + (String) mEapUserCertSpinner.getSelectedItem());
        config.private_key.setValue(
            (mEapUserCertSpinner.getSelectedItemPosition() == 0)
                ? ""
                : KEYSTORE_SPACE
                    + Credentials.USER_PRIVATE_KEY
                    + (String) mEapUserCertSpinner.getSelectedItem());
        config.identity.setValue(
            (mEapIdentityView.length() == 0) ? "" : mEapIdentityView.getText().toString());
        config.anonymous_identity.setValue(
            (mEapAnonymousView.length() == 0) ? "" : mEapAnonymousView.getText().toString());
        if (mPasswordView.length() != 0) {
          config.password.setValue(mPasswordView.getText().toString());
        }
        break;

      default:
        return null;
    }

    config.proxySettings = mProxySettings;
    config.ipAssignment = mIpAssignment;
    config.linkProperties = new LinkProperties(mLinkProperties);

    return config;
  }
  /* package */ WifiConfiguration getConfig() {
    if (mAccessPoint != null && mAccessPoint.networkId != INVALID_NETWORK_ID && !mEdit) {
      return null;
    }

    WifiConfiguration config = new WifiConfiguration();

    if (mAccessPoint == null) {
      config.SSID = AccessPoint.convertToQuotedString(mSsidView.getText().toString());
      // If the user adds a network manually, assume that it is hidden.
      config.hiddenSSID = true;
    } else if (mAccessPoint.networkId == INVALID_NETWORK_ID) {
      config.SSID = AccessPoint.convertToQuotedString(mAccessPoint.ssid);
    } else {
      config.networkId = mAccessPoint.networkId;
    }

    switch (mAccessPointSecurity) {
      case AccessPoint.SECURITY_NONE:
        config.allowedKeyManagement.set(KeyMgmt.NONE);
        break;

      case AccessPoint.SECURITY_WEP:
        config.allowedKeyManagement.set(KeyMgmt.NONE);
        config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
        config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
        if (mPasswordView.length() != 0) {
          int length = mPasswordView.length();
          String password = mPasswordView.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 + '"';
          }
        }
        break;

      case AccessPoint.SECURITY_PSK:
        config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
        if (mPasswordView.length() != 0) {
          String password = mPasswordView.getText().toString();
          if (password.matches("[0-9A-Fa-f]{64}")) {
            config.preSharedKey = password;
          } else {
            config.preSharedKey = '"' + password + '"';
          }
        }
        break;

      case AccessPoint.SECURITY_EAP:
        config.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
        config.allowedKeyManagement.set(KeyMgmt.IEEE8021X);
        config.enterpriseConfig = new WifiEnterpriseConfig();
        int eapMethod = mEapMethodSpinner.getSelectedItemPosition();
        int phase2Method = mPhase2Spinner.getSelectedItemPosition();
        config.enterpriseConfig.setEapMethod(eapMethod);
        switch (eapMethod) {
          case Eap.PEAP:
            // PEAP supports limited phase2 values
            // Map the index from the PHASE2_PEAP_ADAPTER to the one used
            // by the API which has the full list of PEAP methods.
            switch (phase2Method) {
              case WIFI_PEAP_PHASE2_NONE:
                config.enterpriseConfig.setPhase2Method(Phase2.NONE);
                break;
              case WIFI_PEAP_PHASE2_MSCHAPV2:
                config.enterpriseConfig.setPhase2Method(Phase2.MSCHAPV2);
                break;
              case WIFI_PEAP_PHASE2_GTC:
                config.enterpriseConfig.setPhase2Method(Phase2.GTC);
                break;
              default:
                Log.e(TAG, "Unknown phase2 method" + phase2Method);
                break;
            }
            break;
          default:
            // The default index from PHASE2_FULL_ADAPTER maps to the API
            config.enterpriseConfig.setPhase2Method(phase2Method);
            break;
        }
        String caCert = (String) mEapCaCertSpinner.getSelectedItem();
        if (caCert.equals(unspecifiedCert)) caCert = "";
        config.enterpriseConfig.setCaCertificateAlias(caCert);
        String clientCert = (String) mEapUserCertSpinner.getSelectedItem();
        if (clientCert.equals(unspecifiedCert)) clientCert = "";
        config.enterpriseConfig.setClientCertificateAlias(clientCert);
        config.enterpriseConfig.setIdentity(mEapIdentityView.getText().toString());
        config.enterpriseConfig.setAnonymousIdentity(mEapAnonymousView.getText().toString());

        if (mPasswordView.isShown()) {
          // For security reasons, a previous password is not displayed to user.
          // Update only if it has been changed.
          if (mPasswordView.length() > 0) {
            config.enterpriseConfig.setPassword(mPasswordView.getText().toString());
          }
        } else {
          // clear password
          config.enterpriseConfig.setPassword(mPasswordView.getText().toString());
        }
        break;
      default:
        return null;
    }

    config.proxySettings = mProxySettings;
    config.ipAssignment = mIpAssignment;
    config.linkProperties = new LinkProperties(mLinkProperties);

    return config;
  }