From b89cf120e415d9f45492750f401011a25a43426e Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 9 May 2018 16:21:16 +0200 Subject: [PATCH] handle fetching contacts from null account_name --- .../contacts/helpers/ContactsHelper.kt | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt index b60220d7..e52181fa 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -445,7 +445,7 @@ class ContactsHelper(val activity: Activity) { return groups } - private fun getQuestionMarks() = "?,".times(displayContactSources.size).trimEnd(',') + private fun getQuestionMarks() = "?,".times(displayContactSources.filter { it.isNotEmpty() }.size).trimEnd(',') private fun getSourcesSelection(addMimeType: Boolean = false, addContactId: Boolean = false, useRawContactId: Boolean = true): String { val strings = ArrayList() @@ -456,7 +456,16 @@ class ContactsHelper(val activity: Activity) { if (addContactId) { strings.add("${if (useRawContactId) ContactsContract.Data.RAW_CONTACT_ID else ContactsContract.Data.CONTACT_ID} = ?") } else { - strings.add("${ContactsContract.RawContacts.ACCOUNT_NAME} IN (${getQuestionMarks()})") + // sometimes local device storage has null account_name, handle it properly + val accountnameString = StringBuilder() + if (displayContactSources.contains("")) { + accountnameString.append("(") + } + accountnameString.append("${ContactsContract.RawContacts.ACCOUNT_NAME} IN (${getQuestionMarks()})") + if (displayContactSources.contains("")) { + accountnameString.append(" OR ${ContactsContract.RawContacts.ACCOUNT_NAME} IS NULL)") + } + strings.add(accountnameString.toString()) } return TextUtils.join(" AND ", strings) @@ -472,7 +481,7 @@ class ContactsHelper(val activity: Activity) { if (contactId != null) { args.add(contactId.toString()) } else { - args.addAll(displayContactSources) + args.addAll(displayContactSources.filter { it.isNotEmpty() }) } return args.toTypedArray()