Skip to content

Commit 1eafa09

Browse files
committed
Add contact name instead of phone number if it's saved in agenda
1 parent c625e6a commit 1eafa09

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

app/src/main/java/com/addev/listaspam/adapter/CallLogAdapter.kt

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.addev.listaspam.adapter
22

3+
import android.Manifest
34
import android.content.ClipData
45
import android.content.ClipboardManager
56
import android.content.Context
67
import android.content.Intent
8+
import android.content.pm.PackageManager
79
import android.net.Uri
10+
import android.provider.ContactsContract
811
import android.view.Gravity
912
import android.view.LayoutInflater
1013
import android.view.View
@@ -64,23 +67,22 @@ class CallLogAdapter(
6467

6568
fun bind(callLog: CallLogEntry, isBlocked: Boolean, isWhitelisted: Boolean = false) {
6669
val number = callLog.number ?: ""
70+
val contactName = getContactName(context, number)
6771
val textToShow = if (isBlocked) {
68-
context.getString(R.string.blocked_text_format, number)
72+
context.getString(R.string.blocked_text_format, contactName ?: number)
6973
} else if (isWhitelisted) {
70-
context.getString(R.string.whitelisted_text_format, number)
74+
context.getString(R.string.whitelisted_text_format, contactName ?: number)
7175
} else {
72-
number
76+
contactName ?: number
7377
}
7478
numberTextView.text = textToShow
7579
dateTextView.text = formatter.format(callLog.date)
7680
durationTextView.text = context.getString(R.string.duration_label, callLog.duration)
7781

78-
if (isBlocked) {
79-
numberTextView.setTextColor(ContextCompat.getColor(context, android.R.color.holo_red_light))
80-
} else if (isWhitelisted) {
81-
numberTextView.setTextColor(ContextCompat.getColor(context, android.R.color.holo_blue_dark))
82-
} else {
83-
numberTextView.setTextColor(ContextCompat.getColor(context, android.R.color.darker_gray))
82+
when {
83+
isBlocked -> numberTextView.setTextColor(ContextCompat.getColor(context, android.R.color.holo_red_light))
84+
isWhitelisted -> numberTextView.setTextColor(ContextCompat.getColor(context, android.R.color.holo_blue_dark))
85+
else -> numberTextView.setTextColor(ContextCompat.getColor(context, android.R.color.darker_gray))
8486
}
8587

8688
overflowMenuButton.setOnClickListener {
@@ -151,6 +153,22 @@ class CallLogAdapter(
151153
}
152154
}
153155

156+
private fun getContactName(context: Context, phoneNumber: String): String? {
157+
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
158+
val contentResolver = context.contentResolver
159+
val uri = ContactsContract.PhoneLookup.CONTENT_FILTER_URI.buildUpon().appendPath(phoneNumber).build()
160+
val projection = arrayOf(ContactsContract.PhoneLookup.DISPLAY_NAME)
161+
162+
contentResolver.query(uri, projection, null, null, null)?.use { cursor ->
163+
if (cursor.moveToFirst()) {
164+
return cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME))
165+
}
166+
}
167+
}
168+
169+
return null
170+
}
171+
154172
private fun clipboardAction(number: String) {
155173
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
156174
val clip = ClipData.newPlainText("phone number", number)

0 commit comments

Comments
 (0)