From af11b18bd7d27802e7fcf2129fa83f1c0b318447 Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Wed, 2 Feb 2022 17:47:03 +0200 Subject: [PATCH] Update also Android tel uri contact --- .../com/tutpro/baresip/ContactActivity.kt | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/app/src/main/kotlin/com/tutpro/baresip/ContactActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/ContactActivity.kt index bc29441e..0bc5e7c7 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/ContactActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/ContactActivity.kt @@ -182,8 +182,8 @@ class ContactActivity : AppCompatActivity() { requestPermissions(permissions, CONTACT_PERMISSION_REQUEST_CODE) } - Log.d(TAG, "********** Android sip contacts ${logAndroidContacts(this, "sip")}") - Log.d(TAG, "********** Android tel contacts ${logAndroidContacts(this, "tel")}") + // Log.d(TAG, "Android sip contacts ${logAndroidContacts(this, "sip")}") + // Log.d(TAG, "Android tel contacts ${logAndroidContacts(this, "tel")}") Utils.addActivity("contact,$newContact,$uOrI") @@ -434,10 +434,14 @@ class ContactActivity : AppCompatActivity() { .withValue(Data.MIMETYPE, CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) .withValue(CommonDataKinds.StructuredName.DISPLAY_NAME, contact.name) .build()) + val mimeType = if (contact.uri.startsWith("sip:")) + CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE + else + CommonDataKinds.Phone.CONTENT_ITEM_TYPE ops.add(ContentProviderOperation .newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(Data.RAW_CONTACT_ID, 0) - .withValue(Data.MIMETYPE, CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE) + .withValue(Data.MIMETYPE, mimeType) .withValue(Data.DATA1, contact.uri.substringAfter(":")) .build()) if (contact.avatarImage != null) { @@ -461,37 +465,45 @@ class ContactActivity : AppCompatActivity() { } private fun updateAndroidContact(rawContactId: Long, contact: Contact) { - if (updateAndroidSipUri(rawContactId, contact.uri) == 0) - addAndroidSipUri(rawContactId, contact.uri) + if (updateAndroidUri(rawContactId, contact.uri) == 0) + addAndroidUri(rawContactId, contact.uri) if (updateAndroidPhoto(rawContactId, contact.avatarImage) == 0) if (contact.avatarImage != null) addAndroidPhoto(rawContactId, contact.avatarImage!!) } - private fun addAndroidSipUri(rawContactId: Long, sipUri: String) { + private fun addAndroidUri(rawContactId: Long, uri: String) { + val mimeType = if (uri.startsWith("sip:")) + CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE + else + CommonDataKinds.Phone.CONTENT_ITEM_TYPE val ops = ArrayList() ops.add(ContentProviderOperation .newInsert(ContactsContract.Data.CONTENT_URI) .withValue(Data.RAW_CONTACT_ID, rawContactId) - .withValue(Data.MIMETYPE, CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE) - .withValue(Data.DATA1, sipUri.substringAfter(":")) + .withValue(Data.MIMETYPE, mimeType) + .withValue(Data.DATA1, uri.substringAfter(":")) .build()) try { contentResolver.applyBatch(ContactsContract.AUTHORITY, ops) } catch (e: Exception) { - Log.e(TAG, "Adding of SIP URI $sipUri failed") + Log.e(TAG, "Adding of SIP URI $uri failed") } } - private fun updateAndroidSipUri(rawContactId: Long, sipUri: String): Int { + private fun updateAndroidUri(rawContactId: Long, uri: String): Int { + val mimeType = if (uri.startsWith("sip:")) + CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE + else + CommonDataKinds.Phone.CONTENT_ITEM_TYPE val contentValues = ContentValues() - contentValues.put(ContactsContract.Data.DATA1, sipUri) + contentValues.put(ContactsContract.Data.DATA1, uri) val where = "${ContactsContract.Data.RAW_CONTACT_ID}=$rawContactId and " + - "${ContactsContract.Data.MIMETYPE}='${CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE}'" + "${ContactsContract.Data.MIMETYPE}='$mimeType'" return try { contentResolver.update(ContactsContract.Data.CONTENT_URI, contentValues, where, null) } catch (e: Exception) { - Log.e(TAG, "Adding of SIP URI $sipUri failed") + Log.e(TAG, "Adding of SIP URI $uri failed") 0 } }