mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-27 12:05:22 +00:00
Refactor contact photo logic to not reuse drawables.
// FREEBIE
This commit is contained in:
parent
8bb47bbdf1
commit
64df85f3ee
@ -8,7 +8,7 @@ import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import org.thoughtcrime.securesms.contacts.ContactPhotoFactory;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactPhotoFactory;
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
||||
import org.thoughtcrime.securesms.recipients.Recipients;
|
||||
@ -27,10 +27,10 @@ public class AvatarImageView extends ImageView {
|
||||
|
||||
public void setAvatar(@Nullable Recipients recipients, boolean quickContactEnabled) {
|
||||
if (recipients != null) {
|
||||
setImageDrawable(recipients.getContactPhoto(getContext()));
|
||||
setImageDrawable(recipients.getContactPhoto().asDrawable(getContext()));
|
||||
setAvatarClickHandler(recipients, quickContactEnabled);
|
||||
} else {
|
||||
setImageDrawable(ContactPhotoFactory.getDefaultContactPhoto(getContext(), null));
|
||||
setImageDrawable(ContactPhotoFactory.getDefaultContactPhoto(null).asDrawable(getContext()));
|
||||
setOnClickListener(null);
|
||||
}
|
||||
}
|
||||
|
@ -1,151 +0,0 @@
|
||||
package org.thoughtcrime.securesms.contacts;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.graphics.drawable.LayerDrawable;
|
||||
import android.net.Uri;
|
||||
import android.os.Build.VERSION;
|
||||
import android.os.Build.VERSION_CODES;
|
||||
import android.provider.ContactsContract;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Log;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import com.amulyakhare.textdrawable.TextDrawable;
|
||||
import com.amulyakhare.textdrawable.util.ColorGenerator;
|
||||
import com.makeramen.roundedimageview.RoundedDrawable;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
import org.thoughtcrime.securesms.util.BitmapDecodingException;
|
||||
import org.thoughtcrime.securesms.util.BitmapUtil;
|
||||
import org.thoughtcrime.securesms.util.LRUCache;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
public class ContactPhotoFactory {
|
||||
private static final String TAG = ContactPhotoFactory.class.getSimpleName();
|
||||
|
||||
private static final ColorGenerator COLOR_GENERATOR = ColorGenerator.MATERIAL;
|
||||
private static final int UNKNOWN_COLOR = 0xff9E9E9E;
|
||||
|
||||
private static final Object defaultPhotoLock = new Object();
|
||||
private static final Object defaultGroupPhotoLock = new Object();
|
||||
private static final Object loadingPhotoLock = new Object();
|
||||
|
||||
private static Drawable defaultContactPhoto;
|
||||
private static Drawable defaultGroupContactPhoto;
|
||||
private static Drawable loadingPhoto;
|
||||
|
||||
private static final Map<Uri,Bitmap> localUserContactPhotoCache =
|
||||
Collections.synchronizedMap(new LRUCache<Uri,Bitmap>(2));
|
||||
|
||||
public static Drawable getLoadingPhoto(Context context) {
|
||||
synchronized (loadingPhotoLock) {
|
||||
if (loadingPhoto == null)
|
||||
loadingPhoto = RoundedDrawable.fromDrawable(context.getResources().getDrawable(android.R.color.transparent));
|
||||
|
||||
return loadingPhoto;
|
||||
}
|
||||
}
|
||||
|
||||
public static Drawable getDefaultContactPhoto(Context context, @Nullable String name) {
|
||||
int targetSize = context.getResources().getDimensionPixelSize(R.dimen.contact_photo_target_size);
|
||||
|
||||
if (name != null && !name.isEmpty()) {
|
||||
return TextDrawable.builder().beginConfig()
|
||||
.width(targetSize)
|
||||
.height(targetSize)
|
||||
.endConfig()
|
||||
.buildRound(String.valueOf(name.charAt(0)),
|
||||
COLOR_GENERATOR.getColor(name));
|
||||
}
|
||||
|
||||
synchronized (defaultPhotoLock) {
|
||||
if (defaultContactPhoto == null)
|
||||
defaultContactPhoto = TextDrawable.builder().beginConfig()
|
||||
.width(targetSize)
|
||||
.height(targetSize)
|
||||
.endConfig()
|
||||
.buildRound("#", UNKNOWN_COLOR);
|
||||
|
||||
return defaultContactPhoto;
|
||||
}
|
||||
}
|
||||
|
||||
public static Drawable getDefaultGroupPhoto(Context context) {
|
||||
synchronized (defaultGroupPhotoLock) {
|
||||
if (defaultGroupContactPhoto == null) {
|
||||
Drawable background = TextDrawable.builder().buildRound(" ", UNKNOWN_COLOR);
|
||||
RoundedDrawable foreground = (RoundedDrawable) RoundedDrawable.fromDrawable(context.getResources().getDrawable(R.drawable.ic_group_white_24dp));
|
||||
foreground.setScaleType(ImageView.ScaleType.CENTER);
|
||||
|
||||
|
||||
defaultGroupContactPhoto = new ExpandingLayerDrawable(new Drawable[] {background, foreground});
|
||||
}
|
||||
|
||||
return defaultGroupContactPhoto;
|
||||
}
|
||||
}
|
||||
|
||||
public static void clearCache() {
|
||||
localUserContactPhotoCache.clear();
|
||||
}
|
||||
|
||||
public static Drawable getContactPhoto(Context context, Uri uri, String name) {
|
||||
final InputStream inputStream = getContactPhotoStream(context, uri);
|
||||
final int targetSize = context.getResources().getDimensionPixelSize(R.dimen.contact_photo_target_size);
|
||||
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
return RoundedDrawable.fromBitmap(BitmapUtil.createScaledBitmap(inputStream,
|
||||
getContactPhotoStream(context, uri),
|
||||
targetSize,
|
||||
targetSize))
|
||||
.setScaleType(ImageView.ScaleType.CENTER_CROP)
|
||||
.setOval(true);
|
||||
} catch (BitmapDecodingException bde) {
|
||||
Log.w(TAG, bde);
|
||||
}
|
||||
}
|
||||
|
||||
return getDefaultContactPhoto(context, name);
|
||||
}
|
||||
|
||||
public static Drawable getGroupContactPhoto(Context context, @Nullable byte[] avatar) {
|
||||
if (avatar == null) return getDefaultGroupPhoto(context);
|
||||
|
||||
return RoundedDrawable.fromBitmap(BitmapFactory.decodeByteArray(avatar, 0, avatar.length))
|
||||
.setScaleType(ImageView.ScaleType.CENTER_CROP)
|
||||
.setOval(true);
|
||||
}
|
||||
|
||||
private static InputStream getContactPhotoStream(Context context, Uri uri) {
|
||||
if (VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH) {
|
||||
return ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri, true);
|
||||
} else {
|
||||
return ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri);
|
||||
}
|
||||
}
|
||||
|
||||
private static class ExpandingLayerDrawable extends LayerDrawable {
|
||||
|
||||
public ExpandingLayerDrawable(Drawable[] layers) {
|
||||
super(layers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIntrinsicWidth() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIntrinsicHeight() {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
@ -97,7 +97,7 @@ public class ContactSelectionListItem extends RelativeLayout implements Recipien
|
||||
|
||||
private void setContactPhotoImage(@Nullable Recipient recipient) {
|
||||
if (recipient!= null) {
|
||||
contactPhotoImage.setImageDrawable(recipient.getContactPhoto());
|
||||
contactPhotoImage.setImageDrawable(recipient.getContactPhoto().asDrawable(getContext()));
|
||||
recipient.addListener(this);
|
||||
} else {
|
||||
contactPhotoImage.setImageDrawable(null);
|
||||
@ -111,7 +111,7 @@ public class ContactSelectionListItem extends RelativeLayout implements Recipien
|
||||
this.contactPhotoImage.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
contactPhotoImage.setImageDrawable(recipient.getContactPhoto());
|
||||
contactPhotoImage.setImageDrawable(recipient.getContactPhoto().asDrawable(getContext()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -0,0 +1,24 @@
|
||||
package org.thoughtcrime.securesms.contacts.avatars;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import com.makeramen.roundedimageview.RoundedDrawable;
|
||||
|
||||
public class BitmapContactPhoto implements ContactPhoto {
|
||||
|
||||
private final Bitmap bitmap;
|
||||
|
||||
BitmapContactPhoto(Bitmap bitmap) {
|
||||
this.bitmap = bitmap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Drawable asDrawable(Context context) {
|
||||
return RoundedDrawable.fromBitmap(bitmap)
|
||||
.setScaleType(ImageView.ScaleType.CENTER_CROP)
|
||||
.setOval(true);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package org.thoughtcrime.securesms.contacts.avatars;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
||||
public interface ContactPhoto {
|
||||
|
||||
public Drawable asDrawable(Context context);
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package org.thoughtcrime.securesms.contacts.avatars;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.net.Uri;
|
||||
import android.os.Build.VERSION;
|
||||
import android.os.Build.VERSION_CODES;
|
||||
import android.provider.ContactsContract;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.util.BitmapDecodingException;
|
||||
import org.thoughtcrime.securesms.util.BitmapUtil;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
public class ContactPhotoFactory {
|
||||
private static final String TAG = ContactPhotoFactory.class.getSimpleName();
|
||||
|
||||
private static final int UNKNOWN_COLOR = 0xff9E9E9E;
|
||||
|
||||
public static ContactPhoto getLoadingPhoto() {
|
||||
return new TransparentContactPhoto();
|
||||
}
|
||||
|
||||
public static ContactPhoto getDefaultContactPhoto(@Nullable String name) {
|
||||
if (!TextUtils.isEmpty(name)) return new GeneratedContactPhoto(name);
|
||||
else return new GeneratedContactPhoto("#", UNKNOWN_COLOR);
|
||||
}
|
||||
|
||||
public static ContactPhoto getDefaultGroupPhoto() {
|
||||
return new ResourceContactPhoto(R.drawable.ic_group_white_24dp, UNKNOWN_COLOR);
|
||||
}
|
||||
|
||||
public static ContactPhoto getContactPhoto(Context context, Uri uri, String name) {
|
||||
try {
|
||||
InputStream inputStream = getContactPhotoStream(context, uri);
|
||||
int targetSize = context.getResources().getDimensionPixelSize(R.dimen.contact_photo_target_size);
|
||||
|
||||
if (inputStream != null) {
|
||||
return new BitmapContactPhoto(BitmapUtil.createScaledBitmap(inputStream, getContactPhotoStream(context, uri), targetSize, targetSize));
|
||||
}
|
||||
} catch (BitmapDecodingException e) {
|
||||
Log.w(TAG, e);
|
||||
}
|
||||
|
||||
return getDefaultContactPhoto(name);
|
||||
}
|
||||
|
||||
public static ContactPhoto getGroupContactPhoto(@Nullable byte[] avatar) {
|
||||
if (avatar == null) return getDefaultGroupPhoto();
|
||||
|
||||
return new BitmapContactPhoto(BitmapFactory.decodeByteArray(avatar, 0, avatar.length));
|
||||
}
|
||||
|
||||
private static InputStream getContactPhotoStream(Context context, Uri uri) {
|
||||
if (VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH) {
|
||||
return ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri, true);
|
||||
} else {
|
||||
return ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package org.thoughtcrime.securesms.contacts.avatars;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.amulyakhare.textdrawable.TextDrawable;
|
||||
import com.amulyakhare.textdrawable.util.ColorGenerator;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
|
||||
public class GeneratedContactPhoto implements ContactPhoto {
|
||||
|
||||
private final String name;
|
||||
private final int color;
|
||||
|
||||
GeneratedContactPhoto(@NonNull String name) {
|
||||
this(name, ColorGenerator.MATERIAL.getColor(name));
|
||||
}
|
||||
|
||||
GeneratedContactPhoto(@NonNull String name, int color) {
|
||||
this.name = name;
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Drawable asDrawable(Context context) {
|
||||
int targetSize = context.getResources().getDimensionPixelSize(R.dimen.contact_photo_target_size);
|
||||
|
||||
return TextDrawable.builder()
|
||||
.beginConfig()
|
||||
.width(targetSize)
|
||||
.height(targetSize)
|
||||
.endConfig()
|
||||
.buildRound(String.valueOf(name.charAt(0)), color);
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package org.thoughtcrime.securesms.contacts.avatars;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.graphics.drawable.LayerDrawable;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import com.amulyakhare.textdrawable.TextDrawable;
|
||||
import com.makeramen.roundedimageview.RoundedDrawable;
|
||||
|
||||
public class ResourceContactPhoto implements ContactPhoto {
|
||||
|
||||
private final int resourceId;
|
||||
private final int color;
|
||||
|
||||
ResourceContactPhoto(int resourceId, int color) {
|
||||
this.resourceId = resourceId;
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Drawable asDrawable(Context context) {
|
||||
Drawable background = TextDrawable.builder().buildRound(" ", color);
|
||||
RoundedDrawable foreground = (RoundedDrawable) RoundedDrawable.fromDrawable(context.getResources().getDrawable(resourceId));
|
||||
foreground.setScaleType(ImageView.ScaleType.CENTER);
|
||||
|
||||
return new ExpandingLayerDrawable(new Drawable[] {background, foreground});
|
||||
}
|
||||
|
||||
private static class ExpandingLayerDrawable extends LayerDrawable {
|
||||
public ExpandingLayerDrawable(Drawable[] layers) {
|
||||
super(layers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIntrinsicWidth() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIntrinsicHeight() {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package org.thoughtcrime.securesms.contacts.avatars;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
||||
import com.makeramen.roundedimageview.RoundedDrawable;
|
||||
|
||||
public class TransparentContactPhoto implements ContactPhoto {
|
||||
|
||||
TransparentContactPhoto() {}
|
||||
|
||||
@Override
|
||||
public Drawable asDrawable(Context context) {
|
||||
return RoundedDrawable.fromDrawable(context.getResources().getDrawable(android.R.color.transparent));
|
||||
}
|
||||
}
|
@ -345,7 +345,7 @@ public class MmsDatabase extends MessagingDatabase {
|
||||
? Util.toIsoString(notification.getFrom().getTextString())
|
||||
: "";
|
||||
Recipients recipients = RecipientFactory.getRecipientsFromString(context, fromString, false);
|
||||
if (recipients.isEmpty()) recipients = RecipientFactory.getRecipientsFor(context, Recipient.getUnknownRecipient(context), false);
|
||||
if (recipients.isEmpty()) recipients = RecipientFactory.getRecipientsFor(context, Recipient.getUnknownRecipient(), false);
|
||||
return DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients);
|
||||
}
|
||||
|
||||
@ -1054,13 +1054,13 @@ public class MmsDatabase extends MessagingDatabase {
|
||||
|
||||
private Recipients getRecipientsFor(String address) {
|
||||
if (TextUtils.isEmpty(address) || address.equals("insert-address-token")) {
|
||||
return RecipientFactory.getRecipientsFor(context, Recipient.getUnknownRecipient(context), false);
|
||||
return RecipientFactory.getRecipientsFor(context, Recipient.getUnknownRecipient(), false);
|
||||
}
|
||||
|
||||
Recipients recipients = RecipientFactory.getRecipientsFromString(context, address, false);
|
||||
|
||||
if (recipients == null || recipients.isEmpty()) {
|
||||
return RecipientFactory.getRecipientsFor(context, Recipient.getUnknownRecipient(context), false);
|
||||
return RecipientFactory.getRecipientsFor(context, Recipient.getUnknownRecipient(), false);
|
||||
}
|
||||
|
||||
return recipients;
|
||||
|
@ -368,7 +368,7 @@ public class SmsDatabase extends MessagingDatabase {
|
||||
recipients = RecipientFactory.getRecipientsFromString(context, message.getSender(), true);
|
||||
} else {
|
||||
Log.w(TAG, "Sender is null, returning unknown recipient");
|
||||
recipients = RecipientFactory.getRecipientsFor(context, Recipient.getUnknownRecipient(context), false);
|
||||
recipients = RecipientFactory.getRecipientsFor(context, Recipient.getUnknownRecipient(), false);
|
||||
}
|
||||
|
||||
Recipients groupRecipients;
|
||||
@ -615,13 +615,13 @@ public class SmsDatabase extends MessagingDatabase {
|
||||
Recipients recipients = RecipientFactory.getRecipientsFromString(context, address, false);
|
||||
|
||||
if (recipients == null || recipients.isEmpty()) {
|
||||
return RecipientFactory.getRecipientsFor(context, Recipient.getUnknownRecipient(context), false);
|
||||
return RecipientFactory.getRecipientsFor(context, Recipient.getUnknownRecipient(), false);
|
||||
}
|
||||
|
||||
return recipients;
|
||||
} else {
|
||||
Log.w(TAG, "getRecipientsFor() address is null");
|
||||
return RecipientFactory.getRecipientsFor(context, Recipient.getUnknownRecipient(context), false);
|
||||
return RecipientFactory.getRecipientsFor(context, Recipient.getUnknownRecipient(), false);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -192,7 +192,7 @@ public class MessageNotifier {
|
||||
List<NotificationItem> notifications = notificationState.getNotifications();
|
||||
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
|
||||
Recipient recipient = notifications.get(0).getIndividualRecipient();
|
||||
Drawable recipientPhoto = recipient.getContactPhoto();
|
||||
Drawable recipientPhoto = recipient.getContactPhoto().asDrawable(context);
|
||||
int largeIconTargetSize = context.getResources().getDimensionPixelSize(R.dimen.contact_photo_target_size);
|
||||
|
||||
if (recipientPhoto != null) {
|
||||
|
@ -16,12 +16,12 @@
|
||||
*/
|
||||
package org.thoughtcrime.securesms.recipients;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
|
||||
import org.thoughtcrime.securesms.contacts.ContactPhotoFactory;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactPhoto;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactPhotoFactory;
|
||||
import org.thoughtcrime.securesms.recipients.RecipientProvider.RecipientDetails;
|
||||
import org.thoughtcrime.securesms.util.FutureTaskListener;
|
||||
import org.thoughtcrime.securesms.util.GroupUtil;
|
||||
@ -43,15 +43,14 @@ public class Recipient {
|
||||
private String number;
|
||||
private String name;
|
||||
|
||||
private Drawable contactPhoto;
|
||||
private Uri contactUri;
|
||||
private ContactPhoto contactPhoto;
|
||||
private Uri contactUri;
|
||||
|
||||
Recipient(String number, Drawable contactPhoto,
|
||||
long recipientId, ListenableFutureTask<RecipientDetails> future)
|
||||
Recipient(long recipientId, String number, ListenableFutureTask<RecipientDetails> future)
|
||||
{
|
||||
this.number = number;
|
||||
this.contactPhoto = contactPhoto;
|
||||
this.recipientId = recipientId;
|
||||
this.recipientId = recipientId;
|
||||
this.number = number;
|
||||
this.contactPhoto = ContactPhotoFactory.getLoadingPhoto();
|
||||
|
||||
future.addListener(new FutureTaskListener<RecipientDetails>() {
|
||||
@Override
|
||||
@ -60,12 +59,12 @@ public class Recipient {
|
||||
Set<RecipientModifiedListener> localListeners;
|
||||
|
||||
synchronized (Recipient.this) {
|
||||
Recipient.this.name = result.name;
|
||||
Recipient.this.number = result.number;
|
||||
Recipient.this.contactUri = result.contactUri;
|
||||
Recipient.this.contactPhoto = result.avatar;
|
||||
Recipient.this.name = result.name;
|
||||
Recipient.this.number = result.number;
|
||||
Recipient.this.contactUri = result.contactUri;
|
||||
Recipient.this.contactPhoto = result.avatar;
|
||||
|
||||
localListeners = new HashSet<>(listeners);
|
||||
localListeners = new HashSet<>(listeners);
|
||||
listeners.clear();
|
||||
}
|
||||
|
||||
@ -81,12 +80,12 @@ public class Recipient {
|
||||
});
|
||||
}
|
||||
|
||||
Recipient(String name, String number, long recipientId, Uri contactUri, Drawable contactPhoto) {
|
||||
this.number = number;
|
||||
this.recipientId = recipientId;
|
||||
this.contactUri = contactUri;
|
||||
this.name = name;
|
||||
this.contactPhoto = contactPhoto;
|
||||
Recipient(long recipientId, RecipientDetails details) {
|
||||
this.recipientId = recipientId;
|
||||
this.number = details.number;
|
||||
this.contactUri = details.contactUri;
|
||||
this.name = details.name;
|
||||
this.contactPhoto = details.avatar;
|
||||
}
|
||||
|
||||
public synchronized Uri getContactUri() {
|
||||
@ -121,13 +120,12 @@ public class Recipient {
|
||||
return (name == null ? number : name);
|
||||
}
|
||||
|
||||
public synchronized Drawable getContactPhoto() {
|
||||
public synchronized @NonNull ContactPhoto getContactPhoto() {
|
||||
return contactPhoto;
|
||||
}
|
||||
|
||||
public static Recipient getUnknownRecipient(Context context) {
|
||||
return new Recipient("Unknown", "Unknown", -1, null,
|
||||
ContactPhotoFactory.getDefaultContactPhoto(context, null));
|
||||
public static Recipient getUnknownRecipient() {
|
||||
return new Recipient(-1, new RecipientDetails("Unknown", "Unknown", null, ContactPhotoFactory.getDefaultContactPhoto("Unknown")));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -20,7 +20,7 @@ import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import org.thoughtcrime.securesms.contacts.ContactPhotoFactory;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactPhotoFactory;
|
||||
import org.thoughtcrime.securesms.database.CanonicalAddressDatabase;
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
import org.whispersystems.libaxolotl.util.guava.Optional;
|
||||
@ -120,7 +120,6 @@ public class RecipientFactory {
|
||||
}
|
||||
|
||||
public static void clearCache() {
|
||||
ContactPhotoFactory.clearCache();
|
||||
provider.clearCache();
|
||||
}
|
||||
|
||||
|
@ -18,18 +18,18 @@ package org.thoughtcrime.securesms.recipients;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.provider.ContactsContract.Contacts;
|
||||
import android.provider.ContactsContract.PhoneLookup;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Log;
|
||||
|
||||
import org.thoughtcrime.securesms.contacts.ContactPhotoFactory;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactPhoto;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactPhotoFactory;
|
||||
import org.thoughtcrime.securesms.database.CanonicalAddressDatabase;
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.database.GroupDatabase;
|
||||
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase;
|
||||
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.RecipientsPreferences;
|
||||
import org.thoughtcrime.securesms.util.GroupUtil;
|
||||
import org.thoughtcrime.securesms.util.LRUCache;
|
||||
@ -47,6 +47,8 @@ import java.util.concurrent.ExecutorService;
|
||||
|
||||
public class RecipientProvider {
|
||||
|
||||
private static final String TAG = RecipientProvider.class.getSimpleName();
|
||||
|
||||
private static final Map<Long,Recipient> recipientCache = Collections.synchronizedMap(new LRUCache<Long,Recipient>(1000));
|
||||
private static final Map<RecipientIds,Recipients> recipientsCache = Collections.synchronizedMap(new LRUCache<RecipientIds, Recipients>(1000));
|
||||
private static final ExecutorService asyncRecipientResolver = Util.newSingleThreadedLifoExecutor();
|
||||
@ -60,10 +62,18 @@ public class RecipientProvider {
|
||||
|
||||
Recipient getRecipient(Context context, long recipientId, boolean asynchronous) {
|
||||
Recipient cachedRecipient = recipientCache.get(recipientId);
|
||||
if (cachedRecipient != null) return cachedRecipient;
|
||||
|
||||
if (cachedRecipient != null) return cachedRecipient;
|
||||
else if (asynchronous) return getAsynchronousRecipient(context, recipientId);
|
||||
else return getSynchronousRecipient(context, recipientId);
|
||||
String number = CanonicalAddressDatabase.getInstance(context).getAddressFromId(recipientId);
|
||||
|
||||
if (asynchronous) {
|
||||
cachedRecipient = new Recipient(recipientId, number, getRecipientDetailsAsync(context, number));
|
||||
} else {
|
||||
cachedRecipient = new Recipient(recipientId, getRecipientDetailsSync(context, number));
|
||||
}
|
||||
|
||||
recipientCache.put(recipientId, cachedRecipient);
|
||||
return cachedRecipient;
|
||||
}
|
||||
|
||||
Recipients getRecipients(Context context, long[] recipientIds, boolean asynchronous) {
|
||||
@ -83,80 +93,44 @@ public class RecipientProvider {
|
||||
return cachedRecipients;
|
||||
}
|
||||
|
||||
private Recipient getSynchronousRecipient(final Context context, final long recipientId) {
|
||||
Log.w("RecipientProvider", "Cache miss [SYNC]!");
|
||||
|
||||
final Recipient recipient;
|
||||
RecipientDetails details;
|
||||
String number = CanonicalAddressDatabase.getInstance(context).getAddressFromId(recipientId);
|
||||
final boolean isGroupRecipient = GroupUtil.isEncodedGroup(number);
|
||||
|
||||
if (isGroupRecipient) details = getGroupRecipientDetails(context, number);
|
||||
else details = getRecipientDetails(context, number);
|
||||
|
||||
if (details != null) {
|
||||
recipient = new Recipient(details.name, details.number, recipientId, details.contactUri, details.avatar);
|
||||
} else {
|
||||
final Drawable defaultPhoto = isGroupRecipient
|
||||
? ContactPhotoFactory.getDefaultGroupPhoto(context)
|
||||
: ContactPhotoFactory.getDefaultContactPhoto(context, null);
|
||||
|
||||
recipient = new Recipient(null, number, recipientId, null, defaultPhoto);
|
||||
}
|
||||
|
||||
recipientCache.put(recipientId, recipient);
|
||||
return recipient;
|
||||
}
|
||||
|
||||
private Recipient getAsynchronousRecipient(final Context context, final long recipientId) {
|
||||
Log.w("RecipientProvider", "Cache miss [ASYNC]!");
|
||||
|
||||
final String number = CanonicalAddressDatabase.getInstance(context).getAddressFromId(recipientId);
|
||||
final boolean isGroupRecipient = GroupUtil.isEncodedGroup(number);
|
||||
|
||||
Callable<RecipientDetails> task = new Callable<RecipientDetails>() {
|
||||
@Override
|
||||
public RecipientDetails call() throws Exception {
|
||||
if (isGroupRecipient) return getGroupRecipientDetails(context, number);
|
||||
else return getRecipientDetails(context, number);
|
||||
}
|
||||
};
|
||||
|
||||
ListenableFutureTask<RecipientDetails> future = new ListenableFutureTask<>(task);
|
||||
|
||||
asyncRecipientResolver.submit(future);
|
||||
|
||||
Drawable contactPhoto;
|
||||
|
||||
if (isGroupRecipient) {
|
||||
contactPhoto = ContactPhotoFactory.getDefaultGroupPhoto(context);
|
||||
} else {
|
||||
contactPhoto = ContactPhotoFactory.getLoadingPhoto(context);
|
||||
}
|
||||
|
||||
Recipient recipient = new Recipient(number, contactPhoto, recipientId, future);
|
||||
recipientCache.put(recipientId, recipient);
|
||||
|
||||
return recipient;
|
||||
}
|
||||
|
||||
void clearCache() {
|
||||
recipientCache.clear();
|
||||
recipientsCache.clear();
|
||||
}
|
||||
|
||||
private RecipientDetails getRecipientDetails(Context context, String number) {
|
||||
private @NonNull ListenableFutureTask<RecipientDetails> getRecipientDetailsAsync(final Context context,
|
||||
final String number)
|
||||
{
|
||||
Callable<RecipientDetails> task = new Callable<RecipientDetails>() {
|
||||
@Override
|
||||
public RecipientDetails call() throws Exception {
|
||||
return getRecipientDetailsSync(context, number);
|
||||
}
|
||||
};
|
||||
|
||||
ListenableFutureTask<RecipientDetails> future = new ListenableFutureTask<>(task);
|
||||
asyncRecipientResolver.submit(future);
|
||||
return future;
|
||||
}
|
||||
|
||||
private @NonNull RecipientDetails getRecipientDetailsSync(Context context, String number) {
|
||||
if (GroupUtil.isEncodedGroup(number)) return getGroupRecipientDetails(context, number);
|
||||
else return getIndividualRecipientDetails(context, number);
|
||||
}
|
||||
|
||||
private @NonNull RecipientDetails getIndividualRecipientDetails(Context context, String number) {
|
||||
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
|
||||
Cursor cursor = context.getContentResolver().query(uri, CALLER_ID_PROJECTION,
|
||||
null, null, null);
|
||||
|
||||
try {
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
Uri contactUri = Contacts.getLookupUri(cursor.getLong(2), cursor.getString(1));
|
||||
String name = cursor.getString(3).equals(cursor.getString(0)) ? null : cursor.getString(0);
|
||||
Drawable contactPhoto = ContactPhotoFactory.getContactPhoto(context,
|
||||
Uri.withAppendedPath(Contacts.CONTENT_URI, cursor.getLong(2) + ""),
|
||||
name);
|
||||
Uri contactUri = Contacts.getLookupUri(cursor.getLong(2), cursor.getString(1));
|
||||
String name = cursor.getString(3).equals(cursor.getString(0)) ? null : cursor.getString(0);
|
||||
ContactPhoto contactPhoto = ContactPhotoFactory.getContactPhoto(context,
|
||||
Uri.withAppendedPath(Contacts.CONTENT_URI, cursor.getLong(2) + ""),
|
||||
name);
|
||||
|
||||
return new RecipientDetails(cursor.getString(0), cursor.getString(3), contactUri, contactPhoto);
|
||||
}
|
||||
} finally {
|
||||
@ -164,23 +138,23 @@ public class RecipientProvider {
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return new RecipientDetails(null, number, null, ContactPhotoFactory.getDefaultContactPhoto(context, null));
|
||||
return new RecipientDetails(null, number, null, ContactPhotoFactory.getDefaultContactPhoto(null));
|
||||
}
|
||||
|
||||
private RecipientDetails getGroupRecipientDetails(Context context, String groupId) {
|
||||
private @NonNull RecipientDetails getGroupRecipientDetails(Context context, String groupId) {
|
||||
try {
|
||||
GroupDatabase.GroupRecord record = DatabaseFactory.getGroupDatabase(context)
|
||||
.getGroup(GroupUtil.getDecodedId(groupId));
|
||||
|
||||
if (record != null) {
|
||||
Drawable avatar = ContactPhotoFactory.getGroupContactPhoto(context, record.getAvatar());
|
||||
return new RecipientDetails(record.getTitle(), groupId, null, avatar);
|
||||
ContactPhoto contactPhoto = ContactPhotoFactory.getGroupContactPhoto(record.getAvatar());
|
||||
return new RecipientDetails(record.getTitle(), groupId, null, contactPhoto);
|
||||
}
|
||||
|
||||
return null;
|
||||
return new RecipientDetails(null, groupId, null, ContactPhotoFactory.getDefaultGroupPhoto());
|
||||
} catch (IOException e) {
|
||||
Log.w("RecipientProvider", e);
|
||||
return null;
|
||||
return new RecipientDetails(null, groupId, null, ContactPhotoFactory.getDefaultGroupPhoto());
|
||||
}
|
||||
}
|
||||
|
||||
@ -204,12 +178,12 @@ public class RecipientProvider {
|
||||
}
|
||||
|
||||
public static class RecipientDetails {
|
||||
public final String name;
|
||||
public final String number;
|
||||
public final Drawable avatar;
|
||||
public final Uri contactUri;
|
||||
public final String name;
|
||||
public final String number;
|
||||
public final ContactPhoto avatar;
|
||||
public final Uri contactUri;
|
||||
|
||||
public RecipientDetails(String name, String number, Uri contactUri, Drawable avatar) {
|
||||
public RecipientDetails(String name, String number, Uri contactUri, ContactPhoto avatar) {
|
||||
this.name = name;
|
||||
this.number = number;
|
||||
this.avatar = avatar;
|
||||
|
@ -16,14 +16,14 @@
|
||||
*/
|
||||
package org.thoughtcrime.securesms.recipients;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Log;
|
||||
import android.util.Patterns;
|
||||
|
||||
import org.thoughtcrime.securesms.contacts.ContactPhotoFactory;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactPhoto;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactPhotoFactory;
|
||||
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.RecipientsPreferences;
|
||||
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.VibrateState;
|
||||
import org.thoughtcrime.securesms.recipients.Recipient.RecipientModifiedListener;
|
||||
@ -149,9 +149,10 @@ public class Recipients implements Iterable<Recipient>, RecipientModifiedListene
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
public Drawable getContactPhoto(Context context) {
|
||||
public @NonNull
|
||||
ContactPhoto getContactPhoto() {
|
||||
if (recipients.size() == 1) return recipients.get(0).getContactPhoto();
|
||||
else return ContactPhotoFactory.getDefaultGroupPhoto(context);
|
||||
else return ContactPhotoFactory.getDefaultGroupPhoto();
|
||||
}
|
||||
|
||||
public synchronized void addListener(RecipientsModifiedListener listener) {
|
||||
|
Loading…
Reference in New Issue
Block a user