diff --git a/app/build.gradle b/app/build.gradle index 1e61225f..bdd914c4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -45,7 +45,7 @@ ext { } dependencies { - implementation 'com.simplemobiletools:commons:3.20.5' + implementation 'com.simplemobiletools:commons:3.20.6' implementation 'joda-time:joda-time:2.9.9' implementation 'com.facebook.stetho:stetho:1.5.0' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 65dff99d..f8b1cd21 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,6 +9,8 @@ + + ) -> Unit) { Thread { - val sources = LinkedHashSet() - getDeviceContactSources(sources) + val sources = getDeviceContactSources() sources.add(ContactSource(activity.getString(R.string.phone_storage_hidden), SMT_PRIVATE)) callback(ArrayList(sources)) }.start() } - private fun getDeviceContactSources(sources: LinkedHashSet) { + private fun getDeviceContactSources(): LinkedHashSet { + val sources = LinkedHashSet() if (!activity.hasContactPermissions()) { - return + return sources } - val uri = ContactsContract.RawContacts.CONTENT_URI - val projection = arrayOf(ContactsContract.RawContacts.ACCOUNT_NAME, ContactsContract.RawContacts.ACCOUNT_TYPE) - - 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) ?: continue - val type = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_TYPE) ?: continue - val contactSource = ContactSource(name, type) - sources.add(contactSource) - } while (cursor.moveToNext()) + val accountManager = AccountManager.get(activity) + accountManager.accounts.filter { it.name.contains("@") || localAccountTypes.contains(it.type) }.forEach { + if (ContentResolver.getIsSyncable(it, ContactsContract.Contacts.CONTENT_URI.authority) == 1) { + val contactSource = ContactSource(it.name, it.type) + sources.add(contactSource) } - } catch (e: Exception) { - activity.showErrorToast(e) - } finally { - cursor?.close() } if (sources.isEmpty() && activity.config.localAccountName.isEmpty() && activity.config.localAccountType.isEmpty()) { sources.add(ContactSource("", "")) } + + return sources } - private fun getContactSourceType(accountName: String): String { - if (accountName.isEmpty()) { - return "" - } - - val uri = ContactsContract.RawContacts.CONTENT_URI - val projection = arrayOf(ContactsContract.RawContacts.ACCOUNT_TYPE) - val selection = "${ContactsContract.RawContacts.ACCOUNT_NAME} = ?" - val selectionArgs = arrayOf(accountName) - - var cursor: Cursor? = null - try { - cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null) - if (cursor?.moveToFirst() == true) { - return cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_TYPE) - } - } finally { - cursor?.close() - } - return "" - } + private fun getContactSourceType(accountName: String) = getDeviceContactSources().firstOrNull { it.name == accountName }?.type ?: "" private fun getContactProjection() = arrayOf( ContactsContract.Data.CONTACT_ID,