Import K9 identities

This commit is contained in:
M66B 2021-07-29 18:03:20 +02:00
parent ec2ca58edb
commit 134071850a
1 changed files with 85 additions and 34 deletions

View File

@ -38,6 +38,7 @@ import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Pair;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -1202,28 +1203,32 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac
EntityAccount account = null; EntityAccount account = null;
EntityIdentity identity = null; EntityIdentity identity = null;
boolean inIdentity = false;
String iname = null;
String iemail = null;
List<Pair<String, String>> identities = new ArrayList<>();
int eventType = xml.getEventType(); int eventType = xml.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) { while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) { if (eventType == XmlPullParser.START_TAG) {
String name = xml.getName(); String name = xml.getName();
switch (name) { switch (name) {
case "account":
account = new EntityAccount();
account.auth_type = ServiceAuthenticator.AUTH_TYPE_PASSWORD;
account.password = "";
account.synchronize = false;
account.primary = false;
break;
case "incoming-server": case "incoming-server":
String itype = xml.getAttributeValue(null, "type"); if (account != null) {
if ("IMAP".equals(itype)) { String itype = xml.getAttributeValue(null, "type");
account = new EntityAccount(); if ("IMAP".equals(itype))
account.protocol = EntityAccount.TYPE_IMAP; account.protocol = EntityAccount.TYPE_IMAP;
account.auth_type = ServiceAuthenticator.AUTH_TYPE_PASSWORD; else if ("POP3".equals(itype))
account.password = ""; account.protocol = EntityAccount.TYPE_POP;
account.synchronize = false; else
account.primary = false; account = null;
} else if ("POP3".equals(itype)) {
account = new EntityAccount();
account.protocol = EntityAccount.TYPE_POP;
account.auth_type = ServiceAuthenticator.AUTH_TYPE_PASSWORD;
account.password = "";
account.synchronize = false;
account.primary = false;
} }
break; break;
case "outgoing-server": case "outgoing-server":
@ -1275,46 +1280,92 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac
account.encryption = encryption; account.encryption = encryption;
} }
break; break;
case "authentication-type":
eventType = xml.next();
if (eventType != XmlPullParser.TEXT || !"PLAIN".equals(xml.getText())) {
account = null;
identity = null;
}
break;
case "username": case "username":
eventType = xml.next(); eventType = xml.next();
if (eventType == XmlPullParser.TEXT) { if (eventType == XmlPullParser.TEXT) {
String user = xml.getText(); String user = xml.getText();
if (identity != null) { if (identity != null)
identity.name = "K9/" + user;
identity.email = user;
identity.user = user; identity.user = user;
} else if (account != null) { else if (account != null)
account.name = "K9/" + user;
account.user = user; account.user = user;
}
break;
case "name":
eventType = xml.next();
if (eventType == XmlPullParser.TEXT) {
if (inIdentity)
iname = xml.getText();
else {
if (account != null)
account.name = xml.getText();
} }
} }
break; break;
case "email":
eventType = xml.next();
if (eventType == XmlPullParser.TEXT) {
if (inIdentity)
iemail = xml.getText();
}
break;
case "identity":
inIdentity = true;
break;
} }
} else if (eventType == XmlPullParser.END_TAG) { } else if (eventType == XmlPullParser.END_TAG) {
String name = xml.getName(); String name = xml.getName();
if ("account".equals(name)) { switch (name) {
if (account != null && identity != null) { case "account":
try { if (account != null && identity != null) {
db.beginTransaction(); if (TextUtils.isEmpty(account.name))
account.name = account.user;
if (BuildConfig.DEBUG)
account.name = "K9/" + account.name;
account.id = db.account().insertAccount(account); try {
identity.account = account.id; db.beginTransaction();
identity.id = db.identity().insertIdentity(identity);
db.setTransactionSuccessful(); account.id = db.account().insertAccount(account);
} finally { identity.account = account.id;
account = null; for (Pair<String, String> i : identities) {
identity = null; identity.id = null;
db.endTransaction(); identity.name = i.first;
identity.email = i.second;
if (TextUtils.isEmpty(identity.name))
identity.name = identity.user;
if (TextUtils.isEmpty(identity.email))
identity.email = identity.user;
identity.id = db.identity().insertIdentity(identity);
}
db.setTransactionSuccessful();
} finally {
account = null;
identity = null;
identities.clear();
db.endTransaction();
}
} }
} break;
case "identity":
identities.add(new Pair<>(iname, iemail));
iname = null;
iemail = null;
inIdentity = false;
break;
} }
} }
eventType = xml.next(); eventType = xml.next();
} }
} }
return null; return null;