From 35ee1c139157b62a86716e73385aa49bf66e8727 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Mon, 18 Nov 2019 13:02:19 +1100 Subject: [PATCH] Fix avatar images not being updated correctly. --- .../securesms/components/AvatarImageView.java | 64 +++++++++++-------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/src/org/thoughtcrime/securesms/components/AvatarImageView.java b/src/org/thoughtcrime/securesms/components/AvatarImageView.java index 6eae6b42aa..53ce56c737 100644 --- a/src/org/thoughtcrime/securesms/components/AvatarImageView.java +++ b/src/org/thoughtcrime/securesms/components/AvatarImageView.java @@ -104,38 +104,22 @@ public class AvatarImageView extends AppCompatImageView { @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); - if (w == 0 || h == 0 || recipient == null) { return; } - - Drawable image; - Context context = this.getContext(); - if (recipient.isGroupRecipient()) { - - - String name = Optional.fromNullable(recipient.getName()).or(Optional.fromNullable(TextSecurePreferences.getProfileName(context))).or(""); - MaterialColor fallbackColor = recipient.getColor(); - - if (fallbackColor == ContactColors.UNKNOWN_COLOR && !TextUtils.isEmpty(name)) { - fallbackColor = ContactColors.generateFor(name); - } - - image = new GeneratedContactPhoto(name, R.drawable.ic_profile_default).asDrawable(context, fallbackColor.toAvatarColor(context)); - } else { - // Default to primary device image - String ourPublicKey = TextSecurePreferences.getLocalNumber(context); - String ourPrimaryDevice = TextSecurePreferences.getMasterHexEncodedPublicKey(context); - String recipientAddress = recipient.getAddress().serialize(); - String profileAddress = (ourPrimaryDevice != null && ourPublicKey.equals(recipientAddress)) ? ourPrimaryDevice : recipientAddress; - image = new JazzIdenticonDrawable(w, h, profileAddress.toLowerCase()); - } - setImageDrawable(image); + updateImage(w, h); } public void update(String hexEncodedPublicKey) { - this.recipient = Recipient.from(getContext(), Address.fromSerialized(hexEncodedPublicKey), false); + Address address = Address.fromSerialized(hexEncodedPublicKey); + if (!address.equals(recipient.getAddress())) { + this.recipient = Recipient.from(getContext(), address, false); + updateImage(); + } } public void setAvatar(@NonNull GlideRequests requestManager, @Nullable Recipient recipient, boolean quickContactEnabled) { - this.recipient = recipient; + if (this.recipient == null || !this.recipient.equals(recipient)) { + this.recipient = recipient; + updateImage(); + } /* if (recipient != null) { requestManager.load(recipient.getContactPhoto()) @@ -170,4 +154,32 @@ public class AvatarImageView extends AppCompatImageView { } } + private void updateImage() { updateImage(getWidth(), getHeight()); } + private void updateImage(int w, int h) { + if (w == 0 || h == 0 || recipient == null) { return; } + + Drawable image; + Context context = this.getContext(); + if (recipient.isGroupRecipient()) { + + + String name = Optional.fromNullable(recipient.getName()).or(Optional.fromNullable(TextSecurePreferences.getProfileName(context))).or(""); + MaterialColor fallbackColor = recipient.getColor(); + + if (fallbackColor == ContactColors.UNKNOWN_COLOR && !TextUtils.isEmpty(name)) { + fallbackColor = ContactColors.generateFor(name); + } + + image = new GeneratedContactPhoto(name, R.drawable.ic_profile_default).asDrawable(context, fallbackColor.toAvatarColor(context)); + } else { + // Default to primary device image + String ourPublicKey = TextSecurePreferences.getLocalNumber(context); + String ourPrimaryDevice = TextSecurePreferences.getMasterHexEncodedPublicKey(context); + String recipientAddress = recipient.getAddress().serialize(); + String profileAddress = (ourPrimaryDevice != null && ourPublicKey.equals(recipientAddress)) ? ourPrimaryDevice : recipientAddress; + image = new JazzIdenticonDrawable(w, h, profileAddress.toLowerCase()); + } + setImageDrawable(image); + } + }