change the way accounts are fetched
This commit is contained in:
parent
3b908751ca
commit
32a0197f1f
2 changed files with 35 additions and 4 deletions
|
@ -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'
|
||||
|
||||
|
|
|
@ -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<ContactSource> {
|
||||
val uri = ContactsContract.Data.CONTENT_URI
|
||||
val projection = arrayOf(
|
||||
ContactsContract.RawContacts.ACCOUNT_NAME,
|
||||
ContactsContract.RawContacts.ACCOUNT_TYPE
|
||||
)
|
||||
|
||||
val sources = HashSet<ContactSource>()
|
||||
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<Contact>) {
|
||||
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("", ""))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue