From 82b3e0ef58ae788a46b8fe2e4d34d567e5a54f43 Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 20 Sep 2018 11:16:23 +0000 Subject: [PATCH] Fetch contact uri on synchronizing message Refs #117 --- .../eu/faircode/email/AdapterMessage.java | 41 ------------------- .../eu/faircode/email/ServiceSynchronize.java | 36 ++++++++++++++++ 2 files changed, 36 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index 724dbd4d..bdd716ba 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -21,13 +21,11 @@ package eu.faircode.email; import android.Manifest; import android.content.ContentResolver; -import android.content.ContentUris; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; -import android.database.Cursor; import android.graphics.Color; import android.graphics.Typeface; import android.graphics.drawable.Drawable; @@ -36,7 +34,6 @@ import android.os.Bundle; import android.preference.PreferenceManager; import android.provider.ContactsContract; import android.text.format.DateUtils; -import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; @@ -54,9 +51,6 @@ 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; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.PopupMenu; @@ -162,41 +156,6 @@ public class AdapterMessage extends PagedListAdapter 0) { - final long id = message.id; - final Address[] froms = message.from; - executor.submit(new Runnable() { - @Override - public void run() { - try { - 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(); - } - } - } catch (Throwable ex) { - Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); - } - } - }); - } - vwColor.setBackgroundColor(message.accountColor == null ? Color.TRANSPARENT : message.accountColor); vwColor.setVisibility(viewType == ViewType.UNIFIED ? View.VISIBLE : View.GONE); diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 398d7e49..ac2e029d 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -19,14 +19,19 @@ package eu.faircode.email; Copyright 2018 by Marcel Bokhorst (M66B) */ +import android.Manifest; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; +import android.content.ContentResolver; +import android.content.ContentUris; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; +import android.content.pm.PackageManager; +import android.database.Cursor; import android.graphics.drawable.Icon; import android.media.RingtoneManager; import android.net.ConnectivityManager; @@ -39,6 +44,7 @@ import android.os.Build; import android.os.Bundle; import android.os.SystemClock; import android.preference.PreferenceManager; +import android.provider.ContactsContract; import android.text.Html; import android.text.TextUtils; import android.util.Log; @@ -1650,6 +1656,36 @@ public class ServiceSynchronize extends LifecycleService { message.ui_hide = false; message.ui_found = found; + if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS) + == PackageManager.PERMISSION_GRANTED) { + try { + if (message.from != null) + for (Address from : message.from) { + 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); + message.avatar = uri.toString(); + break; + } + } finally { + if (cursor != null) + cursor.close(); + } + } + } catch (Throwable ex) { + Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); + } + } + message.id = db.message().insertMessage(message); Log.i(Helper.TAG, folder.name + " added id=" + message.id + " uid=" + message.uid);