/** Shows/hides password field and client certificate spinner */
  private void updateViewFromAuthType() {
    AuthType authType = getSelectedAuthType();
    boolean isAuthTypeExternal = (AuthType.EXTERNAL == authType);

    if (isAuthTypeExternal) {

      // hide password fields, show client certificate fields
      mPasswordView.setVisibility(View.GONE);
      mPasswordLabelView.setVisibility(View.GONE);
      mClientCertificateLabelView.setVisibility(View.VISIBLE);
      mClientCertificateSpinner.setVisibility(View.VISIBLE);
    } else {

      // show password fields, hide client certificate fields
      mPasswordView.setVisibility(View.VISIBLE);
      mPasswordLabelView.setVisibility(View.VISIBLE);
      mClientCertificateLabelView.setVisibility(View.GONE);
      mClientCertificateSpinner.setVisibility(View.GONE);
    }
  }
  protected void onNext() {
    try {

      ConnectionSecurity connectionSecurity = getSelectedSecurity();

      String username = mUsernameView.getText().toString();
      String password = null;
      String clientCertificateAlias = null;

      AuthType authType = getSelectedAuthType();
      if (authType == AuthType.EXTERNAL) {
        clientCertificateAlias = mClientCertificateSpinner.getAlias();
      } else {
        password = mPasswordView.getText().toString();
      }
      String host = mServerView.getText().toString();
      int port = Integer.parseInt(mPortView.getText().toString());

      Map<String, String> extra = null;
      if (Type.IMAP == mStoreType) {
        extra = new HashMap<String, String>();
        extra.put(
            ImapStoreSettings.AUTODETECT_NAMESPACE_KEY,
            Boolean.toString(mImapAutoDetectNamespaceView.isChecked()));
        extra.put(ImapStoreSettings.PATH_PREFIX_KEY, mImapPathPrefixView.getText().toString());
      } else if (Type.WebDAV == mStoreType) {
        extra = new HashMap<String, String>();
        extra.put(WebDavStoreSettings.PATH_KEY, mWebdavPathPrefixView.getText().toString());
        extra.put(WebDavStoreSettings.AUTH_PATH_KEY, mWebdavAuthPathView.getText().toString());
        extra.put(
            WebDavStoreSettings.MAILBOX_PATH_KEY, mWebdavMailboxPathView.getText().toString());
      }

      mAccount.deleteCertificate(host, port, CheckDirection.INCOMING);
      ServerSettings settings =
          new ServerSettings(
              mStoreType,
              host,
              port,
              connectionSecurity,
              authType,
              username,
              password,
              clientCertificateAlias,
              extra);

      mAccount.setStoreUri(RemoteStore.createStoreUri(settings));

      mAccount.setCompression(NetworkType.MOBILE, mCompressionMobile.isChecked());
      mAccount.setCompression(NetworkType.WIFI, mCompressionWifi.isChecked());
      mAccount.setCompression(NetworkType.OTHER, mCompressionOther.isChecked());
      // mAccount.setSubscribedFoldersOnly(mSubscribedFoldersOnly.isChecked());

      // visual voicemail specific setup
      if (!Intent.ACTION_EDIT.equals(getIntent().getAction())) {
        mAccount.setRequiresCellular(mRequiresCellular.isChecked());
        mAccount.setDescription(mAccountName.getText().toString());
        mAccount.setPhoneNumber("");
        mAccount = AccountCreator.initialVisualVoicemailSetup(AccountSetupIncoming.this, mAccount);
      }

      AccountSetupCheckSettings.actionCheckSettings(this, mAccount, CheckDirection.INCOMING);
    } catch (Exception e) {
      failure(e);
    }
  }
  /**
   * This is invoked only when the user makes changes to a widget, not when widgets are changed
   * programmatically. (The logic is simpler when you know that this is the last thing called after
   * an input change.)
   */
  private void validateFields() {
    AuthType authType = getSelectedAuthType();
    boolean isAuthTypeExternal = (AuthType.EXTERNAL == authType);

    ConnectionSecurity connectionSecurity = getSelectedSecurity();
    boolean hasConnectionSecurity = (connectionSecurity != ConnectionSecurity.NONE);

    if (isAuthTypeExternal && !hasConnectionSecurity) {

      // Notify user of an invalid combination of AuthType.EXTERNAL & ConnectionSecurity.NONE
      String toastText =
          getString(
              R.string.account_setup_incoming_invalid_setting_combo_notice,
              getString(R.string.account_setup_incoming_auth_type_label),
              AuthType.EXTERNAL.toString(),
              getString(R.string.account_setup_incoming_security_label),
              ConnectionSecurity.NONE.toString());
      Toast.makeText(this, toastText, Toast.LENGTH_LONG).show();

      // Reset the views back to their previous settings without recursing through here again
      OnItemSelectedListener onItemSelectedListener = mAuthTypeView.getOnItemSelectedListener();
      mAuthTypeView.setOnItemSelectedListener(null);
      mAuthTypeView.setSelection(mCurrentAuthTypeViewPosition, false);
      mAuthTypeView.setOnItemSelectedListener(onItemSelectedListener);
      updateViewFromAuthType();

      onItemSelectedListener = mSecurityTypeView.getOnItemSelectedListener();
      mSecurityTypeView.setOnItemSelectedListener(null);
      mSecurityTypeView.setSelection(mCurrentSecurityTypeViewPosition, false);
      mSecurityTypeView.setOnItemSelectedListener(onItemSelectedListener);
      updateAuthPlainTextFromSecurityType(getSelectedSecurity());

      mPortView.removeTextChangedListener(validationTextWatcher);
      mPortView.setText(mCurrentPortViewSetting);
      mPortView.addTextChangedListener(validationTextWatcher);

      authType = getSelectedAuthType();
      isAuthTypeExternal = (AuthType.EXTERNAL == authType);

      connectionSecurity = getSelectedSecurity();
      hasConnectionSecurity = (connectionSecurity != ConnectionSecurity.NONE);
    } else {
      mCurrentAuthTypeViewPosition = mAuthTypeView.getSelectedItemPosition();
      mCurrentSecurityTypeViewPosition = mSecurityTypeView.getSelectedItemPosition();
      mCurrentPortViewSetting = mPortView.getText().toString();
    }

    boolean hasValidCertificateAlias = mClientCertificateSpinner.getAlias() != null;
    boolean hasValidUserName = Utility.requiredFieldValid(mUsernameView);

    boolean hasValidPasswordSettings =
        hasValidUserName && !isAuthTypeExternal && Utility.requiredFieldValid(mPasswordView);

    boolean hasValidExternalAuthSettings =
        hasValidUserName && isAuthTypeExternal && hasConnectionSecurity && hasValidCertificateAlias;

    mNextButton.setEnabled(
        Utility.domainFieldValid(mServerView)
            && Utility.requiredFieldValid(mPortView)
            && (hasValidPasswordSettings || hasValidExternalAuthSettings)
            && (Intent.ACTION_EDIT.equals(getIntent().getAction())
                || !mAccountName.getText().toString().equals("")));
    Utility.setCompoundDrawablesAlpha(mNextButton, mNextButton.isEnabled() ? 255 : 128);
  }
  /**
   * Called at the end of either {@code onCreate()} or {@code onRestoreInstanceState()}, after the
   * views have been initialized, so that the listeners are not triggered during the view
   * initialization. This avoids needless calls to {@code validateFields()} which is called
   * immediately after this is called.
   */
  private void initializeViewListeners() {

    /*
     * Updates the port when the user changes the security type. This allows
     * us to show a reasonable default which the user can change.
     */
    mSecurityTypeView.setOnItemSelectedListener(
        new OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            /*
             * We keep our own record of the spinner state so we
             * know for sure that onItemSelected() was called
             * because of user input, not because of spinner
             * state initialization. This assures that the port
             * will not be replaced with a default value except
             * on user input.
             */
            if (mCurrentSecurityTypeViewPosition != position) {
              updatePortFromSecurityType();
              validateFields();
            }
          }

          @Override
          public void onNothingSelected(AdapterView<?> parent) {
            /* unused */
          }
        });

    mAuthTypeView.setOnItemSelectedListener(
        new OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (mCurrentAuthTypeViewPosition == position) {
              return;
            }

            updateViewFromAuthType();
            validateFields();
            AuthType selection = getSelectedAuthType();

            // Have the user select (or confirm) the client certificate
            if (AuthType.EXTERNAL == selection) {

              // This may again invoke validateFields()
              mClientCertificateSpinner.chooseCertificate();
            } else {
              mPasswordView.requestFocus();
            }
          }

          @Override
          public void onNothingSelected(AdapterView<?> parent) {
            /* unused */
          }
        });

    mClientCertificateSpinner.setOnClientCertificateChangedListener(
        clientCertificateChangedListener);
    mUsernameView.addTextChangedListener(validationTextWatcher);
    mPasswordView.addTextChangedListener(validationTextWatcher);
    mServerView.addTextChangedListener(validationTextWatcher);
    mPortView.addTextChangedListener(validationTextWatcher);
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.account_setup_incoming);

    mUsernameView = (EditText) findViewById(R.id.account_username);
    mPasswordView = (EditText) findViewById(R.id.account_password);
    mClientCertificateSpinner =
        (ClientCertificateSpinner) findViewById(R.id.account_client_certificate_spinner);
    mClientCertificateLabelView = (TextView) findViewById(R.id.account_client_certificate_label);
    mPasswordLabelView = (TextView) findViewById(R.id.account_password_label);
    TextView serverLabelView = (TextView) findViewById(R.id.account_server_label);
    mServerView = (EditText) findViewById(R.id.account_server);
    mPortView = (EditText) findViewById(R.id.account_port);
    mSecurityTypeView = (Spinner) findViewById(R.id.account_security_type);
    mAuthTypeView = (Spinner) findViewById(R.id.account_auth_type);
    mImapAutoDetectNamespaceView = (CheckBox) findViewById(R.id.imap_autodetect_namespace);
    mImapPathPrefixView = (EditText) findViewById(R.id.imap_path_prefix);
    mWebdavPathPrefixView = (EditText) findViewById(R.id.webdav_path_prefix);
    mWebdavAuthPathView = (EditText) findViewById(R.id.webdav_auth_path);
    mWebdavMailboxPathView = (EditText) findViewById(R.id.webdav_mailbox_path);
    mNextButton = (Button) findViewById(R.id.next);
    mCompressionMobile = (CheckBox) findViewById(R.id.compression_mobile);
    mCompressionWifi = (CheckBox) findViewById(R.id.compression_wifi);
    mCompressionOther = (CheckBox) findViewById(R.id.compression_other);
    // mSubscribedFoldersOnly = (CheckBox)findViewById(R.id.subscribed_folders_only);
    mRequiresCellular = (CheckBox) findViewById(R.id.account_requires_cellular);
    mAccountName = (EditText) findViewById(R.id.account_name);

    mNextButton.setOnClickListener(this);

    mImapAutoDetectNamespaceView.setOnCheckedChangeListener(
        new OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mImapPathPrefixView.setEnabled(!isChecked);
            if (isChecked && mImapPathPrefixView.hasFocus()) {
              mImapPathPrefixView.focusSearch(View.FOCUS_UP).requestFocus();
            } else if (!isChecked) {
              mImapPathPrefixView.requestFocus();
            }
          }
        });

    mAuthTypeAdapter = AuthTypeAdapter.get(this);
    mAuthTypeView.setAdapter(mAuthTypeAdapter);

    /*
     * Only allow digits in the port field.
     */
    mPortView.setKeyListener(DigitsKeyListener.getInstance("0123456789"));

    String accountUuid = getIntent().getStringExtra(EXTRA_ACCOUNT);
    mAccount = Preferences.getPreferences(this).getAccount(accountUuid);
    // mMakeDefault = getIntent().getBooleanExtra(EXTRA_MAKE_DEFAULT, false);

    /*
     * If we're being reloaded we override the original account with the one
     * we saved
     */
    if (savedInstanceState != null && savedInstanceState.containsKey(EXTRA_ACCOUNT)) {
      accountUuid = savedInstanceState.getString(EXTRA_ACCOUNT);
      mAccount = Preferences.getPreferences(this).getAccount(accountUuid);
    }

    boolean editSettings = Intent.ACTION_EDIT.equals(getIntent().getAction());

    try {
      ServerSettings settings = RemoteStore.decodeStoreUri(mAccount.getStoreUri());

      if (savedInstanceState == null) {
        // The first item is selected if settings.authenticationType is null or is not in
        // mAuthTypeAdapter
        mCurrentAuthTypeViewPosition =
            mAuthTypeAdapter.getAuthPosition(settings.authenticationType);
      } else {
        mCurrentAuthTypeViewPosition = savedInstanceState.getInt(STATE_AUTH_TYPE_POSITION);
      }
      mAuthTypeView.setSelection(mCurrentAuthTypeViewPosition, false);
      updateViewFromAuthType();

      if (settings.username != null) {
        mUsernameView.setText(settings.username);
      }

      if (settings.password != null) {
        mPasswordView.setText(settings.password);
      }

      if (settings.clientCertificateAlias != null) {
        mClientCertificateSpinner.setAlias(settings.clientCertificateAlias);
      }

      mStoreType = settings.type;
      if (Type.POP3 == settings.type) {
        serverLabelView.setText(R.string.account_setup_incoming_pop_server_label);
        findViewById(R.id.imap_path_prefix_section).setVisibility(View.GONE);
        findViewById(R.id.webdav_advanced_header).setVisibility(View.GONE);
        findViewById(R.id.webdav_mailbox_alias_section).setVisibility(View.GONE);
        findViewById(R.id.webdav_owa_path_section).setVisibility(View.GONE);
        findViewById(R.id.webdav_auth_path_section).setVisibility(View.GONE);
        findViewById(R.id.compression_section).setVisibility(View.GONE);
        findViewById(R.id.compression_label).setVisibility(View.GONE);
        // mSubscribedFoldersOnly.setVisibility(View.GONE);
      } else if (Type.IMAP == settings.type) {
        serverLabelView.setText(R.string.account_setup_incoming_imap_server_label);

        ImapStoreSettings imapSettings = (ImapStoreSettings) settings;

        mImapAutoDetectNamespaceView.setChecked(imapSettings.autoDetectNamespace);
        if (imapSettings.pathPrefix != null) {
          mImapPathPrefixView.setText(imapSettings.pathPrefix);
        }

        findViewById(R.id.webdav_advanced_header).setVisibility(View.GONE);
        findViewById(R.id.webdav_mailbox_alias_section).setVisibility(View.GONE);
        findViewById(R.id.webdav_owa_path_section).setVisibility(View.GONE);
        findViewById(R.id.webdav_auth_path_section).setVisibility(View.GONE);

        /*if (!editSettings) {
            findViewById(R.id.imap_folder_setup_section).setVisibility(View.GONE);
        }*/
      } else if (Type.WebDAV == settings.type) {
        serverLabelView.setText(R.string.account_setup_incoming_webdav_server_label);
        mConnectionSecurityChoices =
            new ConnectionSecurity[] {ConnectionSecurity.NONE, ConnectionSecurity.SSL_TLS_REQUIRED};

        // Hide the unnecessary fields
        findViewById(R.id.imap_path_prefix_section).setVisibility(View.GONE);
        findViewById(R.id.account_auth_type_label).setVisibility(View.GONE);
        findViewById(R.id.account_auth_type).setVisibility(View.GONE);
        findViewById(R.id.compression_section).setVisibility(View.GONE);
        findViewById(R.id.compression_label).setVisibility(View.GONE);
        // mSubscribedFoldersOnly.setVisibility(View.GONE);

        WebDavStoreSettings webDavSettings = (WebDavStoreSettings) settings;

        if (webDavSettings.path != null) {
          mWebdavPathPrefixView.setText(webDavSettings.path);
        }

        if (webDavSettings.authPath != null) {
          mWebdavAuthPathView.setText(webDavSettings.authPath);
        }

        if (webDavSettings.mailboxPath != null) {
          mWebdavMailboxPathView.setText(webDavSettings.mailboxPath);
        }
      } else {
        throw new Exception("Unknown account type: " + mAccount.getStoreUri());
      }

      if (!editSettings) {
        mAccount.setDeletePolicy(AccountCreator.getDefaultDeletePolicy(settings.type));
      }

      // Note that mConnectionSecurityChoices is configured above based on server type
      ConnectionSecurityAdapter securityTypesAdapter =
          ConnectionSecurityAdapter.get(this, mConnectionSecurityChoices);
      mSecurityTypeView.setAdapter(securityTypesAdapter);

      // Select currently configured security type
      if (savedInstanceState == null) {
        mCurrentSecurityTypeViewPosition =
            securityTypesAdapter.getConnectionSecurityPosition(settings.connectionSecurity);
      } else {

        /*
         * Restore the spinner state now, before calling
         * setOnItemSelectedListener(), thus avoiding a call to
         * onItemSelected(). Then, when the system restores the state
         * (again) in onRestoreInstanceState(), The system will see that
         * the new state is the same as the current state (set here), so
         * once again onItemSelected() will not be called.
         */
        mCurrentSecurityTypeViewPosition = savedInstanceState.getInt(STATE_SECURITY_TYPE_POSITION);
      }
      mSecurityTypeView.setSelection(mCurrentSecurityTypeViewPosition, false);

      updateAuthPlainTextFromSecurityType(settings.connectionSecurity);

      mCompressionMobile.setChecked(mAccount.useCompression(NetworkType.MOBILE));
      mCompressionWifi.setChecked(mAccount.useCompression(NetworkType.WIFI));
      mCompressionOther.setChecked(mAccount.useCompression(NetworkType.OTHER));

      if (settings.host != null) {
        mServerView.setText(settings.host);
      }

      if (settings.port != -1) {
        mPortView.setText(Integer.toString(settings.port));
      } else {
        updatePortFromSecurityType();
      }
      mCurrentPortViewSetting = mPortView.getText().toString();

      // mSubscribedFoldersOnly.setChecked(mAccount.subscribedFoldersOnly());
    } catch (Exception e) {
      failure(e);
    }
    // show & populate setup fields
    if (!Intent.ACTION_EDIT.equals(getIntent().getAction())) {
      findViewById(R.id.account_name_label).setVisibility(View.VISIBLE);
      mAccountName.setVisibility(View.VISIBLE);
      String desc = mAccount.getDescription();
      mAccountName.setText((desc != null ? desc : ""));
      findViewById(R.id.account_requires_cellular_label).setVisibility(View.VISIBLE);
      mRequiresCellular.setVisibility(View.VISIBLE);
      mRequiresCellular.setChecked(true);
    }
  }