diff --git a/app/src/main/java/org/thoughtcrime/securesms/contacts/sync/ContactHolder.java b/app/src/main/java/org/thoughtcrime/securesms/contacts/sync/ContactHolder.java index 298326d815..8635f326c3 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/contacts/sync/ContactHolder.java +++ b/app/src/main/java/org/thoughtcrime/securesms/contacts/sync/ContactHolder.java @@ -42,6 +42,7 @@ final class ContactHolder { for (PhoneNumberRecord phoneNumberRecord : phoneNumberRecords) { handle.setSystemContactInfo(phoneNumberRecord.getRecipientId(), getProfileName(phoneNumberRecord.getDisplayName()), + phoneNumberRecord.getDisplayName(), phoneNumberRecord.getContactPhotoUri(), phoneNumberRecord.getContactLabel(), phoneNumberRecord.getPhoneType(), diff --git a/app/src/main/java/org/thoughtcrime/securesms/contacts/sync/DirectoryHelper.java b/app/src/main/java/org/thoughtcrime/securesms/contacts/sync/DirectoryHelper.java index 96657e35f0..e8f97ce6d5 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/contacts/sync/DirectoryHelper.java +++ b/app/src/main/java/org/thoughtcrime/securesms/contacts/sync/DirectoryHelper.java @@ -323,17 +323,18 @@ public class DirectoryHelper { BulkOperationsHandle handle = recipientDatabase.beginBulkSystemContactUpdate(); - ContactHolder old = null; try (Cursor cursor = ContactAccessor.getInstance().getAllSystemContacts(context)) { while (cursor != null && cursor.moveToNext()) { - String lookupKey = getLookupKey(cursor); - String mimeType = getMimeType(cursor); - ContactHolder contactHolder = new ContactHolder(lookupKey); + String mimeType = getMimeType(cursor); if (!isPhoneMimeType(mimeType)) { - Log.w(TAG, "Ignoring unexpected mime type: " + mimeType); + Log.w(TAG, "Ignoring unwanted mime type: " + mimeType); + continue; } + String lookupKey = getLookupKey(cursor); + ContactHolder contactHolder = new ContactHolder(lookupKey); + while (!cursor.isAfterLast() && getLookupKey(cursor).equals(lookupKey) && isPhoneMimeType(getMimeType(cursor))) { String number = CursorUtil.requireString(cursor, ContactsContract.CommonDataKinds.Phone.NUMBER); diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java b/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java index 8f0bf26630..58f2f06f33 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java @@ -158,7 +158,7 @@ public class RecipientDatabase extends Database { ID, UUID, USERNAME, PHONE, EMAIL, GROUP_ID, GROUP_TYPE, BLOCKED, MESSAGE_RINGTONE, CALL_RINGTONE, MESSAGE_VIBRATE, CALL_VIBRATE, MUTE_UNTIL, COLOR, SEEN_INVITE_REMINDER, DEFAULT_SUBSCRIPTION_ID, MESSAGE_EXPIRATION_TIME, REGISTERED, PROFILE_KEY, PROFILE_KEY_CREDENTIAL, - SYSTEM_GIVEN_NAME, SYSTEM_FAMILY_NAME, SYSTEM_PHOTO_URI, SYSTEM_PHONE_LABEL, SYSTEM_PHONE_TYPE, SYSTEM_CONTACT_URI, + SYSTEM_JOINED_NAME, SYSTEM_GIVEN_NAME, SYSTEM_FAMILY_NAME, SYSTEM_PHOTO_URI, SYSTEM_PHONE_LABEL, SYSTEM_PHONE_TYPE, SYSTEM_CONTACT_URI, PROFILE_GIVEN_NAME, PROFILE_FAMILY_NAME, SIGNAL_PROFILE_AVATAR, PROFILE_SHARING, LAST_PROFILE_FETCH, NOTIFICATION_CHANNEL, UNIDENTIFIED_ACCESS_MODE, @@ -1271,6 +1271,7 @@ public class RecipientDatabase extends Database { String profileKeyCredentialString = CursorUtil.requireString(cursor, PROFILE_KEY_CREDENTIAL); String systemGivenName = CursorUtil.requireString(cursor, SYSTEM_GIVEN_NAME); String systemFamilyName = CursorUtil.requireString(cursor, SYSTEM_FAMILY_NAME); + String systemDisplayName = CursorUtil.requireString(cursor, SYSTEM_JOINED_NAME); String systemContactPhoto = CursorUtil.requireString(cursor, SYSTEM_PHOTO_URI); String systemPhoneLabel = CursorUtil.requireString(cursor, SYSTEM_PHONE_LABEL); String systemContactUri = CursorUtil.requireString(cursor, SYSTEM_CONTACT_URI); @@ -1357,6 +1358,7 @@ public class RecipientDatabase extends Database { profileKey, profileKeyCredential, ProfileName.fromParts(systemGivenName, systemFamilyName), + systemDisplayName, systemContactPhoto, systemPhoneLabel, systemContactUri, @@ -2849,15 +2851,18 @@ public class RecipientDatabase extends Database { public void setSystemContactInfo(@NonNull RecipientId id, @NonNull ProfileName systemProfileName, + @Nullable String systemDisplayName, @Nullable String photoUri, @Nullable String systemPhoneLabel, int systemPhoneType, @Nullable String systemContactUri) { ContentValues dirtyQualifyingValues = new ContentValues(); + String joinedName = Util.firstNonNull(systemDisplayName, systemProfileName.toString()); + dirtyQualifyingValues.put(SYSTEM_GIVEN_NAME, systemProfileName.getGivenName()); dirtyQualifyingValues.put(SYSTEM_FAMILY_NAME, systemProfileName.getFamilyName()); - dirtyQualifyingValues.put(SYSTEM_JOINED_NAME, systemProfileName.toString()); + dirtyQualifyingValues.put(SYSTEM_JOINED_NAME, joinedName); if (update(id, dirtyQualifyingValues)) { markDirty(id, DirtyState.UPDATE); @@ -2869,7 +2874,6 @@ public class RecipientDatabase extends Database { refreshQualifyingValues.put(SYSTEM_PHONE_TYPE, systemPhoneType); refreshQualifyingValues.put(SYSTEM_CONTACT_URI, systemContactUri); - String joinedName = systemProfileName.toString(); boolean updatedValues = update(id, refreshQualifyingValues); boolean updatedColor = !TextUtils.isEmpty(joinedName) && setColorIfNotSetInternal(id, ContactColors.generateFor(joinedName)); @@ -2953,6 +2957,7 @@ public class RecipientDatabase extends Database { private final byte[] profileKey; private final ProfileKeyCredential profileKeyCredential; private final ProfileName systemProfileName; + private final String systemDisplayName; private final String systemContactPhoto; private final String systemPhoneLabel; private final String systemContactUri; @@ -2995,6 +3000,7 @@ public class RecipientDatabase extends Database { @Nullable byte[] profileKey, @Nullable ProfileKeyCredential profileKeyCredential, @NonNull ProfileName systemProfileName, + @Nullable String systemDisplayName, @Nullable String systemContactPhoto, @Nullable String systemPhoneLabel, @Nullable String systemContactUri, @@ -3035,6 +3041,7 @@ public class RecipientDatabase extends Database { this.profileKey = profileKey; this.profileKeyCredential = profileKeyCredential; this.systemProfileName = systemProfileName; + this.systemDisplayName = systemDisplayName; this.systemContactPhoto = systemContactPhoto; this.systemPhoneLabel = systemPhoneLabel; this.systemContactUri = systemContactUri; @@ -3142,6 +3149,10 @@ public class RecipientDatabase extends Database { return systemProfileName; } + public @NonNull String getSystemDisplayName() { + return systemDisplayName; + } + public @Nullable String getSystemContactPhotoUri() { return systemContactPhoto; } diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/ThreadDatabase.java b/app/src/main/java/org/thoughtcrime/securesms/database/ThreadDatabase.java index 5abc8abb5a..b39faba8bc 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/ThreadDatabase.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/ThreadDatabase.java @@ -1538,6 +1538,7 @@ public class ThreadDatabase extends Database { if (group != null) { RecipientDetails details = new RecipientDetails(group.getTitle(), + null, group.hasAvatar() ? Optional.of(group.getAvatarId()) : Optional.absent(), false, false, diff --git a/app/src/main/java/org/thoughtcrime/securesms/recipients/LiveRecipient.java b/app/src/main/java/org/thoughtcrime/securesms/recipients/LiveRecipient.java index 0dd6a2b094..c84969a3c3 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/recipients/LiveRecipient.java +++ b/app/src/main/java/org/thoughtcrime/securesms/recipients/LiveRecipient.java @@ -212,10 +212,10 @@ public final class LiveRecipient { avatarId = Optional.of(groupRecord.get().getAvatarId()); } - return new RecipientDetails(title, avatarId, false, false, settings, members); + return new RecipientDetails(title, null, avatarId, false, false, settings, members); } - return new RecipientDetails(null, Optional.absent(), false, false, settings, null); + return new RecipientDetails(null, null, Optional.absent(), false, false, settings, null); } synchronized void set(@NonNull Recipient recipient) { diff --git a/app/src/main/java/org/thoughtcrime/securesms/recipients/Recipient.java b/app/src/main/java/org/thoughtcrime/securesms/recipients/Recipient.java index c2159827b3..03f5109939 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/recipients/Recipient.java +++ b/app/src/main/java/org/thoughtcrime/securesms/recipients/Recipient.java @@ -110,6 +110,7 @@ public class Recipient { private final String about; private final String aboutEmoji; private final ProfileName systemProfileName; + private final String systemContactName; /** @@ -347,6 +348,7 @@ public class Recipient { this.about = null; this.aboutEmoji = null; this.systemProfileName = ProfileName.EMPTY; + this.systemContactName = null; } public Recipient(@NonNull RecipientId id, @NonNull RecipientDetails details, boolean resolved) { @@ -393,6 +395,7 @@ public class Recipient { this.about = details.about; this.aboutEmoji = details.aboutEmoji; this.systemProfileName = details.systemProfileName; + this.systemContactName = details.systemContactName; } public @NonNull RecipientId getId() { @@ -432,7 +435,7 @@ public class Recipient { */ public boolean hasAUserSetDisplayName(@NonNull Context context) { return !TextUtils.isEmpty(getGroupName(context)) || - !TextUtils.isEmpty(getSystemProfileName().toString()) || + !TextUtils.isEmpty(systemContactName) || !TextUtils.isEmpty(getProfileName().toString()); } @@ -440,7 +443,7 @@ public class Recipient { String name = getGroupName(context); if (Util.isEmpty(name)) { - name = getSystemProfileName().toString(); + name = systemContactName; } if (Util.isEmpty(name)) { @@ -466,7 +469,7 @@ public class Recipient { String name = getGroupName(context); if (Util.isEmpty(name)) { - name = getSystemProfileName().toString(); + name = systemContactName; } if (Util.isEmpty(name)) { @@ -497,7 +500,7 @@ public class Recipient { name = StringUtil.isolateBidi(name); if (Util.isEmpty(name)) { - name = isSelf ? getGroupName(context) : getSystemProfileName().toString(); + name = isSelf ? getGroupName(context) : systemContactName; name = StringUtil.isolateBidi(name); } diff --git a/app/src/main/java/org/thoughtcrime/securesms/recipients/RecipientDetails.java b/app/src/main/java/org/thoughtcrime/securesms/recipients/RecipientDetails.java index 284fcdd4e4..a328f67ade 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/recipients/RecipientDetails.java +++ b/app/src/main/java/org/thoughtcrime/securesms/recipients/RecipientDetails.java @@ -32,7 +32,8 @@ public class RecipientDetails { final String e164; final String email; final GroupId groupId; - final String groupName; + final String groupName; + final String systemContactName; final String customLabel; final Uri systemContactPhoto; final Uri contactUri; @@ -71,6 +72,7 @@ public class RecipientDetails { final ProfileName systemProfileName; public RecipientDetails(@Nullable String groupName, + @Nullable String systemContactName, @NonNull Optional groupAvatarId, boolean systemContact, boolean isSelf, @@ -119,6 +121,7 @@ public class RecipientDetails { this.aboutEmoji = settings.getAboutEmoji(); this.systemProfileName = settings.getSystemProfileName(); this.groupName = groupName; + this.systemContactName = systemContactName; } /** @@ -167,6 +170,7 @@ public class RecipientDetails { this.about = null; this.aboutEmoji = null; this.systemProfileName = ProfileName.EMPTY; + this.systemContactName = null; } public static @NonNull RecipientDetails forIndividual(@NonNull Context context, @NonNull RecipientSettings settings) { @@ -174,6 +178,6 @@ public class RecipientDetails { boolean isSelf = (settings.getE164() != null && settings.getE164().equals(TextSecurePreferences.getLocalNumber(context))) || (settings.getUuid() != null && settings.getUuid().equals(TextSecurePreferences.getLocalUuid(context))); - return new RecipientDetails(null, Optional.absent(), systemContact, isSelf, settings, null); + return new RecipientDetails(null, settings.getSystemDisplayName(), Optional.absent(), systemContact, isSelf, settings, null); } }