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

Dependency Inversion, remove K9.app references

This commit is contained in:
Jan Berkel 2014-12-17 17:42:22 +01:00
parent b36c788ce0
commit 2f30b3956d
11 changed files with 61 additions and 39 deletions

View File

@ -892,11 +892,6 @@ public class Account implements BaseAccount, StoreConfig {
return Uri.parse("content://accounts/" + getUuid());
}
@Override
public TrustedSocketFactory trustedSocketFactory() {
return new DefaultTrustedSocketFactory(K9.app);
}
public synchronized String getStoreUri() {
return mStoreUri;
}
@ -1303,7 +1298,7 @@ public class Account implements BaseAccount, StoreConfig {
}
public Store getRemoteStore() throws MessagingException {
return RemoteStore.getInstance(this);
return RemoteStore.getInstance(K9.app, this);
}
// It'd be great if this actually went into the store implementation

View File

@ -141,7 +141,7 @@ public class AccountSetupCheckSettings extends K9Activity implements OnClickList
if (!(mAccount.getRemoteStore() instanceof WebDavStore)) {
setMessage(R.string.account_setup_check_settings_check_outgoing_msg);
}
Transport transport = Transport.getInstance(mAccount);
Transport transport = Transport.getInstance(K9.app, mAccount);
transport.close();
transport.open();
transport.close();

View File

