diff --git a/app/build.gradle b/app/build.gradle index ca15f081..dfe8c477 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -45,7 +45,7 @@ ext { } dependencies { - implementation 'com.simplemobiletools:commons:4.0.0' + implementation 'com.simplemobiletools:commons:4.0.3' implementation 'joda-time:joda-time:2.9.9' implementation 'com.facebook.stetho:stetho:1.5.0' 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 e52181fa..7ffa94f4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -1,5 +1,6 @@ package com.simplemobiletools.contacts.helpers +import android.accounts.Account import android.accounts.AccountManager import android.app.Activity import android.content.* @@ -61,6 +62,33 @@ class ContactsHelper(val activity: Activity) { }.start() } + private fun getContentResolverAccounts(): HashSet { + val uri = ContactsContract.Data.CONTENT_URI + val projection = arrayOf( + ContactsContract.RawContacts.ACCOUNT_NAME, + ContactsContract.RawContacts.ACCOUNT_TYPE + ) + + val sources = HashSet() + var cursor: Cursor? = null + try { + cursor = activity.contentResolver.query(uri, projection, null, null, null) + if (cursor?.moveToFirst() == true) { + do { + val name = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME) ?: "" + val type = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_TYPE) ?: "" + val source = ContactSource(name, type) + sources.add(source) + } while (cursor.moveToNext()) + } + } catch (e: Exception) { + } finally { + cursor?.close() + } + + return sources + } + private fun getDeviceContacts(contacts: SparseArray) { if (!activity.hasContactPermissions()) { return @@ -662,14 +690,17 @@ class ContactsHelper(val activity: Activity) { return sources } - val accountManager = AccountManager.get(activity) - accountManager.accounts.filter { it.name.contains("@") || localAccountTypes.contains(it.type) }.forEach { - if (ContentResolver.getIsSyncable(it, ContactsContract.AUTHORITY) == 1) { + val accounts = AccountManager.get(activity).accounts + accounts.forEach { + if (ContentResolver.getIsSyncable(it, ContactsContract.AUTHORITY) == 1 && ContentResolver.getSyncAutomatically(it, ContactsContract.AUTHORITY)) { val contactSource = ContactSource(it.name, it.type) sources.add(contactSource) } } + val contentResolverAccounts = getContentResolverAccounts().filter { !accounts.contains(Account(it.name, it.type)) } + sources.addAll(contentResolverAccounts) + if (sources.isEmpty() && activity.config.localAccountName.isEmpty() && activity.config.localAccountType.isEmpty()) { sources.add(ContactSource("", "")) }