Added possibility to add labels to contacts
This commit is contained in:
@@ -13,14 +13,16 @@ import java.util.ArrayList
|
||||
|
||||
sealed class Contact {
|
||||
|
||||
data class ContactUri(var uri: String, var label: String = "")
|
||||
|
||||
abstract fun name(): String
|
||||
abstract fun id(): Long
|
||||
abstract fun favorite(): Boolean
|
||||
abstract fun colorInt(): Int
|
||||
abstract fun uris(): List<String>
|
||||
abstract fun uris(): List<ContactUri>
|
||||
abstract fun email(): String
|
||||
|
||||
class BaresipContact(var name: String, val uris: ArrayList<String>, var email: String, var color: Int,
|
||||
class BaresipContact(var name: String, val uris: ArrayList<ContactUri>, var email: String, var color: Int,
|
||||
var id: Long, var favorite: Boolean): Contact() {
|
||||
var avatarImage: Bitmap? = null
|
||||
override fun name() = name
|
||||
@@ -31,7 +33,7 @@ sealed class Contact {
|
||||
override fun email() = email
|
||||
}
|
||||
|
||||
class AndroidContact(var name: String, val uris: ArrayList<String>, var email: String, var color: Int,
|
||||
class AndroidContact(var name: String, val uris: ArrayList<ContactUri>, var email: String, var color: Int,
|
||||
var thumbnailUri: Uri?, var id: Long, var favorite: Boolean): Contact() {
|
||||
override fun name() = name
|
||||
override fun id() = id
|
||||
@@ -101,9 +103,9 @@ sealed class Contact {
|
||||
is BaresipContact -> {
|
||||
if (c.name.equals(name, ignoreCase = true)) {
|
||||
for (u in c.uris) {
|
||||
if (tel && !u.startsWith("tel:"))
|
||||
if (tel && !u.uri.startsWith("tel:"))
|
||||
continue
|
||||
uris.add(u)
|
||||
uris.add(u.uri)
|
||||
}
|
||||
return uris
|
||||
}
|
||||
@@ -111,9 +113,9 @@ sealed class Contact {
|
||||
is AndroidContact -> {
|
||||
if (c.name == name) {
|
||||
for (u in c.uris) {
|
||||
if (tel && !u.startsWith("tel:"))
|
||||
if (tel && !u.uri.startsWith("tel:"))
|
||||
continue
|
||||
uris.add(u)
|
||||
uris.add(u.uri)
|
||||
}
|
||||
return uris
|
||||
}
|
||||
@@ -129,14 +131,14 @@ sealed class Contact {
|
||||
when (c) {
|
||||
is BaresipContact -> {
|
||||
for (u in c.uris)
|
||||
if (Utils.uriMatch(u, uri))
|
||||
if (Utils.uriMatch(u.uri, uri))
|
||||
return c
|
||||
}
|
||||
is AndroidContact -> {
|
||||
val cleanUri = uri.filterNot { setOf('-', ' ', '(', ')').contains(it) }
|
||||
for (u in c.uris)
|
||||
if (Utils.uriMatch(
|
||||
u.filterNot { setOf('-', ' ', '(', ')').contains(it) },
|
||||
u.uri.filterNot { setOf('-', ' ', '(', ')').contains(it) },
|
||||
cleanUri
|
||||
)
|
||||
)
|
||||
@@ -161,7 +163,10 @@ sealed class Contact {
|
||||
BaresipService.baresipContacts.value.toList()
|
||||
}
|
||||
for (c in contacts) {
|
||||
contents += "\"${c.name}\" <${c.uris.joinToString(",")}>;email=${c.email};id=${c.id}" +
|
||||
val uris = c.uris.joinToString(",") {
|
||||
if (it.label.isNotEmpty()) "${it.uri}[${it.label}]" else it.uri
|
||||
}
|
||||
contents += "\"${c.name}\" <$uris>;email=${c.email};id=${c.id}" +
|
||||
";color=${c.color};favorite=${if (c.favorite) "yes" else "no"}\n"
|
||||
avatarFiles.remove(c.id.toString() + ".png")
|
||||
}
|
||||
@@ -176,7 +181,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.DATA2 ,*/ ContactsContract.Data.PHOTO_THUMBNAIL_URI,
|
||||
ContactsContract.Data.DATA2, ContactsContract.Data.PHOTO_THUMBNAIL_URI,
|
||||
ContactsContract.Contacts.STARRED)
|
||||
val selection =
|
||||
ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE + "' OR " +
|
||||
@@ -191,12 +196,13 @@ sealed class Contact {
|
||||
val name = cur.getString(1) ?: ""
|
||||
val mime = cur.getString(2)
|
||||
val data = cur.getString(3) ?: continue
|
||||
val thumb = cur.getString(4)?.toUri()
|
||||
val starred = cur.getInt(5)
|
||||
val type = cur.getInt(4)
|
||||
val thumb = cur.getString(5)?.toUri()
|
||||
val starred = cur.getInt(6)
|
||||
val contact = if (contacts.containsKey(id))
|
||||
contacts[id]!!
|
||||
else
|
||||
AndroidContact(name, ArrayList<String>(), "", Utils.randomColor(), thumb, id, starred == 1)
|
||||
AndroidContact(name, ArrayList<ContactUri>(), "", Utils.randomColor(), thumb, id, starred == 1)
|
||||
if (contact.name == "" && name != "")
|
||||
contact.name = name
|
||||
if (contact.thumbnailUri == null && thumb != null)
|
||||
@@ -204,13 +210,14 @@ sealed class Contact {
|
||||
when (mime) {
|
||||
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE -> {
|
||||
val uri = "tel:${data.filterNot { setOf('-', ' ', '(', ')').contains(it) }}"
|
||||
if (uri !in contact.uris)
|
||||
contact.uris.add(uri)
|
||||
val label = typeToString(type)
|
||||
if (contact.uris.none { it.uri == uri })
|
||||
contact.uris.add(ContactUri(uri, label))
|
||||
}
|
||||
ContactsContract.CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE -> {
|
||||
val uri = "sip:$data"
|
||||
if (uri !in contact.uris)
|
||||
contact.uris.add(uri)
|
||||
if (contact.uris.none { it.uri == uri })
|
||||
contact.uris.add(ContactUri(uri, ""))
|
||||
}
|
||||
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE -> {
|
||||
if (contact.email == "")
|
||||
@@ -228,13 +235,22 @@ sealed class Contact {
|
||||
BaresipService.androidContacts.value = newList.toList()
|
||||
}
|
||||
|
||||
@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"
|
||||
else -> ""
|
||||
}
|
||||
}
|
||||
|
||||
fun stringToType(label: String): Int {
|
||||
return when (label) {
|
||||
"Home" -> ContactsContract.CommonDataKinds.Phone.TYPE_HOME
|
||||
"Mobile" -> ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE
|
||||
"Work" -> ContactsContract.CommonDataKinds.Phone.TYPE_WORK
|
||||
"" -> ContactsContract.CommonDataKinds.Phone.TYPE_OTHER
|
||||
else -> ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM
|
||||
}
|
||||
}
|
||||
|
||||
@@ -252,7 +268,17 @@ sealed class Contact {
|
||||
val name = parts[1]
|
||||
val uriParams = parts[2].trim()
|
||||
val urisPart = uriParams.substringAfter("<").substringBefore(">")
|
||||
val uris = ArrayList(urisPart.split(","))
|
||||
val uris = ArrayList<ContactUri>()
|
||||
for (uPart in urisPart.split(",")) {
|
||||
if (uPart.isEmpty()) continue
|
||||
if (uPart.contains("[") && uPart.contains("]")) {
|
||||
val uri = uPart.substringBefore("[")
|
||||
val label = uPart.substringAfter("[").substringBefore("]")
|
||||
uris.add(ContactUri(uri, label))
|
||||
} else {
|
||||
uris.add(ContactUri(uPart, ""))
|
||||
}
|
||||
}
|
||||
val params = uriParams.substringAfter(">;")
|
||||
val email = Utils.paramValue(params, "email")
|
||||
val colorValue = Utils.paramValue(params, "color" )
|
||||
|
||||
Reference in New Issue
Block a user