diff --git a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt index bf600b86..39702e5b 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt @@ -1953,8 +1953,10 @@ class BaresipService: Service() { if (ua.account.blockUnknown && Contact.contactName(e164Uri) == e164Uri) { Log.d(TAG, "Auto-rejecting incoming PSTN call from $uri") telecomCall.disconnect() - toast(String.format(getString(R.string.call_blocked), - Utils.friendlyUri(this, uri, ua.account))) + toast( + String.format(getString(R.string.call_blocked), + Utils.friendlyUri(this, uri, ua.account)) + ) if (ua.account.callHistory) { Blocked( ua.account.aor, diff --git a/app/src/main/kotlin/com/tutpro/baresip/CallDetailsScreen.kt b/app/src/main/kotlin/com/tutpro/baresip/CallDetailsScreen.kt index 4af77a75..33667dee 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/CallDetailsScreen.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/CallDetailsScreen.kt @@ -165,8 +165,7 @@ private fun CallDetailsContent( @Composable private fun Peer(ctx: Context, callRow: CallRow) { val account = Account.ofAor(callRow.aor)!! - val headerText = stringResource(R.string.peer) + " " + - Utils.friendlyUri(ctx, callRow.peerUri, account) + val headerText = stringResource(R.string.peer) + " " + Utils.friendlyUri(ctx, callRow.peerUri, account) Text( text = headerText, modifier = Modifier.fillMaxWidth(), diff --git a/app/src/main/kotlin/com/tutpro/baresip/CallsScreen.kt b/app/src/main/kotlin/com/tutpro/baresip/CallsScreen.kt index 70d6ec6e..d2c25392 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/CallsScreen.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/CallsScreen.kt @@ -335,17 +335,17 @@ private fun Calls( val intent = Intent(ctx, MainActivity::class.java) intent.putExtra("uap", ua.uap) 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) if (contact is Contact.BaresipContact && contact.email.isNotEmpty()) message.value = String.format( ctx.getString(R.string.contact_email_action_question), - peerName + peerNameWithLabel ) else message.value = String.format( ctx.getString(R.string.contact_action_question), - peerName + peerNameWithLabel ) secondButtonText.value = ctx.getString(R.string.call) secondAction.value = { @@ -401,7 +401,8 @@ private fun Calls( showDialog.value = true }, 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) ctx.getString(R.string.calls_calls) else @@ -410,7 +411,7 @@ private fun Calls( if (contactExists) { message.value = String.format( ctx.getString(R.string.calls_delete_question), - peerName, callText + peerNameWithLabel, callText ) secondButtonText.value = "" lastButtonText.value = ctx.getString(R.string.delete) @@ -421,7 +422,7 @@ private fun Calls( else { message.value = String.format( ctx.getString(R.string.calls_add_delete_question), - peerName, callText + peerNameWithLabel, callText ) secondButtonText.value = ctx.getString(R.string.add_contact) secondAction.value = { diff --git a/app/src/main/kotlin/com/tutpro/baresip/ChatScreen.kt b/app/src/main/kotlin/com/tutpro/baresip/ChatScreen.kt index 86689998..4bee6487 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/ChatScreen.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/ChatScreen.kt @@ -221,7 +221,8 @@ private fun TopAppBar( TopAppBar( title = { 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, fontWeight = FontWeight.Bold ) diff --git a/app/src/main/kotlin/com/tutpro/baresip/ChatsScreen.kt b/app/src/main/kotlin/com/tutpro/baresip/ChatsScreen.kt index fa1b7188..37556786 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/ChatsScreen.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/ChatsScreen.kt @@ -347,13 +347,14 @@ private fun Chats( navController.navigate("chat/${aor}/${message.peerUri}") }, 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 = - Contact.nameExists(peer, BaresipService.contacts, false) + Contact.nameExists(peerName, BaresipService.contacts, false) if (contactExists) { dialogMessage.value = String.format( ctx.getString(R.string.short_chat_question), - peer + peerNameWithLabel ) secondButtonText.value = "" lastButtonText.value = ctx.getString(R.string.delete) @@ -362,7 +363,10 @@ private fun Chats( } } else { 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) secondAction.value = { deleteMessages(uaMessages, account, message.peerUri) @@ -382,7 +386,7 @@ private fun Chats( else MaterialTheme.colorScheme.primaryContainer ) { - val peer = Utils.friendlyUri(ctx, message.peerUri, account) + val peerName = Utils.friendlyUri(ctx, message.peerUri, account) val cal = GregorianCalendar() cal.timeInMillis = message.timeStamp val fmt: DateFormat = if (isToday(message.timeStamp)) @@ -396,7 +400,7 @@ private fun Chats( else MaterialTheme.colorScheme.onPrimaryContainer Row { - Text(text = peer, color = textColor, fontSize = 12.sp) + Text(text = peerName, color = textColor, fontSize = 12.sp) Spacer(modifier = Modifier.weight(1f)) Text(text = info, color = textColor, fontSize = 12.sp) } diff --git a/app/src/main/kotlin/com/tutpro/baresip/Contact.kt b/app/src/main/kotlin/com/tutpro/baresip/Contact.kt index e8cc5ddc..31b8ef77 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/Contact.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/Contact.kt @@ -64,16 +64,28 @@ sealed class Contact { companion object { // Return contact name of uri or uri itself if contact with uri is not found - fun contactName(uri: String): String { - var contact = findContact(uri) - if (contact == null) { - val userPart = Utils.uriUserPart(uri).replace("%23", "#") - if (Utils.isTelNumber(userPart)) { - contact = findContact("tel:$userPart") + fun contactName(uri: String, includeLabel: Boolean = false): String { + val contactWithUri = findContactWithUri(uri) + if (contactWithUri != null) { + val name = contactWithUri.first.name() + if (includeLabel) { + 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 } @@ -120,14 +132,14 @@ sealed class Contact { return emptyList() } - fun findContact(uri: String): Contact? { + fun findContactWithUri(uri: String): Pair? { synchronized(BaresipService.contacts) { for (c in BaresipService.contacts) when (c) { is BaresipContact -> { for (u in c.uris) if (Utils.uriMatch(u.uri, uri)) - return c + return Pair(c, u) } is AndroidContact -> { val cleanUri = uri.filterNot { setOf('-', ' ', '(', ')').contains(it) } @@ -137,13 +149,17 @@ sealed class Contact { cleanUri ) ) - return c + return Pair(c, u) } } } return null } + fun findContact(uri: String): Contact? { + return findContactWithUri(uri)?.first + } + fun nameExists(name: String, list: List, ignoreCase: Boolean): Boolean { for (c in list) if (c.name().equals(name, ignoreCase = ignoreCase)) diff --git a/app/src/main/kotlin/com/tutpro/baresip/Utils.kt b/app/src/main/kotlin/com/tutpro/baresip/Utils.kt index 63ed0a5d..dc4456da 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/Utils.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/Utils.kt @@ -154,13 +154,14 @@ object Utils { return if (params.size == 1) listOf() else params.subList(1, params.size) } - fun friendlyUri(ctx: Context, uri: String, account: Account, e164Check: Boolean = true): String { - var u = Contact.contactName(uri) + fun friendlyUri(ctx: Context, uri: String, account: Account, e164Check: Boolean = true, + includeLabel: Boolean = true): String { + var u = Contact.contactName(uri, includeLabel) if (u != uri) return u if (e164Check) { val e164Uri = e164Uri(uri, account.countryCode) - u = Contact.contactName(e164Uri) + u = Contact.contactName(e164Uri, includeLabel) if (u != e164Uri) return u }