[Android] Fix duplicated empty Organization fields in contacts

Adding empty organization data (company, department and title) to
contacts will show empty `Organization` filed in contacts and leads to
multiple empty Organization fields when the same contacts linked.

Fixed by adding `Organization` field only when there is valid data from
server.

Close #7607
This commit is contained in:
bir 2025-10-10 15:01:34 +02:00 committed by paw
parent dd4f0eb408
commit b1300345d4

View file

@ -339,6 +339,15 @@ class AndroidMobileContactsFacade(private val activity: MainActivity) : MobileCo
ContentResolver.setSyncAutomatically(tutaAccount, ContactsContract.AUTHORITY, false) ContentResolver.setSyncAutomatically(tutaAccount, ContactsContract.AUTHORITY, false)
} }
private fun hasDepartmentChanged(
storedContact: AndroidContact,
serverContact: StructuredContact
): Boolean {
// isNullOrEmpty check is required since department field is a nullableString, comparing the empty string with
// null value would fail.
return (storedContact.department != serverContact.department && !storedContact.department.isNullOrEmpty())
}
private fun checkContactDetails( private fun checkContactDetails(
storedContact: AndroidContact, storedContact: AndroidContact,
serverContact: StructuredContact, serverContact: StructuredContact,
@ -348,7 +357,11 @@ class AndroidMobileContactsFacade(private val activity: MainActivity) : MobileCo
checkContactBirthday(storedContact, ops, serverContact) checkContactBirthday(storedContact, ops, serverContact)
} }
if (storedContact.company != serverContact.company || storedContact.role != serverContact.role || storedContact.department != serverContact.department) { if (storedContact.company != serverContact.company || storedContact.role != serverContact.role || hasDepartmentChanged(
storedContact,
serverContact
)
) {
checkContactCompany(storedContact, ops, serverContact) checkContactCompany(storedContact, ops, serverContact)
} }
@ -867,21 +880,24 @@ class AndroidMobileContactsFacade(private val activity: MainActivity) : MobileCo
).build() ).build()
) )
ops.add( // skip adding organization data if its empty to avoid duplicated organization field when linking contacts.
ContentProviderOperation.newInsert(CONTACT_DATA_URI) if (contact.role.isNotEmpty() || contact.company.isNotEmpty() || !contact.department.isNullOrEmpty()) {
.withValueBackReference(RawContacts.Data.RAW_CONTACT_ID, index) ops.add(
.withValue(RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE) ContentProviderOperation.newInsert(CONTACT_DATA_URI)
.withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, contact.company) .withValueBackReference(RawContacts.Data.RAW_CONTACT_ID, index)
.withValue(RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE) .withValue(RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Organization.DEPARTMENT, contact.department) .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, contact.company)
.withValue(RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE) .withValue(RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Organization.TITLE, contact.role) .withValue(ContactsContract.CommonDataKinds.Organization.DEPARTMENT, contact.department)
.withValue(RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE) .withValue(RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
.withValue( .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, contact.role)
ContactsContract.CommonDataKinds.Organization.TYPE, .withValue(RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
ContactsContract.CommonDataKinds.Organization.TYPE_WORK .withValue(
).build() ContactsContract.CommonDataKinds.Organization.TYPE,
) ContactsContract.CommonDataKinds.Organization.TYPE_WORK
).build()
)
}
ops.add( ops.add(
ContentProviderOperation.newInsert(CONTACT_DATA_URI) ContentProviderOperation.newInsert(CONTACT_DATA_URI)