Work on support for multiple numbers in Android contacts
This commit is contained in:
@ -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<String> {
|
||||
val uris = ArrayList<String>()
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user