From fd0f4869eb8d7172615668e618929c532424249c Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 12 Sep 2018 18:12:35 +0000 Subject: [PATCH] Search all from addresses for contact photo --- .../eu/faircode/email/AdapterMessage.java | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index 05525154..187ae80e 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -54,6 +54,7 @@ import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import javax.mail.Address; import javax.mail.internet.InternetAddress; import androidx.annotation.NonNull; @@ -153,27 +154,31 @@ public class AdapterMessage extends PagedListAdapter 0) { final long id = message.id; - final String email = ((InternetAddress) message.from[0]).getAddress(); + final Address[] froms = message.from; executor.submit(new Runnable() { @Override public void run() { try { - Cursor cursor = null; - try { - ContentResolver resolver = context.getContentResolver(); - cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, - new String[]{ContactsContract.CommonDataKinds.Photo.CONTACT_ID}, - ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?", - new String[]{email}, null); - if (cursor.moveToNext()) { - int colContactId = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID); - long contactId = cursor.getLong(colContactId); - Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId); - DB.getInstance(context).message().setMessageAvatar(id, uri.toString()); + for (Address from : froms) { + String email = ((InternetAddress) from).getAddress(); + Cursor cursor = null; + try { + ContentResolver resolver = context.getContentResolver(); + cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, + new String[]{ContactsContract.CommonDataKinds.Photo.CONTACT_ID}, + ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?", + new String[]{email}, null); + if (cursor.moveToNext()) { + int colContactId = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID); + long contactId = cursor.getLong(colContactId); + Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId); + DB.getInstance(context).message().setMessageAvatar(id, uri.toString()); + break; + } + } finally { + if (cursor != null) + cursor.close(); } - } finally { - if (cursor != null) - cursor.close(); } } catch (Throwable ex) { Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));