Synched user agent and contact handling

This commit is contained in:
Juha Heinanen
2026-05-25 09:00:29 +03:00
parent fa71f0e63c
commit a54d79591e
2 changed files with 85 additions and 54 deletions
@@ -76,16 +76,20 @@ sealed class Contact {
}
fun baresipContact(name: String): BaresipContact? {
for (c in BaresipService.baresipContacts.value)
if (c.name == name)
return c
synchronized(BaresipService.baresipContacts) {
for (c in BaresipService.baresipContacts.value)
if (c.name == name)
return c
}
return null
}
fun androidContact(name: String): AndroidContact? {
for (c in BaresipService.androidContacts.value)
if (c.name == name)
return c
synchronized(BaresipService.androidContacts) {
for (c in BaresipService.androidContacts.value)
if (c.name == name)
return c
}
return null
}
@@ -153,7 +157,10 @@ sealed class Contact {
fun saveBaresipContacts() {
val avatarFiles = avatarFileNames()
var contents = ""
for (c in BaresipService.baresipContacts.value) {
val contacts = synchronized(BaresipService.baresipContacts) {
BaresipService.baresipContacts.value.toList()
}
for (c in contacts) {
contents += "\"${c.name}\" <${c.uris.joinToString(",")}>;email=${c.email};id=${c.id}" +
";color=${c.color};favorite=${if (c.favorite) "yes" else "no"}\n"
avatarFiles.remove(c.id.toString() + ".png")
@@ -281,13 +288,21 @@ sealed class Contact {
fun contactsUpdate() {
val newContacts = mutableListOf<Contact>()
if (BaresipService.contactsMode != "android")
for (c in BaresipService.baresipContacts.value)
if (BaresipService.contactsMode != "android") {
val baresipContacts = synchronized(BaresipService.baresipContacts) {
BaresipService.baresipContacts.value.toList()
}
for (c in baresipContacts)
newContacts.add(c.copy())
if (BaresipService.contactsMode != "baresip")
for (c in BaresipService.androidContacts.value)
}
if (BaresipService.contactsMode != "baresip") {
val androidContacts = synchronized(BaresipService.androidContacts) {
BaresipService.androidContacts.value.toList()
}
for (c in androidContacts)
if (!nameExists(c.name, newContacts, true))
newContacts.add(c.copy())
}
newContacts.sortBy {
when (it) {
is BaresipContact -> if (it.favorite) "0" + it.name else "1" + it.name
@@ -299,24 +314,30 @@ sealed class Contact {
}
fun addBaresipContact(contact: BaresipContact) {
BaresipService.baresipContacts.value += contact
synchronized(BaresipService.baresipContacts) {
BaresipService.baresipContacts.value += contact
}
saveBaresipContacts()
contactsUpdate()
}
fun updateBaresipContact(id: Long, contact: BaresipContact) {
val updatedContacts = BaresipService.baresipContacts.value.toMutableList()
updatedContacts.removeIf { it.id == id }
updatedContacts.add(contact)
BaresipService.baresipContacts.value = updatedContacts.toList()
synchronized(BaresipService.baresipContacts) {
val updatedContacts = BaresipService.baresipContacts.value.toMutableList()
updatedContacts.removeIf { it.id == id }
updatedContacts.add(contact)
BaresipService.baresipContacts.value = updatedContacts.toList()
}
saveBaresipContacts()
contactsUpdate()
}
fun removeBaresipContact(contact: BaresipContact) {
val removed = BaresipService.baresipContacts.value.toMutableList()
removed.removeIf { it.id == contact.id }
BaresipService.baresipContacts.value = removed.toList()
synchronized(BaresipService.baresipContacts) {
val removed = BaresipService.baresipContacts.value.toMutableList()
removed.removeIf { it.id == contact.id }
BaresipService.baresipContacts.value = removed.toList()
}
saveBaresipContacts()
contactsUpdate()
}