Get lookup URIs on sync

pull/147/head
M66B 7 years ago
parent ef582f3e99
commit 482875dfcd

@ -638,13 +638,24 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
}.execute(context, owner, aargs, "message:avatar");
} else
bindContactInfo(info, message);
if (message.avatar != null) {
if (autohtml)
properties.setValue("html", message.id, true);
if (autoimages)
properties.setValue("images", message.id, true);
}
private void bindFlagged(TupleMessageEx message) {
int flagged = (message.count - message.unflagged);
ivFlagged.setImageResource(flagged > 0 ? R.drawable.baseline_star_24 : R.drawable.baseline_star_border_24);
ivFlagged.setImageTintList(ColorStateList.valueOf(flagged > 0 ? colorAccent : textColorSecondary));
ivFlagged.setVisibility(message.uid == null ? View.INVISIBLE : View.VISIBLE);
if (viewType == ViewType.THREAD) {
boolean show_expanded = properties.getValue("expanded", message.id);
if (show_expanded)
bindExpanded(message);
else {
properties.setBody(message.id, null);
properties.setHtml(message.id, null);
}
}
}
private void clearExpanded() {
@ -675,6 +686,13 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
rvImage.setVisibility(View.GONE);
}
private void bindFlagged(TupleMessageEx message) {
int flagged = (message.count - message.unflagged);
ivFlagged.setImageResource(flagged > 0 ? R.drawable.baseline_star_24 : R.drawable.baseline_star_border_24);
ivFlagged.setImageTintList(ColorStateList.valueOf(flagged > 0 ? colorAccent : textColorSecondary));
ivFlagged.setVisibility(message.uid == null ? View.INVISIBLE : View.VISIBLE);
}
private void bindContactInfo(ContactInfo info, TupleMessageEx message) {
if (info.hasPhoto())
ivAvatar.setImageBitmap(info.getPhotoBitmap());
@ -682,26 +700,6 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
ivAvatar.setImageResource(R.drawable.baseline_person_24);
ivAvatar.setVisibility(avatars ? View.VISIBLE : View.GONE);
tvFrom.setText(info.getDisplayName(compact));
if (info.hasLookupUri()) {
boolean show_html = properties.getValue("html", message.id);
if (autohtml && !show_html)
properties.setValue("html", message.id, true);
boolean show_images = properties.getValue("images", message.id);
if (autoimages && !show_images)
properties.setValue("images", message.id, true);
}
if (viewType == ViewType.THREAD) {
boolean show_expanded = properties.getValue("expanded", message.id);
if (show_expanded)
bindExpanded(message);
else {
properties.setBody(message.id, null);
properties.setHtml(message.id, null);
}
}
}
private void bindExpanded(final TupleMessageEx message) {

@ -153,4 +153,46 @@ public class ContactInfo {
info.time = new Date().getTime();
return info;
}
static Uri getLookupUri(Context context, Address[] addresses) {
if (addresses == null || addresses.length == 0)
return null;
InternetAddress address = (InternetAddress) addresses[0];
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED)
return null;
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.Contacts.LOOKUP_KEY
},
ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?",
new String[]{
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);
long contactId = cursor.getLong(colContactId);
String lookupKey = cursor.getString(colLookupKey);
return ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
} else
return null;
} finally {
if (cursor != null)
cursor.close();
}
} catch (Throwable ex) {
Log.e(ex);
return null;
}
}
}

@ -91,7 +91,7 @@ public class EntityMessage implements Serializable {
public String deliveredto;
public String inreplyto;
public String thread; // compose = null
public String avatar; // obsolete
public String avatar; // lookup URI from sender
public String sender; // sort key
public Address[] from;
public Address[] to;

@ -490,8 +490,7 @@ public class ServiceSynchronize extends LifecycleService {
// Get contact info
Map<TupleMessageEx, ContactInfo> messageContact = new HashMap<>();
for (TupleMessageEx message : messages)
messageContact.put(message,
ContactInfo.get(this, message.from, false));
messageContact.put(message, ContactInfo.get(this, message.from, false));
// Build pending intent
Intent view = new Intent(this, ActivityView.class);
@ -2673,6 +2672,9 @@ public class ServiceSynchronize extends LifecycleService {
message.ui_ignored = seen;
message.ui_browsed = browsed;
Uri lookupUri = ContactInfo.getLookupUri(context, message.from);
message.avatar = (lookupUri == null ? null : lookupUri.toString());
// Check sender
Address sender = helper.getSender();
if (sender != null && senders.length > 0) {
@ -2747,6 +2749,15 @@ public class ServiceSynchronize extends LifecycleService {
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " unbrowse");
}
if (message.avatar == null) {
Uri lookupUri = ContactInfo.getLookupUri(context, message.from);
if (lookupUri != null) {
update = true;
message.avatar = lookupUri.toString();
Log.i(folder.name + " updated id=" + message.id + " lookup=" + lookupUri);
}
}
if (update)
db.message().updateMessage(message);
}

Loading…
Cancel
Save