From 0a60b7ce21a4835429eb08186a6ad9f32482bbba Mon Sep 17 00:00:00 2001 From: cketti Date: Thu, 7 Apr 2022 05:03:19 +0200 Subject: [PATCH 1/6] Simplify UI to configure IMAP compression --- .../activity/setup/AccountSetupIncoming.java | 26 +++--------- .../res/layout/account_setup_incoming.xml | 40 ++----------------- app/ui/legacy/src/main/res/values/strings.xml | 5 +-- 3 files changed, 11 insertions(+), 60 deletions(-) diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupIncoming.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupIncoming.java index c3f27bcf5..05b98c668 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupIncoming.java +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupIncoming.java @@ -31,9 +31,7 @@ import com.fsck.k9.helper.EmailHelper; import com.fsck.k9.setup.ServerNameSuggester; import com.fsck.k9.ui.base.K9Activity; import com.fsck.k9.activity.setup.AccountSetupCheckSettings.CheckDirection; -import com.fsck.k9.controller.MessagingController; import com.fsck.k9.helper.Utility; -import com.fsck.k9.job.K9JobManager; import com.fsck.k9.mail.AuthType; import com.fsck.k9.mail.ConnectionSecurity; import com.fsck.k9.mail.MailServerDirection; @@ -63,8 +61,6 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener private static final String STATE_SECURITY_TYPE_POSITION = "stateSecurityTypePosition"; private static final String STATE_AUTH_TYPE_POSITION = "authTypePosition"; - private final MessagingController messagingController = DI.get(MessagingController.class); - private final K9JobManager jobManager = DI.get(K9JobManager.class); private final AccountCreator accountCreator = DI.get(AccountCreator.class); private final ServerNameSuggester serverNameSuggester = DI.get(ServerNameSuggester.class); @@ -89,9 +85,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener private Button mNextButton; private Account mAccount; private boolean mMakeDefault; - private CheckBox mCompressionMobile; - private CheckBox mCompressionWifi; - private CheckBox mCompressionOther; + private CheckBox useCompressionCheckBox; private CheckBox mSubscribedFoldersOnly; private AuthTypeAdapter mAuthTypeAdapter; private ConnectionSecurity[] mConnectionSecurityChoices = ConnectionSecurity.values(); @@ -138,9 +132,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener mWebdavAuthPathView = findViewById(R.id.webdav_auth_path); mWebdavMailboxPathView = findViewById(R.id.webdav_mailbox_path); mNextButton = findViewById(R.id.next); - mCompressionMobile = findViewById(R.id.compression_mobile); - mCompressionWifi = findViewById(R.id.compression_wifi); - mCompressionOther = findViewById(R.id.compression_other); + useCompressionCheckBox = findViewById(R.id.use_compression); mSubscribedFoldersOnly = findViewById(R.id.subscribed_folders_only); mAllowClientCertificateView = findViewById(R.id.account_allow_client_certificate); @@ -222,8 +214,7 @@ 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); - findViewById(R.id.compression_section).setVisibility(View.GONE); - findViewById(R.id.compression_label).setVisibility(View.GONE); + useCompressionCheckBox.setVisibility(View.GONE); mSubscribedFoldersOnly.setVisibility(View.GONE); } else if (settings.type.equals(Protocols.IMAP)) { serverLayoutView.setHint(getString(R.string.account_setup_incoming_imap_server_label)); @@ -254,8 +245,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener findViewById(R.id.imap_path_prefix_section).setVisibility(View.GONE); findViewById(R.id.account_auth_type_label).setVisibility(View.GONE); findViewById(R.id.account_auth_type).setVisibility(View.GONE); - findViewById(R.id.compression_section).setVisibility(View.GONE); - findViewById(R.id.compression_label).setVisibility(View.GONE); + useCompressionCheckBox.setVisibility(View.GONE); mSubscribedFoldersOnly.setVisibility(View.GONE); String path = WebDavStoreSettings.getPath(settings); @@ -305,9 +295,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener updateAuthPlainTextFromSecurityType(settings.connectionSecurity); updateViewFromSecurity(); - mCompressionMobile.setChecked(mAccount.useCompression(NetworkType.MOBILE)); - mCompressionWifi.setChecked(mAccount.useCompression(NetworkType.WIFI)); - mCompressionOther.setChecked(mAccount.useCompression(NetworkType.OTHER)); + useCompressionCheckBox.setChecked(mAccount.useCompression(NetworkType.MOBILE)); if (settings.host != null) { mServerView.setText(settings.host); @@ -614,9 +602,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener mAccount.setIncomingServerSettings(settings); - mAccount.setCompression(NetworkType.MOBILE, mCompressionMobile.isChecked()); - mAccount.setCompression(NetworkType.WIFI, mCompressionWifi.isChecked()); - mAccount.setCompression(NetworkType.OTHER, mCompressionOther.isChecked()); + mAccount.setCompression(NetworkType.MOBILE, useCompressionCheckBox.isChecked()); mAccount.setSubscribedFoldersOnly(mSubscribedFoldersOnly.isChecked()); AccountSetupCheckSettings.actionCheckSettings(this, mAccount, CheckDirection.INCOMING); diff --git a/app/ui/legacy/src/main/res/layout/account_setup_incoming.xml b/app/ui/legacy/src/main/res/layout/account_setup_incoming.xml index 38bb5202d..3807ce8c8 100644 --- a/app/ui/legacy/src/main/res/layout/account_setup_incoming.xml +++ b/app/ui/legacy/src/main/res/layout/account_setup_incoming.xml @@ -247,43 +247,11 @@ - - - - - - - - - - + android:layout_width="wrap_content" + android:text="@string/account_setup_incoming_use_compression" /> Delete from server Mark as read on server - Use compression on network: - Mobile - Wi-Fi - Other + Use compression Erase deleted messages on server Immediately From 903d0f43c4a584f4f70da2c81269ce033e05cfa3 Mon Sep 17 00:00:00 2001 From: cketti Date: Thu, 7 Apr 2022 05:12:19 +0200 Subject: [PATCH 2/6] Change `Account` to use a single setting for IMAP compression --- app/core/src/main/java/com/fsck/k9/Account.kt | 21 ++++--------------- .../fsck/k9/AccountPreferenceSerializer.kt | 18 +++------------- .../fsck/k9/backends/ImapBackendFactory.kt | 2 +- .../activity/setup/AccountSetupIncoming.java | 5 ++--- 4 files changed, 10 insertions(+), 36 deletions(-) diff --git a/app/core/src/main/java/com/fsck/k9/Account.kt b/app/core/src/main/java/com/fsck/k9/Account.kt index c1e66a281..2195e1fce 100644 --- a/app/core/src/main/java/com/fsck/k9/Account.kt +++ b/app/core/src/main/java/com/fsck/k9/Account.kt @@ -2,11 +2,9 @@ package com.fsck.k9 import com.fsck.k9.backend.api.SyncConfig.ExpungePolicy import com.fsck.k9.mail.Address -import com.fsck.k9.mail.NetworkType import com.fsck.k9.mail.ServerSettings import java.util.Calendar import java.util.Date -import java.util.concurrent.ConcurrentHashMap /** * Account stores all of the settings for a single account defined by the user. Each account is defined by a UUID. @@ -225,7 +223,10 @@ class Account(override val uuid: String) : BaseAccount { @set:Synchronized var idleRefreshMinutes = 0 - private val compressionMap: MutableMap = ConcurrentHashMap() + @get:JvmName("useCompression") + @get:Synchronized + @set:Synchronized + var useCompression = true @get:Synchronized @set:Synchronized @@ -500,20 +501,6 @@ class Account(override val uuid: String) : BaseAccount { this.sortAscending[sortType] = sortAscending } - @Synchronized - fun setCompression(networkType: NetworkType, useCompression: Boolean) { - compressionMap[networkType] = useCompression - } - - @Synchronized - fun useCompression(networkType: NetworkType): Boolean { - return compressionMap[networkType] ?: return true - } - - fun getCompressionMap(): Map { - return compressionMap.toMap() - } - @Synchronized fun replaceIdentities(identities: List) { this.identities = identities.toMutableList() diff --git a/app/core/src/main/java/com/fsck/k9/AccountPreferenceSerializer.kt b/app/core/src/main/java/com/fsck/k9/AccountPreferenceSerializer.kt index c881cb312..3bb834d7b 100644 --- a/app/core/src/main/java/com/fsck/k9/AccountPreferenceSerializer.kt +++ b/app/core/src/main/java/com/fsck/k9/AccountPreferenceSerializer.kt @@ -15,7 +15,6 @@ import com.fsck.k9.Account.ShowPictures import com.fsck.k9.Account.SortType import com.fsck.k9.Account.SpecialFolderSelection import com.fsck.k9.helper.Utility -import com.fsck.k9.mail.NetworkType import com.fsck.k9.mailstore.StorageManager import com.fsck.k9.preferences.Storage import com.fsck.k9.preferences.StorageEditor @@ -120,10 +119,7 @@ class AccountPreferenceSerializer( isDefaultQuotedTextShown = storage.getBoolean("$accountUuid.defaultQuotedTextShown", DEFAULT_QUOTED_TEXT_SHOWN) isReplyAfterQuote = storage.getBoolean("$accountUuid.replyAfterQuote", DEFAULT_REPLY_AFTER_QUOTE) isStripSignature = storage.getBoolean("$accountUuid.stripSignature", DEFAULT_STRIP_SIGNATURE) - for (type in NetworkType.values()) { - val useCompression = storage.getBoolean("$accountUuid.useCompression.$type", true) - setCompression(type, useCompression) - } + useCompression = storage.getBoolean("$accountUuid.useCompression", true) importedAutoExpandFolder = storage.getString("$accountUuid.autoExpandFolderName", null) @@ -334,13 +330,7 @@ class AccountPreferenceSerializer( editor.putLong("$accountUuid.lastFolderListRefreshTime", lastFolderListRefreshTime) editor.putBoolean("$accountUuid.isFinishedSetup", isFinishedSetup) - val compressionMap = getCompressionMap() - for (type in NetworkType.values()) { - val useCompression = compressionMap[type] - if (useCompression != null) { - editor.putBoolean("$accountUuid.useCompression.$type", useCompression) - } - } + editor.putBoolean("$accountUuid.useCompression", useCompression) } saveIdentities(account, storage, editor) @@ -456,10 +446,8 @@ class AccountPreferenceSerializer( editor.remove("$accountUuid.lastSyncTime") editor.remove("$accountUuid.lastFolderListRefreshTime") editor.remove("$accountUuid.isFinishedSetup") + editor.remove("$accountUuid.useCompression") - for (type in NetworkType.values()) { - editor.remove("$accountUuid.useCompression." + type.name) - } deleteIdentities(account, storage, editor) // TODO: Remove preference settings that may exist for individual folders in the account. } diff --git a/app/k9mail/src/main/java/com/fsck/k9/backends/ImapBackendFactory.kt b/app/k9mail/src/main/java/com/fsck/k9/backends/ImapBackendFactory.kt index 249eaa40b..35ab04af4 100644 --- a/app/k9mail/src/main/java/com/fsck/k9/backends/ImapBackendFactory.kt +++ b/app/k9mail/src/main/java/com/fsck/k9/backends/ImapBackendFactory.kt @@ -66,7 +66,7 @@ class ImapBackendFactory( override fun isSubscribedFoldersOnly() = account.isSubscribedFoldersOnly - override fun useCompression(type: NetworkType) = account.useCompression(type) + override fun useCompression(type: NetworkType) = account.useCompression } } diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupIncoming.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupIncoming.java index 05b98c668..16decb409 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupIncoming.java +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupIncoming.java @@ -35,7 +35,6 @@ import com.fsck.k9.helper.Utility; import com.fsck.k9.mail.AuthType; import com.fsck.k9.mail.ConnectionSecurity; import com.fsck.k9.mail.MailServerDirection; -import com.fsck.k9.mail.NetworkType; import com.fsck.k9.mail.ServerSettings; import com.fsck.k9.mail.store.imap.ImapStoreSettings; import com.fsck.k9.mail.store.webdav.WebDavStoreSettings; @@ -295,7 +294,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener updateAuthPlainTextFromSecurityType(settings.connectionSecurity); updateViewFromSecurity(); - useCompressionCheckBox.setChecked(mAccount.useCompression(NetworkType.MOBILE)); + useCompressionCheckBox.setChecked(mAccount.useCompression()); if (settings.host != null) { mServerView.setText(settings.host); @@ -602,7 +601,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener mAccount.setIncomingServerSettings(settings); - mAccount.setCompression(NetworkType.MOBILE, useCompressionCheckBox.isChecked()); + mAccount.setUseCompression(useCompressionCheckBox.isChecked()); mAccount.setSubscribedFoldersOnly(mSubscribedFoldersOnly.isChecked()); AccountSetupCheckSettings.actionCheckSettings(this, mAccount, CheckDirection.INCOMING); From a796b1d941f5b4d9b9b7fe12aa602f175b1a4aab Mon Sep 17 00:00:00 2001 From: cketti Date: Thu, 7 Apr 2022 05:15:37 +0200 Subject: [PATCH 3/6] Change IMAP code to use single compression setting --- .../fsck/k9/backends/ImapBackendFactory.kt | 3 +-- .../java/com/fsck/k9/mail/NetworkType.java | 25 ------------------- .../fsck/k9/mail/store/imap/ImapSettings.java | 4 +-- .../k9/mail/store/imap/ImapStoreConfig.kt | 4 +-- .../mail/store/imap/RealImapConnection.java | 25 +------------------ .../k9/mail/store/imap/RealImapStore.java | 5 ++-- .../mail/store/imap/SimpleImapSettings.java | 3 +-- 7 files changed, 8 insertions(+), 61 deletions(-) delete mode 100644 mail/common/src/main/java/com/fsck/k9/mail/NetworkType.java diff --git a/app/k9mail/src/main/java/com/fsck/k9/backends/ImapBackendFactory.kt b/app/k9mail/src/main/java/com/fsck/k9/backends/ImapBackendFactory.kt index 35ab04af4..bae731beb 100644 --- a/app/k9mail/src/main/java/com/fsck/k9/backends/ImapBackendFactory.kt +++ b/app/k9mail/src/main/java/com/fsck/k9/backends/ImapBackendFactory.kt @@ -7,7 +7,6 @@ import com.fsck.k9.backend.BackendFactory import com.fsck.k9.backend.api.Backend import com.fsck.k9.backend.imap.ImapBackend import com.fsck.k9.backend.imap.ImapPushConfigProvider -import com.fsck.k9.mail.NetworkType import com.fsck.k9.mail.oauth.OAuth2TokenProvider import com.fsck.k9.mail.power.PowerManager import com.fsck.k9.mail.ssl.TrustedSocketFactory @@ -66,7 +65,7 @@ class ImapBackendFactory( override fun isSubscribedFoldersOnly() = account.isSubscribedFoldersOnly - override fun useCompression(type: NetworkType) = account.useCompression + override fun useCompression() = account.useCompression } } diff --git a/mail/common/src/main/java/com/fsck/k9/mail/NetworkType.java b/mail/common/src/main/java/com/fsck/k9/mail/NetworkType.java deleted file mode 100644 index 769858213..000000000 --- a/mail/common/src/main/java/com/fsck/k9/mail/NetworkType.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.fsck.k9.mail; - -import android.net.ConnectivityManager; - -/** - * Enum for some of - * https://developer.android.com/reference/android/net/ConnectivityManager.html#TYPE_MOBILE etc. - */ -public enum NetworkType { - - WIFI, - MOBILE, - OTHER; - - public static NetworkType fromConnectivityManagerType(int type){ - switch (type) { - case ConnectivityManager.TYPE_MOBILE: - return MOBILE; - case ConnectivityManager.TYPE_WIFI: - return WIFI; - default: - return OTHER; - } - } -} diff --git a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapSettings.java b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapSettings.java index 4689a961a..d5c5e1efb 100644 --- a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapSettings.java +++ b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapSettings.java @@ -2,7 +2,7 @@ package com.fsck.k9.mail.store.imap; import com.fsck.k9.mail.AuthType; import com.fsck.k9.mail.ConnectionSecurity; -import com.fsck.k9.mail.NetworkType; + /** * Settings source for IMAP. Implemented in order to remove coupling between {@link ImapStore} and {@link ImapConnection}. @@ -22,7 +22,7 @@ interface ImapSettings { String getClientCertificateAlias(); - boolean useCompression(NetworkType type); + boolean useCompression(); String getPathPrefix(); diff --git a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapStoreConfig.kt b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapStoreConfig.kt index fef151222..0bfbd58ee 100644 --- a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapStoreConfig.kt +++ b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapStoreConfig.kt @@ -1,9 +1,7 @@ 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 + fun useCompression(): Boolean } diff --git a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapConnection.java b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapConnection.java index 6c5123c7e..4679ffad1 100644 --- a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapConnection.java +++ b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapConnection.java @@ -28,7 +28,6 @@ import java.util.zip.Inflater; import java.util.zip.InflaterInputStream; import android.net.ConnectivityManager; -import android.net.NetworkInfo; import com.fsck.k9.mail.Authentication; import com.fsck.k9.mail.AuthenticationFailedException; @@ -36,7 +35,6 @@ import com.fsck.k9.mail.CertificateValidationException; import com.fsck.k9.mail.ConnectionSecurity; import com.fsck.k9.mail.K9MailLib; import com.fsck.k9.mail.MessagingException; -import com.fsck.k9.mail.NetworkType; import com.fsck.k9.mail.filter.Base64; import com.fsck.k9.mail.filter.PeekableInputStream; import com.fsck.k9.mail.oauth.OAuth2TokenProvider; @@ -577,32 +575,11 @@ class RealImapConnection implements ImapConnection { } private void enableCompressionIfRequested() throws IOException, MessagingException { - if (hasCapability(Capabilities.COMPRESS_DEFLATE) && shouldEnableCompression()) { + if (hasCapability(Capabilities.COMPRESS_DEFLATE) && settings.useCompression()) { enableCompression(); } } - private boolean shouldEnableCompression() { - boolean useCompression = true; - - NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); - if (networkInfo != null) { - int type = networkInfo.getType(); - if (K9MailLib.isDebug()) { - Timber.d("On network type %s", type); - } - - NetworkType networkType = NetworkType.fromConnectivityManagerType(type); - useCompression = settings.useCompression(networkType); - } - - if (K9MailLib.isDebug()) { - Timber.d("useCompression: %b", useCompression); - } - - return useCompression; - } - private void enableCompression() throws IOException, MessagingException { try { executeSimpleCommand(Commands.COMPRESS_DEFLATE); diff --git a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapStore.java b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapStore.java index 8b8e24fa0..42907dc84 100644 --- a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapStore.java +++ b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapStore.java @@ -21,7 +21,6 @@ import com.fsck.k9.mail.ConnectionSecurity; import com.fsck.k9.mail.Flag; import com.fsck.k9.mail.FolderType; import com.fsck.k9.mail.MessagingException; -import com.fsck.k9.mail.NetworkType; import com.fsck.k9.mail.ServerSettings; import com.fsck.k9.mail.oauth.OAuth2TokenProvider; import com.fsck.k9.mail.ssl.TrustedSocketFactory; @@ -382,8 +381,8 @@ class RealImapStore implements ImapStore, ImapConnectionManager, InternalImapSto } @Override - public boolean useCompression(final NetworkType type) { - return config.useCompression(type); + public boolean useCompression() { + return config.useCompression(); } @Override diff --git a/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/SimpleImapSettings.java b/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/SimpleImapSettings.java index e14d09b9d..391ae35d8 100644 --- a/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/SimpleImapSettings.java +++ b/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/SimpleImapSettings.java @@ -3,7 +3,6 @@ package com.fsck.k9.mail.store.imap; import com.fsck.k9.mail.AuthType; import com.fsck.k9.mail.ConnectionSecurity; -import com.fsck.k9.mail.NetworkType; class SimpleImapSettings implements ImapSettings { @@ -55,7 +54,7 @@ class SimpleImapSettings implements ImapSettings { } @Override - public boolean useCompression(NetworkType type) { + public boolean useCompression() { return useCompression; } From 7be4ea62fe68a468fd7ffcae340530d3ae74226b Mon Sep 17 00:00:00 2001 From: cketti Date: Thu, 7 Apr 2022 05:25:29 +0200 Subject: [PATCH 4/6] Remove `ImapStore`'s dependency on `ConnectivityManager` --- .../fsck/k9/backends/ImapBackendFactory.kt | 4 ---- .../java/com/fsck/k9/backends/KoinModule.kt | 1 - .../com/fsck/k9/mail/store/imap/ImapStore.kt | 4 +--- .../mail/store/imap/RealImapConnection.java | 10 ++------- .../k9/mail/store/imap/RealImapStore.java | 7 +----- .../store/imap/RealImapConnectionTest.java | 22 ++++++------------- .../k9/mail/store/imap/RealImapStoreTest.java | 10 +++------ 7 files changed, 14 insertions(+), 44 deletions(-) diff --git a/app/k9mail/src/main/java/com/fsck/k9/backends/ImapBackendFactory.kt b/app/k9mail/src/main/java/com/fsck/k9/backends/ImapBackendFactory.kt index bae731beb..989f42865 100644 --- a/app/k9mail/src/main/java/com/fsck/k9/backends/ImapBackendFactory.kt +++ b/app/k9mail/src/main/java/com/fsck/k9/backends/ImapBackendFactory.kt @@ -1,7 +1,5 @@ package com.fsck.k9.backends -import android.content.Context -import android.net.ConnectivityManager import com.fsck.k9.Account import com.fsck.k9.backend.BackendFactory import com.fsck.k9.backend.api.Backend @@ -21,7 +19,6 @@ import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.map class ImapBackendFactory( - private val context: Context, private val accountManager: AccountManager, private val powerManager: PowerManager, private val idleRefreshManager: IdleRefreshManager, @@ -53,7 +50,6 @@ class ImapBackendFactory( account.incomingServerSettings, config, trustedSocketFactory, - context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager, oAuth2TokenProvider ) } diff --git a/app/k9mail/src/main/java/com/fsck/k9/backends/KoinModule.kt b/app/k9mail/src/main/java/com/fsck/k9/backends/KoinModule.kt index 13d64f39e..8dd9438a3 100644 --- a/app/k9mail/src/main/java/com/fsck/k9/backends/KoinModule.kt +++ b/app/k9mail/src/main/java/com/fsck/k9/backends/KoinModule.kt @@ -20,7 +20,6 @@ val backendsModule = module { } single { ImapBackendFactory( - context = get(), accountManager = get(), powerManager = get(), idleRefreshManager = get(), diff --git a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapStore.kt b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapStore.kt index 1cba6012d..bcb484c6a 100644 --- a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapStore.kt +++ b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapStore.kt @@ -1,6 +1,5 @@ package com.fsck.k9.mail.store.imap -import android.net.ConnectivityManager import com.fsck.k9.mail.MessagingException import com.fsck.k9.mail.ServerSettings import com.fsck.k9.mail.oauth.OAuth2TokenProvider @@ -22,10 +21,9 @@ interface ImapStore { serverSettings: ServerSettings, config: ImapStoreConfig, trustedSocketFactory: TrustedSocketFactory, - connectivityManager: ConnectivityManager, oauthTokenProvider: OAuth2TokenProvider? ): ImapStore { - return RealImapStore(serverSettings, config, trustedSocketFactory, connectivityManager, oauthTokenProvider) + return RealImapStore(serverSettings, config, trustedSocketFactory, oauthTokenProvider) } } } diff --git a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapConnection.java b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapConnection.java index 4679ffad1..b9f8095e6 100644 --- a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapConnection.java +++ b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapConnection.java @@ -27,8 +27,6 @@ import java.util.regex.Pattern; import java.util.zip.Inflater; import java.util.zip.InflaterInputStream; -import android.net.ConnectivityManager; - import com.fsck.k9.mail.Authentication; import com.fsck.k9.mail.AuthenticationFailedException; import com.fsck.k9.mail.CertificateValidationException; @@ -75,7 +73,6 @@ class RealImapConnection implements ImapConnection { private static final int LENGTH_LIMIT_WITH_CONDSTORE = 8172; - private final ConnectivityManager connectivityManager; private final OAuth2TokenProvider oauthTokenProvider; private final TrustedSocketFactory socketFactory; private final int socketConnectTimeout; @@ -95,10 +92,9 @@ class RealImapConnection implements ImapConnection { public RealImapConnection(ImapSettings settings, TrustedSocketFactory socketFactory, - ConnectivityManager connectivityManager, OAuth2TokenProvider oauthTokenProvider, int connectionGeneration) { + OAuth2TokenProvider oauthTokenProvider, int connectionGeneration) { this.settings = settings; this.socketFactory = socketFactory; - this.connectivityManager = connectivityManager; this.oauthTokenProvider = oauthTokenProvider; this.socketConnectTimeout = SOCKET_CONNECT_TIMEOUT; this.socketReadTimeout = SOCKET_READ_TIMEOUT; @@ -106,12 +102,10 @@ class RealImapConnection implements ImapConnection { } public RealImapConnection(ImapSettings settings, TrustedSocketFactory socketFactory, - ConnectivityManager connectivityManager, OAuth2TokenProvider oauthTokenProvider, - int socketConnectTimeout, int socketReadTimeout, + OAuth2TokenProvider oauthTokenProvider, int socketConnectTimeout, int socketReadTimeout, int connectionGeneration) { this.settings = settings; this.socketFactory = socketFactory; - this.connectivityManager = connectivityManager; this.oauthTokenProvider = oauthTokenProvider; this.socketConnectTimeout = socketConnectTimeout; this.socketReadTimeout = socketReadTimeout; diff --git a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapStore.java b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapStore.java index 42907dc84..3011442f7 100644 --- a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapStore.java +++ b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapStore.java @@ -13,7 +13,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import android.net.ConnectivityManager; import androidx.annotation.Nullable; import com.fsck.k9.mail.AuthType; @@ -37,7 +36,6 @@ class RealImapStore implements ImapStore, ImapConnectionManager, InternalImapSto private final ImapStoreConfig config; private final TrustedSocketFactory trustedSocketFactory; private Set permanentFlagsIndex = EnumSet.noneOf(Flag.class); - private ConnectivityManager connectivityManager; private OAuth2TokenProvider oauthTokenProvider; private String host; @@ -56,8 +54,7 @@ class RealImapStore implements ImapStore, ImapConnectionManager, InternalImapSto public RealImapStore(ServerSettings serverSettings, ImapStoreConfig config, - TrustedSocketFactory trustedSocketFactory, ConnectivityManager connectivityManager, - OAuth2TokenProvider oauthTokenProvider) { + TrustedSocketFactory trustedSocketFactory, OAuth2TokenProvider oauthTokenProvider) { this.config = config; this.trustedSocketFactory = trustedSocketFactory; @@ -65,7 +62,6 @@ class RealImapStore implements ImapStore, ImapConnectionManager, InternalImapSto port = serverSettings.port; connectionSecurity = serverSettings.connectionSecurity; - this.connectivityManager = connectivityManager; this.oauthTokenProvider = oauthTokenProvider; authType = serverSettings.authenticationType; @@ -326,7 +322,6 @@ class RealImapStore implements ImapStore, ImapConnectionManager, InternalImapSto return new RealImapConnection( new StoreImapSettings(), trustedSocketFactory, - connectivityManager, oauthTokenProvider, connectionGeneration); } diff --git a/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/RealImapConnectionTest.java b/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/RealImapConnectionTest.java index 9232c9df3..cbfd5f2a5 100644 --- a/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/RealImapConnectionTest.java +++ b/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/RealImapConnectionTest.java @@ -6,7 +6,6 @@ import java.net.UnknownHostException; import java.util.List; import android.app.Activity; -import android.net.ConnectivityManager; import com.fsck.k9.mail.AuthType; import com.fsck.k9.mail.AuthenticationFailedException; @@ -33,7 +32,6 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import static org.mockito.Mockito.mock; @RunWith(K9LibRobolectricTestRunner.class) @@ -53,14 +51,12 @@ public class RealImapConnectionTest { private TrustedSocketFactory socketFactory; - private ConnectivityManager connectivityManager; private OAuth2TokenProvider oAuth2TokenProvider; private SimpleImapSettings settings; @Before public void setUp() throws Exception { - connectivityManager = mock(ConnectivityManager.class); oAuth2TokenProvider = createOAuth2TokenProvider(); socketFactory = TestTrustedSocketFactory.newInstance(); @@ -619,8 +615,7 @@ public class RealImapConnectionTest { public void open_withConnectionError_shouldThrow() throws Exception { settings.setHost("127.1.2.3"); settings.setPort(143); - ImapConnection imapConnection = createImapConnection( - settings, socketFactory, connectivityManager, oAuth2TokenProvider); + ImapConnection imapConnection = createImapConnection(settings, socketFactory, oAuth2TokenProvider); try { imapConnection.open(); @@ -635,8 +630,7 @@ public class RealImapConnectionTest { public void open_withInvalidHostname_shouldThrow() throws Exception { settings.setHost("host name"); settings.setPort(143); - ImapConnection imapConnection = createImapConnection( - settings, socketFactory, connectivityManager, oAuth2TokenProvider); + ImapConnection imapConnection = createImapConnection(settings, socketFactory, oAuth2TokenProvider); try { imapConnection.open(); @@ -826,8 +820,7 @@ public class RealImapConnectionTest { @Test public void isConnected_withoutPreviousOpen_shouldReturnFalse() throws Exception { - ImapConnection imapConnection = createImapConnection( - settings, socketFactory, connectivityManager, oAuth2TokenProvider); + ImapConnection imapConnection = createImapConnection(settings, socketFactory, oAuth2TokenProvider); boolean result = imapConnection.isConnected(); @@ -863,8 +856,7 @@ public class RealImapConnectionTest { @Test public void close_withoutOpen_shouldNotThrow() throws Exception { - ImapConnection imapConnection = createImapConnection( - settings, socketFactory, connectivityManager, oAuth2TokenProvider); + ImapConnection imapConnection = createImapConnection(settings, socketFactory, oAuth2TokenProvider); imapConnection.close(); } @@ -973,8 +965,8 @@ public class RealImapConnectionTest { } private ImapConnection createImapConnection(ImapSettings settings, TrustedSocketFactory socketFactory, - ConnectivityManager connectivityManager, OAuth2TokenProvider oAuth2TokenProvider) { - return new RealImapConnection(settings, socketFactory, connectivityManager, oAuth2TokenProvider, + OAuth2TokenProvider oAuth2TokenProvider) { + return new RealImapConnection(settings, socketFactory, oAuth2TokenProvider, SOCKET_CONNECT_TIMEOUT, SOCKET_READ_TIMEOUT, 1); } @@ -982,7 +974,7 @@ public class RealImapConnectionTest { server.start(); settings.setHost(server.getHost()); settings.setPort(server.getPort()); - return createImapConnection(settings, socketFactory, connectivityManager, oAuth2TokenProvider); + return createImapConnection(settings, socketFactory, oAuth2TokenProvider); } private ImapConnection simpleOpen(MockImapServer server) throws Exception { diff --git a/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/RealImapStoreTest.java b/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/RealImapStoreTest.java index adc69fc5a..ef95cf581 100644 --- a/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/RealImapStoreTest.java +++ b/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/RealImapStoreTest.java @@ -12,8 +12,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import android.net.ConnectivityManager; - import com.fsck.k9.mail.AuthType; import com.fsck.k9.mail.ConnectionSecurity; import com.fsck.k9.mail.FolderType; @@ -47,10 +45,9 @@ public class RealImapStoreTest { public void setUp() throws Exception { ServerSettings serverSettings = createServerSettings(); TrustedSocketFactory trustedSocketFactory = mock(TrustedSocketFactory.class); - ConnectivityManager connectivityManager = mock(ConnectivityManager.class); OAuth2TokenProvider oauth2TokenProvider = mock(OAuth2TokenProvider.class); - imapStore = new TestImapStore(serverSettings, config, trustedSocketFactory, connectivityManager, + imapStore = new TestImapStore(serverSettings, config, trustedSocketFactory, oauth2TokenProvider); } @@ -463,9 +460,8 @@ public class RealImapStoreTest { private String testCombinedPrefix; public TestImapStore(ServerSettings serverSettings, ImapStoreConfig config, - TrustedSocketFactory trustedSocketFactory, ConnectivityManager connectivityManager, - OAuth2TokenProvider oauth2TokenProvider) { - super(serverSettings, config, trustedSocketFactory, connectivityManager, oauth2TokenProvider); + TrustedSocketFactory trustedSocketFactory, OAuth2TokenProvider oauth2TokenProvider) { + super(serverSettings, config, trustedSocketFactory, oauth2TokenProvider); } @Override From 814583a89e4f9cd84ae326f52d67a797532d967e Mon Sep 17 00:00:00 2001 From: cketti Date: Thu, 7 Apr 2022 05:36:14 +0200 Subject: [PATCH 5/6] Add migration for persisted IMAP compression preference --- .../k9/preferences/K9StoragePersister.java | 2 +- .../migrations/StorageMigrationTo18.kt | 36 +++++++++++++++++++ .../migrations/StorageMigrations.kt | 1 + 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 app/storage/src/main/java/com/fsck/k9/preferences/migrations/StorageMigrationTo18.kt diff --git a/app/storage/src/main/java/com/fsck/k9/preferences/K9StoragePersister.java b/app/storage/src/main/java/com/fsck/k9/preferences/K9StoragePersister.java index 0a0c290b8..6ea08aad6 100644 --- a/app/storage/src/main/java/com/fsck/k9/preferences/K9StoragePersister.java +++ b/app/storage/src/main/java/com/fsck/k9/preferences/K9StoragePersister.java @@ -21,7 +21,7 @@ import timber.log.Timber; public class K9StoragePersister implements StoragePersister { - private static final int DB_VERSION = 17; + private static final int DB_VERSION = 18; private static final String DB_NAME = "preferences_storage"; private final Context context; diff --git a/app/storage/src/main/java/com/fsck/k9/preferences/migrations/StorageMigrationTo18.kt b/app/storage/src/main/java/com/fsck/k9/preferences/migrations/StorageMigrationTo18.kt new file mode 100644 index 000000000..15a98f952 --- /dev/null +++ b/app/storage/src/main/java/com/fsck/k9/preferences/migrations/StorageMigrationTo18.kt @@ -0,0 +1,36 @@ +package com.fsck.k9.preferences.migrations + +import android.database.sqlite.SQLiteDatabase + +/** + * Rewrite the per-network type IMAP compression settings to a single setting. + */ +class StorageMigrationTo18( + private val db: SQLiteDatabase, + private val migrationsHelper: StorageMigrationsHelper +) { + fun rewriteImapCompressionSettings() { + val accountUuidsListValue = migrationsHelper.readValue(db, "accountUuids") + if (accountUuidsListValue == null || accountUuidsListValue.isEmpty()) { + return + } + + val accountUuids = accountUuidsListValue.split(",") + for (accountUuid in accountUuids) { + rewriteImapCompressionSetting(accountUuid) + } + } + + private fun rewriteImapCompressionSetting(accountUuid: String) { + val useCompressionWifi = migrationsHelper.readValue(db, "$accountUuid.useCompression.WIFI").toBoolean() + val useCompressionMobile = migrationsHelper.readValue(db, "$accountUuid.useCompression.MOBILE").toBoolean() + val useCompressionOther = migrationsHelper.readValue(db, "$accountUuid.useCompression.OTHER").toBoolean() + + val useCompression = useCompressionWifi && useCompressionMobile && useCompressionOther + migrationsHelper.writeValue(db, "$accountUuid.useCompression", useCompression.toString()) + + migrationsHelper.writeValue(db, "$accountUuid.useCompression.WIFI", null) + migrationsHelper.writeValue(db, "$accountUuid.useCompression.MOBILE", null) + migrationsHelper.writeValue(db, "$accountUuid.useCompression.OTHER", null) + } +} diff --git a/app/storage/src/main/java/com/fsck/k9/preferences/migrations/StorageMigrations.kt b/app/storage/src/main/java/com/fsck/k9/preferences/migrations/StorageMigrations.kt index 8f435b07a..07a6a091b 100644 --- a/app/storage/src/main/java/com/fsck/k9/preferences/migrations/StorageMigrations.kt +++ b/app/storage/src/main/java/com/fsck/k9/preferences/migrations/StorageMigrations.kt @@ -23,5 +23,6 @@ internal object StorageMigrations { if (oldVersion < 15) StorageMigrationTo15(db, migrationsHelper).rewriteIdleRefreshInterval() if (oldVersion < 16) StorageMigrationTo16(db, migrationsHelper).changeDefaultRegisteredNameColor() if (oldVersion < 17) StorageMigrationTo17(db, migrationsHelper).rewriteNotificationLightSettings() + if (oldVersion < 18) StorageMigrationTo18(db, migrationsHelper).rewriteImapCompressionSettings() } } From 72e679dca05a5fd63b31b6b45dd561212d081c47 Mon Sep 17 00:00:00 2001 From: cketti Date: Thu, 7 Apr 2022 05:47:35 +0200 Subject: [PATCH 6/6] Add migration for IMAP compression in settings file --- .../AccountSettingsDescriptions.java | 31 +++++++++++++++++-- .../com/fsck/k9/preferences/Settings.java | 2 +- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/app/core/src/main/java/com/fsck/k9/preferences/AccountSettingsDescriptions.java b/app/core/src/main/java/com/fsck/k9/preferences/AccountSettingsDescriptions.java index 9a295a335..97fcd961d 100644 --- a/app/core/src/main/java/com/fsck/k9/preferences/AccountSettingsDescriptions.java +++ b/app/core/src/main/java/com/fsck/k9/preferences/AccountSettingsDescriptions.java @@ -207,13 +207,16 @@ public class AccountSettingsDescriptions { new V(53, new StringSetting(null)) )); s.put("useCompression.MOBILE", Settings.versions( - new V(1, new BooleanSetting(true)) + new V(1, new BooleanSetting(true)), + new V(81, null) )); s.put("useCompression.OTHER", Settings.versions( - new V(1, new BooleanSetting(true)) + new V(1, new BooleanSetting(true)), + new V(81, null) )); s.put("useCompression.WIFI", Settings.versions( - new V(1, new BooleanSetting(true)) + new V(1, new BooleanSetting(true)), + new V(81, null) )); s.put("vibrate", Settings.versions( new V(1, new BooleanSetting(false)) @@ -270,6 +273,9 @@ public class AccountSettingsDescriptions { s.put("notificationLight", Settings.versions( new V(80, new EnumSetting<>(NotificationLight.class, NotificationLight.Disabled)) )); + s.put("useCompression", Settings.versions( + new V(81, new BooleanSetting(true)) + )); // note that there is no setting for openPgpProvider, because this will have to be set up together // with the actual provider after import anyways. @@ -280,6 +286,7 @@ public class AccountSettingsDescriptions { u.put(54, new SettingsUpgraderV54()); u.put(74, new SettingsUpgraderV74()); u.put(80, new SettingsUpgraderV80()); + u.put(81, new SettingsUpgraderV81()); UPGRADERS = Collections.unmodifiableMap(u); } @@ -546,4 +553,22 @@ public class AccountSettingsDescriptions { return SetsKt.setOf("led", "ledColor"); } } + + /** + * Rewrite the per-network type IMAP compression settings to a single setting. + */ + private static class SettingsUpgraderV81 implements SettingsUpgrader { + @Override + public Set upgrade(Map settings) { + Boolean useCompressionWifi = (Boolean) settings.get("useCompression.WIFI"); + Boolean useCompressionMobile = (Boolean) settings.get("useCompression.MOBILE"); + Boolean useCompressionOther = (Boolean) settings.get("useCompression.OTHER"); + + boolean useCompression = useCompressionWifi != null && useCompressionMobile != null && + useCompressionOther != null && useCompressionWifi && useCompressionMobile && useCompressionOther; + settings.put("useCompression", useCompression); + + return SetsKt.setOf("useCompression.WIFI", "useCompression.MOBILE", "useCompression.OTHER"); + } + } } diff --git a/app/core/src/main/java/com/fsck/k9/preferences/Settings.java b/app/core/src/main/java/com/fsck/k9/preferences/Settings.java index 8124a0c34..db1674f7c 100644 --- a/app/core/src/main/java/com/fsck/k9/preferences/Settings.java +++ b/app/core/src/main/java/com/fsck/k9/preferences/Settings.java @@ -36,7 +36,7 @@ public class Settings { * * @see SettingsExporter */ - public static final int VERSION = 80; + public static final int VERSION = 81; static Map validate(int version, Map> settings, Map importedSettings, boolean useDefaultValues) {