1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-27 03:32:16 -05:00

Remove duplication, enum-ify String

This commit is contained in:
Art O Cathain 2015-02-18 19:42:33 +00:00
parent ebef8eccb9
commit 583a637d79
12 changed files with 71 additions and 40 deletions

View File

@ -16,10 +16,13 @@ import java.util.Map;
* @see com.fsck.k9.mail.store.StoreConfig#getTransportUri()
*/
public class ServerSettings {
public enum Type { IMAP, SMTP, WebDAV, POP3 }
/**
* Name of the store or transport type (e.g. "IMAP").
* Name of the store or transport type (e.g. IMAP).
*/
public final String type;
public final Type type;
/**
* The host name of the server.
@ -99,7 +102,7 @@ public class ServerSettings {
* @param clientCertificateAlias
* see {@link ServerSettings#clientCertificateAlias}
*/
public ServerSettings(String type, String host, int port,
public ServerSettings(Type type, String host, int port,
ConnectionSecurity connectionSecurity, AuthType authenticationType, String username,
String password, String clientCertificateAlias) {
this.type = type;
@ -135,7 +138,7 @@ public class ServerSettings {
* @param extra
* see {@link ServerSettings#extra}
*/
public ServerSettings(String type, String host, int port,
public ServerSettings(Type type, String host, int port,
ConnectionSecurity connectionSecurity, AuthType authenticationType, String username,
String password, String clientCertificateAlias, Map<String, String> extra) {
this.type = type;
@ -158,7 +161,7 @@ public class ServerSettings {
* @param type
* see {@link ServerSettings#type}
*/
public ServerSettings(String type) {
public ServerSettings(Type type) {
this.type = type;
host = null;
port = -1;

View File

@ -77,7 +77,7 @@ import static com.fsck.k9.mail.K9MailLib.PUSH_WAKE_LOCK_TIMEOUT;
* </pre>
*/
public class ImapStore extends RemoteStore {
public static final String STORE_TYPE = "IMAP";
public static final ServerSettings.Type STORE_TYPE = ServerSettings.Type.IMAP;
private static final int IDLE_READ_TIMEOUT_INCREMENT = 5 * 60 * 1000;
private static final int IDLE_FAILURE_COUNT_LIMIT = 10;

View File

@ -38,7 +38,7 @@ import static com.fsck.k9.mail.K9MailLib.LOG_TAG;
import static com.fsck.k9.mail.CertificateValidationException.Reason.MissingCapability;
public class Pop3Store extends RemoteStore {
public static final String STORE_TYPE = "POP3";
public static final ServerSettings.Type STORE_TYPE = ServerSettings.Type.POP3;
private static final String STLS_COMMAND = "STLS";
private static final String USER_COMMAND = "USER";

View File

@ -58,7 +58,7 @@ import static com.fsck.k9.mail.K9MailLib.LOG_TAG;
* </pre>
*/
public class WebDavStore extends RemoteStore {
public static final String STORE_TYPE = "WebDAV";
public static final ServerSettings.Type STORE_TYPE = ServerSettings.Type.WebDAV;
// Authentication types
private static final short AUTH_TYPE_NONE = 0;

View File

@ -32,7 +32,7 @@ import static com.fsck.k9.mail.CertificateValidationException.Reason.MissingCapa
public class SmtpTransport extends Transport {
private TrustedSocketFactory mTrustedSocketFactory;
public static final String TRANSPORT_TYPE = "SMTP";
public static final ServerSettings.Type TRANSPORT_TYPE = ServerSettings.Type.SMTP;
/**
* Decodes a SmtpTransport URI.

View File

@ -16,7 +16,7 @@ import java.util.Collections;
import static com.fsck.k9.mail.K9MailLib.LOG_TAG;
public class WebDavTransport extends Transport {
public static final String TRANSPORT_TYPE = WebDavStore.STORE_TYPE;
public static final ServerSettings.Type TRANSPORT_TYPE = WebDavStore.STORE_TYPE;
/**
* Decodes a WebDavTransport URI.

View File

@ -0,0 +1,31 @@
package com.fsck.k9.account;
import com.fsck.k9.Account.DeletePolicy;
import com.fsck.k9.mail.ServerSettings.Type;
import java.util.HashMap;
import java.util.Map;
/**
* Deals with logic surrounding account creation.
* <p/>
* TODO Move much of the code from com.fsck.k9.activity.setup.* into here
*/
public class AccountCreator {
private static Map<Type, DeletePolicy> defaults;
public static DeletePolicy calculateDefaultDeletePolicy(Type type) {
return getDefaults().get(type);
}
private static synchronized Map<Type, DeletePolicy> getDefaults() {
if (null == defaults) {
defaults = new HashMap<Type, DeletePolicy>();
defaults.put(Type.IMAP, DeletePolicy.ON_DELETE);
defaults.put(Type.POP3, DeletePolicy.NEVER);
defaults.put(Type.WebDAV, DeletePolicy.ON_DELETE);
}
return defaults;
}
}

View File

@ -43,6 +43,7 @@ import com.fsck.k9.mail.Transport;
import com.fsck.k9.mail.store.imap.ImapStore;
import com.fsck.k9.mail.store.RemoteStore;
import com.fsck.k9.mail.transport.SmtpTransport;
import com.fsck.k9.account.AccountCreator;
import com.fsck.k9.view.ClientCertificateSpinner;
import com.fsck.k9.view.ClientCertificateSpinner.OnClientCertificateChangedListener;
@ -321,21 +322,12 @@ public class AccountSetupBasics extends K9Activity
mAccount.setEmail(email);
mAccount.setStoreUri(incomingUri.toString());
mAccount.setTransportUri(outgoingUri.toString());
mAccount.setDraftsFolderName(getString(R.string.special_mailbox_name_drafts));
mAccount.setTrashFolderName(getString(R.string.special_mailbox_name_trash));
mAccount.setArchiveFolderName(getString(R.string.special_mailbox_name_archive));
// Yahoo! has a special folder for Spam, called "Bulk Mail".
if (incomingUriTemplate.getHost().toLowerCase(Locale.US).endsWith(".yahoo.com")) {
mAccount.setSpamFolderName("Bulk Mail");
} else {
mAccount.setSpamFolderName(getString(R.string.special_mailbox_name_spam));
}
mAccount.setSentFolderName(getString(R.string.special_mailbox_name_sent));
if (incomingUri.toString().startsWith("imap")) {
mAccount.setDeletePolicy(DeletePolicy.ON_DELETE);
} else if (incomingUri.toString().startsWith("pop3")) {
mAccount.setDeletePolicy(DeletePolicy.NEVER);
}
setupFolderNames(incomingUriTemplate.getHost().toLowerCase(Locale.US));
ServerSettings incomingSettings = RemoteStore.decodeStoreUri(incomingUri.toString());
mAccount.setDeletePolicy(AccountCreator.calculateDefaultDeletePolicy(incomingSettings.type));
// Check incoming here. Then check outgoing in onActivityResult()
AccountSetupCheckSettings.actionCheckSettings(this, mAccount, CheckDirection.INCOMING);
} catch (URISyntaxException use) {
@ -427,22 +419,28 @@ public class AccountSetupBasics extends K9Activity
mAccount.setStoreUri(storeUri);
mAccount.setTransportUri(transportUri);
setupFolderNames(domain);
AccountSetupAccountType.actionSelectAccountType(this, mAccount, false);
finish();
}
private void setupFolderNames(String domain) {
mAccount.setDraftsFolderName(getString(R.string.special_mailbox_name_drafts));
mAccount.setTrashFolderName(getString(R.string.special_mailbox_name_trash));
mAccount.setSentFolderName(getString(R.string.special_mailbox_name_sent));
mAccount.setArchiveFolderName(getString(R.string.special_mailbox_name_archive));
// Yahoo! has a special folder for Spam, called "Bulk Mail".
if (domain.endsWith(".yahoo.com")) {
mAccount.setSpamFolderName("Bulk Mail");
} else {
mAccount.setSpamFolderName(getString(R.string.special_mailbox_name_spam));
}
AccountSetupAccountType.actionSelectAccountType(this, mAccount, false);
finish();
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.next:

View File

@ -16,7 +16,6 @@ import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
import com.fsck.k9.*;
import com.fsck.k9.Account.DeletePolicy;
import com.fsck.k9.Account.FolderMode;
import com.fsck.k9.Account.NetworkType;
import com.fsck.k9.activity.K9Activity;
@ -34,6 +33,7 @@ import com.fsck.k9.mail.store.webdav.WebDavStore;
import com.fsck.k9.mail.store.imap.ImapStore.ImapStoreSettings;
import com.fsck.k9.mail.store.webdav.WebDavStore.WebDavStoreSettings;
import com.fsck.k9.mail.transport.SmtpTransport;
import com.fsck.k9.account.AccountCreator;
import com.fsck.k9.service.MailService;
import com.fsck.k9.view.ClientCertificateSpinner;
import com.fsck.k9.view.ClientCertificateSpinner.OnClientCertificateChangedListener;
@ -56,7 +56,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
private static final String WEBDAV_PORT = "80";
private static final String WEBDAV_SSL_PORT = "443";
private String mStoreType;
private ServerSettings.Type mStoreType;
private EditText mUsernameView;
private EditText mPasswordView;
private ClientCertificateSpinner mClientCertificateSpinner;
@ -202,7 +202,6 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
findViewById(R.id.compression_section).setVisibility(View.GONE);
findViewById(R.id.compression_label).setVisibility(View.GONE);
mSubscribedFoldersOnly.setVisibility(View.GONE);
mAccount.setDeletePolicy(DeletePolicy.NEVER);
} else if (ImapStore.STORE_TYPE.equals(settings.type)) {
serverLabelView.setText(R.string.account_setup_incoming_imap_server_label);
mDefaultPort = IMAP_PORT;
@ -219,7 +218,6 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
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);
mAccount.setDeletePolicy(DeletePolicy.ON_DELETE);
if (!Intent.ACTION_EDIT.equals(getIntent().getAction())) {
findViewById(R.id.imap_folder_setup_section).setVisibility(View.GONE);
@ -253,11 +251,12 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
if (webDavSettings.mailboxPath != null) {
mWebdavMailboxPathView.setText(webDavSettings.mailboxPath);
}
mAccount.setDeletePolicy(DeletePolicy.ON_DELETE);
} else {
} else {
throw new Exception("Unknown account type: " + mAccount.getStoreUri());
}
mAccount.setDeletePolicy(AccountCreator.calculateDefaultDeletePolicy(settings.type));
// Note that mConnectionSecurityChoices is configured above based on server type
ConnectionSecurityAdapter securityTypesAdapter =
ConnectionSecurityAdapter.get(this, mConnectionSecurityChoices);

View File

@ -485,7 +485,7 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener,
String newHost = mServerView.getText().toString();
int newPort = Integer.parseInt(mPortView.getText().toString());
String type = SmtpTransport.TRANSPORT_TYPE;
ServerSettings.Type type = SmtpTransport.TRANSPORT_TYPE;
ServerSettings server = new ServerSettings(type, newHost, newPort, securityType, authType, username, password, clientCertificateAlias);
uri = Transport.createTransportUri(server);
mAccount.deleteCertificate(newHost, newPort, CheckDirection.OUTGOING);

View File

@ -225,7 +225,7 @@ public class SettingsExporter {
// Write incoming server settings
ServerSettings incoming = RemoteStore.decodeStoreUri(account.getStoreUri());
serializer.startTag(null, INCOMING_SERVER_ELEMENT);
serializer.attribute(null, TYPE_ATTRIBUTE, incoming.type);
serializer.attribute(null, TYPE_ATTRIBUTE, incoming.type.name());
writeElement(serializer, HOST_ELEMENT, incoming.host);
if (incoming.port != -1) {
@ -257,7 +257,7 @@ public class SettingsExporter {
// Write outgoing server settings
ServerSettings outgoing = Transport.decodeTransportUri(account.getTransportUri());
serializer.startTag(null, OUTGOING_SERVER_ELEMENT);
serializer.attribute(null, TYPE_ATTRIBUTE, outgoing.type);
serializer.attribute(null, TYPE_ATTRIBUTE, outgoing.type.name());
writeElement(serializer, HOST_ELEMENT, outgoing.host);
if (outgoing.port != -1) {

View File

@ -384,7 +384,7 @@ public class SettingsImporter {
boolean createAccountDisabled = AuthType.EXTERNAL != incoming.authenticationType &&
(incoming.password == null || incoming.password.isEmpty());
if (account.outgoing == null && !WebDavStore.STORE_TYPE.equals(account.incoming.type)) {
if (account.outgoing == null && !WebDavStore.STORE_TYPE.name().equals(account.incoming.type)) {
// All account types except WebDAV need to provide outgoing server settings
throw new InvalidSettingValueException();
}
@ -1100,7 +1100,7 @@ public class SettingsImporter {
private final ImportedServer mImportedServer;
public ImportedServerSettings(ImportedServer server) {
super(server.type, server.host, convertPort(server.port),
super(ServerSettings.Type.valueOf(server.type), server.host, convertPort(server.port),
convertConnectionSecurity(server.connectionSecurity),
server.authenticationType, server.username, server.password,
server.clientCertificateAlias);