|
1 | 1 | package com.addev.listaspam.adapter |
2 | 2 |
|
| 3 | +import android.Manifest |
3 | 4 | import android.content.ClipData |
4 | 5 | import android.content.ClipboardManager |
5 | 6 | import android.content.Context |
6 | 7 | import android.content.Intent |
| 8 | +import android.content.pm.PackageManager |
7 | 9 | import android.net.Uri |
| 10 | +import android.provider.ContactsContract |
8 | 11 | import android.view.Gravity |
9 | 12 | import android.view.LayoutInflater |
10 | 13 | import android.view.View |
@@ -64,23 +67,22 @@ class CallLogAdapter( |
64 | 67 |
|
65 | 68 | fun bind(callLog: CallLogEntry, isBlocked: Boolean, isWhitelisted: Boolean = false) { |
66 | 69 | val number = callLog.number ?: "" |
| 70 | + val contactName = getContactName(context, number) |
67 | 71 | val textToShow = if (isBlocked) { |
68 | | - context.getString(R.string.blocked_text_format, number) |
| 72 | + context.getString(R.string.blocked_text_format, contactName ?: number) |
69 | 73 | } else if (isWhitelisted) { |
70 | | - context.getString(R.string.whitelisted_text_format, number) |
| 74 | + context.getString(R.string.whitelisted_text_format, contactName ?: number) |
71 | 75 | } else { |
72 | | - number |
| 76 | + contactName ?: number |
73 | 77 | } |
74 | 78 | numberTextView.text = textToShow |
75 | 79 | dateTextView.text = formatter.format(callLog.date) |
76 | 80 | durationTextView.text = context.getString(R.string.duration_label, callLog.duration) |
77 | 81 |
|
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)) |
84 | 86 | } |
85 | 87 |
|
86 | 88 | overflowMenuButton.setOnClickListener { |
@@ -151,6 +153,22 @@ class CallLogAdapter( |
151 | 153 | } |
152 | 154 | } |
153 | 155 |
|
| 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 | + |
154 | 172 | private fun clipboardAction(number: String) { |
155 | 173 | val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager |
156 | 174 | val clip = ClipData.newPlainText("phone number", number) |
|
0 commit comments