Browse Source

Fetch contact uri on synchronizing message

Refs #117
main
M66B 6 years ago
parent
commit
82b3e0ef58
2 changed files with 36 additions and 41 deletions
  1. +0
    -41
      app/src/main/java/eu/faircode/email/AdapterMessage.java
  2. +36
    -0
      app/src/main/java/eu/faircode/email/ServiceSynchronize.java

+ 0
- 41
app/src/main/java/eu/faircode/email/AdapterMessage.java View File

@ -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<TupleMessageEx, AdapterMess
}
ivAvatar.setVisibility(photo ? View.VISIBLE : View.GONE);
if (avatars && message.from != null && message.from.length > 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);


+ 36
- 0
app/src/main/java/eu/faircode/email/ServiceSynchronize.java View File

@ -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);


Loading…
Cancel
Save