avoid some unnecessary cursor parsing at fetching contact names

This commit is contained in:
tibbi 2020-04-03 16:21:12 +02:00
parent 862cc56f29
commit c1363e4d22
1 changed files with 14 additions and 10 deletions

View File

@ -134,20 +134,24 @@ class MainActivity : SimpleActivity() {
if (cursor?.moveToFirst() == true) {
do {
val mimetype = cursor.getStringValue(ContactsContract.Data.MIMETYPE)
val firstName = cursor.getStringValue(CommonDataKinds.StructuredName.GIVEN_NAME) ?: ""
val middleName = cursor.getStringValue(CommonDataKinds.StructuredName.MIDDLE_NAME) ?: ""
val familyName = cursor.getStringValue(CommonDataKinds.StructuredName.FAMILY_NAME) ?: ""
val isPerson = mimetype == CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE
if (isPerson && (firstName.isNotEmpty() || middleName.isNotEmpty() || familyName.isNotEmpty())) {
val names = arrayOf(firstName, middleName, familyName).filter { it.isNotEmpty() }
return TextUtils.join(" ", names)
if (isPerson) {
val firstName = cursor.getStringValue(CommonDataKinds.StructuredName.GIVEN_NAME) ?: ""
val middleName = cursor.getStringValue(CommonDataKinds.StructuredName.MIDDLE_NAME) ?: ""
val familyName = cursor.getStringValue(CommonDataKinds.StructuredName.FAMILY_NAME) ?: ""
if (firstName.isNotEmpty() || middleName.isNotEmpty() || familyName.isNotEmpty()) {
val names = arrayOf(firstName, middleName, familyName).filter { it.isNotEmpty() }
return TextUtils.join(" ", names)
}
}
val isOrganization = mimetype == CommonDataKinds.Organization.CONTENT_ITEM_TYPE
val company = cursor.getStringValue(CommonDataKinds.Organization.COMPANY) ?: ""
val jobTitle = cursor.getStringValue(CommonDataKinds.Organization.TITLE) ?: ""
if (isOrganization && (company.isNotEmpty() || jobTitle.isNotEmpty())) {
return "$company $jobTitle".trim()
if (isOrganization) {
val company = cursor.getStringValue(CommonDataKinds.Organization.COMPANY) ?: ""
val jobTitle = cursor.getStringValue(CommonDataKinds.Organization.TITLE) ?: ""
if (company.isNotEmpty() || jobTitle.isNotEmpty()) {
return "$company $jobTitle".trim()
}
}
} while (cursor.moveToNext())
}