From 589bd3079cbd95ab0bc1f818ccf61ef398a083ab Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Sat, 13 Aug 2022 09:52:38 +0300 Subject: [PATCH] When calling Android contact, use the latest peer URI if contact has it --- .../kotlin/com/tutpro/baresip/CallHistory.kt | 4 ++-- .../kotlin/com/tutpro/baresip/ChatsActivity.kt | 2 +- .../main/kotlin/com/tutpro/baresip/Contact.kt | 18 ++++++++++++++---- .../kotlin/com/tutpro/baresip/MainActivity.kt | 10 +++++----- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/app/src/main/kotlin/com/tutpro/baresip/CallHistory.kt b/app/src/main/kotlin/com/tutpro/baresip/CallHistory.kt index 7a31e8f6..d4037403 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/CallHistory.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/CallHistory.kt @@ -36,9 +36,9 @@ class NewCallHistory(val aor: String, val peerUri: String, val direction: String return BaresipService.callHistory.count { it.aor == aor } } - fun aorLatestHistory(aor: String): NewCallHistory? { + fun aorLatestPeerUri(aor: String): String? { for (h in BaresipService.callHistory.reversed()) - if (h.aor == aor) return h + if (h.aor == aor) return h.peerUri return null } diff --git a/app/src/main/kotlin/com/tutpro/baresip/ChatsActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/ChatsActivity.kt index 48f9a359..5b7aaf17 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/ChatsActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/ChatsActivity.kt @@ -130,7 +130,7 @@ class ChatsActivity: AppCompatActivity() { plusButton.setOnClickListener { val uriText = peerUri.text.toString().trim() if (uriText.isNotEmpty()) { - var uri = Contact.contactUri(uriText) + var uri = Contact.contactUri(uriText, null) if (uri == null) uri = if (Utils.isTelNumber(uriText)) "tel:$uriText" diff --git a/app/src/main/kotlin/com/tutpro/baresip/Contact.kt b/app/src/main/kotlin/com/tutpro/baresip/Contact.kt index 59e13dae..cf920b38 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/Contact.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/Contact.kt @@ -51,7 +51,8 @@ sealed class Contact { } // Return uri of contact name or null if contact is not found - fun contactUri(name: String): String? { + // If Android contact has more than one uri, return latest if given and found + fun contactUri(name: String, latest: String?): String? { for (c in contacts()) when (c) { is BaresipContact -> { @@ -62,10 +63,19 @@ sealed class Contact { } is AndroidContact -> { if (c.name == name) { - return if (c.uris.isNotEmpty()) - c.uris.first() - else + return if (c.uris.isNotEmpty()) { + var uri = c.uris.first() + if (c.uris.size > 1 && latest != null) { + for (u in c.uris) + if (u == latest) { + uri = latest + break + } + } + uri + } else { null + } } } } diff --git a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt index 965b2745..f2af566d 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt @@ -1413,7 +1413,7 @@ class MainActivity : AppCompatActivity() { dialog.dismiss() var uriText = transferUri.text.toString().trim() if (uriText.isNotEmpty()) { - uriText = Contact.contactUri(uriText) ?: uriText + uriText = Contact.contactUri(uriText, null) ?: uriText if (Utils.isTelNumber(uriText)) uriText = "tel:$uriText" val uri = if (Utils.isTelUri(uriText)) @@ -1737,7 +1737,7 @@ class MainActivity : AppCompatActivity() { if (Call.calls().isEmpty()) { var uriText = callUri.text.toString().trim() if (uriText.isNotEmpty()) { - uriText = Contact.contactUri(uriText) ?: uriText + uriText = Contact.contactUri(uriText, NewCallHistory.aorLatestPeerUri(aor)) ?: uriText if (Utils.isTelNumber(uriText)) uriText = "tel:$uriText" val uri = if (Utils.isTelUri(uriText)) { @@ -1787,9 +1787,9 @@ class MainActivity : AppCompatActivity() { callHandler.postDelayed(callRunnable!!, 1000) } } else { - val latest = NewCallHistory.aorLatestHistory(aor) - if (latest != null) - callUri.setText(Utils.friendlyUri(this, latest.peerUri, ua.account)) + val latestPeerUri = NewCallHistory.aorLatestPeerUri(aor) + if (latestPeerUri != null) + callUri.setText(Utils.friendlyUri(this, latestPeerUri, ua.account)) } } }