Add/update contacts in transaction

This commit is contained in:
M66B 2019-05-13 13:25:57 +02:00
parent be8c5a99e3
commit 102838c007
1 changed files with 33 additions and 28 deletions

View File

@ -1484,10 +1484,10 @@ class Core {
}
}
private static void updateContactInfo(Context context, EntityFolder folder, EntityMessage message) {
DB db = DB.getInstance(context);
private static void updateContactInfo(Context context, final EntityFolder folder, final EntityMessage message) {
final DB db = DB.getInstance(context);
int type = (folder.isOutgoing() ? EntityContact.TYPE_TO : EntityContact.TYPE_FROM);
final int type = (folder.isOutgoing() ? EntityContact.TYPE_TO : EntityContact.TYPE_FROM);
Address[] recipients = (type == EntityContact.TYPE_TO
? message.to
: (message.reply != null ? message.reply : message.from));
@ -1512,31 +1512,36 @@ class Core {
if (recipients != null) {
for (Address recipient : recipients) {
String email = ((InternetAddress) recipient).getAddress();
String name = ((InternetAddress) recipient).getPersonal();
Uri avatar = ContactInfo.getLookupUri(context, new Address[]{recipient});
EntityContact contact = db.contact().getContact(folder.account, type, email);
if (contact == null) {
contact = new EntityContact();
contact.account = folder.account;
contact.type = type;
contact.email = email;
contact.name = name;
contact.avatar = (avatar == null ? null : avatar.toString());
contact.times_contacted = 1;
contact.first_contacted = message.received;
contact.last_contacted = message.received;
contact.id = db.contact().insertContact(contact);
Log.i("Inserted contact=" + contact + " type=" + type);
} else {
contact.name = name;
contact.avatar = (avatar == null ? null : avatar.toString());
contact.times_contacted++;
contact.first_contacted = Math.min(contact.first_contacted, message.received);
contact.last_contacted = message.received;
db.contact().updateContact(contact);
Log.i("Updated contact=" + contact + " type=" + type);
}
final String email = ((InternetAddress) recipient).getAddress();
final String name = ((InternetAddress) recipient).getPersonal();
final Uri avatar = ContactInfo.getLookupUri(context, new Address[]{recipient});
db.runInTransaction(new Runnable() {
@Override
public void run() {
EntityContact contact = db.contact().getContact(folder.account, type, email);
if (contact == null) {
contact = new EntityContact();
contact.account = folder.account;
contact.type = type;
contact.email = email;
contact.name = name;
contact.avatar = (avatar == null ? null : avatar.toString());
contact.times_contacted = 1;
contact.first_contacted = message.received;
contact.last_contacted = message.received;
contact.id = db.contact().insertContact(contact);
Log.i("Inserted contact=" + contact + " type=" + type);
} else {
contact.name = name;
contact.avatar = (avatar == null ? null : avatar.toString());
contact.times_contacted++;
contact.first_contacted = Math.min(contact.first_contacted, message.received);
contact.last_contacted = message.received;
db.contact().updateContact(contact);
Log.i("Updated contact=" + contact + " type=" + type);
}
}
});
}
}
}