Remove saved folder settings

This commit is contained in:
cketti 2020-04-11 00:15:18 +02:00
parent ce29fd20fb
commit bdb501b80d
4 changed files with 57 additions and 10 deletions

View file

@ -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<String, String> 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<String, String> readAllValues(SQLiteDatabase database) {
HashMap<String, String> 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<String, String> 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);

View file

@ -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)
}
}
}
}

View file

@ -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()
}
}

View file

@ -3,6 +3,7 @@ package com.fsck.k9.preferences.migrations
import android.database.sqlite.SQLiteDatabase
interface StorageMigrationsHelper {
fun readAllValues(db: SQLiteDatabase): Map<String, String>
fun readValue(db: SQLiteDatabase, key: String): String?
fun writeValue(db: SQLiteDatabase, key: String, value: String?)
fun insertValue(db: SQLiteDatabase, key: String, value: String?)