Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace StorageManager with StorageFilesProvider #8677

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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")
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ val mailStoreModule = module {
)
}
single { MessageViewInfoExtractorFactory(get(), get(), get()) }
single { StorageManager.getInstance(get()) }
single<StorageFilesProviderFactory> { AndroidStorageFilesProviderFactory(context = get()) }
single { SpecialFolderSelectionStrategy() }
single {
K9BackendStorageFactory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -177,18 +176,18 @@ 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;

SchemaDefinitionFactory schemaDefinitionFactory = DI.get(SchemaDefinitionFactory.class);
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);
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -76,30 +76,13 @@ public interface SchemaDefinition {

private SchemaDefinition mSchemaDefinition;

private String uUid;

/**
* @param context
* Never <code>null</code>.
* @param uUid
* Never <code>null</code>.
* @param schemaDefinition
* Never <code>null</code>.
*/
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).
Expand Down Expand Up @@ -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
Expand All @@ -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?
Expand Down Expand Up @@ -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()) {
Expand All @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.fsck.k9.mailstore

interface StorageFilesProviderFactory {
fun createStorageFilesProvider(accountId: String): StorageFilesProvider
}
Loading
Loading