@@ -162,13 +162,10 @@ class SpamUtils {
162162 return @launch
163163 }
164164
165- if (details != null ) {
166- val isNumberInAgenda = isNumberInAgenda(details)
167-
168- // Don't check number if is in contacts
169- if (isNumberInAgenda) {
170- return @launch
171- }
165+ // Don't check number if is in contacts
166+ val isNumberInAgenda = isNumberInAgenda(context, number)
167+ if (isNumberInAgenda) {
168+ return @launch
172169 }
173170
174171 if (shouldBlockNonContacts(context)) {
@@ -347,15 +344,42 @@ class SpamUtils {
347344 * Checks if a phone number exists in the device's contact agenda.
348345 *
349346 * This function determines whether a given phone number is associated with
350- * a contact stored in the user's address book by checking if the contact
351- * display name is available.
347+ * a contact stored in the user's address book by querying the contacts database.
352348 *
353- * @param details The call/message details object containing contact information
354- * @return true if the number is found in the agenda (has a contact display name),
355- * false otherwise
349+ * @param context Context for accessing content resolver
350+ * @param phoneNumber The phone number to check
351+ * @return true if the number is found in the contacts, false otherwise
356352 */
357- private fun isNumberInAgenda (details : Call .Details ): Boolean {
358- return details.getContactDisplayName() != null
353+ private fun isNumberInAgenda (context : Context , phoneNumber : String ): Boolean {
354+ if (ActivityCompat .checkSelfPermission(
355+ context,
356+ Manifest .permission.READ_CONTACTS
357+ ) != PackageManager .PERMISSION_GRANTED
358+ ) {
359+ return false
360+ }
361+
362+ val uri = Uri .withAppendedPath(
363+ ContactsContract .PhoneLookup .CONTENT_FILTER_URI ,
364+ Uri .encode(phoneNumber)
365+ )
366+
367+ var cursor: Cursor ? = null
368+ try {
369+ cursor = context.contentResolver.query(
370+ uri,
371+ arrayOf(ContactsContract .PhoneLookup .DISPLAY_NAME ),
372+ null ,
373+ null ,
374+ null
375+ )
376+ return cursor != null && cursor.moveToFirst()
377+ } catch (e: Exception ) {
378+ e.printStackTrace()
379+ return false
380+ } finally {
381+ cursor?.close()
382+ }
359383 }
360384
361385 // ...scraper logic removed...
0 commit comments