/** Updates the title and summary; may indirectly call notifyChanged() */
  private void refresh() {
    setTitle(ssid);

    Context context = getContext();
    if (mConfig != null && mConfig.status == WifiConfiguration.Status.DISABLED) {
      switch (mConfig.disableReason) {
        case WifiConfiguration.DISABLED_AUTH_FAILURE:
          setSummary(context.getString(R.string.wifi_disabled_password_failure));
          break;
        case WifiConfiguration.DISABLED_DHCP_FAILURE:
        case WifiConfiguration.DISABLED_DNS_FAILURE:
          setSummary(context.getString(R.string.wifi_disabled_network_failure));
          break;
        case WifiConfiguration.DISABLED_UNKNOWN_REASON:
          setSummary(context.getString(R.string.wifi_disabled_generic));
      }
    } else if (mRssi == Integer.MAX_VALUE) { // Wifi out of range
      setSummary(context.getString(R.string.wifi_not_in_range));
    } else if (mState != null) { // This is the active connection
      setSummary(Summary.get(context, mState));
    } else { // In range, not disabled.
      StringBuilder summary = new StringBuilder();
      if (mConfig != null) { // Is saved network
        summary.append(context.getString(R.string.wifi_remembered));
      }

      if (security != SECURITY_NONE) {
        String securityStrFormat;
        if (summary.length() == 0) {
          securityStrFormat = context.getString(R.string.wifi_secured_first_item);
        } else {
          securityStrFormat = context.getString(R.string.wifi_secured_second_item);
        }
        summary.append(String.format(securityStrFormat, getSecurityString(true)));
      }

      if (mConfig == null && wpsAvailable) { // Only list WPS available for unsaved networks
        if (summary.length() == 0) {
          summary.append(context.getString(R.string.wifi_wps_available_first_item));
        } else {
          summary.append(context.getString(R.string.wifi_wps_available_second_item));
        }
      }
      setSummary(summary.toString());
    }
  }
  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();
    }
  }