mirror of
https://github.com/oxen-io/session-android.git
synced 2025-10-25 05:39:18 +00:00
Use Glide for all contact photo caching
// FREEBIE
This commit is contained in:
@@ -18,6 +18,7 @@ package org.thoughtcrime.securesms.recipients;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
@@ -26,7 +27,8 @@ import android.util.Log;
|
||||
import org.thoughtcrime.securesms.color.MaterialColor;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactColors;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactPhoto;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactPhotoFactory;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.FallbackContactPhoto;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.TransparentContactPhoto;
|
||||
import org.thoughtcrime.securesms.database.Address;
|
||||
import org.thoughtcrime.securesms.database.GroupDatabase;
|
||||
import org.thoughtcrime.securesms.database.RecipientDatabase.RecipientSettings;
|
||||
@@ -62,15 +64,16 @@ public class Recipient implements RecipientModifiedListener {
|
||||
private boolean stale;
|
||||
private boolean resolving;
|
||||
|
||||
private ContactPhoto contactPhoto;
|
||||
private Uri contactUri;
|
||||
private @Nullable Uri ringtone = null;
|
||||
private long mutedUntil = 0;
|
||||
private boolean blocked = false;
|
||||
private VibrateState vibrate = VibrateState.DEFAULT;
|
||||
private int expireMessages = 0;
|
||||
private Optional<Integer> defaultSubscriptionId = Optional.absent();
|
||||
private @NonNull RegisteredState registered = RegisteredState.UNKNOWN;
|
||||
private @Nullable ContactPhoto contactPhoto;
|
||||
private @NonNull FallbackContactPhoto fallbackContactPhoto;
|
||||
private Uri contactUri;
|
||||
private @Nullable Uri ringtone = null;
|
||||
private long mutedUntil = 0;
|
||||
private boolean blocked = false;
|
||||
private VibrateState vibrate = VibrateState.DEFAULT;
|
||||
private int expireMessages = 0;
|
||||
private Optional<Integer> defaultSubscriptionId = Optional.absent();
|
||||
private @NonNull RegisteredState registered = RegisteredState.UNKNOWN;
|
||||
|
||||
private @Nullable MaterialColor color;
|
||||
private boolean seenInviteReminder;
|
||||
@@ -101,15 +104,16 @@ public class Recipient implements RecipientModifiedListener {
|
||||
@NonNull Optional<RecipientDetails> details,
|
||||
@NonNull ListenableFutureTask<RecipientDetails> future)
|
||||
{
|
||||
this.address = address;
|
||||
this.contactPhoto = ContactPhotoFactory.getLoadingPhoto();
|
||||
this.color = null;
|
||||
this.resolving = true;
|
||||
this.address = address;
|
||||
this.fallbackContactPhoto = new TransparentContactPhoto();
|
||||
this.color = null;
|
||||
this.resolving = true;
|
||||
|
||||
if (stale != null) {
|
||||
this.name = stale.name;
|
||||
this.contactUri = stale.contactUri;
|
||||
this.contactPhoto = stale.contactPhoto;
|
||||
this.fallbackContactPhoto = stale.fallbackContactPhoto;
|
||||
this.color = stale.color;
|
||||
this.customLabel = stale.customLabel;
|
||||
this.ringtone = stale.ringtone;
|
||||
@@ -132,6 +136,7 @@ public class Recipient implements RecipientModifiedListener {
|
||||
if (details.isPresent()) {
|
||||
this.name = details.get().name;
|
||||
this.contactPhoto = details.get().avatar;
|
||||
this.fallbackContactPhoto = details.get().fallbackAvatar;
|
||||
this.color = details.get().color;
|
||||
this.ringtone = details.get().ringtone;
|
||||
this.mutedUntil = details.get().mutedUntil;
|
||||
@@ -158,6 +163,7 @@ public class Recipient implements RecipientModifiedListener {
|
||||
Recipient.this.name = result.name;
|
||||
Recipient.this.contactUri = result.contactUri;
|
||||
Recipient.this.contactPhoto = result.avatar;
|
||||
Recipient.this.fallbackContactPhoto = result.fallbackAvatar;
|
||||
Recipient.this.color = result.color;
|
||||
Recipient.this.customLabel = result.customLabel;
|
||||
Recipient.this.ringtone = result.ringtone;
|
||||
@@ -202,6 +208,7 @@ public class Recipient implements RecipientModifiedListener {
|
||||
this.contactUri = details.contactUri;
|
||||
this.name = details.name;
|
||||
this.contactPhoto = details.avatar;
|
||||
this.fallbackContactPhoto = details.fallbackAvatar;
|
||||
this.color = details.color;
|
||||
this.customLabel = details.customLabel;
|
||||
this.ringtone = details.ringtone;
|
||||
@@ -345,7 +352,15 @@ public class Recipient implements RecipientModifiedListener {
|
||||
return (getName() == null ? address.serialize() : getName());
|
||||
}
|
||||
|
||||
public synchronized @NonNull ContactPhoto getContactPhoto() {
|
||||
public synchronized @NonNull Drawable getFallbackContactPhotoDrawable(Context context, boolean inverted) {
|
||||
return getFallbackContactPhoto().asDrawable(context, getColor().toConversationColor(context), inverted);
|
||||
}
|
||||
|
||||
public synchronized @NonNull FallbackContactPhoto getFallbackContactPhoto() {
|
||||
return fallbackContactPhoto;
|
||||
}
|
||||
|
||||
public synchronized @Nullable ContactPhoto getContactPhoto() {
|
||||
return contactPhoto;
|
||||
}
|
||||
|
||||
@@ -361,7 +376,7 @@ public class Recipient implements RecipientModifiedListener {
|
||||
return ringtone;
|
||||
}
|
||||
|
||||
public void setRingtone(Uri ringtone) {
|
||||
public void setRingtone(@Nullable Uri ringtone) {
|
||||
synchronized (this) {
|
||||
this.ringtone = ringtone;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/*
|
||||
* Copyright (C) 2011 Whisper Systems
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
@@ -29,11 +29,16 @@ import android.util.Log;
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.color.MaterialColor;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactPhoto;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactPhotoFactory;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.FallbackContactPhoto;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.GeneratedContactPhoto;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.GroupRecordContactPhoto;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ProfileContactPhoto;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ResourceContactPhoto;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.SystemContactPhoto;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.TransparentContactPhoto;
|
||||
import org.thoughtcrime.securesms.database.Address;
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord;
|
||||
import org.thoughtcrime.securesms.database.RecipientDatabase;
|
||||
import org.thoughtcrime.securesms.database.RecipientDatabase.RecipientSettings;
|
||||
import org.thoughtcrime.securesms.database.RecipientDatabase.RegisteredState;
|
||||
import org.thoughtcrime.securesms.database.RecipientDatabase.VibrateState;
|
||||
@@ -61,13 +66,12 @@ class RecipientProvider {
|
||||
PhoneLookup.LOOKUP_KEY,
|
||||
PhoneLookup._ID,
|
||||
PhoneLookup.NUMBER,
|
||||
PhoneLookup.LABEL
|
||||
PhoneLookup.LABEL,
|
||||
PhoneLookup.PHOTO_URI
|
||||
};
|
||||
|
||||
private static final Map<String, RecipientDetails> STATIC_DETAILS = new HashMap<String, RecipientDetails>() {{
|
||||
put("262966", new RecipientDetails("Amazon", null, null,
|
||||
ContactPhotoFactory.getResourceContactPhoto(R.drawable.ic_amazon),
|
||||
false, null, null));
|
||||
put("262966", new RecipientDetails("Amazon", null, null, null, new ResourceContactPhoto(R.drawable.ic_amazon), false, null, null));
|
||||
}};
|
||||
|
||||
@NonNull Recipient getRecipient(Context context, Address address, Optional<RecipientSettings> settings, Optional<GroupRecord> groupRecord, boolean asynchronous) {
|
||||
@@ -100,7 +104,7 @@ class RecipientProvider {
|
||||
if (address.isGroup() && settings.isPresent() && groupRecord.isPresent()) {
|
||||
return Optional.of(getGroupRecipientDetails(context, address, groupRecord, settings, true));
|
||||
} else if (!address.isGroup() && settings.isPresent()) {
|
||||
return Optional.of(new RecipientDetails(null, null, null, ContactPhotoFactory.getLoadingPhoto(), !TextUtils.isEmpty(settings.get().getSystemDisplayName()), settings.get(), null));
|
||||
return Optional.of(new RecipientDetails(null, null, null, null, new TransparentContactPhoto(), !TextUtils.isEmpty(settings.get().getSystemDisplayName()), settings.get(), null));
|
||||
}
|
||||
|
||||
return Optional.absent();
|
||||
@@ -126,10 +130,17 @@ class RecipientProvider {
|
||||
}
|
||||
|
||||
private @NonNull RecipientDetails getIndividualRecipientDetails(Context context, @NonNull Address address, Optional<RecipientSettings> settings) {
|
||||
ContactPhoto contactPhoto = null;
|
||||
FallbackContactPhoto fallbackContactPhoto = new GeneratedContactPhoto("#");
|
||||
|
||||
if (!settings.isPresent()) {
|
||||
settings = DatabaseFactory.getRecipientDatabase(context).getRecipientSettings(address);
|
||||
}
|
||||
|
||||
if (settings.isPresent() && !TextUtils.isEmpty(settings.get().getProfileAvatar())) {
|
||||
contactPhoto = new ProfileContactPhoto(address, settings.get().getProfileAvatar());
|
||||
}
|
||||
|
||||
if (address.isPhone() && !TextUtils.isEmpty(address.toPhoneString())) {
|
||||
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address.toPhoneString()));
|
||||
Cursor cursor = context.getContentResolver().query(uri, CALLER_ID_PROJECTION, null, null, null);
|
||||
@@ -138,14 +149,19 @@ class RecipientProvider {
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
final String resultNumber = cursor.getString(3);
|
||||
if (resultNumber != null) {
|
||||
Uri contactUri = Contacts.getLookupUri(cursor.getLong(2), cursor.getString(1));
|
||||
String name = resultNumber.equals(cursor.getString(0)) ? null : cursor.getString(0);
|
||||
ContactPhoto contactPhoto = ContactPhotoFactory.getContactPhoto(context,
|
||||
Uri.withAppendedPath(Contacts.CONTENT_URI, cursor.getLong(2) + ""),
|
||||
address,
|
||||
name);
|
||||
Uri contactUri = Contacts.getLookupUri(cursor.getLong(2), cursor.getString(1));
|
||||
String name = resultNumber.equals(cursor.getString(0)) ? null : cursor.getString(0);
|
||||
String photoUri = cursor.getString(5);
|
||||
|
||||
return new RecipientDetails(cursor.getString(0), cursor.getString(4), contactUri, contactPhoto, true, settings.orNull(), null);
|
||||
if (!TextUtils.isEmpty(photoUri)) {
|
||||
contactPhoto = new SystemContactPhoto(address, Uri.parse(photoUri), 0);
|
||||
}
|
||||
|
||||
if (!TextUtils.isEmpty(name)) {
|
||||
fallbackContactPhoto = new GeneratedContactPhoto(name);
|
||||
}
|
||||
|
||||
return new RecipientDetails(cursor.getString(0), cursor.getString(4), contactUri, contactPhoto, fallbackContactPhoto, true, settings.orNull(), null);
|
||||
} else {
|
||||
Log.w(TAG, "resultNumber is null");
|
||||
}
|
||||
@@ -157,10 +173,13 @@ class RecipientProvider {
|
||||
}
|
||||
|
||||
if (STATIC_DETAILS.containsKey(address.serialize())) return STATIC_DETAILS.get(address.serialize());
|
||||
else return new RecipientDetails(null, null, null, ContactPhotoFactory.getSignalAvatarContactPhoto(context, address, null, context.getResources().getDimensionPixelSize(R.dimen.contact_photo_target_size)), false, settings.orNull(), null);
|
||||
else return new RecipientDetails(null, null, null, contactPhoto, fallbackContactPhoto, false, settings.orNull(), null);
|
||||
}
|
||||
|
||||
private @NonNull RecipientDetails getGroupRecipientDetails(Context context, Address groupId, Optional<GroupRecord> groupRecord, Optional<RecipientSettings> settings, boolean asynchronous) {
|
||||
ContactPhoto contactPhoto = null;
|
||||
FallbackContactPhoto fallbackContactPhoto = new ResourceContactPhoto(R.drawable.ic_group_white_24dp, R.drawable.ic_group_large);
|
||||
|
||||
if (!groupRecord.isPresent()) {
|
||||
groupRecord = DatabaseFactory.getGroupDatabase(context).getGroup(groupId.toGroupString());
|
||||
}
|
||||
@@ -170,7 +189,6 @@ class RecipientProvider {
|
||||
}
|
||||
|
||||
if (groupRecord.isPresent()) {
|
||||
ContactPhoto contactPhoto = ContactPhotoFactory.getGroupContactPhoto(groupRecord.get().getAvatar());
|
||||
String title = groupRecord.get().getTitle();
|
||||
List<Address> memberAddresses = groupRecord.get().getMembers();
|
||||
List<Recipient> members = new LinkedList<>();
|
||||
@@ -183,40 +201,47 @@ class RecipientProvider {
|
||||
title = context.getString(R.string.RecipientProvider_unnamed_group);;
|
||||
}
|
||||
|
||||
return new RecipientDetails(title, null, null, contactPhoto, false, settings.orNull(), members);
|
||||
if (groupRecord.get().getAvatar() != null) {
|
||||
contactPhoto = new GroupRecordContactPhoto(groupId, groupRecord.get().getAvatarId());
|
||||
}
|
||||
|
||||
return new RecipientDetails(title, null, null, contactPhoto, fallbackContactPhoto, false, settings.orNull(), members);
|
||||
}
|
||||
|
||||
return new RecipientDetails(context.getString(R.string.RecipientProvider_unnamed_group), null, null, ContactPhotoFactory.getDefaultGroupPhoto(), false, settings.orNull(), null);
|
||||
return new RecipientDetails(context.getString(R.string.RecipientProvider_unnamed_group), null, null, contactPhoto, fallbackContactPhoto, false, settings.orNull(), null);
|
||||
}
|
||||
|
||||
static class RecipientDetails {
|
||||
@Nullable public final String name;
|
||||
@Nullable public final String customLabel;
|
||||
@NonNull public final ContactPhoto avatar;
|
||||
@Nullable public final Uri contactUri;
|
||||
@Nullable public final MaterialColor color;
|
||||
@Nullable public final Uri ringtone;
|
||||
public final long mutedUntil;
|
||||
@Nullable public final VibrateState vibrateState;
|
||||
public final boolean blocked;
|
||||
public final int expireMessages;
|
||||
@NonNull public final List<Recipient> participants;
|
||||
@Nullable public final String profileName;
|
||||
public final boolean seenInviteReminder;
|
||||
public final Optional<Integer> defaultSubscriptionId;
|
||||
@NonNull public final RegisteredState registered;
|
||||
@Nullable public final byte[] profileKey;
|
||||
@Nullable public final String profileAvatar;
|
||||
public final boolean profileSharing;
|
||||
public final boolean systemContact;
|
||||
@Nullable public final String name;
|
||||
@Nullable public final String customLabel;
|
||||
@Nullable public final ContactPhoto avatar;
|
||||
@NonNull public final FallbackContactPhoto fallbackAvatar;
|
||||
@Nullable public final Uri contactUri;
|
||||
@Nullable public final MaterialColor color;
|
||||
@Nullable public final Uri ringtone;
|
||||
public final long mutedUntil;
|
||||
@Nullable public final VibrateState vibrateState;
|
||||
public final boolean blocked;
|
||||
public final int expireMessages;
|
||||
@NonNull public final List<Recipient> participants;
|
||||
@Nullable public final String profileName;
|
||||
public final boolean seenInviteReminder;
|
||||
public final Optional<Integer> defaultSubscriptionId;
|
||||
@NonNull public final RegisteredState registered;
|
||||
@Nullable public final byte[] profileKey;
|
||||
@Nullable public final String profileAvatar;
|
||||
public final boolean profileSharing;
|
||||
public final boolean systemContact;
|
||||
|
||||
public RecipientDetails(@Nullable String name, @Nullable String customLabel,
|
||||
@Nullable Uri contactUri, @NonNull ContactPhoto avatar,
|
||||
@Nullable Uri contactUri, @Nullable ContactPhoto avatar,
|
||||
@NonNull FallbackContactPhoto fallbackAvatar,
|
||||
boolean systemContact, @Nullable RecipientSettings settings,
|
||||
@Nullable List<Recipient> participants)
|
||||
{
|
||||
this.customLabel = customLabel;
|
||||
this.avatar = avatar;
|
||||
this.fallbackAvatar = fallbackAvatar;
|
||||
this.contactUri = contactUri;
|
||||
this.color = settings != null ? settings.getColor() : null;
|
||||
this.ringtone = settings != null ? settings.getRingtone() : null;
|
||||
|
||||
Reference in New Issue
Block a user