@Override
 public boolean onContextItemSelected(MenuItem item) {
   if (mSelectedAccessPoint == null) {
     return super.onContextItemSelected(item);
   }
   switch (item.getItemId()) {
     case MENU_ID_CONNECT:
       {
         if (mSelectedAccessPoint.networkId != INVALID_NETWORK_ID) {
           mWifiManager.connect(mSelectedAccessPoint.networkId, mConnectListener);
         } else if (mSelectedAccessPoint.security == AccessPoint.SECURITY_NONE) {
           /** Bypass dialog for unsecured networks */
           mSelectedAccessPoint.generateOpenNetworkConfig();
           mWifiManager.connect(mSelectedAccessPoint.getConfig(), mConnectListener);
         } else {
           showDialog(mSelectedAccessPoint, true);
         }
         return true;
       }
     case MENU_ID_FORGET:
       {
         mWifiManager.forget(mSelectedAccessPoint.networkId, mForgetListener);
         return true;
       }
     case MENU_ID_MODIFY:
       {
         showDialog(mSelectedAccessPoint, true);
         return true;
       }
   }
   return super.onContextItemSelected(item);
 }
  private void updateConnectionState(DetailedState state) {
    /* sticky broadcasts can call this when wifi is disabled */
    if (!mWifiManager.isWifiEnabled()) {
      mScanner.pause();
      return;
    }

    if (state == DetailedState.OBTAINING_IPADDR) {
      mScanner.pause();
    } else {
      mScanner.resume();
    }

    mLastInfo = mWifiManager.getConnectionInfo();
    if (state != null) {
      mLastState = state;
    }

    for (int i = getPreferenceScreen().getPreferenceCount() - 1; i >= 0; --i) {
      // Maybe there's a WifiConfigPreference
      Preference preference = getPreferenceScreen().getPreference(i);
      if (preference instanceof AccessPoint) {
        final AccessPoint accessPoint = (AccessPoint) preference;
        accessPoint.update(mLastInfo, mLastState);
      }
    }
  }
  private void showProxyFields() {
    WifiConfiguration config = null;

    mView.findViewById(R.id.proxy_settings_fields).setVisibility(View.VISIBLE);

    if (mAccessPoint != null && mAccessPoint.networkId != INVALID_NETWORK_ID) {
      config = mAccessPoint.getConfig();
    }

    if (mProxySettingsSpinner.getSelectedItemPosition() == PROXY_STATIC) {
      mView.findViewById(R.id.proxy_warning_limited_support).setVisibility(View.VISIBLE);
      mView.findViewById(R.id.proxy_fields).setVisibility(View.VISIBLE);
      if (mProxyHostView == null) {
        mProxyHostView = (TextView) mView.findViewById(R.id.proxy_hostname);
        mProxyHostView.addTextChangedListener(this);
        mProxyPortView = (TextView) mView.findViewById(R.id.proxy_port);
        mProxyPortView.addTextChangedListener(this);
        mProxyExclusionListView = (TextView) mView.findViewById(R.id.proxy_exclusionlist);
        mProxyExclusionListView.addTextChangedListener(this);
      }
      if (config != null) {
        ProxyProperties proxyProperties = config.linkProperties.getHttpProxy();
        if (proxyProperties != null) {
          mProxyHostView.setText(proxyProperties.getHost());
          mProxyPortView.setText(Integer.toString(proxyProperties.getPort()));
          mProxyExclusionListView.setText(proxyProperties.getExclusionList());
        }
      }
    } else {
      mView.findViewById(R.id.proxy_warning_limited_support).setVisibility(View.GONE);
      mView.findViewById(R.id.proxy_fields).setVisibility(View.GONE);
    }
  }
