From e268b6bcce7fae4f809c88b62abdb0c100c72e20 Mon Sep 17 00:00:00 2001 From: cketti Date: Sun, 15 Dec 2024 22:57:32 +0100 Subject: [PATCH] Replace `StorageManager` with `StorageFilesProvider` --- .../mailstore/AndroidStorageFilesProvider.kt | 17 + .../AndroidStorageFilesProviderFactory.kt | 11 + .../java/com/fsck/k9/mailstore/KoinModule.kt | 2 +- .../com/fsck/k9/mailstore/LocalStore.java | 13 +- .../fsck/k9/mailstore/LockableDatabase.java | 38 +-- .../fsck/k9/mailstore/StorageFilesProvider.kt | 15 + .../mailstore/StorageFilesProviderFactory.kt | 5 + .../com/fsck/k9/mailstore/StorageManager.java | 294 ------------------ .../java/com/fsck/k9/storage/KoinModule.kt | 6 +- .../storage/messages/AttachmentFileManager.kt | 8 +- .../k9/storage/messages/DatabaseOperations.kt | 10 +- .../k9/storage/messages/K9MessageStore.kt | 9 +- .../storage/messages/K9MessageStoreFactory.kt | 11 +- .../messages/CopyMessageOperationsTest.kt | 15 +- .../messages/DeleteFolderOperationsTest.kt | 15 +- .../messages/DeleteMessageOperationsTest.kt | 15 +- .../messages/SaveMessageOperationsTest.kt | 15 +- 17 files changed, 106 insertions(+), 393 deletions(-) create mode 100644 legacy/core/src/main/java/com/fsck/k9/mailstore/AndroidStorageFilesProvider.kt create mode 100644 legacy/core/src/main/java/com/fsck/k9/mailstore/AndroidStorageFilesProviderFactory.kt create mode 100644 legacy/core/src/main/java/com/fsck/k9/mailstore/StorageFilesProvider.kt create mode 100644 legacy/core/src/main/java/com/fsck/k9/mailstore/StorageFilesProviderFactory.kt delete mode 100644 legacy/core/src/main/java/com/fsck/k9/mailstore/StorageManager.java diff --git a/legacy/core/src/main/java/com/fsck/k9/mailstore/AndroidStorageFilesProvider.kt b/legacy/core/src/main/java/com/fsck/k9/mailstore/AndroidStorageFilesProvider.kt new file mode 100644 index 00000000000..d3e5661d2ff --- /dev/null +++ b/legacy/core/src/main/java/com/fsck/k9/mailstore/AndroidStorageFilesProvider.kt @@ -0,0 +1,17 @@ +package com.fsck.k9.mailstore + +import android.content.Context +import java.io.File + +internal class AndroidStorageFilesProvider( + private val context: Context, + private val accountId: String, +) : StorageFilesProvider { + override fun getDatabaseFile(): File { + return context.getDatabasePath("$accountId.db") + } + + override fun getAttachmentDirectory(): File { + return context.getDatabasePath("$accountId.db_att") + } +} diff --git a/legacy/core/src/main/java/com/fsck/k9/mailstore/AndroidStorageFilesProviderFactory.kt b/legacy/core/src/main/java/com/fsck/k9/mailstore/AndroidStorageFilesProviderFactory.kt new file mode 100644 index 00000000000..723070b00df --- /dev/null +++ b/legacy/core/src/main/java/com/fsck/k9/mailstore/AndroidStorageFilesProviderFactory.kt @@ -0,0 +1,11 @@ +package com.fsck.k9.mailstore + +import android.content.Context + +class AndroidStorageFilesProviderFactory( + private val context: Context, +) : StorageFilesProviderFactory { + override fun createStorageFilesProvider(accountId: String): StorageFilesProvider { + return AndroidStorageFilesProvider(context, accountId) + } +} diff --git a/legacy/core/src/main/java/com/fsck/k9/mailstore/KoinModule.kt b/legacy/core/src/main/java/com/fsck/k9/mailstore/KoinModule.kt index 2872d79514a..c4399a6f3bb 100644 --- a/legacy/core/src/main/java/com/fsck/k9/mailstore/KoinModule.kt +++ b/legacy/core/src/main/java/com/fsck/k9/mailstore/KoinModule.kt @@ -15,7 +15,7 @@ val mailStoreModule = module { ) } single { MessageViewInfoExtractorFactory(get(), get(), get()) } - single { StorageManager.getInstance(get()) } + single { AndroidStorageFilesProviderFactory(context = get()) } single { SpecialFolderSelectionStrategy() } single { K9BackendStorageFactory( diff --git a/legacy/core/src/main/java/com/fsck/k9/mailstore/LocalStore.java b/legacy/core/src/main/java/com/fsck/k9/mailstore/LocalStore.java index 9b044397a70..4c138fb9342 100644 --- a/legacy/core/src/main/java/com/fsck/k9/mailstore/LocalStore.java +++ b/legacy/core/src/main/java/com/fsck/k9/mailstore/LocalStore.java @@ -46,7 +46,6 @@ import com.fsck.k9.mailstore.LocalFolder.DataLocation; import com.fsck.k9.mailstore.LockableDatabase.DbCallback; import com.fsck.k9.mailstore.LockableDatabase.SchemaDefinition; -import com.fsck.k9.mailstore.StorageManager.InternalStorageProvider; import com.fsck.k9.message.extractors.AttachmentInfoExtractor; import app.k9mail.legacy.search.LocalSearch; import app.k9mail.legacy.search.api.SearchAttribute; @@ -160,9 +159,9 @@ public class LocalStore { */ private static final int THREAD_FLAG_UPDATE_BATCH_SIZE = 500; - private final Context context; private final PendingCommandSerializer pendingCommandSerializer; private final AttachmentInfoExtractor attachmentInfoExtractor; + private final StorageFilesProvider storageFilesProvider; private final Account account; private final LockableDatabase database; @@ -177,10 +176,10 @@ static LocalStore createInstance(Account account, Context context) throws Messag * This constructor is only used by {@link LocalStoreProvider#getInstance(Account)} */ private LocalStore(final Account account, final Context context) throws MessagingException { - this.context = context; - pendingCommandSerializer = PendingCommandSerializer.getInstance(); attachmentInfoExtractor = DI.get(AttachmentInfoExtractor.class); + StorageFilesProviderFactory storageFilesProviderFactory = DI.get(StorageFilesProviderFactory.class); + storageFilesProvider = storageFilesProviderFactory.createStorageFilesProvider(account.getUuid()); this.account = account; @@ -188,7 +187,7 @@ private LocalStore(final Account account, final Context context) throws Messagin RealMigrationsHelper migrationsHelper = new RealMigrationsHelper(); SchemaDefinition schemaDefinition = schemaDefinitionFactory.createSchemaDefinition(migrationsHelper); - database = new LockableDatabase(context, account.getUuid(), schemaDefinition); + database = new LockableDatabase(context, storageFilesProvider, schemaDefinition); database.open(); Clock clock = DI.get(Clock.class); @@ -650,9 +649,7 @@ public void close() { } File getAttachmentFile(String attachmentId) { - final StorageManager storageManager = StorageManager.getInstance(context); - final File attachmentDirectory = storageManager.getAttachmentDirectory( - account.getUuid(), database.getStorageProviderId()); + File attachmentDirectory = storageFilesProvider.getAttachmentDirectory(); return new File(attachmentDirectory, attachmentId); } diff --git a/legacy/core/src/main/java/com/fsck/k9/mailstore/LockableDatabase.java b/legacy/core/src/main/java/com/fsck/k9/mailstore/LockableDatabase.java index 0e112593bc6..55af3deed39 100644 --- a/legacy/core/src/main/java/com/fsck/k9/mailstore/LockableDatabase.java +++ b/legacy/core/src/main/java/com/fsck/k9/mailstore/LockableDatabase.java @@ -13,7 +13,6 @@ import com.fsck.k9.K9; import com.fsck.k9.helper.FileHelper; import com.fsck.k9.mail.MessagingException; -import com.fsck.k9.mailstore.StorageManager.InternalStorageProvider; import timber.log.Timber; import static java.lang.System.currentTimeMillis; @@ -65,6 +64,7 @@ public interface SchemaDefinition { } private Context context; + private final StorageFilesProvider storageFilesProvider; /** * {@link ThreadLocal} to check whether a DB transaction is occurring in the @@ -76,30 +76,13 @@ public interface SchemaDefinition { private SchemaDefinition mSchemaDefinition; - private String uUid; - - /** - * @param context - * Never null. - * @param uUid - * Never null. - * @param schemaDefinition - * Never null. - */ - public LockableDatabase(final Context context, final String uUid, final SchemaDefinition schemaDefinition) { + public LockableDatabase(Context context, StorageFilesProvider storageFilesProvider, + SchemaDefinition schemaDefinition) { this.context = context; - this.uUid = uUid; + this.storageFilesProvider = storageFilesProvider; this.mSchemaDefinition = schemaDefinition; } - public String getStorageProviderId() { - return InternalStorageProvider.ID; - } - - private StorageManager getStorageManager() { - return StorageManager.getInstance(context); - } - /** * Lock the storage for shared operations (concurrent threads are allowed to * run simultaneously). @@ -230,10 +213,7 @@ private void doOpenOrCreateDb(final File databaseFile) { } protected File prepareStorage() { - String providerId = getStorageProviderId(); - final StorageManager storageManager = getStorageManager(); - - final File databaseFile = storageManager.getDatabase(uUid, providerId); + final File databaseFile = storageFilesProvider.getDatabaseFile(); final File databaseParentDir = databaseFile.getParentFile(); if (databaseParentDir.isFile()) { // should be safe to unconditionally delete clashing file: user is not supposed to mess with our directory @@ -247,7 +227,7 @@ protected File prepareStorage() { FileHelper.touchFile(databaseParentDir, ".nomedia"); } - final File attachmentDir = storageManager.getAttachmentDirectory(uUid, providerId); + final File attachmentDir = storageFilesProvider.getAttachmentDirectory(); final File attachmentParentDir = attachmentDir.getParentFile(); if (!attachmentParentDir.exists()) { // noinspection ResultOfMethodCallIgnored, TODO maybe throw UnavailableStorageException? @@ -275,15 +255,13 @@ public void delete() { private void delete(final boolean recreate) { lockWrite(); try { - String storageProviderId = getStorageProviderId(); try { mDb.close(); } catch (Exception e) { Timber.d("Exception caught in DB close: %s", e.getMessage()); } - final StorageManager storageManager = getStorageManager(); try { - final File attachmentDirectory = storageManager.getAttachmentDirectory(uUid, storageProviderId); + final File attachmentDirectory = storageFilesProvider.getAttachmentDirectory(); final File[] attachments = attachmentDirectory.listFiles(); for (File attachment : attachments) { if (attachment.exists()) { @@ -303,7 +281,7 @@ private void delete(final boolean recreate) { Timber.d("Exception caught in clearing attachments: %s", e.getMessage()); } try { - deleteDatabase(storageManager.getDatabase(uUid, storageProviderId)); + deleteDatabase(storageFilesProvider.getDatabaseFile()); } catch (Exception e) { Timber.i(e, "LockableDatabase: delete(): Unable to delete backing DB file"); } diff --git a/legacy/core/src/main/java/com/fsck/k9/mailstore/StorageFilesProvider.kt b/legacy/core/src/main/java/com/fsck/k9/mailstore/StorageFilesProvider.kt new file mode 100644 index 00000000000..37a36f56d76 --- /dev/null +++ b/legacy/core/src/main/java/com/fsck/k9/mailstore/StorageFilesProvider.kt @@ -0,0 +1,15 @@ +package com.fsck.k9.mailstore + +import java.io.File + +interface StorageFilesProvider { + /** + * Returns the file that should be used for the message store database. + */ + fun getDatabaseFile(): File + + /** + * Returns the directory under which to store attachments. + */ + fun getAttachmentDirectory(): File +} diff --git a/legacy/core/src/main/java/com/fsck/k9/mailstore/StorageFilesProviderFactory.kt b/legacy/core/src/main/java/com/fsck/k9/mailstore/StorageFilesProviderFactory.kt new file mode 100644 index 00000000000..1a16101ea99 --- /dev/null +++ b/legacy/core/src/main/java/com/fsck/k9/mailstore/StorageFilesProviderFactory.kt @@ -0,0 +1,5 @@ +package com.fsck.k9.mailstore + +interface StorageFilesProviderFactory { + fun createStorageFilesProvider(accountId: String): StorageFilesProvider +} diff --git a/legacy/core/src/main/java/com/fsck/k9/mailstore/StorageManager.java b/legacy/core/src/main/java/com/fsck/k9/mailstore/StorageManager.java deleted file mode 100644 index 6aece52ec30..00000000000 --- a/legacy/core/src/main/java/com/fsck/k9/mailstore/StorageManager.java +++ /dev/null @@ -1,294 +0,0 @@ -package com.fsck.k9.mailstore; - - -import java.io.File; -import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import android.content.Context; -import android.os.Environment; - - -/** - * Manager for different {@link StorageProvider} -classes that abstract access - * to sd-cards, additional internal memory and other storage-locations. - */ -public class StorageManager { - - /** - * Provides entry points (File objects) to an underlying storage, - * alleviating the caller from having to know where that storage is located. - * - *

- * Allow checking for the denoted storage availability since its lifecycle - * can evolving (a storage might become unavailable at some time and be back - * online later). - *

- */ - public interface StorageProvider { - - /** - * Retrieve the uniquely identifier for the current implementation. - * - *

- * It is expected that the identifier doesn't change over reboots since - * it'll be used to save settings and retrieve the provider at a later - * time. - *

- * - *

- * The returned identifier doesn't have to be user friendly. - *

- * - * @return Never null. - */ - String getId(); - - /** - * Hook point for provider initialization. - * - * @param context - * Never null. - */ - void init(Context context); - - /** - * Some implementations may not be able to return valid File handles - * because the device doesn't provide the denoted storage. You can check - * the provider compatibility with this method to prevent from having to - * invoke this provider ever again. - * - * @param context - * TODO - * @return Whether this provider supports the current device. - * @see StorageManager#getAvailableProviders() - */ - boolean isSupported(Context context); - - /** - * Return the {@link File} to the chosen email database file. The - * resulting {@link File} doesn't necessarily match an existing file on - * the filesystem. - * - * @param context - * Never null. - * @param id - * Never null. - * @return Never null. - */ - File getDatabase(Context context, String id); - - /** - * Return the {@link File} to the chosen attachment directory. The - * resulting {@link File} doesn't necessarily match an existing - * directory on the filesystem. - * - * @param context - * Never null. - * @param id - * Never null. - * @return Never null. - */ - File getAttachmentDirectory(Context context, String id); - } - - /** - * Strategy to access the always available internal storage. - * - *

- * This implementation is expected to work on every device since it's based - * on the regular Android API {@link Context#getDatabasePath(String)} and - * uses the result to retrieve the DB path and the attachment directory path. - *

- * - *

- * The underlying storage has always been used by K-9. - *

- */ - public static class InternalStorageProvider implements StorageProvider { - public static final String ID = "InternalStorage"; - - @Override - public String getId() { - return ID; - } - - @Override - public void init(Context context) { - } - - @Override - public boolean isSupported(Context context) { - return true; - } - - @Override - public File getDatabase(Context context, String id) { - return context.getDatabasePath(id + ".db"); - } - - @Override - public File getAttachmentDirectory(Context context, String id) { - // we store attachments in the database directory - return context.getDatabasePath(id + ".db_att"); - } - } - - /** - * Strategy for accessing the storage as returned by - * {@link Environment#getExternalStorageDirectory()}. In order to be - * compliant with Android recommendation regarding application uninstalling - * and to prevent from cluttering the storage root, the chosen directory - * will be - * <STORAGE_ROOT>/Android/data/<APPLICATION_PACKAGE_NAME>/files/ - * - *

- * The denoted storage is usually a SD card. - *

- * - *

- * This provider is expected to work on all devices but the returned - * underlying storage might not be always available, due to - * mount/unmount/USB share events. - *

- */ - public static class ExternalStorageProvider implements StorageProvider { - public static final String ID = "ExternalStorage"; - - /** - * Chosen base directory. - */ - private File mApplicationDirectory; - - - @Override - public String getId() { - return ID; - } - - @Override - public void init(Context context) { - mApplicationDirectory = context.getExternalFilesDir(null); - } - - @Override - public boolean isSupported(Context context) { - return true; - } - - @Override - public File getDatabase(Context context, String id) { - return new File(mApplicationDirectory, id + ".db"); - } - - @Override - public File getAttachmentDirectory(Context context, String id) { - return new File(mApplicationDirectory, id + ".db_att"); - } - } - - /** - * The active storage providers. - */ - private final Map mProviders = new LinkedHashMap<>(); - - protected final Context context; - - private static transient StorageManager instance; - - public static synchronized StorageManager getInstance(final Context context) { - if (instance == null) { - Context applicationContext = context.getApplicationContext(); - instance = new StorageManager(applicationContext); - } - return instance; - } - - /** - * @param context - * Never null. - * @throws NullPointerException - * If context is null. - */ - protected StorageManager(final Context context) throws NullPointerException { - if (context == null) { - throw new NullPointerException("No Context given"); - } - - this.context = context; - - /* - * 20101113/fiouzy: - * - * Here is where we define which providers are used, currently we only - * allow the internal storage and the regular external storage. - * - * !!! Make sure InternalStorageProvider is the first provider as it'll - * be considered as the default provider !!! - */ - final List allProviders = Arrays.asList( - new InternalStorageProvider(), - new ExternalStorageProvider() - ); - for (final StorageProvider provider : allProviders) { - // check for provider compatibility - if (provider.isSupported(context)) { - // provider is compatible! proceeding - - provider.init(context); - mProviders.put(provider.getId(), provider); - } - } - - } - - /** - * @return Never null. - */ - public String getDefaultProviderId() { - // assume there is at least 1 provider defined - return mProviders.keySet().iterator().next(); - } - - /** - * @param providerId - * Never null. - * @return null if not found. - */ - protected StorageProvider getProvider(final String providerId) { - return mProviders.get(providerId); - } - - /** - * @param dbName - * Never null. - * @param providerId - * Never null. - * @return The resolved database file for the given provider ID. - */ - public File getDatabase(final String dbName, final String providerId) { - StorageProvider provider = getProvider(providerId); - // TODO fallback to internal storage if no provider - return provider.getDatabase(context, dbName); - } - - /** - * @param dbName - * Never null. - * @param providerId - * Never null. - * @return The resolved attachment directory for the given provider ID. - */ - public File getAttachmentDirectory(final String dbName, final String providerId) { - StorageProvider provider = getProvider(providerId); - // TODO fallback to internal storage if no provider - return provider.getAttachmentDirectory(context, dbName); - } - - public Set getAvailableProviders() { - return mProviders.keySet(); - } -} diff --git a/legacy/storage/src/main/java/com/fsck/k9/storage/KoinModule.kt b/legacy/storage/src/main/java/com/fsck/k9/storage/KoinModule.kt index 274c025c44d..d1a8b4e2aa5 100644 --- a/legacy/storage/src/main/java/com/fsck/k9/storage/KoinModule.kt +++ b/legacy/storage/src/main/java/com/fsck/k9/storage/KoinModule.kt @@ -10,7 +10,11 @@ import org.koin.dsl.module val storageModule = module { single { K9SchemaDefinitionFactory() } single { - K9MessageStoreFactory(localStoreProvider = get(), storageManager = get(), basicPartInfoExtractor = get()) + K9MessageStoreFactory( + localStoreProvider = get(), + storageFilesProviderFactory = get(), + basicPartInfoExtractor = get(), + ) } single { K9NotificationStoreProvider(localStoreProvider = get()) diff --git a/legacy/storage/src/main/java/com/fsck/k9/storage/messages/AttachmentFileManager.kt b/legacy/storage/src/main/java/com/fsck/k9/storage/messages/AttachmentFileManager.kt index 9123a6dc47c..2f45ae8e037 100644 --- a/legacy/storage/src/main/java/com/fsck/k9/storage/messages/AttachmentFileManager.kt +++ b/legacy/storage/src/main/java/com/fsck/k9/storage/messages/AttachmentFileManager.kt @@ -2,14 +2,12 @@ package com.fsck.k9.storage.messages import com.fsck.k9.K9 import com.fsck.k9.helper.FileHelper -import com.fsck.k9.mailstore.StorageManager -import com.fsck.k9.mailstore.StorageManager.InternalStorageProvider +import com.fsck.k9.mailstore.StorageFilesProvider import java.io.File import timber.log.Timber internal class AttachmentFileManager( - private val storageManager: StorageManager, - private val accountUuid: String, + private val storageFilesProvider: StorageFilesProvider, ) { fun deleteFile(messagePartId: Long) { val file = getAttachmentFile(messagePartId) @@ -30,7 +28,7 @@ internal class AttachmentFileManager( } fun getAttachmentFile(messagePartId: Long): File { - val attachmentDirectory = storageManager.getAttachmentDirectory(accountUuid, InternalStorageProvider.ID) + val attachmentDirectory = storageFilesProvider.getAttachmentDirectory() return File(attachmentDirectory, messagePartId.toString()) } } diff --git a/legacy/storage/src/main/java/com/fsck/k9/storage/messages/DatabaseOperations.kt b/legacy/storage/src/main/java/com/fsck/k9/storage/messages/DatabaseOperations.kt index 93debbab1d4..27b8e200ec9 100644 --- a/legacy/storage/src/main/java/com/fsck/k9/storage/messages/DatabaseOperations.kt +++ b/legacy/storage/src/main/java/com/fsck/k9/storage/messages/DatabaseOperations.kt @@ -1,17 +1,15 @@ package com.fsck.k9.storage.messages import com.fsck.k9.mailstore.LockableDatabase -import com.fsck.k9.mailstore.StorageManager +import com.fsck.k9.mailstore.StorageFilesProvider import timber.log.Timber internal class DatabaseOperations( private val lockableDatabase: LockableDatabase, - val storageManager: StorageManager, - val accountUuid: String, + private val storageFilesProvider: StorageFilesProvider, ) { fun getSize(): Long { - val storageProviderId = lockableDatabase.storageProviderId - val attachmentDirectory = storageManager.getAttachmentDirectory(accountUuid, storageProviderId) + val attachmentDirectory = storageFilesProvider.getAttachmentDirectory() return lockableDatabase.execute(false) { val attachmentFiles = attachmentDirectory.listFiles() ?: emptyArray() @@ -21,7 +19,7 @@ internal class DatabaseOperations( accumulatedSize + file.length() } - val databaseFile = storageManager.getDatabase(accountUuid, storageProviderId) + val databaseFile = storageFilesProvider.getDatabaseFile() val databaseSize = databaseFile.length() databaseSize + attachmentsSize diff --git a/legacy/storage/src/main/java/com/fsck/k9/storage/messages/K9MessageStore.kt b/legacy/storage/src/main/java/com/fsck/k9/storage/messages/K9MessageStore.kt index d3662c79fa5..67e6578de1e 100644 --- a/legacy/storage/src/main/java/com/fsck/k9/storage/messages/K9MessageStore.kt +++ b/legacy/storage/src/main/java/com/fsck/k9/storage/messages/K9MessageStore.kt @@ -12,17 +12,16 @@ import com.fsck.k9.mail.Flag import com.fsck.k9.mail.FolderType import com.fsck.k9.mail.Header import com.fsck.k9.mailstore.LockableDatabase -import com.fsck.k9.mailstore.StorageManager +import com.fsck.k9.mailstore.StorageFilesProvider import com.fsck.k9.message.extractors.BasicPartInfoExtractor import java.util.Date class K9MessageStore( database: LockableDatabase, - storageManager: StorageManager, + storageFilesProvider: StorageFilesProvider, basicPartInfoExtractor: BasicPartInfoExtractor, - accountUuid: String, ) : MessageStore { - private val attachmentFileManager = AttachmentFileManager(storageManager, accountUuid) + private val attachmentFileManager = AttachmentFileManager(storageFilesProvider) private val threadMessageOperations = ThreadMessageOperations() private val saveMessageOperations = SaveMessageOperations( database, @@ -43,7 +42,7 @@ class K9MessageStore( private val updateFolderOperations = UpdateFolderOperations(database) private val deleteFolderOperations = DeleteFolderOperations(database, attachmentFileManager) private val keyValueStoreOperations = KeyValueStoreOperations(database) - private val databaseOperations = DatabaseOperations(database, storageManager, accountUuid) + private val databaseOperations = DatabaseOperations(database, storageFilesProvider) override fun saveRemoteMessage(folderId: Long, messageServerId: String, messageData: SaveMessageData) { saveMessageOperations.saveRemoteMessage(folderId, messageServerId, messageData) diff --git a/legacy/storage/src/main/java/com/fsck/k9/storage/messages/K9MessageStoreFactory.kt b/legacy/storage/src/main/java/com/fsck/k9/storage/messages/K9MessageStoreFactory.kt index 1c70725db93..03ef3564468 100644 --- a/legacy/storage/src/main/java/com/fsck/k9/storage/messages/K9MessageStoreFactory.kt +++ b/legacy/storage/src/main/java/com/fsck/k9/storage/messages/K9MessageStoreFactory.kt @@ -5,17 +5,22 @@ import app.k9mail.legacy.mailstore.ListenableMessageStore import app.k9mail.legacy.mailstore.MessageStoreFactory import com.fsck.k9.mailstore.LocalStoreProvider import com.fsck.k9.mailstore.NotifierMessageStore -import com.fsck.k9.mailstore.StorageManager +import com.fsck.k9.mailstore.StorageFilesProviderFactory import com.fsck.k9.message.extractors.BasicPartInfoExtractor class K9MessageStoreFactory( private val localStoreProvider: LocalStoreProvider, - private val storageManager: StorageManager, + private val storageFilesProviderFactory: StorageFilesProviderFactory, private val basicPartInfoExtractor: BasicPartInfoExtractor, ) : MessageStoreFactory { override fun create(account: Account): ListenableMessageStore { val localStore = localStoreProvider.getInstance(account) - val messageStore = K9MessageStore(localStore.database, storageManager, basicPartInfoExtractor, account.uuid) + val storageFilesProvider = storageFilesProviderFactory.createStorageFilesProvider(account.uuid) + val messageStore = K9MessageStore( + localStore.database, + storageFilesProvider, + basicPartInfoExtractor, + ) val notifierMessageStore = NotifierMessageStore(messageStore, localStore) return ListenableMessageStore(notifierMessageStore) } diff --git a/legacy/storage/src/test/java/com/fsck/k9/storage/messages/CopyMessageOperationsTest.kt b/legacy/storage/src/test/java/com/fsck/k9/storage/messages/CopyMessageOperationsTest.kt index beb5e30f66d..dbc21b0b5d4 100644 --- a/legacy/storage/src/test/java/com/fsck/k9/storage/messages/CopyMessageOperationsTest.kt +++ b/legacy/storage/src/test/java/com/fsck/k9/storage/messages/CopyMessageOperationsTest.kt @@ -6,28 +6,23 @@ import assertk.assertions.isEqualTo import assertk.assertions.isNotIn import assertk.assertions.isNull import com.fsck.k9.mail.crlf -import com.fsck.k9.mailstore.StorageManager +import com.fsck.k9.mailstore.StorageFilesProvider import com.fsck.k9.storage.RobolectricTest import okio.buffer import okio.sink import okio.source import org.junit.After import org.junit.Test -import org.mockito.kotlin.anyOrNull -import org.mockito.kotlin.doReturn -import org.mockito.kotlin.eq -import org.mockito.kotlin.mock - -private const val ACCOUNT_UUID = "00000000-0000-4000-0000-000000000000" class CopyMessageOperationsTest : RobolectricTest() { private val messagePartDirectory = createRandomTempDirectory() private val sqliteDatabase = createDatabase() - private val storageManager = mock { - on { getAttachmentDirectory(eq(ACCOUNT_UUID), anyOrNull()) } doReturn messagePartDirectory + private val storageFilesProvider = object : StorageFilesProvider { + override fun getDatabaseFile() = error("Not implemented") + override fun getAttachmentDirectory() = messagePartDirectory } private val lockableDatabase = createLockableDatabaseMock(sqliteDatabase) - private val attachmentFileManager = AttachmentFileManager(storageManager, ACCOUNT_UUID) + private val attachmentFileManager = AttachmentFileManager(storageFilesProvider) private val threadMessageOperations = ThreadMessageOperations() private val copyMessageOperations = CopyMessageOperations( lockableDatabase, diff --git a/legacy/storage/src/test/java/com/fsck/k9/storage/messages/DeleteFolderOperationsTest.kt b/legacy/storage/src/test/java/com/fsck/k9/storage/messages/DeleteFolderOperationsTest.kt index 9726ec4f903..15113a7722b 100644 --- a/legacy/storage/src/test/java/com/fsck/k9/storage/messages/DeleteFolderOperationsTest.kt +++ b/legacy/storage/src/test/java/com/fsck/k9/storage/messages/DeleteFolderOperationsTest.kt @@ -6,25 +6,20 @@ import assertk.assertions.extracting import assertk.assertions.hasSize import assertk.assertions.isEqualTo import assertk.assertions.isNotNull -import com.fsck.k9.mailstore.StorageManager +import com.fsck.k9.mailstore.StorageFilesProvider import com.fsck.k9.storage.RobolectricTest import org.junit.After import org.junit.Test -import org.mockito.kotlin.anyOrNull -import org.mockito.kotlin.doReturn -import org.mockito.kotlin.eq -import org.mockito.kotlin.mock - -private const val ACCOUNT_UUID = "00000000-0000-4000-0000-000000000000" class DeleteFolderOperationsTest : RobolectricTest() { private val messagePartDirectory = createRandomTempDirectory() private val sqliteDatabase = createDatabase() - private val storageManager = mock { - on { getAttachmentDirectory(eq(ACCOUNT_UUID), anyOrNull()) } doReturn messagePartDirectory + private val storageFilesProvider = object : StorageFilesProvider { + override fun getDatabaseFile() = error("Not implemented") + override fun getAttachmentDirectory() = messagePartDirectory } private val lockableDatabase = createLockableDatabaseMock(sqliteDatabase) - private val attachmentFileManager = AttachmentFileManager(storageManager, ACCOUNT_UUID) + private val attachmentFileManager = AttachmentFileManager(storageFilesProvider) private val deleteFolderOperations = DeleteFolderOperations(lockableDatabase, attachmentFileManager) @After diff --git a/legacy/storage/src/test/java/com/fsck/k9/storage/messages/DeleteMessageOperationsTest.kt b/legacy/storage/src/test/java/com/fsck/k9/storage/messages/DeleteMessageOperationsTest.kt index 56833d2db83..02161b09c8c 100644 --- a/legacy/storage/src/test/java/com/fsck/k9/storage/messages/DeleteMessageOperationsTest.kt +++ b/legacy/storage/src/test/java/com/fsck/k9/storage/messages/DeleteMessageOperationsTest.kt @@ -8,25 +8,20 @@ import assertk.assertions.isEmpty import assertk.assertions.isEqualTo import assertk.assertions.isNotNull import assertk.assertions.isNull -import com.fsck.k9.mailstore.StorageManager +import com.fsck.k9.mailstore.StorageFilesProvider import com.fsck.k9.storage.RobolectricTest import org.junit.After import org.junit.Test -import org.mockito.kotlin.anyOrNull -import org.mockito.kotlin.doReturn -import org.mockito.kotlin.eq -import org.mockito.kotlin.mock - -private const val ACCOUNT_UUID = "00000000-0000-4000-0000-000000000000" class DeleteMessageOperationsTest : RobolectricTest() { private val messagePartDirectory = createRandomTempDirectory() private val sqliteDatabase = createDatabase() - private val storageManager = mock { - on { getAttachmentDirectory(eq(ACCOUNT_UUID), anyOrNull()) } doReturn messagePartDirectory + private val storageFilesProvider = object : StorageFilesProvider { + override fun getDatabaseFile() = error("Not implemented") + override fun getAttachmentDirectory() = messagePartDirectory } private val lockableDatabase = createLockableDatabaseMock(sqliteDatabase) - private val attachmentFileManager = AttachmentFileManager(storageManager, ACCOUNT_UUID) + private val attachmentFileManager = AttachmentFileManager(storageFilesProvider) private val deleteMessageOperations = DeleteMessageOperations(lockableDatabase, attachmentFileManager) @After diff --git a/legacy/storage/src/test/java/com/fsck/k9/storage/messages/SaveMessageOperationsTest.kt b/legacy/storage/src/test/java/com/fsck/k9/storage/messages/SaveMessageOperationsTest.kt index 75d70bab044..f2806260eda 100644 --- a/legacy/storage/src/test/java/com/fsck/k9/storage/messages/SaveMessageOperationsTest.kt +++ b/legacy/storage/src/test/java/com/fsck/k9/storage/messages/SaveMessageOperationsTest.kt @@ -16,28 +16,23 @@ import com.fsck.k9.mail.MessageDownloadState import com.fsck.k9.mail.Multipart import com.fsck.k9.mail.Part import com.fsck.k9.mail.buildMessage -import com.fsck.k9.mailstore.StorageManager +import com.fsck.k9.mailstore.StorageFilesProvider import com.fsck.k9.message.extractors.BasicPartInfoExtractor import com.fsck.k9.storage.RobolectricTest import java.io.ByteArrayOutputStream import java.util.Stack import org.junit.After import org.junit.Test -import org.mockito.kotlin.anyOrNull -import org.mockito.kotlin.doReturn -import org.mockito.kotlin.eq -import org.mockito.kotlin.mock - -private const val ACCOUNT_UUID = "00000000-0000-4000-0000-000000000000" class SaveMessageOperationsTest : RobolectricTest() { private val messagePartDirectory = createRandomTempDirectory() private val sqliteDatabase = createDatabase() - private val storageManager = mock { - on { getAttachmentDirectory(eq(ACCOUNT_UUID), anyOrNull()) } doReturn messagePartDirectory + private val storageFilesProvider = object : StorageFilesProvider { + override fun getDatabaseFile() = error("Not implemented") + override fun getAttachmentDirectory() = messagePartDirectory } private val lockableDatabase = createLockableDatabaseMock(sqliteDatabase) - private val attachmentFileManager = AttachmentFileManager(storageManager, ACCOUNT_UUID) + private val attachmentFileManager = AttachmentFileManager(storageFilesProvider) private val basicPartInfoExtractor = BasicPartInfoExtractor() private val threadMessageOperations = ThreadMessageOperations() private val saveMessageOperations = SaveMessageOperations(