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
This commit is contained in:
Moxie Marlinspike 2015-05-08 15:08:44 -07:00
parent 338caf7da9
commit 68234959c4
3 changed files with 11 additions and 17 deletions

View File

@ -1,17 +1,8 @@
package org.thoughtcrime.securesms.contacts; package org.thoughtcrime.securesms.contacts;
import android.content.Context; import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; 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.Drawable;
import android.graphics.drawable.LayerDrawable; import android.graphics.drawable.LayerDrawable;
import android.net.Uri; import android.net.Uri;
@ -20,7 +11,6 @@ import android.os.Build.VERSION_CODES;
import android.provider.ContactsContract; import android.provider.ContactsContract;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.Log; import android.util.Log;
import android.view.Gravity;
import android.widget.ImageView; import android.widget.ImageView;
import com.amulyakhare.textdrawable.TextDrawable; import com.amulyakhare.textdrawable.TextDrawable;
@ -63,9 +53,13 @@ public class ContactPhotoFactory {
} }
} }
public static Drawable getDefaultContactPhoto(@Nullable String name) { public static Drawable getDefaultContactPhoto(Context context, @Nullable String name) {
if (name != null && !name.isEmpty()) { if (name != null && !name.isEmpty()) {
return TextDrawable.builder().buildRound(String.valueOf(name.charAt(0)), 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)); COLOR_GENERATOR.getColor(name));
} }
@ -118,7 +112,7 @@ public class ContactPhotoFactory {
} }
} }
return getDefaultContactPhoto(name); return getDefaultContactPhoto(context, name);
} }
public static Drawable getGroupContactPhoto(Context context, @Nullable byte[] avatar) { public static Drawable getGroupContactPhoto(Context context, @Nullable byte[] avatar) {

View File

@ -147,7 +147,7 @@ public class Recipient {
public static Recipient getUnknownRecipient(Context context) { public static Recipient getUnknownRecipient(Context context) {
return new Recipient("Unknown", "Unknown", -1, null, return new Recipient("Unknown", "Unknown", -1, null,
ContactPhotoFactory.getDefaultContactPhoto(null)); ContactPhotoFactory.getDefaultContactPhoto(context, null));
} }
@Override @Override

View File

@ -75,7 +75,7 @@ public class RecipientProvider {
} else { } else {
final Drawable defaultPhoto = isGroupRecipient final Drawable defaultPhoto = isGroupRecipient
? ContactPhotoFactory.getDefaultGroupPhoto(context) ? ContactPhotoFactory.getDefaultGroupPhoto(context)
: ContactPhotoFactory.getDefaultContactPhoto(null); : ContactPhotoFactory.getDefaultContactPhoto(context, null);
recipient = new Recipient(null, number, recipientId, null, defaultPhoto); recipient = new Recipient(null, number, recipientId, null, defaultPhoto);
} }
@ -143,7 +143,7 @@ public class RecipientProvider {
cursor.close(); 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) { private RecipientDetails getGroupRecipientDetails(Context context, String groupId) {