Set structured name when creating contacts.

Fixes #12305. When creating copies of contacts, set the structured name
fields instead of the unstructured name fields. This fixes bugs where
certain types of names are displayed incorrectly because the structured
name is inaccurately reconstructed from the unstructured name.

Closes #13366
This commit is contained in:
Pratyush Venkatakrishnan 2024-01-16 14:47:29 -05:00 committed by Greyson Parrelli
parent c54313c32e
commit cf89c988cf
2 changed files with 40 additions and 9 deletions

View file

@ -37,7 +37,7 @@ class ContactListViewModel(application: Application) : AndroidViewModel(applicat
val contactList: List<ContactDetails> = SystemContactsRepository.getAllSystemContacts(
context = application,
e164Formatter = { number -> PhoneNumberUtils.formatNumberToE164(number, "US") ?: number }
).use { it.toList() }
).use { it.toList().sortedBy { c -> c.givenName } }
_contacts.postValue(contactList)
} else {

View file

@ -488,7 +488,12 @@ object SystemContactsRepository {
// Data entry for name
ContentProviderOperation.newInsert(dataUri)
.withValueBackReference(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, operationIndex)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, systemContactInfo.displayName)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, systemContactInfo.name.displayName)
.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, systemContactInfo.name.givenName)
.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, systemContactInfo.name.familyName)
.withValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX, systemContactInfo.name.prefix)
.withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, systemContactInfo.name.suffix)
.withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, systemContactInfo.name.middleName)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.build(),
@ -596,13 +601,37 @@ object SystemContactsRepository {
val systemNumber: String? = contactCursor.requireString(ContactsContract.PhoneLookup.NUMBER)
if (systemNumber != null && e164Formatter(systemNumber) == e164) {
val phoneLookupId = contactCursor.requireLong(ContactsContract.PhoneLookup._ID)
val idProjection = arrayOf(ContactsContract.RawContacts._ID)
val idSelection = "${ContactsContract.RawContacts.CONTACT_ID} = ? "
val idArgs = SqlUtil.buildArgs(phoneLookupId)
context.contentResolver.query(ContactsContract.RawContacts.CONTENT_URI, arrayOf(ContactsContract.RawContacts._ID), "${ContactsContract.RawContacts.CONTACT_ID} = ? ", SqlUtil.buildArgs(phoneLookupId), null)?.use { idCursor ->
context.contentResolver.query(ContactsContract.RawContacts.CONTENT_URI, idProjection, idSelection, idArgs, null)?.use { idCursor ->
if (idCursor.moveToNext()) {
val rawContactId = idCursor.requireLong(ContactsContract.RawContacts._ID)
val nameProjection = arrayOf(
ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
ContactsContract.CommonDataKinds.StructuredName.PREFIX,
ContactsContract.CommonDataKinds.StructuredName.SUFFIX,
ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME
)
val nameSelection = "${ContactsContract.Data.RAW_CONTACT_ID} = ? AND ${ContactsContract.Data.MIMETYPE} = ?"
val nameArgs = SqlUtil.buildArgs(rawContactId, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
context.contentResolver.query(ContactsContract.Data.CONTENT_URI, nameProjection, nameSelection, nameArgs, null)?.use { nameCursor ->
if (nameCursor.moveToNext()) {
return SystemContactInfo(
displayName = contactCursor.requireString(ContactsContract.PhoneLookup.DISPLAY_NAME),
name = NameDetails(
displayName = nameCursor.requireString(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME),
givenName = nameCursor.requireString(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME),
familyName = nameCursor.requireString(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME),
prefix = nameCursor.requireString(ContactsContract.CommonDataKinds.StructuredName.PREFIX),
suffix = nameCursor.requireString(ContactsContract.CommonDataKinds.StructuredName.SUFFIX),
middleName = nameCursor.requireString(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME)
),
displayPhone = systemNumber,
siblingRawContactId = idCursor.requireLong(ContactsContract.RawContacts._ID),
siblingRawContactId = rawContactId,
type = contactCursor.requireInt(ContactsContract.PhoneLookup.TYPE)
)
}
@ -610,6 +639,8 @@ object SystemContactsRepository {
}
}
}
}
}
return null
}
@ -824,7 +855,7 @@ object SystemContactsRepository {
)
private data class SystemContactInfo(
val displayName: String?,
val name: NameDetails,
val displayPhone: String,
val siblingRawContactId: Long,
val type: Int