From 102838c007111cde4da6d53ecfbc94a81508db6e Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 13 May 2019 13:25:57 +0200 Subject: [PATCH] Add/update contacts in transaction --- app/src/main/java/eu/faircode/email/Core.java | 61 ++++++++++--------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index c9fdcd9a32..9184910e8b 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -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); + } + } + }); } } }