Skip to content

Commit 32a0197

Browse files
committed
change the way accounts are fetched
1 parent 3b90875 commit 32a0197

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ ext {
4545
}
4646

4747
dependencies {
48-
implementation 'com.simplemobiletools:commons:4.0.0'
48+
implementation 'com.simplemobiletools:commons:4.0.3'
4949
implementation 'joda-time:joda-time:2.9.9'
5050
implementation 'com.facebook.stetho:stetho:1.5.0'
5151

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.simplemobiletools.contacts.helpers
22

3+
import android.accounts.Account
34
import android.accounts.AccountManager
45
import android.app.Activity
56
import android.content.*
@@ -61,6 +62,33 @@ class ContactsHelper(val activity: Activity) {
6162
}.start()
6263
}
6364

65+
private fun getContentResolverAccounts(): HashSet<ContactSource> {
66+
val uri = ContactsContract.Data.CONTENT_URI
67+
val projection = arrayOf(
68+
ContactsContract.RawContacts.ACCOUNT_NAME,
69+
ContactsContract.RawContacts.ACCOUNT_TYPE
70+
)
71+
72+
val sources = HashSet<ContactSource>()
73+
var cursor: Cursor? = null
74+
try {
75+
cursor = activity.contentResolver.query(uri, projection, null, null, null)
76+
if (cursor?.moveToFirst() == true) {
77+
do {
78+
val name = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME) ?: ""
79+
val type = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_TYPE) ?: ""
80+
val source = ContactSource(name, type)
81+
sources.add(source)
82+
} while (cursor.moveToNext())
83+
}
84+
} catch (e: Exception) {
85+
} finally {
86+
cursor?.close()
87+
}
88+
89+
return sources
90+
}
91+
6492
private fun getDeviceContacts(contacts: SparseArray<Contact>) {
6593
if (!activity.hasContactPermissions()) {
6694
return
@@ -662,14 +690,17 @@ class ContactsHelper(val activity: Activity) {
662690
return sources
663691
}
664692

665-
val accountManager = AccountManager.get(activity)
666-
accountManager.accounts.filter { it.name.contains("@") || localAccountTypes.contains(it.type) }.forEach {
667-
if (ContentResolver.getIsSyncable(it, ContactsContract.AUTHORITY) == 1) {
693+
val accounts = AccountManager.get(activity).accounts
694+
accounts.forEach {
695+
if (ContentResolver.getIsSyncable(it, ContactsContract.AUTHORITY) == 1 && ContentResolver.getSyncAutomatically(it, ContactsContract.AUTHORITY)) {
668696
val contactSource = ContactSource(it.name, it.type)
669697
sources.add(contactSource)
670698
}
671699
}
672700

701+
val contentResolverAccounts = getContentResolverAccounts().filter { !accounts.contains(Account(it.name, it.type)) }
702+
sources.addAll(contentResolverAccounts)
703+
673704
if (sources.isEmpty() && activity.config.localAccountName.isEmpty() && activity.config.localAccountType.isEmpty()) {
674705
sources.add(ContactSource("", ""))
675706
}

0 commit comments

Comments
 (0)