예제 #4
0
 /**
  * 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);
 }
  /** Returns sorted list of access points */
  private List<AccessPoint> constructAccessPoints() {
    ArrayList<AccessPoint> accessPoints = new ArrayList<AccessPoint>();
    /**
     * Lookup table to more quickly update AccessPoints by only considering objects with the correct
     * SSID. Maps SSID -> List of AccessPoints with the given SSID.
     */
    Multimap<String, AccessPoint> apMap = new Multimap<String, AccessPoint>();

    final List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
    if (configs != null) {
      for (WifiConfiguration config : configs) {
        AccessPoint accessPoint = new AccessPoint(getActivity(), config);
        accessPoint.update(mLastInfo, mLastState);
        accessPoints.add(accessPoint);
        apMap.put(accessPoint.ssid, accessPoint);
      }
    }

    final List<ScanResult> results = mWifiManager.getScanResults();
    if (results != null) {
      for (ScanResult result : results) {
        // Ignore hidden networks.
        if (result.SSID == null || result.SSID.length() == 0) {
          continue;
        }

        // Ignore IBSS if chipset does not support them
        if (!mIbssSupported && result.capabilities.contains("[IBSS]")) {
          continue;
        }

        boolean found = false;
        for (AccessPoint accessPoint : apMap.getAll(result.SSID)) {
          if (accessPoint.update(result)) found = true;
        }
        if (!found) {
          AccessPoint accessPoint = new AccessPoint(getActivity(), result);
          accessPoints.add(accessPoint);
          apMap.put(accessPoint.ssid, accessPoint);
        }
      }
    }

    // Pre-sort accessPoints to speed preference insertion
    Collections.sort(accessPoints);
    return accessPoints;
  }
 @Override
 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
   if (preference instanceof AccessPoint) {
     mSelectedAccessPoint = (AccessPoint) preference;
     /** Bypass dialog for unsecured, unsaved networks */
     if (mSelectedAccessPoint.security == AccessPoint.SECURITY_NONE
         && mSelectedAccessPoint.networkId == INVALID_NETWORK_ID
         && !mSelectedAccessPoint.isIBSS) {
       mSelectedAccessPoint.generateOpenNetworkConfig();
       mWifiManager.connect(mSelectedAccessPoint.getConfig(), mConnectListener);
     } else {
       showDialog(mSelectedAccessPoint, false);
     }
   } else {
     return super.onPreferenceTreeClick(screen, preference);
   }
   return true;
 }
  @Override
  public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo info) {
    if (info instanceof AdapterContextMenuInfo) {
      Preference preference =
          (Preference) getListView().getItemAtPosition(((AdapterContextMenuInfo) info).position);

      if (preference instanceof AccessPoint) {
        mSelectedAccessPoint = (AccessPoint) preference;
        menu.setHeaderTitle(mSelectedAccessPoint.ssid);
        if (mSelectedAccessPoint.getLevel() != -1 && mSelectedAccessPoint.getState() == null) {
          menu.add(Menu.NONE, MENU_ID_CONNECT, 0, R.string.wifi_menu_connect);
        }
        if (mSelectedAccessPoint.networkId != INVALID_NETWORK_ID) {
          menu.add(Menu.NONE, MENU_ID_FORGET, 0, R.string.wifi_menu_forget);
          menu.add(Menu.NONE, MENU_ID_MODIFY, 0, R.string.wifi_menu_modify);
        }
      }
    }
  }
  private void showSecurityFields() {
    if (mInXlSetupWizard) {
      // Note: XL SetupWizard won't hide "EAP" settings here.
      if (!((WifiSettingsForSetupWizardXL) mConfigUi.getContext())
          .initSecurityFields(mView, mAccessPointSecurity)) {
        return;
      }
    }
    if (mAccessPointSecurity == AccessPoint.SECURITY_NONE) {
      mView.findViewById(R.id.security_fields).setVisibility(View.GONE);
      return;
    }
    mView.findViewById(R.id.security_fields).setVisibility(View.VISIBLE);

    if (mPasswordView == null) {
      mPasswordView = (TextView) mView.findViewById(R.id.password);
      mPasswordView.addTextChangedListener(this);
      ((CheckBox) mView.findViewById(R.id.show_password)).setOnClickListener(this);

      if (mAccessPoint != null && mAccessPoint.networkId != INVALID_NETWORK_ID) {
        mPasswordView.setHint(R.string.wifi_unchanged);
      }
    }

    if (mAccessPointSecurity != AccessPoint.SECURITY_EAP) {
      mView.findViewById(R.id.eap).setVisibility(View.GONE);
      return;
    }
    mView.findViewById(R.id.eap).setVisibility(View.VISIBLE);

    if (mEapMethodSpinner == null) {
      mEapMethodSpinner = (Spinner) mView.findViewById(R.id.method);
      mPhase2Spinner = (Spinner) mView.findViewById(R.id.phase2);
      mEapCaCertSpinner = (Spinner) mView.findViewById(R.id.ca_cert);
      mEapUserCertSpinner = (Spinner) mView.findViewById(R.id.user_cert);
      mEapIdentityView = (TextView) mView.findViewById(R.id.identity);
      mEapAnonymousView = (TextView) mView.findViewById(R.id.anonymous);

      loadCertificates(mEapCaCertSpinner, Credentials.CA_CERTIFICATE);
      loadCertificates(mEapUserCertSpinner, Credentials.USER_PRIVATE_KEY);

      if (mAccessPoint != null && mAccessPoint.networkId != INVALID_NETWORK_ID) {
        WifiConfiguration config = mAccessPoint.getConfig();
        setSelection(mEapMethodSpinner, config.eap.value());
        setSelection(mPhase2Spinner, config.phase2.value());
        setCertificate(mEapCaCertSpinner, Credentials.CA_CERTIFICATE, config.ca_cert.value());
        setCertificate(
            mEapUserCertSpinner, Credentials.USER_PRIVATE_KEY, config.private_key.value());
        mEapIdentityView.setText(config.identity.value());
        mEapAnonymousView.setText(config.anonymous_identity.value());
      }
    }
  }
  private void showIpConfigFields() {
    WifiConfiguration config = null;

    mView.findViewById(R.id.ip_fields).setVisibility(View.VISIBLE);

    if (mAccessPoint != null && mAccessPoint.networkId != INVALID_NETWORK_ID) {
      config = mAccessPoint.getConfig();
    }

    if (mIpSettingsSpinner.getSelectedItemPosition() == STATIC_IP) {
      mView.findViewById(R.id.staticip).setVisibility(View.VISIBLE);
      if (mIpAddressView == null) {
        mIpAddressView = (TextView) mView.findViewById(R.id.ipaddress);
        mIpAddressView.addTextChangedListener(this);
        mGatewayView = (TextView) mView.findViewById(R.id.gateway);
        mGatewayView.addTextChangedListener(this);
        mNetworkPrefixLengthView = (TextView) mView.findViewById(R.id.network_prefix_length);
        mNetworkPrefixLengthView.addTextChangedListener(this);
        mDns1View = (TextView) mView.findViewById(R.id.dns1);
        mDns1View.addTextChangedListener(this);
        mDns2View = (TextView) mView.findViewById(R.id.dns2);
        mDns2View.addTextChangedListener(this);
      }
      if (config != null) {
        LinkProperties linkProperties = config.linkProperties;
        Iterator<LinkAddress> iterator = linkProperties.getLinkAddresses().iterator();
        if (iterator.hasNext()) {
          LinkAddress linkAddress = iterator.next();
          mIpAddressView.setText(linkAddress.getAddress().getHostAddress());
          mNetworkPrefixLengthView.setText(Integer.toString(linkAddress.getNetworkPrefixLength()));
        }

        for (RouteInfo route : linkProperties.getRoutes()) {
          if (route.isDefaultRoute()) {
            mGatewayView.setText(route.getGateway().getHostAddress());
            break;
          }
        }

        Iterator<InetAddress> dnsIterator = linkProperties.getDnses().iterator();
        if (dnsIterator.hasNext()) {
          mDns1View.setText(dnsIterator.next().getHostAddress());
        }
        if (dnsIterator.hasNext()) {
          mDns2View.setText(dnsIterator.next().getHostAddress());
        }
      }
    } else {
      mView.findViewById(R.id.staticip).setVisibility(View.GONE);
    }
  }
  @Override
  public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    // If the dialog is showing, save its state.
    if (mDialog != null && mDialog.isShowing()) {
      outState.putBoolean(SAVE_DIALOG_EDIT_MODE, mDlgEdit);
      if (mDlgAccessPoint != null) {
        mAccessPointSavedState = new Bundle();
        mDlgAccessPoint.saveWifiState(mAccessPointSavedState);
        outState.putBundle(SAVE_DIALOG_ACCESS_POINT_STATE, mAccessPointSavedState);
      }
    }
  }
  /* package */ void forget() {
    if (mSelectedAccessPoint.networkId == INVALID_NETWORK_ID) {
      // Should not happen, but a monkey seems to triger it
      Log.e(TAG, "Failed to forget invalid network " + mSelectedAccessPoint.getConfig());
      return;
    }

    mWifiManager.forget(mSelectedAccessPoint.networkId, mForgetListener);

    if (mWifiManager.isWifiEnabled()) {
      mScanner.resume();
    }
    updateAccessPoints();

    // We need to rename/replace "Next" button in wifi setup context.
    changeNextButtonState(false);
  }
  /* 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;
  }
  public WifiConfigController(
      WifiConfigUiBase parent, View view, AccessPoint accessPoint, boolean edit) {
    mConfigUi = parent;
    mInXlSetupWizard = (parent instanceof WifiConfigUiForSetupWizardXL);

    mView = view;
    mAccessPoint = accessPoint;
    mAccessPointSecurity = (accessPoint == null) ? AccessPoint.SECURITY_NONE : accessPoint.security;
    mEdit = edit;

    final Context context = mConfigUi.getContext();
    final Resources resources = context.getResources();

    if (mAccessPoint == null) { // new network
      mConfigUi.setTitle(R.string.wifi_add_network);

      mSsidView = (TextView) mView.findViewById(R.id.ssid);
      mSsidView.addTextChangedListener(this);
      mSecuritySpinner = ((Spinner) mView.findViewById(R.id.security));
      mSecuritySpinner.setOnItemSelectedListener(this);
      if (mInXlSetupWizard) {
        mView.findViewById(R.id.type_ssid).setVisibility(View.VISIBLE);
        mView.findViewById(R.id.type_security).setVisibility(View.VISIBLE);
        // We want custom layout. The content must be same as the other cases.

        ArrayAdapter<String> adapter =
            new ArrayAdapter<String>(
                context,
                R.layout.wifi_setup_custom_list_item_1,
                android.R.id.text1,
                context.getResources().getStringArray(R.array.wifi_security_no_eap));
        mSecuritySpinner.setAdapter(adapter);
      } else {
        mView.findViewById(R.id.type).setVisibility(View.VISIBLE);
      }
      mConfigUi.setSubmitButton(context.getString(R.string.wifi_save));
    } else {
      mConfigUi.setTitle(mAccessPoint.ssid);

      mIpSettingsSpinner = (Spinner) mView.findViewById(R.id.ip_settings);
      mIpSettingsSpinner.setOnItemSelectedListener(this);
      mProxySettingsSpinner = (Spinner) mView.findViewById(R.id.proxy_settings);
      mProxySettingsSpinner.setOnItemSelectedListener(this);

      ViewGroup group = (ViewGroup) mView.findViewById(R.id.info);

      DetailedState state = mAccessPoint.getState();
      if (state != null) {
        addRow(group, R.string.wifi_status, Summary.get(mConfigUi.getContext(), state));
      }

      int level = mAccessPoint.getLevel();
      if (level != -1) {
        String[] signal = resources.getStringArray(R.array.wifi_signal);
        addRow(group, R.string.wifi_signal, signal[level]);
      }

      WifiInfo info = mAccessPoint.getInfo();
      if (info != null && info.getLinkSpeed() != -1) {
        addRow(group, R.string.wifi_speed, info.getLinkSpeed() + WifiInfo.LINK_SPEED_UNITS);
      }

      addRow(group, R.string.wifi_security, mAccessPoint.getSecurityString(false));

      boolean showAdvancedFields = false;
      if (mAccessPoint.networkId != INVALID_NETWORK_ID) {
        WifiConfiguration config = mAccessPoint.getConfig();
        if (config.ipAssignment == IpAssignment.STATIC) {
          mIpSettingsSpinner.setSelection(STATIC_IP);
          showAdvancedFields = true;
        } else {
          mIpSettingsSpinner.setSelection(DHCP);
        }
        // Display IP addresses
        for (InetAddress a : config.linkProperties.getAddresses()) {
          addRow(group, R.string.wifi_ip_address, a.getHostAddress());
        }

        if (config.proxySettings == ProxySettings.STATIC) {
          mProxySettingsSpinner.setSelection(PROXY_STATIC);
          showAdvancedFields = true;
        } else {
          mProxySettingsSpinner.setSelection(PROXY_NONE);
        }

        if (config.status == Status.DISABLED
            && config.disableReason == WifiConfiguration.DISABLED_DNS_FAILURE) {
          addRow(
              group,
              R.string.wifi_disabled_heading,
              context.getString(R.string.wifi_disabled_help));
        }
      }

      /* Show network setup options only for a new network */
      if (mAccessPoint.networkId == INVALID_NETWORK_ID && mAccessPoint.wpsAvailable) {
        showNetworkSetupFields();
      }

      if (mAccessPoint.networkId == INVALID_NETWORK_ID || mEdit) {
        showSecurityFields();
        showIpConfigFields();
        showProxyFields();
        mView.findViewById(R.id.wifi_advanced_toggle).setVisibility(View.VISIBLE);
        mView.findViewById(R.id.wifi_advanced_togglebox).setOnClickListener(this);
        if (showAdvancedFields) {
          ((CheckBox) mView.findViewById(R.id.wifi_advanced_togglebox)).setChecked(true);
          mView.findViewById(R.id.wifi_advanced_fields).setVisibility(View.VISIBLE);
        }
      }

      if (mEdit) {
        mConfigUi.setSubmitButton(context.getString(R.string.wifi_save));
      } else {
        if (state == null && level != -1) {
          mConfigUi.setSubmitButton(context.getString(R.string.wifi_connect));
        } else {
          mView.findViewById(R.id.ip_fields).setVisibility(View.GONE);
        }
        if (mAccessPoint.networkId != INVALID_NETWORK_ID) {
          mConfigUi.setForgetButton(context.getString(R.string.wifi_forget));
        }
      }
    }

    mConfigUi.setCancelButton(context.getString(R.string.wifi_cancel));
    if (mConfigUi.getSubmitButton() != null) {
      enableSubmitIfAppropriate();
    }
  }
  private void showSecurityFields() {
    if (mInXlSetupWizard) {
      // Note: XL SetupWizard won't hide "EAP" settings here.
      if (!((WifiSettingsForSetupWizardXL) mConfigUi.getContext())
          .initSecurityFields(mView, mAccessPointSecurity)) {
        return;
      }
    }
    if (mAccessPointSecurity == AccessPoint.SECURITY_NONE) {
      mView.findViewById(R.id.security_fields).setVisibility(View.GONE);
      return;
    }
    mView.findViewById(R.id.security_fields).setVisibility(View.VISIBLE);

    if (mPasswordView == null) {
      mPasswordView = (TextView) mView.findViewById(R.id.password);
      mPasswordView.addTextChangedListener(this);
      ((CheckBox) mView.findViewById(R.id.show_password)).setOnCheckedChangeListener(this);

      if (mAccessPoint != null && mAccessPoint.networkId != INVALID_NETWORK_ID) {
        mPasswordView.setHint(R.string.wifi_unchanged);
      }
    }

    if (mAccessPointSecurity != AccessPoint.SECURITY_EAP) {
      mView.findViewById(R.id.eap).setVisibility(View.GONE);
      return;
    }
    mView.findViewById(R.id.eap).setVisibility(View.VISIBLE);

    if (mEapMethodSpinner == null) {
      mEapMethodSpinner = (Spinner) mView.findViewById(R.id.method);
      mEapMethodSpinner.setOnItemSelectedListener(this);
      mPhase2Spinner = (Spinner) mView.findViewById(R.id.phase2);
      mEapCaCertSpinner = (Spinner) mView.findViewById(R.id.ca_cert);
      mEapUserCertSpinner = (Spinner) mView.findViewById(R.id.user_cert);
      mEapIdentityView = (TextView) mView.findViewById(R.id.identity);
      mEapAnonymousView = (TextView) mView.findViewById(R.id.anonymous);

      loadCertificates(mEapCaCertSpinner, Credentials.CA_CERTIFICATE);
      loadCertificates(mEapUserCertSpinner, Credentials.USER_PRIVATE_KEY);

      // Modifying an existing network
      if (mAccessPoint != null && mAccessPoint.networkId != INVALID_NETWORK_ID) {
        WifiEnterpriseConfig enterpriseConfig = mAccessPoint.getConfig().enterpriseConfig;
        int eapMethod = enterpriseConfig.getEapMethod();
        int phase2Method = enterpriseConfig.getPhase2Method();
        mEapMethodSpinner.setSelection(eapMethod);
        showEapFieldsByMethod(eapMethod);
        switch (eapMethod) {
          case Eap.PEAP:
            switch (phase2Method) {
              case Phase2.NONE:
                mPhase2Spinner.setSelection(WIFI_PEAP_PHASE2_NONE);
                break;
              case Phase2.MSCHAPV2:
                mPhase2Spinner.setSelection(WIFI_PEAP_PHASE2_MSCHAPV2);
                break;
              case Phase2.GTC:
                mPhase2Spinner.setSelection(WIFI_PEAP_PHASE2_GTC);
                break;
              default:
                Log.e(TAG, "Invalid phase 2 method " + phase2Method);
                break;
            }
            break;
          default:
            mPhase2Spinner.setSelection(phase2Method);
            break;
        }
        setSelection(mEapCaCertSpinner, enterpriseConfig.getCaCertificateAlias());
        setSelection(mEapUserCertSpinner, enterpriseConfig.getClientCertificateAlias());
        mEapIdentityView.setText(enterpriseConfig.getIdentity());
        mEapAnonymousView.setText(enterpriseConfig.getAnonymousIdentity());
      } else {
        // Choose a default for a new network and show only appropriate
        // fields
        mEapMethodSpinner.setSelection(Eap.PEAP);
        showEapFieldsByMethod(Eap.PEAP);
      }
    } else {
      showEapFieldsByMethod(mEapMethodSpinner.getSelectedItemPosition());
    }
  }
  /* 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;
  }