Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c7e46faf authored by Joe Steele's avatar Joe Steele
Browse files

Simplify code with better use of enum ConnectionSecurity

parent 90fedf71
Loading
Loading
Loading
Loading
+52 −48
Original line number Diff line number Diff line
@@ -39,27 +39,13 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
    private static final String EXTRA_ACCOUNT = "account";
    private static final String EXTRA_MAKE_DEFAULT = "makeDefault";

    private static final int[] POP3_PORTS = {
        110, 995, 995, 110, 110
    };

    private static final int[] IMAP_PORTS = {
        143, 993, 993, 143, 143
    };

    private static final int[] WEBDAV_PORTS = {
        80, 443, 443, 443, 443
    };

    private static final ConnectionSecurity[] CONNECTION_SECURITY_TYPES = {
        ConnectionSecurity.NONE,
        ConnectionSecurity.SSL_TLS_OPTIONAL,
        ConnectionSecurity.SSL_TLS_REQUIRED,
        ConnectionSecurity.STARTTLS_OPTIONAL,
        ConnectionSecurity.STARTTLS_REQUIRED
    };
    private static final String POP3_PORT = "110";
    private static final String POP3_SSL_PORT = "995";
    private static final String IMAP_PORT = "143";
    private static final String IMAP_SSL_PORT = "993";
    private static final String WEBDAV_PORT = "80";
    private static final String WEBDAV_SSL_PORT = "443";

    private int[] mAccountPorts;
    private String mStoreType;
    private EditText mUsernameView;
    private EditText mPasswordView;
@@ -80,6 +66,8 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
    private CheckBox mCompressionOther;
    private CheckBox mSubscribedFoldersOnly;
    private ArrayAdapter<AuthType> mAuthTypeAdapter;
    private String mDefaultPort = "";
    private String mDefaultSslPort = "";

    public static void actionIncomingSettings(Activity context, Account account, boolean makeDefault) {
        Intent i = new Intent(context, AccountSetupIncoming.class);
@@ -136,18 +124,8 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
            }
        });

        SpinnerOption securityTypes[] = {
            new SpinnerOption(0, getString(R.string.account_setup_incoming_security_none_label)),
            new SpinnerOption(1,
            getString(R.string.account_setup_incoming_security_ssl_optional_label)),
            new SpinnerOption(2, getString(R.string.account_setup_incoming_security_ssl_label)),
            new SpinnerOption(3,
            getString(R.string.account_setup_incoming_security_tls_optional_label)),
            new SpinnerOption(4, getString(R.string.account_setup_incoming_security_tls_label)),
        };

        ArrayAdapter<SpinnerOption> securityTypesAdapter = new ArrayAdapter<SpinnerOption>(this,
                android.R.layout.simple_spinner_item, securityTypes);
        ArrayAdapter<ConnectionSecurity> securityTypesAdapter = new ArrayAdapter<ConnectionSecurity>(this,
                android.R.layout.simple_spinner_item, ConnectionSecurity.values());
        securityTypesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSecurityTypeView.setAdapter(securityTypesAdapter);

@@ -212,10 +190,14 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
            int position = mAuthTypeAdapter.getPosition(settings.authenticationType);
            mAuthTypeView.setSelection(position, false);

            // Select currently configured security type
            mSecurityTypeView.setSelection(settings.connectionSecurity.ordinal(), false);

            mStoreType = settings.type;
            if (Pop3Store.STORE_TYPE.equals(settings.type)) {
                serverLabelView.setText(R.string.account_setup_incoming_pop_server_label);
                mAccountPorts = POP3_PORTS;
                mDefaultPort = POP3_PORT;
                mDefaultSslPort = POP3_SSL_PORT;
                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);
