mirror of
https://github.com/SimpleMobileTools/Simple-Contacts.git
synced 2025-06-05 21:59:27 +02:00
change the way accounts are fetched
This commit is contained in:
@ -45,7 +45,7 @@ ext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'com.simplemobiletools:commons:4.0.0'
|
implementation 'com.simplemobiletools:commons:4.0.3'
|
||||||
implementation 'joda-time:joda-time:2.9.9'
|
implementation 'joda-time:joda-time:2.9.9'
|
||||||
implementation 'com.facebook.stetho:stetho:1.5.0'
|
implementation 'com.facebook.stetho:stetho:1.5.0'
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.simplemobiletools.contacts.helpers
|
package com.simplemobiletools.contacts.helpers
|
||||||
|
|
||||||
|
import android.accounts.Account
|
||||||
import android.accounts.AccountManager
|
import android.accounts.AccountManager
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.content.*
|
import android.content.*
|
||||||
@ -61,6 +62,33 @@ class ContactsHelper(val activity: Activity) {
|
|||||||
}.start()
|
}.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>) {
|
private fun getDeviceContacts(contacts: SparseArray<Contact>) {
|
||||||
if (!activity.hasContactPermissions()) {
|
if (!activity.hasContactPermissions()) {
|
||||||
return
|
return
|
||||||
@ -662,14 +690,17 @@ class ContactsHelper(val activity: Activity) {
|
|||||||
return sources
|
return sources
|
||||||
}
|
}
|
||||||
|
|
||||||
val accountManager = AccountManager.get(activity)
|
val accounts = AccountManager.get(activity).accounts
|
||||||
accountManager.accounts.filter { it.name.contains("@") || localAccountTypes.contains(it.type) }.forEach {
|
accounts.forEach {
|
||||||
if (ContentResolver.getIsSyncable(it, ContactsContract.AUTHORITY) == 1) {
|
if (ContentResolver.getIsSyncable(it, ContactsContract.AUTHORITY) == 1 && ContentResolver.getSyncAutomatically(it, ContactsContract.AUTHORITY)) {
|
||||||
val contactSource = ContactSource(it.name, it.type)
|
val contactSource = ContactSource(it.name, it.type)
|
||||||
sources.add(contactSource)
|
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()) {
|
if (sources.isEmpty() && activity.config.localAccountName.isEmpty() && activity.config.localAccountType.isEmpty()) {
|
||||||
sources.add(ContactSource("", ""))
|
sources.add(ContactSource("", ""))
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user