Merge pull request #1421 from sdex/check_contact_exists

Check if the number exists in the contacts and the private contacts
This commit is contained in:
Tibor Kaputa 2022-05-27 16:55:41 +02:00 committed by GitHub
commit 10c8ac2f1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -380,22 +380,17 @@ class SimpleContactsHelper(val context: Context) {
}
}
fun exists(number: String): Boolean {
if (!context.hasPermission(PERMISSION_READ_CONTACTS)) {
return false
}
val uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number))
val projection = arrayOf(PhoneLookup._ID)
try {
val cursor = context.contentResolver.query(uri, projection, null, null, null)
cursor?.use {
return it.moveToFirst()
fun exists(number: String, callback: (Boolean) -> Unit) {
val privateCursor = context.getMyContactsCursor(false, true)
SimpleContactsHelper(context).getAvailableContacts(false) { contacts ->
val contact = contacts.firstOrNull { it.doesHavePhoneNumber(number) }
if (contact != null) {
callback.invoke(true)
} else {
val privateContacts = MyContactsContentProvider.getSimpleContacts(context, privateCursor)
val privateContact = privateContacts.firstOrNull { it.doesHavePhoneNumber(number) }
callback.invoke(privateContact != null)
}
} catch (ignored: Exception) {
}
return false
}
}