From 7b74f87e0f62899fb73d89e912f4caf1bf10a10f Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 8 Sep 2018 14:57:03 +0000 Subject: [PATCH] Show contact photos Fixes #61 --- FAQ.md | 2 +- .../eu/faircode/email/AdapterMessage.java | 64 +++++++++++++++++++ app/src/main/res/layout/item_message.xml | 13 +++- app/src/main/res/values/strings.xml | 2 +- 4 files changed, 78 insertions(+), 3 deletions(-) diff --git a/FAQ.md b/FAQ.md index 4510597b..e2ecdb1f 100644 --- a/FAQ.md +++ b/FAQ.md @@ -14,7 +14,7 @@ If you have a question, please check the frequently asked questions below first. * Run at startup (RECEIVE_BOOT_COMPLETED): to start monitoring on device start * In-app billing (BILLING): to allow in-app purchases * Foreground service (FOREGROUND_SERVICE): to run a foreground service on Android 9 Pie and later, see also the next question -* Optional: read your contacts (READ_CONTACTS): to autocomplete addresses +* Optional: read your contacts (READ_CONTACTS): to autocomplete addresses and to show photos * Optional: find accounts on the device (GET_ACCOUNTS): to use [OAuth](https://en.wikipedia.org/wiki/OAuth) instead of passwords diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index 1e48c93f..a6df66fe 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -19,11 +19,19 @@ package eu.faircode.email; Copyright 2018 by Marcel Bokhorst (M66B) */ +import android.Manifest; +import android.content.ContentResolver; +import android.content.ContentUris; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; +import android.database.Cursor; import android.graphics.Typeface; +import android.graphics.drawable.Drawable; +import android.net.Uri; import android.os.Bundle; import android.preference.PreferenceManager; +import android.provider.ContactsContract; import android.text.format.DateUtils; import android.view.LayoutInflater; import android.view.Menu; @@ -35,13 +43,17 @@ import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; +import java.io.InputStream; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; +import javax.mail.internet.InternetAddress; + import androidx.annotation.NonNull; import androidx.appcompat.widget.PopupMenu; +import androidx.core.content.ContextCompat; import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.Observer; import androidx.localbroadcastmanager.content.LocalBroadcastManager; @@ -55,6 +67,7 @@ public class AdapterMessage extends PagedListAdapter 0) { + itemView.setHasTransientState(true); + + Bundle args = new Bundle(); + args.putSerializable("from", message.from[0]); + + new SimpleTask() { + @Override + protected Drawable onLoad(Context context, Bundle args) { + Cursor cursor = null; + try { + InternetAddress from = (InternetAddress) args.getSerializable("from"); + 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[]{from.getAddress()}, 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); + InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri); + return Drawable.createFromStream(is, from.getPersonal()); + } + } finally { + if (cursor != null) + cursor.close(); + } + return null; + } + + @Override + protected void onLoaded(Bundle args, Drawable photo) { + if (photo != null) { + ivAvatar.setImageDrawable(photo); + ivAvatar.setVisibility(View.VISIBLE); + } + itemView.setHasTransientState(false); + } + + @Override + protected void onException(Bundle args, Throwable ex) { + itemView.setHasTransientState(false); + } + }.load(context, owner, args); + } + ivFlagged.setVisibility(message.ui_flagged ? View.VISIBLE : View.GONE); if (EntityFolder.DRAFTS.equals(message.folderType) || @@ -276,6 +339,7 @@ public class AdapterMessage extends PagedListAdapter + + In the next dialog, select \"All apps\" at the top, select this app and select and confirm \"Don\'t optimize\" Disable data saving Grant permissions - To autocomplete addresses (optional) + To get contact information (optional) To do Done Light theme