From 5a4e576d79b225ba1ce4d47e0525d98c04f8c51f Mon Sep 17 00:00:00 2001 From: cketti Date: Sat, 6 Apr 2024 15:11:32 +0200 Subject: [PATCH] Extract data holder classes from `SettingsImporter` --- .../com/fsck/k9/preferences/ImportContents.kt | 18 +++ .../com/fsck/k9/preferences/ImportResults.kt | 23 ++++ .../k9/preferences/SettingsFileContents.kt | 67 ++++++++++ .../fsck/k9/preferences/SettingsImporter.java | 118 ------------------ .../k9/preferences/SettingsImporterTest.kt | 17 ++- .../import/ui/SettingsImportViewModel.kt | 5 +- 6 files changed, 118 insertions(+), 130 deletions(-) create mode 100644 app/core/src/main/java/com/fsck/k9/preferences/ImportContents.kt create mode 100644 app/core/src/main/java/com/fsck/k9/preferences/ImportResults.kt create mode 100644 app/core/src/main/java/com/fsck/k9/preferences/SettingsFileContents.kt diff --git a/app/core/src/main/java/com/fsck/k9/preferences/ImportContents.kt b/app/core/src/main/java/com/fsck/k9/preferences/ImportContents.kt new file mode 100644 index 000000000..efaa29276 --- /dev/null +++ b/app/core/src/main/java/com/fsck/k9/preferences/ImportContents.kt @@ -0,0 +1,18 @@ +package com.fsck.k9.preferences + +/** + * Class to list the contents of an import file/stream. + * + * @see SettingsImporter.getImportStreamContents + */ +data class ImportContents( + /** + * True, if the import file contains global settings. + */ + val globalSettings: Boolean, + + /** + * The list of accounts found in the import file. + */ + val accounts: List, +) diff --git a/app/core/src/main/java/com/fsck/k9/preferences/ImportResults.kt b/app/core/src/main/java/com/fsck/k9/preferences/ImportResults.kt new file mode 100644 index 000000000..31aa3727d --- /dev/null +++ b/app/core/src/main/java/com/fsck/k9/preferences/ImportResults.kt @@ -0,0 +1,23 @@ +package com.fsck.k9.preferences + +data class ImportResults( + @JvmField val globalSettings: Boolean, + @JvmField val importedAccounts: List, + @JvmField val erroneousAccounts: List, +) + +data class AccountDescriptionPair( + @JvmField val original: AccountDescription, + @JvmField val imported: AccountDescription, + @JvmField val overwritten: Boolean, + @JvmField val authorizationNeeded: Boolean, + @JvmField val incomingPasswordNeeded: Boolean, + @JvmField val outgoingPasswordNeeded: Boolean, + @JvmField val incomingServerName: String, + @JvmField val outgoingServerName: String, +) + +data class AccountDescription( + @JvmField val name: String, + @JvmField val uuid: String, +) diff --git a/app/core/src/main/java/com/fsck/k9/preferences/SettingsFileContents.kt b/app/core/src/main/java/com/fsck/k9/preferences/SettingsFileContents.kt new file mode 100644 index 000000000..a19c7b567 --- /dev/null +++ b/app/core/src/main/java/com/fsck/k9/preferences/SettingsFileContents.kt @@ -0,0 +1,67 @@ +package com.fsck.k9.preferences + +import com.fsck.k9.mail.AuthType + +internal class Imported { + @JvmField var contentVersion: Int = 0 + + @JvmField var globalSettings: ImportedSettings? = null + + @JvmField var accounts: Map? = null +} + +internal class ImportedSettings { + @JvmField var settings = mutableMapOf() +} + +internal class ImportedAccount { + @JvmField var uuid: String? = null + + @JvmField var name: String? = null + + @JvmField var incoming: ImportedServer? = null + + @JvmField var outgoing: ImportedServer? = null + + @JvmField var settings: ImportedSettings? = null + + @JvmField var identities: List? = null + + @JvmField var folders: List? = null +} + +internal class ImportedServer { + @JvmField var type: String? = null + + @JvmField var host: String? = null + + @JvmField var port: String? = null + + @JvmField var connectionSecurity: String? = null + + @JvmField var authenticationType: AuthType? = null + + @JvmField var username: String? = null + + @JvmField var password: String? = null + + @JvmField var clientCertificateAlias: String? = null + + @JvmField var extras: ImportedSettings? = null +} + +internal class ImportedIdentity { + @JvmField var name: String? = null + + @JvmField var email: String? = null + + @JvmField var description: String? = null + + @JvmField var settings: ImportedSettings? = null +} + +internal class ImportedFolder { + @JvmField var name: String? = null + + @JvmField var settings: ImportedSettings? = null +} diff --git a/app/core/src/main/java/com/fsck/k9/preferences/SettingsImporter.java b/app/core/src/main/java/com/fsck/k9/preferences/SettingsImporter.java index f7308902c..241582a51 100644 --- a/app/core/src/main/java/com/fsck/k9/preferences/SettingsImporter.java +++ b/app/core/src/main/java/com/fsck/k9/preferences/SettingsImporter.java @@ -40,76 +40,6 @@ import static java.util.Collections.unmodifiableMap; public class SettingsImporter { - - /** - * Class to list the contents of an import file/stream. - * - * @see SettingsImporter#getImportStreamContents(InputStream) - */ - public static class ImportContents { - /** - * True, if the import file contains global settings. - */ - public final boolean globalSettings; - - /** - * The list of accounts found in the import file. Never {@code null}. - */ - public final List accounts; - - private ImportContents(boolean globalSettings, List accounts) { - this.globalSettings = globalSettings; - this.accounts = accounts; - } - } - - public static class AccountDescription { - public final String name; - public final String uuid; - - private AccountDescription(String name, String uuid) { - this.name = name; - this.uuid = uuid; - } - } - - public static class AccountDescriptionPair { - public final AccountDescription original; - public final AccountDescription imported; - public final boolean overwritten; - public final boolean authorizationNeeded; - public final boolean incomingPasswordNeeded; - public final boolean outgoingPasswordNeeded; - public final String incomingServerName; - public final String outgoingServerName; - - private AccountDescriptionPair(AccountDescription original, AccountDescription imported, - boolean overwritten, boolean authorizationNeeded, boolean incomingPasswordNeeded, - boolean outgoingPasswordNeeded, String incomingServerName, String outgoingServerName) { - this.original = original; - this.imported = imported; - this.overwritten = overwritten; - this.authorizationNeeded = authorizationNeeded; - this.incomingPasswordNeeded = incomingPasswordNeeded; - this.outgoingPasswordNeeded = outgoingPasswordNeeded; - this.incomingServerName = incomingServerName; - this.outgoingServerName = outgoingServerName; - } - } - - public static class ImportResults { - public final boolean globalSettings; - public final List importedAccounts; - public final List erroneousAccounts; - - private ImportResults(boolean globalSettings, List importedAccounts, - List erroneousAccounts) { - this.globalSettings = globalSettings; - this.importedAccounts = importedAccounts; - this.erroneousAccounts = erroneousAccounts; - } - } - /** * Parses an import {@link InputStream} and returns information on whether it contains global * settings and/or account settings. For all account configurations found, the name of the @@ -1080,52 +1010,4 @@ public class SettingsImporter { return ConnectionSecurity.SSL_TLS_REQUIRED; } } - - @VisibleForTesting - static class Imported { - public int contentVersion; - public ImportedSettings globalSettings; - public Map accounts; - } - - private static class ImportedSettings { - public Map settings = new HashMap<>(); - } - - @VisibleForTesting - static class ImportedAccount { - public String uuid; - public String name; - public ImportedServer incoming; - public ImportedServer outgoing; - public ImportedSettings settings; - public List identities; - public List folders; - } - - @VisibleForTesting - static class ImportedServer { - public String type; - public String host; - public String port; - public String connectionSecurity; - public AuthType authenticationType; - public String username; - public String password; - public String clientCertificateAlias; - public ImportedSettings extras; - } - - @VisibleForTesting - static class ImportedIdentity { - public String name; - public String email; - public String description; - public ImportedSettings settings; - } - - private static class ImportedFolder { - public String name; - public ImportedSettings settings; - } } diff --git a/app/core/src/test/java/com/fsck/k9/preferences/SettingsImporterTest.kt b/app/core/src/test/java/com/fsck/k9/preferences/SettingsImporterTest.kt index fe1af0e33..ebb84449c 100644 --- a/app/core/src/test/java/com/fsck/k9/preferences/SettingsImporterTest.kt +++ b/app/core/src/test/java/com/fsck/k9/preferences/SettingsImporterTest.kt @@ -12,19 +12,13 @@ import assertk.assertions.isEmpty import assertk.assertions.isEqualTo import assertk.assertions.isFalse import assertk.assertions.isInstanceOf +import assertk.assertions.isNotNull import assertk.assertions.isTrue import assertk.assertions.key import assertk.assertions.prop import com.fsck.k9.K9RobolectricTest import com.fsck.k9.Preferences import com.fsck.k9.mail.AuthType -import com.fsck.k9.preferences.SettingsImporter.AccountDescription -import com.fsck.k9.preferences.SettingsImporter.AccountDescriptionPair -import com.fsck.k9.preferences.SettingsImporter.ImportContents -import com.fsck.k9.preferences.SettingsImporter.ImportResults -import com.fsck.k9.preferences.SettingsImporter.ImportedAccount -import com.fsck.k9.preferences.SettingsImporter.ImportedIdentity -import com.fsck.k9.preferences.SettingsImporter.ImportedServer import java.util.UUID import org.junit.Before import org.junit.Test @@ -130,7 +124,7 @@ class SettingsImporterTest : K9RobolectricTest() { val results = SettingsImporter.parseSettings(inputStream, true, accountUuids, true) - assertThat(results.accounts).all { + assertThat(results.accounts).isNotNull().all { hasSize(1) key(accountUuid).all { prop(ImportedAccount::uuid).isEqualTo(accountUuid) @@ -161,11 +155,12 @@ class SettingsImporterTest : K9RobolectricTest() { val results = SettingsImporter.parseSettings(inputStream, true, accountUuids, true) - assertThat(results.accounts).all { + assertThat(results.accounts).isNotNull().all { hasSize(1) key(accountUuid).all { prop(ImportedAccount::uuid).isEqualTo(accountUuid) - prop(ImportedAccount::identities).extracting(ImportedIdentity::email).containsExactly("user@gmail.com") + prop(ImportedAccount::identities).isNotNull() + .extracting(ImportedIdentity::email).containsExactly("user@gmail.com") } } } @@ -191,8 +186,10 @@ class SettingsImporterTest : K9RobolectricTest() { val results = SettingsImporter.parseSettings(inputStream, true, accountUuids, false) assertThat(results.accounts) + .isNotNull() .key(accountUuid) .prop(ImportedAccount::incoming) + .isNotNull() .prop(ImportedServer::authenticationType) .isEqualTo(AuthType.CRAM_MD5) } diff --git a/feature/settings/import/src/main/kotlin/app/k9mail/feature/settings/import/ui/SettingsImportViewModel.kt b/feature/settings/import/src/main/kotlin/app/k9mail/feature/settings/import/ui/SettingsImportViewModel.kt index c7092b996..aafe84e93 100644 --- a/feature/settings/import/src/main/kotlin/app/k9mail/feature/settings/import/ui/SettingsImportViewModel.kt +++ b/feature/settings/import/src/main/kotlin/app/k9mail/feature/settings/import/ui/SettingsImportViewModel.kt @@ -12,8 +12,9 @@ import androidx.lifecycle.viewModelScope import app.k9mail.feature.settings.import.SettingsImportExternalContract.AccountActivator import com.fsck.k9.helper.SingleLiveEvent import com.fsck.k9.helper.measureRealtimeMillisWithResult +import com.fsck.k9.preferences.ImportContents +import com.fsck.k9.preferences.ImportResults import com.fsck.k9.preferences.SettingsImporter -import com.fsck.k9.preferences.SettingsImporter.ImportResults import com.fsck.k9.ui.base.bundle.getEnum import com.fsck.k9.ui.base.bundle.putEnum import kotlinx.coroutines.Dispatchers @@ -380,7 +381,7 @@ internal class SettingsImportViewModel( } } - private fun readSettings(contentUri: Uri): SettingsImporter.ImportContents { + private fun readSettings(contentUri: Uri): ImportContents { return context.contentResolver.openInputStream(contentUri).use { inputStream -> SettingsImporter.getImportStreamContents(inputStream) }