From 12a242c11a232bee8f72f1c4149a6f99c569c738 Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Sun, 5 Mar 2023 06:12:14 +0200 Subject: [PATCH] Work on support for multiple numbers in Android contacts --- .../main/kotlin/com/tutpro/baresip/Contact.kt | 51 +++++++++++++++---- .../kotlin/com/tutpro/baresip/MainActivity.kt | 22 +++++++- 2 files changed, 62 insertions(+), 11 deletions(-) diff --git a/app/src/main/kotlin/com/tutpro/baresip/Contact.kt b/app/src/main/kotlin/com/tutpro/baresip/Contact.kt index e35b73e5..81a97828 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/Contact.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/Contact.kt @@ -51,16 +51,14 @@ sealed class Contact { return uri } - // Return uri of contact name or null if contact is not found - // 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 -> { if (c.name.equals(name, ignoreCase = true)) return c.uri.removePrefix("<") - .replaceAfter(">", "") - .replace(">", "") + .replaceAfter(">", "") + .replace(">", "") } is AndroidContact -> { if (c.name == name) { @@ -83,6 +81,30 @@ sealed class Contact { return null } + // Return URIs of contact name or null if contact is not found + fun contactUris(name: String): ArrayList { + val uris = ArrayList() + for (c in contacts()) + when (c) { + is BaresipContact -> { + if (c.name.equals(name, ignoreCase = true)) { + uris.add(c.uri.removePrefix("<") + .replaceAfter(">", "") + .replace(">", "")) + return uris + } + } + is AndroidContact -> { + if (c.name == name) { + for (u in c.uris) + uris.add(u) + return uris + } + } + } + return uris + } + fun findContact(uri: String): Contact? { for (c in contacts()) when (c) { @@ -127,7 +149,7 @@ sealed class Contact { // cursor using getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE val projection = arrayOf(ContactsContract.Data.CONTACT_ID, ContactsContract.Data.DISPLAY_NAME, ContactsContract.Data.MIMETYPE, ContactsContract.Data.DATA1, - ContactsContract.Data.PHOTO_THUMBNAIL_URI) + /* ContactsContract.Data.DATA2 ,*/ ContactsContract.Data.PHOTO_THUMBNAIL_URI) val selection = ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE + "' OR " + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'" @@ -149,8 +171,10 @@ sealed class Contact { contact.name = name if (contact.thumbnailUri == null && thumb != null) contact.thumbnailUri = thumb - if (mime == ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) - contact.uris.add("tel:${data.filterNot{setOf('-', ' ', '(', ')').contains(it)}}") + if (mime == ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) { + contact.uris.add("tel:${data.filterNot { setOf('-', ' ', '(', ')').contains(it) }}") + // contact.types.add(typeToString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE))) + } else if (mime == ContactsContract.CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE) contact.uris.add("sip:$data") else @@ -164,6 +188,16 @@ sealed class Contact { BaresipService.androidContacts.add(value) } + @Suppress("unused") + private fun typeToString(type: Int): String { + return when(type) { + ContactsContract.CommonDataKinds.Phone.TYPE_HOME -> "Home" + ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE -> "Mobile" + ContactsContract.CommonDataKinds.Phone.TYPE_WORK -> "Work" + else -> "Unknown" + } + } + fun restoreBaresipContacts(): Boolean { val content = Utils.getFileContents(BaresipService.filesPath + "/contacts") ?: return false @@ -235,8 +269,7 @@ sealed class Contact { is BaresipContact -> BaresipService.contactNames.add(c.name) is AndroidContact -> - if (c.uris.size == 1) - BaresipService.contactNames.add(c.name) + BaresipService.contactNames.add(c.name) } } diff --git a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt index 6c494f10..c9cefdae 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt @@ -1751,14 +1751,31 @@ class MainActivity : AppCompatActivity() { } } - private fun makeCall() { + private fun makeCall(lookContact: Boolean = true) { callUri.setAdapter(null) val ua = BaresipService.uas[aorSpinner.selectedItemPosition] val aor = ua.account.aor if (Call.calls().isEmpty()) { var uriText = callUri.text.toString().trim() if (uriText.isNotEmpty()) { - uriText = Contact.contactUri(uriText, NewCallHistory.aorLatestPeerUri(aor)) ?: uriText + if (lookContact) { + val uris = Contact.contactUris(uriText) + if (uris.size == 1) + uriText = uris[0] + else if (uris.size > 1) { + val builder = MaterialAlertDialogBuilder(this, R.style.AlertDialogTheme) + with(builder) { + setTitle("Choose Destination") + setItems(uris.toTypedArray()) { _, which -> + callUri.setText(uris[which]) + makeCall(false) + } + setNeutralButton(getString(R.string.cancel)) { _: DialogInterface, _: Int -> } + show() + } + return + } + } if (Utils.isTelNumber(uriText)) uriText = "tel:$uriText" val uri = if (Utils.isTelUri(uriText)) { @@ -1785,6 +1802,7 @@ class MainActivity : AppCompatActivity() { } else { Utils.uriComplete(uriText, aor) } + Log.d(TAG, "********* uriText = $uriText") if (!Utils.checkUri(uri)) { Utils.alertView(this, getString(R.string.notice), String.format(getString(R.string.invalid_sip_or_tel_uri), uri)