From 68234959c472b582f704964fa7ee78d804449301 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Fri, 8 May 2015 15:08:44 -0700 Subject: [PATCH] Fix for occasional generated avatar mis-sizing in conversation. Drawables are (strangely) mutable objects. We reuse a single drawable for each recipient, but some avatar views (the conversation list -- 40dp) are larger than others (the conversation -- 30dp). This results in a situation where TextDrawable doesn't render itself appropriately, because the bounds are modified by a larger view. Giving the Drawable an intrinsic width and height resolves this conflict. // FREEBIE --- .../contacts/ContactPhotoFactory.java | 22 +++++++------------ .../securesms/recipients/Recipient.java | 2 +- .../recipients/RecipientProvider.java | 4 ++-- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/org/thoughtcrime/securesms/contacts/ContactPhotoFactory.java b/src/org/thoughtcrime/securesms/contacts/ContactPhotoFactory.java index 37cee29e76..5e5f5d929d 100644 --- a/src/org/thoughtcrime/securesms/contacts/ContactPhotoFactory.java +++ b/src/org/thoughtcrime/securesms/contacts/ContactPhotoFactory.java @@ -1,17 +1,8 @@ package org.thoughtcrime.securesms.contacts; import android.content.Context; -import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.ColorFilter; -import android.graphics.Matrix; -import android.graphics.Paint; -import android.graphics.PixelFormat; -import android.graphics.drawable.BitmapDrawable; -import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.LayerDrawable; import android.net.Uri; @@ -20,7 +11,6 @@ import android.os.Build.VERSION_CODES; import android.provider.ContactsContract; import android.support.annotation.Nullable; import android.util.Log; -import android.view.Gravity; import android.widget.ImageView; import com.amulyakhare.textdrawable.TextDrawable; @@ -63,10 +53,14 @@ public class ContactPhotoFactory { } } - public static Drawable getDefaultContactPhoto(@Nullable String name) { + public static Drawable getDefaultContactPhoto(Context context, @Nullable String name) { if (name != null && !name.isEmpty()) { - return TextDrawable.builder().buildRound(String.valueOf(name.charAt(0)), - COLOR_GENERATOR.getColor(name)); + 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_GENERATOR.getColor(name)); } synchronized (defaultPhotoLock) { @@ -118,7 +112,7 @@ public class ContactPhotoFactory { } } - return getDefaultContactPhoto(name); + return getDefaultContactPhoto(context, name); } public static Drawable getGroupContactPhoto(Context context, @Nullable byte[] avatar) { diff --git a/src/org/thoughtcrime/securesms/recipients/Recipient.java b/src/org/thoughtcrime/securesms/recipients/Recipient.java index c14cdc862f..1081166eb2 100644 --- a/src/org/thoughtcrime/securesms/recipients/Recipient.java +++ b/src/org/thoughtcrime/securesms/recipients/Recipient.java @@ -147,7 +147,7 @@ public class Recipient { public static Recipient getUnknownRecipient(Context context) { return new Recipient("Unknown", "Unknown", -1, null, - ContactPhotoFactory.getDefaultContactPhoto(null)); + ContactPhotoFactory.getDefaultContactPhoto(context, null)); } @Override diff --git a/src/org/thoughtcrime/securesms/recipients/RecipientProvider.java b/src/org/thoughtcrime/securesms/recipients/RecipientProvider.java index 39c3a44e4e..4f70437b5d 100644 --- a/src/org/thoughtcrime/securesms/recipients/RecipientProvider.java +++ b/src/org/thoughtcrime/securesms/recipients/RecipientProvider.java @@ -75,7 +75,7 @@ public class RecipientProvider { } else { final Drawable defaultPhoto = isGroupRecipient ? ContactPhotoFactory.getDefaultGroupPhoto(context) - : ContactPhotoFactory.getDefaultContactPhoto(null); + : ContactPhotoFactory.getDefaultContactPhoto(context, null); recipient = new Recipient(null, number, recipientId, null, defaultPhoto); } @@ -143,7 +143,7 @@ public class RecipientProvider { cursor.close(); } - return new RecipientDetails(null, number, null, ContactPhotoFactory.getDefaultContactPhoto(null)); + return new RecipientDetails(null, number, null, ContactPhotoFactory.getDefaultContactPhoto(context, null)); } private RecipientDetails getGroupRecipientDetails(Context context, String groupId) {