diff --git a/app/src/main/java/org/thoughtcrime/securesms/jobs/RetrieveProfileJob.java b/app/src/main/java/org/thoughtcrime/securesms/jobs/RetrieveProfileJob.java index 133285ed5..5bf55715f 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/jobs/RetrieveProfileJob.java +++ b/app/src/main/java/org/thoughtcrime/securesms/jobs/RetrieveProfileJob.java @@ -376,19 +376,27 @@ public class RetrieveProfileJob extends BaseJob { String plaintextProfileName = Util.emptyIfNull(ProfileUtil.decryptName(profileKey, profileName)); - if (!Objects.equals(plaintextProfileName, recipient.getProfileName().serialize())) { - String newProfileName = TextUtils.isEmpty(plaintextProfileName) ? ProfileName.EMPTY.serialize() : plaintextProfileName; - String previousProfileName = recipient.getProfileName().serialize(); + ProfileName remoteProfileName = ProfileName.fromSerialized(plaintextProfileName); + ProfileName localProfileName = recipient.getProfileName(); + if (!remoteProfileName.equals(localProfileName)) { Log.i(TAG, "Profile name updated. Writing new value."); - DatabaseFactory.getRecipientDatabase(context).setProfileName(recipient.getId(), ProfileName.fromSerialized(plaintextProfileName)); + DatabaseFactory.getRecipientDatabase(context).setProfileName(recipient.getId(), remoteProfileName); - if (!recipient.isBlocked() && !recipient.isGroup() && !recipient.isLocalNumber() && !TextUtils.isEmpty(previousProfileName)) { + String remoteDisplayName = remoteProfileName.toString(); + String localDisplayName = localProfileName.toString(); + + if (!recipient.isBlocked() && + !recipient.isGroup() && + !recipient.isLocalNumber() && + !localDisplayName.isEmpty() && + !remoteDisplayName.equals(localDisplayName)) + { Log.i(TAG, "Writing a profile name change event."); - DatabaseFactory.getSmsDatabase(context).insertProfileNameChangeMessages(recipient, newProfileName, previousProfileName); + DatabaseFactory.getSmsDatabase(context).insertProfileNameChangeMessages(recipient, remoteDisplayName, localDisplayName); } else { - Log.i(TAG, String.format(Locale.US, "Name changed, but wasn't relevant to write an event. blocked: %b, group: %b, self: %b, firstSet: %b", - recipient.isBlocked(), recipient.isGroup(), recipient.isLocalNumber(), TextUtils.isEmpty(previousProfileName))); + Log.i(TAG, String.format(Locale.US, "Name changed, but wasn't relevant to write an event. blocked: %s, group: %s, self: %s, firstSet: %s, displayChange: %s", + recipient.isBlocked(), recipient.isGroup(), recipient.isLocalNumber(), localDisplayName.isEmpty(), !remoteDisplayName.equals(localDisplayName))); } } diff --git a/app/src/main/java/org/thoughtcrime/securesms/profiles/ProfileName.java b/app/src/main/java/org/thoughtcrime/securesms/profiles/ProfileName.java index a5273a4a4..eb393dab7 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/profiles/ProfileName.java +++ b/app/src/main/java/org/thoughtcrime/securesms/profiles/ProfileName.java @@ -13,6 +13,8 @@ import org.thoughtcrime.securesms.util.StringUtil; import org.thoughtcrime.securesms.util.cjkv.CJKVUtil; import org.whispersystems.signalservice.api.crypto.ProfileCipher; +import java.util.Objects; + public final class ProfileName implements Parcelable { public static final ProfileName EMPTY = new ProfileName("", ""); @@ -133,6 +135,20 @@ public final class ProfileName implements Parcelable { dest.writeString(familyName); } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ProfileName that = (ProfileName) o; + return Objects.equals(givenName, that.givenName) && + Objects.equals(familyName, that.familyName); + } + + @Override + public int hashCode() { + return Objects.hash(givenName, familyName); + } + public static final Creator CREATOR = new Creator() { @Override public ProfileName createFromParcel(Parcel in) {