diff --git a/src/com/fsck/k9/activity/setup/AccountSetupIncoming.java b/src/com/fsck/k9/activity/setup/AccountSetupIncoming.java index 5d417309f..abe3fc7b3 100644 --- a/src/com/fsck/k9/activity/setup/AccountSetupIncoming.java +++ b/src/com/fsck/k9/activity/setup/AccountSetupIncoming.java @@ -79,7 +79,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener private CheckBox mCompressionWifi; private CheckBox mCompressionOther; private CheckBox mSubscribedFoldersOnly; - private ArrayAdapter mAuthTypeAdapter; + private AuthTypeAdapter mAuthTypeAdapter; private String mDefaultPort = ""; private String mDefaultSslPort = ""; private ConnectionSecurity[] mConnectionSecurityChoices = ConnectionSecurity.values(); @@ -142,7 +142,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener } }); - mAuthTypeAdapter = AuthType.getArrayAdapter(this); + mAuthTypeAdapter = AuthTypeAdapter.get(this); mAuthTypeView.setAdapter(mAuthTypeAdapter); /* @@ -168,7 +168,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener if (savedInstanceState == null) { // The first item is selected if settings.authenticationType is null or is not in mAuthTypeAdapter - mCurrentAuthTypeViewPosition = mAuthTypeAdapter.getPosition(settings.authenticationType); + mCurrentAuthTypeViewPosition = mAuthTypeAdapter.getAuthPosition(settings.authenticationType); } else { mCurrentAuthTypeViewPosition = savedInstanceState.getInt(STATE_AUTH_TYPE_POSITION); } @@ -257,13 +257,13 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener } // Note that mConnectionSecurityChoices is configured above based on server type - ArrayAdapter securityTypesAdapter = - ConnectionSecurity.getArrayAdapter(this, mConnectionSecurityChoices); + ConnectionSecurityAdapter securityTypesAdapter = + ConnectionSecurityAdapter.get(this, mConnectionSecurityChoices); mSecurityTypeView.setAdapter(securityTypesAdapter); // Select currently configured security type if (savedInstanceState == null) { - mCurrentSecurityTypeViewPosition = securityTypesAdapter.getPosition(settings.connectionSecurity); + mCurrentSecurityTypeViewPosition = securityTypesAdapter.getConnectionSecurityPosition(settings.connectionSecurity); } else { /* @@ -347,7 +347,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener updateViewFromAuthType(); validateFields(); - AuthType selection = (AuthType) mAuthTypeView.getSelectedItem(); + AuthType selection = getSelectedAuthType(); // Have the user select (or confirm) the client certificate if (AuthType.EXTERNAL == selection) { @@ -395,7 +395,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener * Shows/hides password field and client certificate spinner */ private void updateViewFromAuthType() { - AuthType authType = (AuthType) mAuthTypeView.getSelectedItem(); + AuthType authType = getSelectedAuthType(); boolean isAuthTypeExternal = (AuthType.EXTERNAL == authType); if (isAuthTypeExternal) { @@ -421,10 +421,10 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener * that this is the last thing called after an input change.) */ private void validateFields() { - AuthType authType = (AuthType) mAuthTypeView.getSelectedItem(); + AuthType authType = getSelectedAuthType(); boolean isAuthTypeExternal = (AuthType.EXTERNAL == authType); - ConnectionSecurity connectionSecurity = (ConnectionSecurity) mSecurityTypeView.getSelectedItem(); + ConnectionSecurity connectionSecurity = getSelectedSecurity(); boolean hasConnectionSecurity = (connectionSecurity != ConnectionSecurity.NONE); if (isAuthTypeExternal && !hasConnectionSecurity) { @@ -448,16 +448,16 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener mSecurityTypeView.setOnItemSelectedListener(null); mSecurityTypeView.setSelection(mCurrentSecurityTypeViewPosition, false); mSecurityTypeView.setOnItemSelectedListener(onItemSelectedListener); - updateAuthPlainTextFromSecurityType((ConnectionSecurity) mSecurityTypeView.getSelectedItem()); + updateAuthPlainTextFromSecurityType(getSelectedSecurity()); mPortView.removeTextChangedListener(validationTextWatcher); mPortView.setText(mCurrentPortViewSetting); mPortView.addTextChangedListener(validationTextWatcher); - authType = (AuthType) mAuthTypeView.getSelectedItem(); + authType = getSelectedAuthType(); isAuthTypeExternal = (AuthType.EXTERNAL == authType); - connectionSecurity = (ConnectionSecurity) mSecurityTypeView.getSelectedItem(); + connectionSecurity = getSelectedSecurity(); hasConnectionSecurity = (connectionSecurity != ConnectionSecurity.NONE); } else { mCurrentAuthTypeViewPosition = mAuthTypeView.getSelectedItemPosition(); @@ -484,7 +484,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener } private void updatePortFromSecurityType() { - ConnectionSecurity securityType = (ConnectionSecurity) mSecurityTypeView.getSelectedItem(); + ConnectionSecurity securityType = getSelectedSecurity(); updateAuthPlainTextFromSecurityType(securityType); // Remove listener so as not to trigger validateFields() which is called @@ -512,13 +512,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener } private void updateAuthPlainTextFromSecurityType(ConnectionSecurity securityType) { - switch (securityType) { - case NONE: - AuthType.PLAIN.useInsecureText(true, mAuthTypeAdapter); - break; - default: - AuthType.PLAIN.useInsecureText(false, mAuthTypeAdapter); - } + mAuthTypeAdapter.useInsecureText(securityType == ConnectionSecurity.NONE); } @Override @@ -547,7 +541,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener String password = null; String clientCertificateAlias = null; - AuthType authType = (AuthType) mAuthTypeView.getSelectedItem(); + AuthType authType = getSelectedAuthType(); if (AuthType.EXTERNAL == authType) { clientCertificateAlias = mClientCertificateSpinner.getAlias(); } else { @@ -575,13 +569,13 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener protected void onNext() { try { - ConnectionSecurity connectionSecurity = (ConnectionSecurity) mSecurityTypeView.getSelectedItem(); + ConnectionSecurity connectionSecurity = getSelectedSecurity(); String username = mUsernameView.getText().toString(); String password = null; String clientCertificateAlias = null; - AuthType authType = (AuthType) mAuthTypeView.getSelectedItem(); + AuthType authType = getSelectedAuthType(); if (authType == AuthType.EXTERNAL) { clientCertificateAlias = mClientCertificateSpinner.getAlias(); } else { @@ -645,6 +639,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener toast.show(); } + /* * Calls validateFields() which enables or disables the Next button * based on the fields' validity. @@ -669,4 +664,14 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener validateFields(); } }; + + private AuthType getSelectedAuthType() { + AuthTypeHolder holder = (AuthTypeHolder) mAuthTypeView.getSelectedItem(); + return holder.authType; + } + + private ConnectionSecurity getSelectedSecurity() { + ConnectionSecurityHolder holder = (ConnectionSecurityHolder) mSecurityTypeView.getSelectedItem(); + return holder.connectionSecurity; + } } diff --git a/src/com/fsck/k9/activity/setup/AccountSetupOutgoing.java b/src/com/fsck/k9/activity/setup/AccountSetupOutgoing.java index 94b704774..7a3e7228c 100644 --- a/src/com/fsck/k9/activity/setup/AccountSetupOutgoing.java +++ b/src/com/fsck/k9/activity/setup/AccountSetupOutgoing.java @@ -55,7 +55,7 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener, private int mCurrentSecurityTypeViewPosition; private Spinner mAuthTypeView; private int mCurrentAuthTypeViewPosition; - private ArrayAdapter mAuthTypeAdapter; + private AuthTypeAdapter mAuthTypeAdapter; private Button mNextButton; private Account mAccount; private boolean mMakeDefault; @@ -112,9 +112,9 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener, mNextButton.setOnClickListener(this); - mSecurityTypeView.setAdapter(ConnectionSecurity.getArrayAdapter(this)); + mSecurityTypeView.setAdapter(ConnectionSecurityAdapter.get(this)); - mAuthTypeAdapter = AuthType.getArrayAdapter(this); + mAuthTypeAdapter = AuthTypeAdapter.get(this); mAuthTypeView.setAdapter(mAuthTypeAdapter); /* @@ -143,7 +143,7 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener, if (savedInstanceState == null) { // The first item is selected if settings.authenticationType is null or is not in mAuthTypeAdapter - mCurrentAuthTypeViewPosition = mAuthTypeAdapter.getPosition(settings.authenticationType); + mCurrentAuthTypeViewPosition = mAuthTypeAdapter.getAuthPosition(settings.authenticationType); } else { mCurrentAuthTypeViewPosition = savedInstanceState.getInt(STATE_AUTH_TYPE_POSITION); } @@ -229,8 +229,8 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener, if (mCurrentSecurityTypeViewPosition != position) { updatePortFromSecurityType(); - boolean isInsecure = (ConnectionSecurity.NONE == mSecurityTypeView.getSelectedItem()); - boolean isAuthExternal = (AuthType.EXTERNAL == mAuthTypeView.getSelectedItem()); + boolean isInsecure = (ConnectionSecurity.NONE == getSelectedSecurity()); + boolean isAuthExternal = (AuthType.EXTERNAL == getSelectedAuthType()); boolean loginNotRequired = !mRequireLoginView.isChecked(); /* @@ -246,7 +246,7 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener, if (isInsecure && isAuthExternal && loginNotRequired) { OnItemSelectedListener onItemSelectedListener = mAuthTypeView.getOnItemSelectedListener(); mAuthTypeView.setOnItemSelectedListener(null); - mCurrentAuthTypeViewPosition = mAuthTypeAdapter.getPosition(AuthType.PLAIN); + mCurrentAuthTypeViewPosition = mAuthTypeAdapter.getAuthPosition(AuthType.PLAIN); mAuthTypeView.setSelection(mCurrentAuthTypeViewPosition, false); mAuthTypeView.setOnItemSelectedListener(onItemSelectedListener); updateViewFromAuthType(); @@ -270,7 +270,7 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener, updateViewFromAuthType(); validateFields(); - AuthType selection = (AuthType) mAuthTypeView.getSelectedItem(); + AuthType selection = getSelectedAuthType(); // Have the user select (or confirm) the client certificate if (AuthType.EXTERNAL == selection) { @@ -330,7 +330,7 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener, * Shows/hides password field and client certificate spinner */ private void updateViewFromAuthType() { - AuthType authType = (AuthType) mAuthTypeView.getSelectedItem(); + AuthType authType = getSelectedAuthType(); boolean isAuthTypeExternal = (AuthType.EXTERNAL == authType); if (isAuthTypeExternal) { @@ -356,10 +356,10 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener, * that this is the last thing called after an input change.) */ private void validateFields() { - AuthType authType = (AuthType) mAuthTypeView.getSelectedItem(); + AuthType authType = getSelectedAuthType(); boolean isAuthTypeExternal = (AuthType.EXTERNAL == authType); - ConnectionSecurity connectionSecurity = (ConnectionSecurity) mSecurityTypeView.getSelectedItem(); + ConnectionSecurity connectionSecurity = getSelectedSecurity(); boolean hasConnectionSecurity = (connectionSecurity != ConnectionSecurity.NONE); if (isAuthTypeExternal && !hasConnectionSecurity) { @@ -383,16 +383,16 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener, mSecurityTypeView.setOnItemSelectedListener(null); mSecurityTypeView.setSelection(mCurrentSecurityTypeViewPosition, false); mSecurityTypeView.setOnItemSelectedListener(onItemSelectedListener); - updateAuthPlainTextFromSecurityType((ConnectionSecurity) mSecurityTypeView.getSelectedItem()); + updateAuthPlainTextFromSecurityType(getSelectedSecurity()); mPortView.removeTextChangedListener(validationTextWatcher); mPortView.setText(mCurrentPortViewSetting); mPortView.addTextChangedListener(validationTextWatcher); - authType = (AuthType) mAuthTypeView.getSelectedItem(); + authType = getSelectedAuthType(); isAuthTypeExternal = (AuthType.EXTERNAL == authType); - connectionSecurity = (ConnectionSecurity) mSecurityTypeView.getSelectedItem(); + connectionSecurity = getSelectedSecurity(); hasConnectionSecurity = (connectionSecurity != ConnectionSecurity.NONE); } else { mCurrentAuthTypeViewPosition = mAuthTypeView.getSelectedItemPosition(); @@ -421,7 +421,7 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener, } private void updatePortFromSecurityType() { - ConnectionSecurity securityType = (ConnectionSecurity) mSecurityTypeView.getSelectedItem(); + ConnectionSecurity securityType = getSelectedSecurity(); updateAuthPlainTextFromSecurityType(securityType); // Remove listener so as not to trigger validateFields() which is called @@ -449,13 +449,7 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener, } private void updateAuthPlainTextFromSecurityType(ConnectionSecurity securityType) { - switch (securityType) { - case NONE: - AuthType.PLAIN.useInsecureText(true, mAuthTypeAdapter); - break; - default: - AuthType.PLAIN.useInsecureText(false, mAuthTypeAdapter); - } + mAuthTypeAdapter.useInsecureText(securityType == ConnectionSecurity.NONE); } @Override @@ -472,7 +466,7 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener, } protected void onNext() { - ConnectionSecurity securityType = (ConnectionSecurity) mSecurityTypeView.getSelectedItem(); + ConnectionSecurity securityType = getSelectedSecurity(); String uri; String username = null; String password = null; @@ -481,7 +475,7 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener, if (mRequireLoginView.isChecked()) { username = mUsernameView.getText().toString(); - authType = (AuthType) mAuthTypeView.getSelectedItem(); + authType = getSelectedAuthType(); if (AuthType.EXTERNAL == authType) { clientCertificateAlias = mClientCertificateSpinner.getAlias(); } else { @@ -511,6 +505,7 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener, mRequireLoginSettingsView.setVisibility(isChecked ? View.VISIBLE : View.GONE); validateFields(); } + private void failure(Exception use) { Log.e(K9.LOG_TAG, "Failure", use); String toastText = getString(R.string.account_setup_bad_uri, use.getMessage()); @@ -541,4 +536,14 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener, validateFields(); } }; + + private AuthType getSelectedAuthType() { + AuthTypeHolder holder = (AuthTypeHolder) mAuthTypeView.getSelectedItem(); + return holder.authType; + } + + private ConnectionSecurity getSelectedSecurity() { + ConnectionSecurityHolder holder = (ConnectionSecurityHolder) mSecurityTypeView.getSelectedItem(); + return holder.connectionSecurity; + } } diff --git a/src/com/fsck/k9/activity/setup/AuthTypeAdapter.java b/src/com/fsck/k9/activity/setup/AuthTypeAdapter.java new file mode 100644 index 000000000..2d11a70f2 --- /dev/null +++ b/src/com/fsck/k9/activity/setup/AuthTypeAdapter.java @@ -0,0 +1,52 @@ +package com.fsck.k9.activity.setup; + +import android.content.Context; +import android.widget.ArrayAdapter; + +import com.fsck.k9.mail.AuthType; + + +class AuthTypeAdapter extends ArrayAdapter { + public AuthTypeAdapter(Context context, int simple_spinner_item, AuthTypeHolder[] holders) { + super(context, simple_spinner_item, holders); + } + + public static AuthTypeAdapter get(Context context) { + AuthType[] authTypes = new AuthType[]{AuthType.PLAIN, AuthType.CRAM_MD5, AuthType.EXTERNAL}; + AuthTypeHolder[] holders = new AuthTypeHolder[authTypes.length]; + for (int i = 0; i < authTypes.length; i++) { + holders[i] = new AuthTypeHolder(authTypes[i], context.getResources()); + } + AuthTypeAdapter authTypesAdapter = new AuthTypeAdapter(context, + android.R.layout.simple_spinner_item, holders); + authTypesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + return authTypesAdapter; + } + + /** + * Used to select an appropriate localized text label for the + * {@code AuthType.PLAIN} option presented to users. + * + * @param insecure + *

+ * A value of {@code true} will use "Normal password". + *

+ * A value of {@code false} will use + * "Password, transmitted insecurely" + */ + public void useInsecureText(boolean insecure) { + for (int i=0; i { + public ConnectionSecurityAdapter(Context context, int resource, ConnectionSecurityHolder[] securityTypes) { + super(context, resource, securityTypes); + } + + public static ConnectionSecurityAdapter get(Context context) { + return get(context, ConnectionSecurity.values()); + } + + public static ConnectionSecurityAdapter get(Context context, + ConnectionSecurity[] items) { + ConnectionSecurityHolder[] holders = new ConnectionSecurityHolder[items.length]; + for (int i = 0; i < items.length; i++) { + holders[i] = new ConnectionSecurityHolder(items[i], context.getResources()); + } + ConnectionSecurityAdapter securityTypesAdapter = new ConnectionSecurityAdapter(context, + android.R.layout.simple_spinner_item, holders); + securityTypesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + return securityTypesAdapter; + } + + public int getConnectionSecurityPosition(ConnectionSecurity connectionSecurity) { + for (int i=0; i authTypesAdapter) { - if (insecure) { - mResourceId = R.string.account_setup_auth_type_insecure_password; - } else { - mResourceId = R.string.account_setup_auth_type_normal_password; - } - authTypesAdapter.notifyDataSetChanged(); - } - }, - - CRAM_MD5(R.string.account_setup_auth_type_encrypted_password), - - EXTERNAL(R.string.account_setup_auth_type_tls_client_certificate), + PLAIN, + CRAM_MD5, + EXTERNAL, /* * The following are obsolete authentication settings that were used with @@ -41,45 +22,6 @@ public enum AuthType { * still exist in a user's settings from a previous version or may be found * when importing settings. */ - AUTOMATIC(0), - - LOGIN(0); - - static public ArrayAdapter getArrayAdapter(Context context) { - AuthType[] authTypes = new AuthType[]{PLAIN, CRAM_MD5, EXTERNAL}; - ArrayAdapter authTypesAdapter = new ArrayAdapter(context, - android.R.layout.simple_spinner_item, authTypes); - authTypesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - return authTypesAdapter; - } - - int mResourceId; - - private AuthType(int id) { - mResourceId = id; - } - - /** - * Used to select an appropriate localized text label for the - * {@code AuthType.PLAIN} option presented to users. - * - * @param insecure - *

- * A value of {@code true} will use "Normal password". - *

- * A value of {@code false} will use - * "Password, transmitted insecurely" - */ - public void useInsecureText(boolean insecure, ArrayAdapter authTypesAdapter) { - // Do nothing. Overridden in AuthType.PLAIN - } - - @Override - public String toString() { - if (mResourceId == 0) { - return name(); - } else { - return K9.app.getString(mResourceId); - } - } + AUTOMATIC, + LOGIN } diff --git a/src/com/fsck/k9/mail/ConnectionSecurity.java b/src/com/fsck/k9/mail/ConnectionSecurity.java index c32bf289b..d4a49268e 100644 --- a/src/com/fsck/k9/mail/ConnectionSecurity.java +++ b/src/com/fsck/k9/mail/ConnectionSecurity.java @@ -1,35 +1,7 @@ package com.fsck.k9.mail; -import android.content.Context; -import android.widget.ArrayAdapter; - -import com.fsck.k9.K9; -import com.fsck.k9.R; - public enum ConnectionSecurity { - NONE(R.string.account_setup_incoming_security_none_label), - STARTTLS_REQUIRED(R.string.account_setup_incoming_security_tls_label), - SSL_TLS_REQUIRED(R.string.account_setup_incoming_security_ssl_label); - - static public ArrayAdapter getArrayAdapter(Context context) { - return getArrayAdapter(context, ConnectionSecurity.values()); - } - - static public ArrayAdapter getArrayAdapter(Context context, ConnectionSecurity[] securityTypes) { - ArrayAdapter securityTypesAdapter = new ArrayAdapter(context, - android.R.layout.simple_spinner_item, securityTypes); - securityTypesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - return securityTypesAdapter; - } - - private final int mResourceId; - - private ConnectionSecurity(int id) { - mResourceId = id; - } - - @Override - public String toString() { - return K9.app.getString(mResourceId); - } + NONE, + STARTTLS_REQUIRED, + SSL_TLS_REQUIRED }