From bdb501b80d597203a9b3cff9b0b0f4653c8510d4 Mon Sep 17 00:00:00 2001 From: cketti Date: Sat, 11 Apr 2020 00:15:18 +0200 Subject: [PATCH] Remove saved folder settings --- .../k9/preferences/K9StoragePersister.java | 28 +++++++++----- .../migrations/StorageMigrationTo10.kt | 37 +++++++++++++++++++ .../migrations/StorageMigrations.kt | 1 + .../migrations/StorageMigrationsHelper.kt | 1 + 4 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 app/storage/src/main/java/com/fsck/k9/preferences/migrations/StorageMigrationTo10.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 01a3ff69c..88b32c564 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 = 9; + private static final int DB_VERSION = 10; private static final String DB_NAME = "preferences_storage"; private final Context context; @@ -141,13 +141,20 @@ public class K9StoragePersister implements StoragePersister { public Map loadValues() { long startTime = SystemClock.elapsedRealtime(); Timber.i("Loading preferences from DB into Storage"); + + try (SQLiteDatabase database = openDB()) { + return readAllValues(database); + } finally { + long endTime = SystemClock.elapsedRealtime(); + Timber.i("Preferences load took %d ms", endTime - startTime); + } + } + + private Map readAllValues(SQLiteDatabase database) { HashMap loadedValues = new HashMap<>(); Cursor cursor = null; - SQLiteDatabase mDb = null; try { - mDb = openDB(); - - cursor = mDb.rawQuery("SELECT primkey, value FROM preferences_storage", null); + cursor = database.rawQuery("SELECT primkey, value FROM preferences_storage", null); while (cursor.moveToNext()) { String key = cursor.getString(0); String value = cursor.getString(1); @@ -156,11 +163,6 @@ public class K9StoragePersister implements StoragePersister { } } finally { Utility.closeQuietly(cursor); - if (mDb != null) { - mDb.close(); - } - long endTime = SystemClock.elapsedRealtime(); - Timber.i("Preferences load took %d ms", endTime - startTime); } return loadedValues; @@ -229,6 +231,12 @@ public class K9StoragePersister implements StoragePersister { K9StoragePersister.this.writeValue(db, key, value); } + @NotNull + @Override + public Map readAllValues(@NotNull SQLiteDatabase db) { + return K9StoragePersister.this.readAllValues(db); + } + @Override public String readValue(@NotNull SQLiteDatabase db, @NotNull String key) { return K9StoragePersister.this.readValue(db, key); diff --git a/app/storage/src/main/java/com/fsck/k9/preferences/migrations/StorageMigrationTo10.kt b/app/storage/src/main/java/com/fsck/k9/preferences/migrations/StorageMigrationTo10.kt new file mode 100644 index 000000000..aa7f134f7 --- /dev/null +++ b/app/storage/src/main/java/com/fsck/k9/preferences/migrations/StorageMigrationTo10.kt @@ -0,0 +1,37 @@ +package com.fsck.k9.preferences.migrations + +import android.database.sqlite.SQLiteDatabase + +/** + * Remove saved folder settings + * + * Folder settings are now only written to 'Storage' when settings are imported. The saved settings will be used when + * folders are first created and then the saved settings are discarded. + * Since this wasn't the procedure in earlier app versions, we remove any saved folder settings in this migration. + */ +class StorageMigrationTo10( + private val db: SQLiteDatabase, + private val migrationsHelper: StorageMigrationsHelper +) { + private val folderKeyPattern = Regex("[^.]+\\..+\\.([^.]+)") + private val folderSettingKeys = setOf( + "displayMode", + "notifyMode", + "syncMode", + "pushMode", + "inTopGroup", + "integrate" + ) + + fun removeSavedFolderSettings() { + val loadedValues = migrationsHelper.readAllValues(db) + + for (key in loadedValues.keys) { + val matches = folderKeyPattern.matchEntire(key) ?: continue + val folderKey = matches.groupValues[1] + if (folderKey in folderSettingKeys) { + migrationsHelper.writeValue(db, key, 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 aefc9c9b6..2b77b029f 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 @@ -15,5 +15,6 @@ internal object StorageMigrations { if (oldVersion < 7) StorageMigrationTo7(db, migrationsHelper).rewriteEnumOrdinalsToNames() if (oldVersion < 8) StorageMigrationTo8(db, migrationsHelper).rewriteTheme() if (oldVersion < 9) StorageMigrationTo9(db, migrationsHelper).disablePush() + if (oldVersion < 10) StorageMigrationTo10(db, migrationsHelper).removeSavedFolderSettings() } } diff --git a/app/storage/src/main/java/com/fsck/k9/preferences/migrations/StorageMigrationsHelper.kt b/app/storage/src/main/java/com/fsck/k9/preferences/migrations/StorageMigrationsHelper.kt index 652dd9a11..9365516b8 100644 --- a/app/storage/src/main/java/com/fsck/k9/preferences/migrations/StorageMigrationsHelper.kt +++ b/app/storage/src/main/java/com/fsck/k9/preferences/migrations/StorageMigrationsHelper.kt @@ -3,6 +3,7 @@ package com.fsck.k9.preferences.migrations import android.database.sqlite.SQLiteDatabase interface StorageMigrationsHelper { + fun readAllValues(db: SQLiteDatabase): Map fun readValue(db: SQLiteDatabase, key: String): String? fun writeValue(db: SQLiteDatabase, key: String, value: String?) fun insertValue(db: SQLiteDatabase, key: String, value: String?)