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? { fun baresipContact(name: String): BaresipContact? {
for (c in BaresipService.baresipContacts.value) synchronized(BaresipService.baresipContacts) {
if (c.name == name) for (c in BaresipService.baresipContacts.value)
return c if (c.name == name)
return c
}
return null return null
} }
fun androidContact(name: String): AndroidContact? { fun androidContact(name: String): AndroidContact? {
for (c in BaresipService.androidContacts.value) synchronized(BaresipService.androidContacts) {
if (c.name == name) for (c in BaresipService.androidContacts.value)
return c if (c.name == name)
return c
}
return null return null
} }
@@ -153,7 +157,10 @@ sealed class Contact {
fun saveBaresipContacts() { fun saveBaresipContacts() {
val avatarFiles = avatarFileNames() val avatarFiles = avatarFileNames()
var contents = "" 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}" + contents += "\"${c.name}\" <${c.uris.joinToString(",")}>;email=${c.email};id=${c.id}" +
";color=${c.color};favorite=${if (c.favorite) "yes" else "no"}\n" ";color=${c.color};favorite=${if (c.favorite) "yes" else "no"}\n"
avatarFiles.remove(c.id.toString() + ".png") avatarFiles.remove(c.id.toString() + ".png")
@@ -281,13 +288,21 @@ sealed class Contact {
fun contactsUpdate() { fun contactsUpdate() {
val newContacts = mutableListOf<Contact>() val newContacts = mutableListOf<Contact>()
if (BaresipService.contactsMode != "android") if (BaresipService.contactsMode != "android") {
for (c in BaresipService.baresipContacts.value) val baresipContacts = synchronized(BaresipService.baresipContacts) {
BaresipService.baresipContacts.value.toList()
}
for (c in baresipContacts)
newContacts.add(c.copy()) 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)) if (!nameExists(c.name, newContacts, true))
newContacts.add(c.copy()) newContacts.add(c.copy())
}
newContacts.sortBy { newContacts.sortBy {
when (it) { when (it) {
is BaresipContact -> if (it.favorite) "0" + it.name else "1" + it.name is BaresipContact -> if (it.favorite) "0" + it.name else "1" + it.name
@@ -299,24 +314,30 @@ sealed class Contact {
} }
fun addBaresipContact(contact: BaresipContact) { fun addBaresipContact(contact: BaresipContact) {
BaresipService.baresipContacts.value += contact synchronized(BaresipService.baresipContacts) {
BaresipService.baresipContacts.value += contact
}
saveBaresipContacts() saveBaresipContacts()
contactsUpdate() contactsUpdate()
} }
fun updateBaresipContact(id: Long, contact: BaresipContact) { fun updateBaresipContact(id: Long, contact: BaresipContact) {
val updatedContacts = BaresipService.baresipContacts.value.toMutableList() synchronized(BaresipService.baresipContacts) {
updatedContacts.removeIf { it.id == id } val updatedContacts = BaresipService.baresipContacts.value.toMutableList()
updatedContacts.add(contact) updatedContacts.removeIf { it.id == id }
BaresipService.baresipContacts.value = updatedContacts.toList() updatedContacts.add(contact)
BaresipService.baresipContacts.value = updatedContacts.toList()
}
saveBaresipContacts() saveBaresipContacts()
contactsUpdate() contactsUpdate()
} }
fun removeBaresipContact(contact: BaresipContact) { fun removeBaresipContact(contact: BaresipContact) {
val removed = BaresipService.baresipContacts.value.toMutableList() synchronized(BaresipService.baresipContacts) {
removed.removeIf { it.id == contact.id } val removed = BaresipService.baresipContacts.value.toMutableList()
BaresipService.baresipContacts.value = removed.toList() removed.removeIf { it.id == contact.id }
BaresipService.baresipContacts.value = removed.toList()
}
saveBaresipContacts() saveBaresipContacts()
contactsUpdate() contactsUpdate()
} }
@@ -16,22 +16,28 @@ class UserAgent(val uap: Long, virtualAccount: Account? = null) {
} }
fun add() { fun add() {
val updatedUas = uas.value.toMutableList() synchronized(uas) {
updatedUas.add(this) val updatedUas = uas.value.toMutableList()
uas.value = updatedUas.toList() updatedUas.add(this)
uasStatus.value = statusMap() uas.value = updatedUas.toList()
uasStatus.value = statusMap()
}
} }
fun remove() { fun remove() {
val updatedUas = uas.value.toMutableList() synchronized(uas) {
updatedUas.remove(this) val updatedUas = uas.value.toMutableList()
uas.value = updatedUas.toList() updatedUas.remove(this)
uasStatus.value = statusMap() uas.value = updatedUas.toList()
uasStatus.value = statusMap()
}
} }
fun updateStatus(status: Int) { fun updateStatus(status: Int) {
uas.value.find { it.uap == this.uap }?.status = status synchronized(uas) {
uasStatus.value = statusMap() uas.value.find { it.uap == this.uap }?.status = status
uasStatus.value = statusMap()
}
} }
fun calls(dir: String = ""): ArrayList<Call> { fun calls(dir: String = ""): ArrayList<Call> {
@@ -62,12 +68,14 @@ class UserAgent(val uap: Long, virtualAccount: Account? = null) {
} }
fun makeDefault() { fun makeDefault() {
val index = uas.value.indexOf(this) synchronized(uas) {
val updatedUas = uas.value.toMutableList() val index = uas.value.indexOf(this)
updatedUas.removeAt(index) val updatedUas = uas.value.toMutableList()
updatedUas.add(0, this) updatedUas.removeAt(index)
uas.value = updatedUas.toList() updatedUas.add(0, this)
uasStatus.value = statusMap() uas.value = updatedUas.toList()
uasStatus.value = statusMap()
}
} }
companion object { companion object {
@@ -114,25 +122,27 @@ class UserAgent(val uap: Long, virtualAccount: Account? = null) {
} }
fun updateColorblindStatus() { fun updateColorblindStatus() {
val updatedUas = uas.value.toMutableList() synchronized(uas) {
for (ua in updatedUas) val updatedUas = uas.value.toMutableList()
ua.status = for (ua in updatedUas)
if (colorblind) ua.status =
when (ua.status) { if (colorblind)
R.drawable.circle_green -> R.drawable.circle_green_blind when (ua.status) {
R.drawable.circle_yellow -> R.drawable.circle_yellow_blind R.drawable.circle_green -> R.drawable.circle_green_blind
R.drawable.circle_red -> R.drawable.circle_red_blind R.drawable.circle_yellow -> R.drawable.circle_yellow_blind
else -> R.drawable.circle_white R.drawable.circle_red -> R.drawable.circle_red_blind
} else -> R.drawable.circle_white
else }
when (ua.status) { else
R.drawable.circle_green_blind -> R.drawable.circle_green when (ua.status) {
R.drawable.circle_yellow_blind -> R.drawable.circle_yellow R.drawable.circle_green_blind -> R.drawable.circle_green
R.drawable.circle_red_blind -> R.drawable.circle_red R.drawable.circle_yellow_blind -> R.drawable.circle_yellow
else -> R.drawable.circle_white R.drawable.circle_red_blind -> R.drawable.circle_red
} else -> R.drawable.circle_white
uas.value = updatedUas.toList() }
uasStatus.value = statusMap() uas.value = updatedUas.toList()
uasStatus.value = statusMap()
}
} }
fun statusMap(): Map<String, Int> { fun statusMap(): Map<String, Int> {