From 05a03e11eae228a5889e26cf2e183935b25135e6 Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 21 Jan 2019 18:51:53 +0000 Subject: [PATCH] Small improvement --- .../eu/faircode/email/AdapterMessage.java | 8 +-- .../java/eu/faircode/email/ContactInfo.java | 66 +++++++++---------- 2 files changed, 36 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index 43ea17382a..47f182903a 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -469,6 +469,8 @@ public class AdapterMessage extends RecyclerView.Adapter 0 ? colorAccent : textColorSecondary)); ivFlagged.setVisibility(message.uid == null ? View.INVISIBLE : View.VISIBLE); - tvFrom.setText(MessageHelper.formatAddresses(outgoing ? message.to : message.from, !compact, false)); tvSize.setText(message.size == null ? null : Helper.humanReadableByteCount(message.size, true)); tvSize.setVisibility(message.size == null || message.content ? View.GONE : View.VISIBLE); tvTime.setText(tf.format(message.received)); diff --git a/app/src/main/java/eu/faircode/email/ContactInfo.java b/app/src/main/java/eu/faircode/email/ContactInfo.java index 689ae9441b..c1ef892b59 100644 --- a/app/src/main/java/eu/faircode/email/ContactInfo.java +++ b/app/src/main/java/eu/faircode/email/ContactInfo.java @@ -71,7 +71,7 @@ public class ContactInfo { } static ContactInfo get(Context context, Address[] addresses) { - if (addresses == null) + if (addresses == null || addresses.length == 0) return null; if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS) @@ -79,40 +79,38 @@ public class ContactInfo { return null; try { - for (Address address : addresses) { - Cursor cursor = null; - try { - ContentResolver resolver = context.getContentResolver(); - cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, - new String[]{ - ContactsContract.CommonDataKinds.Photo.CONTACT_ID, - ContactsContract.Contacts.LOOKUP_KEY, - ContactsContract.Contacts.DISPLAY_NAME - }, - ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?", - new String[]{ - ((InternetAddress) address).getAddress() - }, null); - - if (cursor != null && cursor.moveToNext()) { - int colContactId = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID); - int colLookupKey = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY); - int colDisplayName = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); - - long contactId = cursor.getLong(colContactId); - String lookupKey = cursor.getString(colLookupKey); - Uri lookupUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey); - - ContactInfo info = new ContactInfo(); - info.is = ContactsContract.Contacts.openContactPhotoInputStream(resolver, lookupUri); - info.displayName = cursor.getString(colDisplayName); - info.lookupUri = lookupUri; - return info; - } - } finally { - if (cursor != null) - cursor.close(); + Cursor cursor = null; + try { + ContentResolver resolver = context.getContentResolver(); + cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, + new String[]{ + ContactsContract.CommonDataKinds.Photo.CONTACT_ID, + ContactsContract.Contacts.LOOKUP_KEY, + ContactsContract.Contacts.DISPLAY_NAME + }, + ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?", + new String[]{ + ((InternetAddress) addresses[0]).getAddress() + }, null); + + if (cursor != null && cursor.moveToNext()) { + int colContactId = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID); + int colLookupKey = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY); + int colDisplayName = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); + + long contactId = cursor.getLong(colContactId); + String lookupKey = cursor.getString(colLookupKey); + Uri lookupUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey); + + ContactInfo info = new ContactInfo(); + info.is = ContactsContract.Contacts.openContactPhotoInputStream(resolver, lookupUri); + info.displayName = cursor.getString(colDisplayName); + info.lookupUri = lookupUri; + return info; } + } finally { + if (cursor != null) + cursor.close(); } } catch (Throwable ex) { Log.e(ex);