Migrate storeUri/transportUri to new server settings format

This commit is contained in:
cketti 2021-02-01 15:31:36 +01:00
parent 08d6d9bebc
commit 1f2404557b
7 changed files with 81 additions and 9 deletions

View file

@ -21,7 +21,7 @@ import timber.log.Timber;
public class K9StoragePersister implements StoragePersister {
private static final int DB_VERSION = 11;
private static final int DB_VERSION = 12;
private static final String DB_NAME = "preferences_storage";
private final Context context;

View file

@ -0,0 +1,65 @@
package com.fsck.k9.preferences.migrations
import android.database.sqlite.SQLiteDatabase
import com.fsck.k9.ServerSettingsSerializer
import com.fsck.k9.mail.filter.Base64
import com.fsck.k9.preferences.migrations.migration12.ImapStoreUriDecoder
import com.fsck.k9.preferences.migrations.migration12.Pop3StoreUriDecoder
import com.fsck.k9.preferences.migrations.migration12.SmtpTransportUriDecoder
import com.fsck.k9.preferences.migrations.migration12.WebDavStoreUriDecoder
/**
* Convert server settings from the old URI format to the new JSON format
*/
class StorageMigrationTo12(
private val db: SQLiteDatabase,
private val migrationsHelper: StorageMigrationsHelper
) {
private val serverSettingsSerializer = ServerSettingsSerializer()
fun removeStoreAndTransportUri() {
val accountUuidsListValue = migrationsHelper.readValue(db, "accountUuids")
if (accountUuidsListValue == null || accountUuidsListValue.isEmpty()) {
return
}
val accountUuids = accountUuidsListValue.split(",")
for (accountUuid in accountUuids) {
convertStoreUri(accountUuid)
convertTransportUri(accountUuid)
}
}
private fun convertStoreUri(accountUuid: String) {
val storeUri = migrationsHelper.readValue(db, "$accountUuid.storeUri")?.base64Decode() ?: return
val serverSettings = when {
storeUri.startsWith("imap") -> ImapStoreUriDecoder.decode(storeUri)
storeUri.startsWith("pop3") -> Pop3StoreUriDecoder.decode(storeUri)
storeUri.startsWith("webdav") -> WebDavStoreUriDecoder.decode(storeUri)
else -> error("Unsupported account type")
}
val json = serverSettingsSerializer.serialize(serverSettings)
migrationsHelper.insertValue(db, "$accountUuid.incomingServerSettings", json)
migrationsHelper.writeValue(db, "$accountUuid.storeUri", null)
}
private fun convertTransportUri(accountUuid: String) {
val transportUri = migrationsHelper.readValue(db, "$accountUuid.transportUri")?.base64Decode() ?: return
val serverSettings = when {
transportUri.startsWith("smtp") -> SmtpTransportUriDecoder.decodeSmtpUri(transportUri)
transportUri.startsWith("webdav") -> WebDavStoreUriDecoder.decode(transportUri)
else -> error("Unsupported account type")
}
val json = serverSettingsSerializer.serialize(serverSettings)
migrationsHelper.insertValue(db, "$accountUuid.outgoingServerSettings", json)
migrationsHelper.writeValue(db, "$accountUuid.transportUri", null)
}
private fun String.base64Decode() = Base64.decode(this)
}

View file

@ -17,5 +17,6 @@ internal object StorageMigrations {
if (oldVersion < 9) StorageMigrationTo9(db, migrationsHelper).disablePush()
if (oldVersion < 10) StorageMigrationTo10(db, migrationsHelper).removeSavedFolderSettings()
if (oldVersion < 11) StorageMigrationTo11(db, migrationsHelper).upgradeMessageViewContentFontSize()
if (oldVersion < 12) StorageMigrationTo12(db, migrationsHelper).removeStoreAndTransportUri()
}
}

View file

@ -1,14 +1,14 @@
package com.fsck.k9.backend.imap;
package com.fsck.k9.preferences.migrations.migration12;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import com.fsck.k9.mail.AuthType;
import com.fsck.k9.mail.ConnectionSecurity;
import com.fsck.k9.mail.ServerSettings;
import com.fsck.k9.mail.store.imap.ImapStoreSettings;
import static com.fsck.k9.mail.helper.UrlEncodingHelper.decodeUtf8;
@ -136,7 +136,9 @@ public class ImapStoreUriDecoder {
}
}
Map<String, String> extra = ImapStoreSettings.createExtra(autoDetectNamespace, pathPrefix);
Map<String, String> extra = new HashMap<>();
extra.put("autoDetectNamespace", Boolean.toString(autoDetectNamespace));
extra.put("pathPrefix", pathPrefix);
return new ServerSettings("imap", host, port, connectionSecurity, authenticationType, username,
password, clientCertificateAlias, extra);

View file

@ -1,4 +1,4 @@
package com.fsck.k9.backend.pop3;
package com.fsck.k9.preferences.migrations.migration12;
import java.net.URI;

View file

@ -1,4 +1,4 @@
package com.fsck.k9.mail.transport.smtp;
package com.fsck.k9.preferences.migrations.migration12;
import java.io.UnsupportedEncodingException;

View file

@ -1,12 +1,12 @@
package com.fsck.k9.backend.webdav;
package com.fsck.k9.preferences.migrations.migration12;
import com.fsck.k9.mail.ConnectionSecurity;
import com.fsck.k9.mail.ServerSettings;
import com.fsck.k9.mail.store.webdav.WebDavStoreSettings;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import static com.fsck.k9.mail.helper.UrlEncodingHelper.decodeUtf8;
@ -108,7 +108,11 @@ public class WebDavStoreUriDecoder {
}
}
Map<String, String> extra = WebDavStoreSettings.createExtra(alias, path, authPath, mailboxPath);
Map<String, String> extra = new HashMap<>();
extra.put("alias", alias);
extra.put("path", path);
extra.put("authPath", authPath);
extra.put("mailboxPath", mailboxPath);
return new ServerSettings("webdav", host, port, connectionSecurity, null, username, password, null, extra);
}