Skip to content

Commit b89cf12

Browse files
committed
handle fetching contacts from null account_name
1 parent 24d1934 commit b89cf12

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ class ContactsHelper(val activity: Activity) {
445445
return groups
446446
}
447447

448-
private fun getQuestionMarks() = "?,".times(displayContactSources.size).trimEnd(',')
448+
private fun getQuestionMarks() = "?,".times(displayContactSources.filter { it.isNotEmpty() }.size).trimEnd(',')
449449

450450
private fun getSourcesSelection(addMimeType: Boolean = false, addContactId: Boolean = false, useRawContactId: Boolean = true): String {
451451
val strings = ArrayList<String>()
@@ -456,7 +456,16 @@ class ContactsHelper(val activity: Activity) {
456456
if (addContactId) {
457457
strings.add("${if (useRawContactId) ContactsContract.Data.RAW_CONTACT_ID else ContactsContract.Data.CONTACT_ID} = ?")
458458
} else {
459-
strings.add("${ContactsContract.RawContacts.ACCOUNT_NAME} IN (${getQuestionMarks()})")
459+
// sometimes local device storage has null account_name, handle it properly
460+
val accountnameString = StringBuilder()
461+
if (displayContactSources.contains("")) {
462+
accountnameString.append("(")
463+
}
464+
accountnameString.append("${ContactsContract.RawContacts.ACCOUNT_NAME} IN (${getQuestionMarks()})")
465+
if (displayContactSources.contains("")) {
466+
accountnameString.append(" OR ${ContactsContract.RawContacts.ACCOUNT_NAME} IS NULL)")
467+
}
468+
strings.add(accountnameString.toString())
460469
}
461470

462471
return TextUtils.join(" AND ", strings)
@@ -472,7 +481,7 @@ class ContactsHelper(val activity: Activity) {
472481
if (contactId != null) {
473482
args.add(contactId.toString())
474483
} else {
475-
args.addAll(displayContactSources)
484+
args.addAll(displayContactSources.filter { it.isNotEmpty() })
476485
}
477486

478487
return args.toTypedArray()

0 commit comments

Comments
 (0)