@@ -227,7 +209,8 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
                mAccount.setDeletePolicy(Account.DELETE_POLICY_NEVER);
            } else if (ImapStore.STORE_TYPE.equals(settings.type)) {
                serverLabelView.setText(R.string.account_setup_incoming_imap_server_label);
                mAccountPorts = IMAP_PORTS;
                mDefaultPort = IMAP_PORT;
                mDefaultSslPort = IMAP_SSL_PORT;

                ImapStoreSettings imapSettings = (ImapStoreSettings) settings;

@@ -247,7 +230,8 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
                }
            } else if (WebDavStore.STORE_TYPE.equals(settings.type)) {
                serverLabelView.setText(R.string.account_setup_incoming_webdav_server_label);
                mAccountPorts = WEBDAV_PORTS;
                mDefaultPort = WEBDAV_PORT;
                mDefaultSslPort = WEBDAV_SSL_PORT;

                // Hide the unnecessary fields
                findViewById(R.id.imap_path_prefix_section).setVisibility(View.GONE);
@@ -275,13 +259,6 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
                throw new Exception("Unknown account type: " + mAccount.getStoreUri());
            }

            // Select currently configured security type
            for (int i = 0; i < CONNECTION_SECURITY_TYPES.length; i++) {
                if (CONNECTION_SECURITY_TYPES[i] == settings.connectionSecurity) {
                    SpinnerOption.setSpinnerOptionValue(mSecurityTypeView, i);
                }
            }

            /*
             * Updates the port when the user changes the security type. This allows
             * us to show a reasonable default which the user can change.
@@ -340,10 +317,38 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
    }

    private void updatePortFromSecurityType() {
        if (mAccountPorts != null) {
            int securityType = (Integer)((SpinnerOption)mSecurityTypeView.getSelectedItem()).value;
            mPortView.setText(Integer.toString(mAccountPorts[securityType]));
        ConnectionSecurity securityType = (ConnectionSecurity) mSecurityTypeView.getSelectedItem();
        mPortView.setText(getDefaultPort(securityType));
    }

    private String getDefaultPort(ConnectionSecurity securityType) {
        String port;
        switch (securityType) {
        case NONE:
            port = mDefaultPort;
            break;
        case STARTTLS_OPTIONAL:
        case STARTTLS_REQUIRED:
            if (WebDavStore.STORE_TYPE.equals(mStoreType)) {
                /*
                 * The concept of STARTTLS is not really applicable for WebDav and should never
                 * have been made a user-selectable option.  But now we must support the setting
                 * if it exists.
                 */
                port = mDefaultSslPort;
            } else {
                port = mDefaultPort;
            }
            break;
        case SSL_TLS_OPTIONAL:
        case SSL_TLS_REQUIRED:
            port = mDefaultSslPort;
            break;
        default:
            Log.e(K9.LOG_TAG, "Unhandled ConnectionSecurity type encountered");
            port = "";
        }
        return port;
    }

    @Override
