When showing name of contact, add label when appropriate

This commit is contained in:
Juha Heinanen
2026-06-01 11:33:52 +03:00
parent dd40777f36
commit bd7766b2cc
7 changed files with 55 additions and 31 deletions
@@ -1953,8 +1953,10 @@ class BaresipService: Service() {
if (ua.account.blockUnknown && Contact.contactName(e164Uri) == e164Uri) { if (ua.account.blockUnknown && Contact.contactName(e164Uri) == e164Uri) {
Log.d(TAG, "Auto-rejecting incoming PSTN call from $uri") Log.d(TAG, "Auto-rejecting incoming PSTN call from $uri")
telecomCall.disconnect() telecomCall.disconnect()
toast(String.format(getString(R.string.call_blocked), toast(
Utils.friendlyUri(this, uri, ua.account))) String.format(getString(R.string.call_blocked),
Utils.friendlyUri(this, uri, ua.account))
)
if (ua.account.callHistory) { if (ua.account.callHistory) {
Blocked( Blocked(
ua.account.aor, ua.account.aor,
@@ -165,8 +165,7 @@ private fun CallDetailsContent(
@Composable @Composable
private fun Peer(ctx: Context, callRow: CallRow) { private fun Peer(ctx: Context, callRow: CallRow) {
val account = Account.ofAor(callRow.aor)!! val account = Account.ofAor(callRow.aor)!!
val headerText = stringResource(R.string.peer) + " " + val headerText = stringResource(R.string.peer) + " " + Utils.friendlyUri(ctx, callRow.peerUri, account)
Utils.friendlyUri(ctx, callRow.peerUri, account)
Text( Text(
text = headerText, text = headerText,
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
@@ -335,17 +335,17 @@ private fun Calls(
val intent = Intent(ctx, MainActivity::class.java) val intent = Intent(ctx, MainActivity::class.java)
intent.putExtra("uap", ua.uap) intent.putExtra("uap", ua.uap)
intent.putExtra("peer", peerUri) intent.putExtra("peer", peerUri)
val peerName = Utils.friendlyUri(ctx, peerUri, ua.account) val peerNameWithLabel = Utils.friendlyUri(ctx, peerUri, ua.account)
val contact = Contact.findContact(peerUri) val contact = Contact.findContact(peerUri)
if (contact is Contact.BaresipContact && contact.email.isNotEmpty()) if (contact is Contact.BaresipContact && contact.email.isNotEmpty())
message.value = String.format( message.value = String.format(
ctx.getString(R.string.contact_email_action_question), ctx.getString(R.string.contact_email_action_question),
peerName peerNameWithLabel
) )
else else
message.value = String.format( message.value = String.format(
ctx.getString(R.string.contact_action_question), ctx.getString(R.string.contact_action_question),
peerName peerNameWithLabel
) )
secondButtonText.value = ctx.getString(R.string.call) secondButtonText.value = ctx.getString(R.string.call)
secondAction.value = { secondAction.value = {
@@ -401,7 +401,8 @@ private fun Calls(
showDialog.value = true showDialog.value = true
}, },
onLongClick = { onLongClick = {
val peerName = Utils.friendlyUri(ctx, peerUri, ua.account) val peerName = Utils.friendlyUri(ctx, peerUri, ua.account, includeLabel = false)
val peerNameWithLabel = Utils.friendlyUri(ctx, peerUri, ua.account)
val callText: String = if (callRow.details.size > 1) val callText: String = if (callRow.details.size > 1)
ctx.getString(R.string.calls_calls) ctx.getString(R.string.calls_calls)
else else
@@ -410,7 +411,7 @@ private fun Calls(
if (contactExists) { if (contactExists) {
message.value = String.format( message.value = String.format(
ctx.getString(R.string.calls_delete_question), ctx.getString(R.string.calls_delete_question),
peerName, callText peerNameWithLabel, callText
) )
secondButtonText.value = "" secondButtonText.value = ""
lastButtonText.value = ctx.getString(R.string.delete) lastButtonText.value = ctx.getString(R.string.delete)
@@ -421,7 +422,7 @@ private fun Calls(
else { else {
message.value = String.format( message.value = String.format(
ctx.getString(R.string.calls_add_delete_question), ctx.getString(R.string.calls_add_delete_question),
peerName, callText peerNameWithLabel, callText
) )
secondButtonText.value = ctx.getString(R.string.add_contact) secondButtonText.value = ctx.getString(R.string.add_contact)
secondAction.value = { secondAction.value = {
@@ -221,7 +221,8 @@ private fun TopAppBar(
TopAppBar( TopAppBar(
title = { title = {
Text( Text(
text = format(ctx.getString(R.string.chat_with), Utils.friendlyUri(ctx, peerUri, account)), text = format(ctx.getString(R.string.chat_with),
Utils.friendlyUri(ctx, peerUri, account)),
fontSize = 22.sp, fontSize = 22.sp,
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
) )
@@ -347,13 +347,14 @@ private fun Chats(
navController.navigate("chat/${aor}/${message.peerUri}") navController.navigate("chat/${aor}/${message.peerUri}")
}, },
onLongClick = { onLongClick = {
val peer = Utils.friendlyUri(ctx, message.peerUri, account) val peerName = Utils.friendlyUri(ctx, message.peerUri, account, includeLabel = false)
val peerNameWithLabel = Utils.friendlyUri(ctx, message.peerUri, account)
val contactExists = val contactExists =
Contact.nameExists(peer, BaresipService.contacts, false) Contact.nameExists(peerName, BaresipService.contacts, false)
if (contactExists) { if (contactExists) {
dialogMessage.value = String.format( dialogMessage.value = String.format(
ctx.getString(R.string.short_chat_question), ctx.getString(R.string.short_chat_question),
peer peerNameWithLabel
) )
secondButtonText.value = "" secondButtonText.value = ""
lastButtonText.value = ctx.getString(R.string.delete) lastButtonText.value = ctx.getString(R.string.delete)
@@ -362,7 +363,10 @@ private fun Chats(
} }
} else { } else {
dialogMessage.value = dialogMessage.value =
String.format(ctx.getString(R.string.long_chat_question), peer) String.format(
ctx.getString(R.string.long_chat_question),
peerName
)
secondButtonText.value = ctx.getString(R.string.delete) secondButtonText.value = ctx.getString(R.string.delete)
secondAction.value = { secondAction.value = {
deleteMessages(uaMessages, account, message.peerUri) deleteMessages(uaMessages, account, message.peerUri)
@@ -382,7 +386,7 @@ private fun Chats(
else else
MaterialTheme.colorScheme.primaryContainer MaterialTheme.colorScheme.primaryContainer
) { ) {
val peer = Utils.friendlyUri(ctx, message.peerUri, account) val peerName = Utils.friendlyUri(ctx, message.peerUri, account)
val cal = GregorianCalendar() val cal = GregorianCalendar()
cal.timeInMillis = message.timeStamp cal.timeInMillis = message.timeStamp
val fmt: DateFormat = if (isToday(message.timeStamp)) val fmt: DateFormat = if (isToday(message.timeStamp))
@@ -396,7 +400,7 @@ private fun Chats(
else else
MaterialTheme.colorScheme.onPrimaryContainer MaterialTheme.colorScheme.onPrimaryContainer
Row { Row {
Text(text = peer, color = textColor, fontSize = 12.sp) Text(text = peerName, color = textColor, fontSize = 12.sp)
Spacer(modifier = Modifier.weight(1f)) Spacer(modifier = Modifier.weight(1f))
Text(text = info, color = textColor, fontSize = 12.sp) Text(text = info, color = textColor, fontSize = 12.sp)
} }
@@ -64,16 +64,28 @@ sealed class Contact {
companion object { companion object {
// Return contact name of uri or uri itself if contact with uri is not found // Return contact name of uri or uri itself if contact with uri is not found
fun contactName(uri: String): String { fun contactName(uri: String, includeLabel: Boolean = false): String {
var contact = findContact(uri) val contactWithUri = findContactWithUri(uri)
if (contact == null) { if (contactWithUri != null) {
val userPart = Utils.uriUserPart(uri).replace("%23", "#") val name = contactWithUri.first.name()
if (Utils.isTelNumber(userPart)) { if (includeLabel) {
contact = findContact("tel:$userPart") val label = contactWithUri.second.label
return if (label.isNotEmpty()) "$name $label" else name
}
return name
}
val userPart = Utils.uriUserPart(uri).replace("%23", "#")
if (Utils.isTelNumber(userPart)) {
val telContactWithUri = findContactWithUri("tel:$userPart")
if (telContactWithUri != null) {
val name = telContactWithUri.first.name()
if (includeLabel) {
val label = telContactWithUri.second.label
return if (label.isNotEmpty()) "$name $label" else name
}
return name
} }
} }
if (contact != null)
return contact.name()
return uri return uri
} }
@@ -120,14 +132,14 @@ sealed class Contact {
return emptyList() return emptyList()
} }
fun findContact(uri: String): Contact? { fun findContactWithUri(uri: String): Pair<Contact, ContactUri>? {
synchronized(BaresipService.contacts) { synchronized(BaresipService.contacts) {
for (c in BaresipService.contacts) for (c in BaresipService.contacts)
when (c) { when (c) {
is BaresipContact -> { is BaresipContact -> {
for (u in c.uris) for (u in c.uris)
if (Utils.uriMatch(u.uri, uri)) if (Utils.uriMatch(u.uri, uri))
return c return Pair(c, u)
} }
is AndroidContact -> { is AndroidContact -> {
val cleanUri = uri.filterNot { setOf('-', ' ', '(', ')').contains(it) } val cleanUri = uri.filterNot { setOf('-', ' ', '(', ')').contains(it) }
@@ -137,13 +149,17 @@ sealed class Contact {
cleanUri cleanUri
) )
) )
return c return Pair(c, u)
} }
} }
} }
return null return null
} }
fun findContact(uri: String): Contact? {
return findContactWithUri(uri)?.first
}
fun nameExists(name: String, list: List<Contact>, ignoreCase: Boolean): Boolean { fun nameExists(name: String, list: List<Contact>, ignoreCase: Boolean): Boolean {
for (c in list) for (c in list)
if (c.name().equals(name, ignoreCase = ignoreCase)) if (c.name().equals(name, ignoreCase = ignoreCase))
@@ -154,13 +154,14 @@ object Utils {
return if (params.size == 1) listOf() else params.subList(1, params.size) return if (params.size == 1) listOf() else params.subList(1, params.size)
} }
fun friendlyUri(ctx: Context, uri: String, account: Account, e164Check: Boolean = true): String { fun friendlyUri(ctx: Context, uri: String, account: Account, e164Check: Boolean = true,
var u = Contact.contactName(uri) includeLabel: Boolean = true): String {
var u = Contact.contactName(uri, includeLabel)
if (u != uri) if (u != uri)
return u return u
if (e164Check) { if (e164Check) {
val e164Uri = e164Uri(uri, account.countryCode) val e164Uri = e164Uri(uri, account.countryCode)
u = Contact.contactName(e164Uri) u = Contact.contactName(e164Uri, includeLabel)
if (u != e164Uri) if (u != e164Uri)
return u return u
} }