Create ImapStoreConfig

This commit is contained in:
cketti 2020-05-01 05:27:46 +02:00
parent a6f75dc587
commit 82db38bd08
8 changed files with 54 additions and 27 deletions

View file

@ -8,11 +8,13 @@ import com.fsck.k9.backend.api.Backend
import com.fsck.k9.backend.imap.ImapBackend
import com.fsck.k9.backend.imap.ImapStoreUriCreator
import com.fsck.k9.backend.imap.ImapStoreUriDecoder
import com.fsck.k9.mail.NetworkType
import com.fsck.k9.mail.ServerSettings
import com.fsck.k9.mail.oauth.OAuth2TokenProvider
import com.fsck.k9.mail.power.PowerManager
import com.fsck.k9.mail.ssl.TrustedSocketFactory
import com.fsck.k9.mail.store.imap.ImapStore
import com.fsck.k9.mail.store.imap.ImapStoreConfig
import com.fsck.k9.mail.transport.smtp.SmtpTransport
import com.fsck.k9.mail.transport.smtp.SmtpTransportUriCreator
import com.fsck.k9.mail.transport.smtp.SmtpTransportUriDecoder
@ -37,15 +39,27 @@ class ImapBackendFactory(
private fun createImapStore(account: Account): ImapStore {
val oAuth2TokenProvider: OAuth2TokenProvider? = null
val serverSettings = ImapStoreUriDecoder.decode(account.storeUri)
val config = createImapStoreConfig(account)
return ImapStore(
serverSettings,
account,
config,
trustedSocketFactory,
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager,
oAuth2TokenProvider
)
}
private fun createImapStoreConfig(account: Account): ImapStoreConfig {
return object : ImapStoreConfig {
override val logLabel
get() = account.description
override fun isSubscribedFoldersOnly() = account.isSubscribedFoldersOnly
override fun useCompression(type: NetworkType) = account.useCompression(type)
}
}
private fun createSmtpTransport(account: Account): SmtpTransport {
val serverSettings = decodeTransportUri(account.transportUri)
val oauth2TokenProvider: OAuth2TokenProvider? = null

View file

@ -8,11 +8,13 @@ import com.fsck.k9.backend.api.Backend
import com.fsck.k9.backend.imap.ImapBackend
import com.fsck.k9.backend.imap.ImapStoreUriCreator
import com.fsck.k9.backend.imap.ImapStoreUriDecoder
import com.fsck.k9.mail.NetworkType
import com.fsck.k9.mail.ServerSettings
import com.fsck.k9.mail.oauth.OAuth2TokenProvider
import com.fsck.k9.mail.power.PowerManager
import com.fsck.k9.mail.ssl.TrustedSocketFactory
import com.fsck.k9.mail.store.imap.ImapStore
import com.fsck.k9.mail.store.imap.ImapStoreConfig
import com.fsck.k9.mail.transport.smtp.SmtpTransport
import com.fsck.k9.mail.transport.smtp.SmtpTransportUriCreator
import com.fsck.k9.mail.transport.smtp.SmtpTransportUriDecoder
@ -37,15 +39,27 @@ class ImapBackendFactory(
private fun createImapStore(account: Account): ImapStore {
val oAuth2TokenProvider: OAuth2TokenProvider? = null
val serverSettings = ImapStoreUriDecoder.decode(account.storeUri)
val config = createImapStoreConfig(account)
return ImapStore(
serverSettings,
account,
config,
trustedSocketFactory,
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager,
oAuth2TokenProvider
)
}
private fun createImapStoreConfig(account: Account): ImapStoreConfig {
return object : ImapStoreConfig {
override val logLabel
get() = account.description
override fun isSubscribedFoldersOnly() = account.isSubscribedFoldersOnly
override fun useCompression(type: NetworkType) = account.useCompression(type)
}
}
private fun createSmtpTransport(account: Account): SmtpTransport {
val serverSettings = decodeTransportUri(account.transportUri)
val oauth2TokenProvider: OAuth2TokenProvider? = null

View file

@ -1,11 +1,6 @@
package com.fsck.k9.mail.store;
import com.fsck.k9.mail.NetworkType;
public interface StoreConfig {
boolean isSubscribedFoldersOnly();
boolean useCompression(NetworkType type);
String getDraftsFolder();
}

View file

@ -1313,7 +1313,7 @@ public class ImapFolder {
}
protected String getLogId() {
String id = store.getStoreConfig().toString() + ":" + getServerId() + "/" + Thread.currentThread().getName();
String id = store.getLogLabel() + ":" + getServerId() + "/" + Thread.currentThread().getName();
if (connection != null) {
id += "/" + connection.getLogId();
}

View file

@ -24,7 +24,6 @@ import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.NetworkType;
import com.fsck.k9.mail.oauth.OAuth2TokenProvider;
import com.fsck.k9.mail.ssl.TrustedSocketFactory;
import com.fsck.k9.mail.store.StoreConfig;
import timber.log.Timber;
@ -34,7 +33,7 @@ import timber.log.Timber;
* </pre>
*/
public class ImapStore {
private final StoreConfig storeConfig;
private final ImapStoreConfig config;
private final TrustedSocketFactory trustedSocketFactory;
private Set<Flag> permanentFlagsIndex = EnumSet.noneOf(Flag.class);
private ConnectivityManager connectivityManager;
@ -62,10 +61,10 @@ public class ImapStore {
private final Map<String, ImapFolder> folderCache = new HashMap<>();
public ImapStore(ImapStoreSettings serverSettings, StoreConfig storeConfig,
public ImapStore(ImapStoreSettings serverSettings, ImapStoreConfig config,
TrustedSocketFactory trustedSocketFactory, ConnectivityManager connectivityManager,
OAuth2TokenProvider oauthTokenProvider) {
this.storeConfig = storeConfig;
this.config = config;
this.trustedSocketFactory = trustedSocketFactory;
host = serverSettings.host;
@ -125,7 +124,7 @@ public class ImapStore {
try {
List<FolderListItem> folders = listFolders(connection, false);
if (!storeConfig.isSubscribedFoldersOnly()) {
if (!config.isSubscribedFoldersOnly()) {
return folders;
}
@ -329,8 +328,8 @@ public class ImapStore {
return folderNameCodec;
}
StoreConfig getStoreConfig() {
return storeConfig;
String getLogLabel() {
return config.getLogLabel();
}
Set<Flag> getPermanentFlagsIndex() {
@ -376,7 +375,7 @@ public class ImapStore {
@Override
public boolean useCompression(final NetworkType type) {
return storeConfig.useCompression(type);
return config.useCompression(type);
}
@Override

View file

@ -0,0 +1,9 @@
package com.fsck.k9.mail.store.imap
import com.fsck.k9.mail.NetworkType
interface ImapStoreConfig {
val logLabel: String
fun isSubscribedFoldersOnly(): Boolean
fun useCompression(type: NetworkType): Boolean
}

View file

@ -23,7 +23,6 @@ import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.Part;
import com.fsck.k9.mail.internet.BinaryTempFileBody;
import com.fsck.k9.mail.internet.MimeHeader;
import com.fsck.k9.mail.store.StoreConfig;
import okio.Buffer;
import org.apache.james.mime4j.util.MimeUtil;
import org.junit.Before;
@ -67,15 +66,13 @@ public class ImapFolderTest {
private ImapStore imapStore;
private ImapConnection imapConnection;
private StoreConfig storeConfig;
@Before
public void setUp() throws Exception {
BinaryTempFileBody.setTempDirectory(RuntimeEnvironment.application.getCacheDir());
imapStore = mock(ImapStore.class);
storeConfig = mock(StoreConfig.class);
when(imapStore.getCombinedPrefix()).thenReturn("");
when(imapStore.getStoreConfig()).thenReturn(storeConfig);
when(imapStore.getLogLabel()).thenReturn("Account");
imapConnection = mock(ImapConnection.class);
}

View file

@ -20,7 +20,6 @@ import com.fsck.k9.mail.FolderType;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.oauth.OAuth2TokenProvider;
import com.fsck.k9.mail.ssl.TrustedSocketFactory;
import com.fsck.k9.mail.store.StoreConfig;
import org.junit.Before;
import org.junit.Test;
import org.mockito.internal.util.collections.Sets;
@ -39,7 +38,7 @@ import static org.mockito.Mockito.when;
public class ImapStoreTest {
private StoreConfig storeConfig = mock(StoreConfig.class);
private ImapStoreConfig config = mock(ImapStoreConfig.class);
private TestImapStore imapStore;
@Before
@ -49,7 +48,7 @@ public class ImapStoreTest {
ConnectivityManager connectivityManager = mock(ConnectivityManager.class);
OAuth2TokenProvider oauth2TokenProvider = mock(OAuth2TokenProvider.class);
imapStore = new TestImapStore(serverSettings, storeConfig, trustedSocketFactory, connectivityManager,
imapStore = new TestImapStore(serverSettings, config, trustedSocketFactory, connectivityManager,
oauth2TokenProvider);
}
@ -155,7 +154,7 @@ public class ImapStoreTest {
@Test
public void getFolders_withoutSubscribedFoldersOnly() throws Exception {
when(storeConfig.isSubscribedFoldersOnly()).thenReturn(false);
when(config.isSubscribedFoldersOnly()).thenReturn(false);
ImapConnection imapConnection = mock(ImapConnection.class);
List<ImapResponse> imapResponses = Arrays.asList(
createImapResponse("* LIST (\\HasNoChildren) \".\" \"INBOX\""),
@ -175,7 +174,7 @@ public class ImapStoreTest {
@Test
public void getFolders_withSubscribedFoldersOnly_shouldOnlyReturnExistingSubscribedFolders()
throws Exception {
when(storeConfig.isSubscribedFoldersOnly()).thenReturn(true);
when(config.isSubscribedFoldersOnly()).thenReturn(true);
ImapConnection imapConnection = mock(ImapConnection.class);
List<ImapResponse> lsubResponses = Arrays.asList(
createImapResponse("* LSUB (\\HasNoChildren) \".\" \"INBOX\""),
@ -430,10 +429,10 @@ public class ImapStoreTest {
private Deque<ImapConnection> imapConnections = new ArrayDeque<>();
private String testCombinedPrefix;
public TestImapStore(ImapStoreSettings serverSettings, StoreConfig storeConfig,
public TestImapStore(ImapStoreSettings serverSettings, ImapStoreConfig config,
TrustedSocketFactory trustedSocketFactory, ConnectivityManager connectivityManager,
OAuth2TokenProvider oauth2TokenProvider) {
super(serverSettings, storeConfig, trustedSocketFactory, connectivityManager, oauth2TokenProvider);
super(serverSettings, config, trustedSocketFactory, connectivityManager, oauth2TokenProvider);
}
@Override