@@ -389,8 +394,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener

    protected void onNext() {
        try {
            ConnectionSecurity connectionSecurity = CONNECTION_SECURITY_TYPES[
                    (Integer)((SpinnerOption)mSecurityTypeView.getSelectedItem()).value];
            ConnectionSecurity connectionSecurity = (ConnectionSecurity) mSecurityTypeView.getSelectedItem();

            String username = mUsernameView.getText().toString();
            String password = mPasswordView.getText().toString();
+26 −27
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ import com.fsck.k9.activity.K9Activity;
import com.fsck.k9.activity.setup.AccountSetupCheckSettings.CheckDirection;
import com.fsck.k9.helper.Utility;
import com.fsck.k9.mail.AuthType;
import com.fsck.k9.mail.ConnectionSecurity;

import java.io.UnsupportedEncodingException;
import java.net.URI;
@@ -31,23 +32,12 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener,

    private static final String EXTRA_MAKE_DEFAULT = "makeDefault";

    private static final int smtpPorts[] = {
        587, 465, 465, 587, 587
    };
    private static final String SMTP_PORT = "587";
    private static final String SMTP_SSL_PORT = "465";

    private static final String smtpSchemes[] = {
        "smtp", "smtp+ssl", "smtp+ssl+", "smtp+tls", "smtp+tls+"
    };
    /*
    private static final int webdavPorts[] =
    {
        80, 443, 443, 443, 443
    };
    private static final String webdavSchemes[] =
    {
        "webdav", "webdav+ssl", "webdav+ssl+", "webdav+tls", "webdav+tls+"
    };
    */
    private EditText mUsernameView;
    private EditText mPasswordView;
    private EditText mServerView;
@@ -111,18 +101,8 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener,
        mNextButton.setOnClickListener(this);
        mRequireLoginView.setOnCheckedChangeListener(this);

        SpinnerOption securityTypes[] = {
            new SpinnerOption(0, getString(R.string.account_setup_incoming_security_none_label)),
            new SpinnerOption(1,
            getString(R.string.account_setup_incoming_security_ssl_optional_label)),
            new SpinnerOption(2, getString(R.string.account_setup_incoming_security_ssl_label)),
            new SpinnerOption(3,
            getString(R.string.account_setup_incoming_security_tls_optional_label)),
            new SpinnerOption(4, getString(R.string.account_setup_incoming_security_tls_label)),
        };

        ArrayAdapter<SpinnerOption> securityTypesAdapter = new ArrayAdapter<SpinnerOption>(this,
                android.R.layout.simple_spinner_item, securityTypes);
        ArrayAdapter<ConnectionSecurity> securityTypesAdapter = new ArrayAdapter<ConnectionSecurity>(this,
                android.R.layout.simple_spinner_item, ConnectionSecurity.values());
        securityTypesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSecurityTypeView.setAdapter(securityTypesAdapter);

@@ -267,8 +247,27 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener,
    }

    private void updatePortFromSecurityType() {
        int securityType = (Integer)((SpinnerOption)mSecurityTypeView.getSelectedItem()).value;
        mPortView.setText(Integer.toString(smtpPorts[securityType]));
        ConnectionSecurity securityType = (ConnectionSecurity) mSecurityTypeView.getSelectedItem();
        mPortView.setText(getDefaultSmtpPort(securityType));
    }

    private String getDefaultSmtpPort(ConnectionSecurity securityType) {
        String port;
        switch (securityType) {
        case NONE:
        case STARTTLS_OPTIONAL:
        case STARTTLS_REQUIRED:
            port = SMTP_PORT;
            break;
        case SSL_TLS_OPTIONAL:
        case SSL_TLS_REQUIRED:
            port = SMTP_SSL_PORT;
            break;
        default:
            port = "";
            Log.e(K9.LOG_TAG, "Unhandled ConnectionSecurity type encountered");
        }
        return port;
    }

    @Override
+19 −15
Original line number Diff line number Diff line
package com.fsck.k9.mail;

/**
 * The currently available connection security types.
 *
 * <p>
 * Right now this enum is only used by {@link ServerSettings} and converted to store- or
 * transport-specific constants in the different {@link Store} and {@link Transport}
 * implementations. In the future we probably want to change this and use
 * {@code ConnectionSecurity} exclusively.
 * </p>
 */
import com.fsck.k9.K9;
import com.fsck.k9.R;

public enum ConnectionSecurity {
    NONE,
    STARTTLS_OPTIONAL,
    STARTTLS_REQUIRED,
    SSL_TLS_OPTIONAL,
    SSL_TLS_REQUIRED
    NONE(R.string.account_setup_incoming_security_none_label),
    STARTTLS_OPTIONAL(R.string.account_setup_incoming_security_tls_optional_label),
    STARTTLS_REQUIRED(R.string.account_setup_incoming_security_tls_label),
    SSL_TLS_OPTIONAL(R.string.account_setup_incoming_security_ssl_optional_label),
    SSL_TLS_REQUIRED(R.string.account_setup_incoming_security_ssl_label);

    private final int mResourceId;

    private ConnectionSecurity(int id) {
        mResourceId = id;
    }

    @Override
    public String toString() {
        return K9.app.getString(mResourceId);
    }
}
+11 −33
Original line number Diff line number Diff line
@@ -112,12 +112,6 @@ import com.jcraft.jzlib.ZOutputStream;
public class ImapStore extends Store {
    public static final String STORE_TYPE = "IMAP";

    public static final int CONNECTION_SECURITY_NONE = 0;
    public static final int CONNECTION_SECURITY_TLS_OPTIONAL = 1;
    public static final int CONNECTION_SECURITY_TLS_REQUIRED = 2;
    public static final int CONNECTION_SECURITY_SSL_REQUIRED = 3;
    public static final int CONNECTION_SECURITY_SSL_OPTIONAL = 4;

    private static final int IDLE_READ_TIMEOUT_INCREMENT = 5 * 60 * 1000;
    private static final int IDLE_FAILURE_COUNT_LIMIT = 10;
    private static int MAX_DELAY_TIME = 5 * 60 * 1000; // 5 minutes
@@ -355,7 +349,7 @@ public class ImapStore extends Store {
    private int mPort;
    private String mUsername;
    private String mPassword;
    private int mConnectionSecurity;
    private ConnectionSecurity mConnectionSecurity;
    private AuthType mAuthType;
    private volatile String mPathPrefix;
    private volatile String mCombinedPrefix = null;
@@ -374,7 +368,7 @@ public class ImapStore extends Store {
        }

        @Override
        public int getConnectionSecurity() {
        public ConnectionSecurity getConnectionSecurity() {
            return mConnectionSecurity;
        }

@@ -460,23 +454,7 @@ public class ImapStore extends Store {
        mHost = settings.host;
        mPort = settings.port;

        switch (settings.connectionSecurity) {
        case NONE:
            mConnectionSecurity = CONNECTION_SECURITY_NONE;
            break;
        case STARTTLS_OPTIONAL:
            mConnectionSecurity = CONNECTION_SECURITY_TLS_OPTIONAL;
            break;
        case STARTTLS_REQUIRED:
            mConnectionSecurity = CONNECTION_SECURITY_TLS_REQUIRED;
            break;
        case SSL_TLS_OPTIONAL:
            mConnectionSecurity = CONNECTION_SECURITY_SSL_OPTIONAL;
            break;
        case SSL_TLS_REQUIRED:
            mConnectionSecurity = CONNECTION_SECURITY_SSL_REQUIRED;
            break;
        }
        mConnectionSecurity = settings.connectionSecurity;

        mAuthType = settings.authenticationType;
        mUsername = settings.username;
@@ -2427,7 +2405,7 @@ public class ImapStore extends Store {
            }

            try {
                int connectionSecurity = mSettings.getConnectionSecurity();
                ConnectionSecurity connectionSecurity = mSettings.getConnectionSecurity();

                // Try all IPv4 and IPv6 addresses of the host
                InetAddress[] addresses = InetAddress.getAllByName(mSettings.getHost());
@@ -2441,10 +2419,10 @@ public class ImapStore extends Store {
                        SocketAddress socketAddress = new InetSocketAddress(addresses[i],
                                mSettings.getPort());

                        if (connectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED ||
                                connectionSecurity == CONNECTION_SECURITY_SSL_OPTIONAL) {
                        if (connectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED ||
                                connectionSecurity == ConnectionSecurity.SSL_TLS_OPTIONAL) {
                            SSLContext sslContext = SSLContext.getInstance("TLS");
                            boolean secure = connectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED;
                            boolean secure = connectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED;
                            sslContext
                                    .init(null,
                                            new TrustManager[] { TrustManagerFactory.get(
@@ -2494,15 +2472,15 @@ public class ImapStore extends Store {
                    }
                }

                if (mSettings.getConnectionSecurity() == CONNECTION_SECURITY_TLS_OPTIONAL
                        || mSettings.getConnectionSecurity() == CONNECTION_SECURITY_TLS_REQUIRED) {
                if (mSettings.getConnectionSecurity() == ConnectionSecurity.STARTTLS_OPTIONAL
                        || mSettings.getConnectionSecurity() == ConnectionSecurity.STARTTLS_REQUIRED) {

                    if (hasCapability("STARTTLS")) {
                        // STARTTLS
                        executeSimpleCommand("STARTTLS");

                        SSLContext sslContext = SSLContext.getInstance("TLS");
                        boolean secure = mSettings.getConnectionSecurity() == CONNECTION_SECURITY_TLS_REQUIRED;
                        boolean secure = mSettings.getConnectionSecurity() == ConnectionSecurity.STARTTLS_REQUIRED;
                        sslContext.init(null,
                                new TrustManager[] { TrustManagerFactory.get(
                                        mSettings.getHost(),
@@ -2523,7 +2501,7 @@ public class ImapStore extends Store {
                        if (responses.size() != 2) {
                            throw new MessagingException("Invalid CAPABILITY response received");
                        }
                    } else if (mSettings.getConnectionSecurity() == CONNECTION_SECURITY_TLS_REQUIRED) {
                    } else if (mSettings.getConnectionSecurity() == ConnectionSecurity.STARTTLS_REQUIRED) {
                        throw new MessagingException("TLS not supported but required");
                    }
                }
+9 −31
Original line number Diff line number Diff line
@@ -37,12 +37,6 @@ import java.util.Set;
public class Pop3Store extends Store {
    public static final String STORE_TYPE = "POP3";

    public static final int CONNECTION_SECURITY_NONE = 0;
    public static final int CONNECTION_SECURITY_TLS_OPTIONAL = 1;
    public static final int CONNECTION_SECURITY_TLS_REQUIRED = 2;
    public static final int CONNECTION_SECURITY_SSL_REQUIRED = 3;
    public static final int CONNECTION_SECURITY_SSL_OPTIONAL = 4;

    private static final String STLS_COMMAND = "STLS";
    private static final String USER_COMMAND = "USER";
    private static final String PASS_COMMAND = "PASS";
@@ -200,7 +194,7 @@ public class Pop3Store extends Store {
    private String mUsername;
    private String mPassword;
    private AuthType mAuthType;
    private int mConnectionSecurity;
    private ConnectionSecurity mConnectionSecurity;
    private HashMap<String, Folder> mFolders = new HashMap<String, Folder>();
    private Pop3Capabilities mCapabilities;

@@ -225,23 +219,7 @@ public class Pop3Store extends Store {
        mHost = settings.host;
        mPort = settings.port;

        switch (settings.connectionSecurity) {
        case NONE:
            mConnectionSecurity = CONNECTION_SECURITY_NONE;
            break;
        case STARTTLS_OPTIONAL:
            mConnectionSecurity = CONNECTION_SECURITY_TLS_OPTIONAL;
            break;
        case STARTTLS_REQUIRED:
            mConnectionSecurity = CONNECTION_SECURITY_TLS_REQUIRED;
            break;
        case SSL_TLS_OPTIONAL:
            mConnectionSecurity = CONNECTION_SECURITY_SSL_OPTIONAL;
            break;
        case SSL_TLS_REQUIRED:
            mConnectionSecurity = CONNECTION_SECURITY_SSL_REQUIRED;
            break;
        }
        mConnectionSecurity = settings.connectionSecurity;

        mUsername = settings.username;
        mPassword = settings.password;
@@ -321,10 +299,10 @@ public class Pop3Store extends Store {

            try {
                SocketAddress socketAddress = new InetSocketAddress(mHost, mPort);
                if (mConnectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED ||
                        mConnectionSecurity == CONNECTION_SECURITY_SSL_OPTIONAL) {
                if (mConnectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED ||
                        mConnectionSecurity == ConnectionSecurity.SSL_TLS_OPTIONAL) {
                    SSLContext sslContext = SSLContext.getInstance("TLS");
                    final boolean secure = mConnectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED;
                    final boolean secure = mConnectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED;
                    sslContext.init(null,
                            new TrustManager[] { TrustManagerFactory.get(mHost,
                                    mPort, secure) }, new SecureRandom());
@@ -345,14 +323,14 @@ public class Pop3Store extends Store {
                String serverGreeting = executeSimpleCommand(null);

                mCapabilities = getCapabilities();
                if (mConnectionSecurity == CONNECTION_SECURITY_TLS_OPTIONAL
                        || mConnectionSecurity == CONNECTION_SECURITY_TLS_REQUIRED) {
                if (mConnectionSecurity == ConnectionSecurity.STARTTLS_OPTIONAL
                        || mConnectionSecurity == ConnectionSecurity.STARTTLS_REQUIRED) {

                    if (mCapabilities.stls) {
                        executeSimpleCommand(STLS_COMMAND);

                        SSLContext sslContext = SSLContext.getInstance("TLS");
                        boolean secure = mConnectionSecurity == CONNECTION_SECURITY_TLS_REQUIRED;
                        boolean secure = mConnectionSecurity == ConnectionSecurity.STARTTLS_REQUIRED;
                        sslContext.init(null,
                                new TrustManager[] { TrustManagerFactory.get(
                                        mHost, mPort, secure) },
@@ -366,7 +344,7 @@ public class Pop3Store extends Store {
                            throw new MessagingException("Unable to connect socket");
                        }
                        mCapabilities = getCapabilities();
                    } else if (mConnectionSecurity == CONNECTION_SECURITY_TLS_REQUIRED) {
                    } else if (mConnectionSecurity == ConnectionSecurity.STARTTLS_REQUIRED) {
                        throw new MessagingException("TLS not supported but required");
                    }
                }
Loading