[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,6 +880,8 @@ class AndroidMobileContactsFacade(private val activity: MainActivity) : MobileCo
).build() ).build()
) )
// skip adding organization data if its empty to avoid duplicated organization field when linking contacts.
if (contact.role.isNotEmpty() || contact.company.isNotEmpty() || !contact.department.isNullOrEmpty()) {
ops.add( ops.add(
ContentProviderOperation.newInsert(CONTACT_DATA_URI) ContentProviderOperation.newInsert(CONTACT_DATA_URI)
.withValueBackReference(RawContacts.Data.RAW_CONTACT_ID, index) .withValueBackReference(RawContacts.Data.RAW_CONTACT_ID, index)
@ -882,6 +897,7 @@ class AndroidMobileContactsFacade(private val activity: MainActivity) : MobileCo
ContactsContract.CommonDataKinds.Organization.TYPE_WORK ContactsContract.CommonDataKinds.Organization.TYPE_WORK
).build() ).build()
) )
}
ops.add( ops.add(
ContentProviderOperation.newInsert(CONTACT_DATA_URI) ContentProviderOperation.newInsert(CONTACT_DATA_URI)