Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 7e63b051 authored by Wolf-Martell Montwé's avatar Wolf-Martell Montwé Committed by GitHub
Browse files

Merge pull request #8517 from cketti/remove-LockableDatabase_switchProvider

Remove `ExternalStorageProvider` support from `LockableDatabase`
parents b08bd846 d61b7f21
Loading
Loading
Loading
Loading
+0 −7
Original line number Diff line number Diff line
@@ -46,13 +46,6 @@ class Account(
    @set:Synchronized
    var oAuthState: String? = null

    /**
     * Storage provider ID, used to locate and manage the underlying DB/file storage.
     */
    @get:Synchronized
    @set:Synchronized
    var localStorageProviderId: String? = null

    @get:Synchronized
    @set:Synchronized
    override var name: String? = null
+0 −9
Original line number Diff line number Diff line
@@ -20,13 +20,11 @@ import app.k9mail.legacy.notification.NotificationSettings
import app.k9mail.legacy.notification.NotificationVibration
import app.k9mail.legacy.notification.VibratePattern
import com.fsck.k9.helper.Utility
import com.fsck.k9.mailstore.StorageManager
import com.fsck.k9.preferences.Storage
import com.fsck.k9.preferences.StorageEditor
import timber.log.Timber

class AccountPreferenceSerializer(
    private val storageManager: StorageManager,
    private val resourceProvider: CoreResourceProvider,
    private val serverSettingsSerializer: ServerSettingsSerializer,
) {
@@ -42,10 +40,6 @@ class AccountPreferenceSerializer(
                storage.getString("$accountUuid.$OUTGOING_SERVER_SETTINGS_KEY", ""),
            )
            oAuthState = storage.getString("$accountUuid.oAuthState", null)
            localStorageProviderId = storage.getString(
                "$accountUuid.localStorageProvider",
                storageManager.defaultProviderId,
            )
            name = storage.getString("$accountUuid.description", null)
            alwaysBcc = storage.getString("$accountUuid.alwaysBcc", alwaysBcc)
            automaticCheckIntervalMinutes = storage.getInt(
@@ -278,7 +272,6 @@ class AccountPreferenceSerializer(
                serverSettingsSerializer.serialize(outgoingServerSettings),
            )
            editor.putString("$accountUuid.oAuthState", oAuthState)
            editor.putString("$accountUuid.localStorageProvider", localStorageProviderId)
            editor.putString("$accountUuid.description", name)
            editor.putString("$accountUuid.alwaysBcc", alwaysBcc)
            editor.putInt("$accountUuid.automaticCheckIntervalMinutes", automaticCheckIntervalMinutes)
@@ -468,7 +461,6 @@ class AccountPreferenceSerializer(
        editor.remove("$accountUuid.defaultQuotedTextShown")
        editor.remove("$accountUuid.displayCount")
        editor.remove("$accountUuid.inboxFolderName")
        editor.remove("$accountUuid.localStorageProvider")
        editor.remove("$accountUuid.messageFormat")
        editor.remove("$accountUuid.messageReadReceipt")
        editor.remove("$accountUuid.notifyMailCheck")
@@ -570,7 +562,6 @@ class AccountPreferenceSerializer(

    fun loadDefaults(account: Account) {
        with(account) {
            localStorageProviderId = storageManager.defaultProviderId
            automaticCheckIntervalMinutes = DEFAULT_SYNC_INTERVAL
            idleRefreshMinutes = 24
            displayCount = K9.DEFAULT_VISIBLE_LIMIT
+0 −15
Original line number Diff line number Diff line
@@ -189,21 +189,10 @@ public class LocalStore {
        SchemaDefinition schemaDefinition = schemaDefinitionFactory.createSchemaDefinition(migrationsHelper);

        database = new LockableDatabase(context, account.getUuid(), schemaDefinition);
        database.setStorageProviderId(account.getLocalStorageProviderId());
        database.open();

        Clock clock = DI.get(Clock.class);
        outboxStateRepository = new OutboxStateRepository(database, clock);

        // If "External storage" is selected as storage location, move database to internal storage
        //TODO: Remove this code after 2020-12-31.
        // If the database is still on external storage after this date, we'll just ignore it and create a new one on
        // internal storage.
        if (!InternalStorageProvider.ID.equals(account.getLocalStorageProviderId())) {
            switchLocalStorage(InternalStorageProvider.ID);
            account.setLocalStorageProviderId(InternalStorageProvider.ID);
            getPreferences().saveAccount(account);
        }
    }

    public static int getDbVersion() {
@@ -211,10 +200,6 @@ public class LocalStore {
        return schemaDefinitionFactory.getDatabaseVersion();
    }

    public void switchLocalStorage(final String newStorageProviderId) throws MessagingException {
        database.switchProvider(newStorageProviderId);
    }

    Account getAccount() {
        return account;
    }
+9 −64
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ import android.database.sqlite.SQLiteException;
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;
@@ -46,8 +47,6 @@ public class LockableDatabase {
        void doDbUpgrade(SQLiteDatabase db);
    }

    private String mStorageProviderId;

    private SQLiteDatabase mDb;
    /**
     * Reentrant read lock
@@ -93,12 +92,8 @@ public class LockableDatabase {
        this.mSchemaDefinition = schemaDefinition;
    }

    public void setStorageProviderId(String mStorageProviderId) {
        this.mStorageProviderId = mStorageProviderId;
    }

    public String getStorageProviderId() {
        return mStorageProviderId;
        return InternalStorageProvider.ID;
    }

    private StorageManager getStorageManager() {
@@ -196,51 +191,6 @@ public class LockableDatabase {
        }
    }

    /**
     * @param newProviderId
     *            Never <code>null</code>.
     * @throws MessagingException
     */
    public void switchProvider(final String newProviderId) throws MessagingException {
        if (newProviderId.equals(mStorageProviderId)) {
            Timber.v("LockableDatabase: Ignoring provider switch request as they are equal: %s", newProviderId);
            return;
        }

        Timber.v("LockableDatabase: Switching provider from %s to %s", mStorageProviderId, newProviderId);

        final String oldProviderId = mStorageProviderId;
        lockWrite();
        try {
            try {
                mDb.close();
            } catch (Exception e) {
                Timber.i(e, "Unable to close DB on local store migration");
            }

            final StorageManager storageManager = getStorageManager();
            File oldDatabase = storageManager.getDatabase(uUid, oldProviderId);

            // create new path
            prepareStorage(newProviderId);

            // move all database files
            FileHelper.moveRecursive(oldDatabase, storageManager.getDatabase(uUid, newProviderId));
            // move all attachment files
            FileHelper.moveRecursive(storageManager.getAttachmentDirectory(uUid, oldProviderId),
                    storageManager.getAttachmentDirectory(uUid, newProviderId));
            // remove any remaining old journal files
            deleteDatabase(oldDatabase);

            mStorageProviderId = newProviderId;

            // re-initialize this class with the new Uri
            openOrCreateDataspace();
        } finally {
            unlockWrite();
        }
    }

    public void open() {
        lockWrite();
        try {
@@ -253,7 +203,7 @@ public class LockableDatabase {
    private void openOrCreateDataspace() {
        lockWrite();
        try {
            final File databaseFile = prepareStorage(mStorageProviderId);
            final File databaseFile = prepareStorage();
            try {
                doOpenOrCreateDb(databaseFile);
            } catch (SQLiteException e) {
@@ -276,17 +226,11 @@ public class LockableDatabase {
    }

    private void doOpenOrCreateDb(final File databaseFile) {
        if (StorageManager.InternalStorageProvider.ID.equals(mStorageProviderId)) {
            // internal storage
            mDb = context.openOrCreateDatabase(databaseFile.getName(), Context.MODE_PRIVATE,
                    null);
        } else {
            // external storage
            mDb = SQLiteDatabase.openOrCreateDatabase(databaseFile, null);
        }
        mDb = context.openOrCreateDatabase(databaseFile.getName(), Context.MODE_PRIVATE, null);
    }

    protected File prepareStorage(final String providerId) {
    protected File prepareStorage() {
        String providerId = getStorageProviderId();
        final StorageManager storageManager = getStorageManager();

        final File databaseFile = storageManager.getDatabase(uUid, providerId);
@@ -331,6 +275,7 @@ public class LockableDatabase {
    private void delete(final boolean recreate) {
        lockWrite();
        try {
            String storageProviderId = getStorageProviderId();
            try {
                mDb.close();
            } catch (Exception e) {
@@ -338,7 +283,7 @@ public class LockableDatabase {
            }
            final StorageManager storageManager = getStorageManager();
            try {
                final File attachmentDirectory = storageManager.getAttachmentDirectory(uUid, mStorageProviderId);
                final File attachmentDirectory = storageManager.getAttachmentDirectory(uUid, storageProviderId);
                final File[] attachments = attachmentDirectory.listFiles();
                for (File attachment : attachments) {
                    if (attachment.exists()) {
@@ -358,7 +303,7 @@ public class LockableDatabase {
                Timber.d("Exception caught in clearing attachments: %s", e.getMessage());
            }
            try {
                deleteDatabase(storageManager.getDatabase(uUid, mStorageProviderId));
                deleteDatabase(storageManager.getDatabase(uUid, storageProviderId));
            } catch (Exception e) {
                Timber.i(e, "LockableDatabase: delete(): Unable to delete backing DB file");
            }
+0 −1
Original line number Diff line number Diff line
@@ -37,7 +37,6 @@ val coreNotificationModule = module {
    }
    single {
        AccountPreferenceSerializer(
            storageManager = get(),
            resourceProvider = get(),
            serverSettingsSerializer = get(),
        )
Loading