@@ -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