@ -3489,7 +3489,7 @@ public class MessagingController implements Runnable {
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "Scanning folder '" + account.getOutboxFolderName() + "' (" + ((LocalFolder)localFolder).getId() + ") for messages to send");
Transport transport = Transport.getInstance(account);
Transport transport = Transport.getInstance(K9.app, account);
for (Message message : localMessages) {
if (message.isSet(Flag.DELETED)) {
message.destroy();

View File

@ -1,6 +1,9 @@
package com.fsck.k9.mail;
import android.content.Context;
import com.fsck.k9.mail.ssl.DefaultTrustedSocketFactory;
import com.fsck.k9.mail.store.StoreConfig;
import com.fsck.k9.mail.transport.SmtpTransport;
import com.fsck.k9.mail.transport.WebDavTransport;
@ -15,10 +18,10 @@ public abstract class Transport {
// RFC 1047
protected static final int SOCKET_READ_TIMEOUT = 300000;
public synchronized static Transport getInstance(StoreConfig storeConfig) throws MessagingException {
public synchronized static Transport getInstance(Context context, StoreConfig storeConfig) throws MessagingException {
String uri = storeConfig.getTransportUri();
if (uri.startsWith("smtp")) {
return new SmtpTransport(storeConfig);
return new SmtpTransport(storeConfig, new DefaultTrustedSocketFactory(context));
} else if (uri.startsWith("webdav")) {
return new WebDavTransport(storeConfig);
} else {

View File

@ -1,11 +1,9 @@
package com.fsck.k9.mail.store;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import com.fsck.k9.K9;
import com.fsck.k9.mail.Authentication;
import com.fsck.k9.mail.AuthenticationFailedException;
import com.fsck.k9.mail.CertificateValidationException;
@ -54,19 +52,23 @@ import static com.fsck.k9.mail.K9MailLib.LOG_TAG;
* A cacheable class that stores the details for a single IMAP connection.
*/
class ImapConnection {
private final TrustedSocketFactory socketFactory;
private Socket mSocket;
private PeekableInputStream mIn;
private OutputStream mOut;
private ImapResponseParser mParser;
private int mNextCommandTag;
private Set<String> capabilities = new HashSet<String>();
private ImapSettings mSettings;
private ConnectivityManager mConnectivityManager;
private final TrustedSocketFactory mSocketFactory;
public ImapConnection(final ImapSettings settings, TrustedSocketFactory socketFactory) {
public ImapConnection(ImapSettings settings,
TrustedSocketFactory socketFactory,
ConnectivityManager connectivityManager) {
this.mSettings = settings;
this.socketFactory = socketFactory;
this.mSocketFactory = socketFactory;
this.mConnectivityManager = connectivityManager;
}
public Set<String> getCapabilities() {
@ -154,7 +156,7 @@ class ImapConnection {
mSettings.getPort());
if (connectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED) {
mSocket = socketFactory.createSocket(
mSocket = mSocketFactory.createSocket(
null,
mSettings.getHost(),
mSettings.getPort(),
@ -207,7 +209,7 @@ class ImapConnection {
// STARTTLS
executeSimpleCommand("STARTTLS");
mSocket = socketFactory.createSocket(
mSocket = mSocketFactory.createSocket(
mSocket,
mSettings.getHost(),
mSettings.getPort(),
@ -277,10 +279,9 @@ class ImapConnection {
Log.d(LOG_TAG, ImapCommands.CAPABILITY_COMPRESS_DEFLATE + " = " + hasCapability(ImapCommands.CAPABILITY_COMPRESS_DEFLATE));
}
if (hasCapability(ImapCommands.CAPABILITY_COMPRESS_DEFLATE)) {
ConnectivityManager connectivityManager = (ConnectivityManager) K9.app.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean useCompression = true;
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
NetworkInfo netInfo = mConnectivityManager.getActiveNetworkInfo();
if (netInfo != null) {
int type = netInfo.getType();
if (K9MailLib.isDebug())

View File

@ -32,6 +32,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import android.net.ConnectivityManager;
import android.os.PowerManager;
import android.text.TextUtils;
import android.util.Log;
@ -59,6 +60,7 @@ import com.fsck.k9.mail.internet.MimeHeader;
import com.fsck.k9.mail.internet.MimeMessage;
import com.fsck.k9.mail.internet.MimeMultipart;
import com.fsck.k9.mail.internet.MimeUtility;
import com.fsck.k9.mail.ssl.TrustedSocketFactory;
import com.fsck.k9.mail.store.ImapResponseParser.ImapList;
import com.fsck.k9.mail.store.ImapResponseParser.ImapResponse;
import com.fsck.k9.mail.transport.imap.ImapSettings;
@ -86,6 +88,7 @@ public class ImapStore extends RemoteStore {
private Set<Flag> mPermanentFlagsIndex = EnumSet.noneOf(Flag.class);
private static final String[] EMPTY_STRING_ARRAY = new String[0];
private ConnectivityManager mConnectivityManager;
/**
* Decodes an ImapStore URI.
@ -234,7 +237,7 @@ public class ImapStore extends RemoteStore {
}
try {
Map<String, String> extra = server.getExtra();
String path = null;
String path;
if (extra != null) {
boolean autoDetectNamespace = Boolean.TRUE.toString().equals(
extra.get(ImapStoreSettings.AUTODETECT_NAMESPACE_KEY));
@ -392,8 +395,11 @@ public class ImapStore extends RemoteStore {
*/
private final Map<String, ImapFolder> mFolderCache = new HashMap<String, ImapFolder>();
public ImapStore(StoreConfig storeConfig) throws MessagingException {
super(storeConfig);
public ImapStore(StoreConfig storeConfig,
TrustedSocketFactory trustedSocketFactory,
ConnectivityManager connectivityManager)
throws MessagingException {
super(storeConfig, trustedSocketFactory);
ImapStoreSettings settings;
try {
@ -406,6 +412,7 @@ public class ImapStore extends RemoteStore {
mPort = settings.port;
mConnectionSecurity = settings.connectionSecurity;
mConnectivityManager = connectivityManager;
mAuthType = settings.authenticationType;
mUsername = settings.username;
@ -635,7 +642,11 @@ public class ImapStore extends RemoteStore {
@Override
public void checkSettings() throws MessagingException {
try {
ImapConnection connection = new ImapConnection(new StoreImapSettings(), mStoreConfig.trustedSocketFactory());
ImapConnection connection = new ImapConnection(
new StoreImapSettings(),
mTrustedSocketFactory,
mConnectivityManager);
connection.open();
autoconfigureFolders(connection);
connection.close();
@ -660,7 +671,9 @@ public class ImapStore extends RemoteStore {
}
}
if (connection == null) {
connection = new ImapConnection(new StoreImapSettings(), mStoreConfig.trustedSocketFactory());
connection = new ImapConnection(new StoreImapSettings(),
mTrustedSocketFactory,
mConnectivityManager);
}
return connection;
}

View File

@ -9,6 +9,7 @@ import com.fsck.k9.mail.filter.Hex;
import com.fsck.k9.mail.internet.MimeMessage;
import com.fsck.k9.mail.CertificateValidationException;
import com.fsck.k9.mail.MessageRetrievalListener;
import com.fsck.k9.mail.ssl.TrustedSocketFactory;
import javax.net.ssl.SSLException;
@ -207,8 +208,8 @@ public class Pop3Store extends RemoteStore {
private boolean mTopNotSupported;
public Pop3Store(StoreConfig storeConfig) throws MessagingException {
super(storeConfig);
public Pop3Store(StoreConfig storeConfig, TrustedSocketFactory socketFactory) throws MessagingException {
super(storeConfig, socketFactory);
ServerSettings settings;
try {
@ -302,7 +303,7 @@ public class Pop3Store extends RemoteStore {
try {
SocketAddress socketAddress = new InetSocketAddress(mHost, mPort);
if (mConnectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED) {
mSocket = mStoreConfig.trustedSocketFactory().createSocket(null, mHost, mPort, mClientCertificateAlias);
mSocket = mTrustedSocketFactory.createSocket(null, mHost, mPort, mClientCertificateAlias);
} else {
mSocket = new Socket();
}
@ -324,7 +325,7 @@ public class Pop3Store extends RemoteStore {
if (mCapabilities.stls) {
executeSimpleCommand(STLS_COMMAND);
mSocket = mStoreConfig.trustedSocketFactory().createSocket(
mSocket = mTrustedSocketFactory.createSocket(
mSocket,
mHost,
mPort,

View File

@ -1,8 +1,13 @@
package com.fsck.k9.mail.store;
import android.content.Context;
import android.net.ConnectivityManager;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.ServerSettings;
import com.fsck.k9.mail.Store;
import com.fsck.k9.mail.ssl.DefaultTrustedSocketFactory;
import com.fsck.k9.mail.ssl.TrustedSocketFactory;
import java.util.HashMap;
import java.util.Map;
@ -12,6 +17,7 @@ public abstract class RemoteStore extends Store {
protected static final int SOCKET_READ_TIMEOUT = 60000;
protected StoreConfig mStoreConfig;
protected TrustedSocketFactory mTrustedSocketFactory;
/**
* Remote stores indexed by Uri.
@ -19,14 +25,16 @@ public abstract class RemoteStore extends Store {
private static Map<String, Store> sStores = new HashMap<String, Store>();
public RemoteStore(StoreConfig storeConfig) {
public RemoteStore(StoreConfig storeConfig, TrustedSocketFactory trustedSocketFactory) {
mStoreConfig = storeConfig;
mTrustedSocketFactory = trustedSocketFactory;
}
/**
* Get an instance of a remote mail store.
*/
public synchronized static Store getInstance(StoreConfig storeConfig) throws MessagingException {
public synchronized static Store getInstance(Context context,
StoreConfig storeConfig) throws MessagingException {
String uri = storeConfig.getStoreUri();
if (uri.startsWith("local")) {
@ -36,9 +44,12 @@ public abstract class RemoteStore extends Store {
Store store = sStores.get(uri);
if (store == null) {
if (uri.startsWith("imap")) {
store = new ImapStore(storeConfig);
store = new ImapStore(storeConfig,
new DefaultTrustedSocketFactory(context),
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
} else if (uri.startsWith("pop3")) {
store = new Pop3Store(storeConfig);
store = new Pop3Store(storeConfig,
new DefaultTrustedSocketFactory(context));
} else if (uri.startsWith("webdav")) {
store = new WebDavStore(storeConfig);
}

View File

@ -1,6 +1,5 @@
package com.fsck.k9.mail.store;
import com.fsck.k9.mail.ssl.TrustedSocketFactory;
public interface StoreConfig {
String getStoreUri();
@ -30,6 +29,4 @@ public interface StoreConfig {
int getDisplayCount();
int getIdleRefreshMinutes();
TrustedSocketFactory trustedSocketFactory();
}

View File

@ -295,7 +295,7 @@ public class WebDavStore extends RemoteStore {
public WebDavStore(StoreConfig storeConfig) throws MessagingException {
super(storeConfig);
super(storeConfig, null);
WebDavStoreSettings settings;
try {

View File

@ -186,7 +186,8 @@ public class SmtpTransport extends Transport {
private boolean m8bitEncodingAllowed;
private int mLargestAcceptableMessage;
public SmtpTransport(StoreConfig storeConfig) throws MessagingException {
public SmtpTransport(StoreConfig storeConfig, TrustedSocketFactory trustedSocketFactory)
throws MessagingException {
ServerSettings settings;
try {
settings = decodeUri(storeConfig.getTransportUri());
@ -203,7 +204,7 @@ public class SmtpTransport extends Transport {
mUsername = settings.username;
mPassword = settings.password;
mClientCertificateAlias = settings.clientCertificateAlias;
mTrustedSocketFactory = storeConfig.trustedSocketFactory();
mTrustedSocketFactory = trustedSocketFactory;
}
@Override