Skip to content

Commit fcedd3d

Browse files
committed
feat: add sync method for checking known contacts
1 parent 74502a9 commit fcedd3d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

commons/src/main/kotlin/org/fossify/commons/helpers/SimpleContactsHelper.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,24 @@ class SimpleContactsHelper(val context: Context) {
396396
}
397397
}
398398
}
399+
400+
/**
401+
* Synchronous version of [exists] that checks if a number exists in contacts.
402+
*/
403+
fun existsSync(number: String, privateCursor: Cursor? = null): Boolean {
404+
if (number.isEmpty()) return false
405+
if (context.hasPermission(PERMISSION_READ_CONTACTS)) {
406+
try {
407+
val uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number))
408+
context.contentResolver.query(uri, arrayOf(PhoneLookup._ID), null, null, null)
409+
?.use { cursor ->
410+
if (cursor.moveToFirst()) return true
411+
}
412+
} catch (ignored: Exception) {
413+
}
414+
}
415+
416+
val privateContacts = MyContactsContentProvider.getSimpleContacts(context, privateCursor)
417+
return privateContacts.any { it.doesHavePhoneNumber(number) }
418+
}
399419
}

0 commit comments

Comments
 (0)