diff --git a/app/src/main/kotlin/com/tutpro/baresip/Contact.kt b/app/src/main/kotlin/com/tutpro/baresip/Contact.kt index aa121ce3..99e8a638 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/Contact.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/Contact.kt @@ -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() - 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() } diff --git a/app/src/main/kotlin/com/tutpro/baresip/UserAgent.kt b/app/src/main/kotlin/com/tutpro/baresip/UserAgent.kt index 2a217436..d0d3a201 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/UserAgent.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/UserAgent.kt @@ -16,22 +16,28 @@ class UserAgent(val uap: Long, virtualAccount: Account? = null) { } fun add() { - val updatedUas = uas.value.toMutableList() - updatedUas.add(this) - uas.value = updatedUas.toList() - uasStatus.value = statusMap() + synchronized(uas) { + val updatedUas = uas.value.toMutableList() + updatedUas.add(this) + uas.value = updatedUas.toList() + uasStatus.value = statusMap() + } } fun remove() { - val updatedUas = uas.value.toMutableList() - updatedUas.remove(this) - uas.value = updatedUas.toList() - uasStatus.value = statusMap() + synchronized(uas) { + val updatedUas = uas.value.toMutableList() + updatedUas.remove(this) + uas.value = updatedUas.toList() + uasStatus.value = statusMap() + } } fun updateStatus(status: Int) { - uas.value.find { it.uap == this.uap }?.status = status - uasStatus.value = statusMap() + synchronized(uas) { + uas.value.find { it.uap == this.uap }?.status = status + uasStatus.value = statusMap() + } } fun calls(dir: String = ""): ArrayList { @@ -62,12 +68,14 @@ class UserAgent(val uap: Long, virtualAccount: Account? = null) { } fun makeDefault() { - val index = uas.value.indexOf(this) - val updatedUas = uas.value.toMutableList() - updatedUas.removeAt(index) - updatedUas.add(0, this) - uas.value = updatedUas.toList() - uasStatus.value = statusMap() + synchronized(uas) { + val index = uas.value.indexOf(this) + val updatedUas = uas.value.toMutableList() + updatedUas.removeAt(index) + updatedUas.add(0, this) + uas.value = updatedUas.toList() + uasStatus.value = statusMap() + } } companion object { @@ -114,25 +122,27 @@ class UserAgent(val uap: Long, virtualAccount: Account? = null) { } fun updateColorblindStatus() { - val updatedUas = uas.value.toMutableList() - for (ua in updatedUas) - ua.status = - if (colorblind) - when (ua.status) { - R.drawable.circle_green -> R.drawable.circle_green_blind - R.drawable.circle_yellow -> R.drawable.circle_yellow_blind - R.drawable.circle_red -> R.drawable.circle_red_blind - else -> R.drawable.circle_white - } - else - when (ua.status) { - R.drawable.circle_green_blind -> R.drawable.circle_green - R.drawable.circle_yellow_blind -> R.drawable.circle_yellow - R.drawable.circle_red_blind -> R.drawable.circle_red - else -> R.drawable.circle_white - } - uas.value = updatedUas.toList() - uasStatus.value = statusMap() + synchronized(uas) { + val updatedUas = uas.value.toMutableList() + for (ua in updatedUas) + ua.status = + if (colorblind) + when (ua.status) { + R.drawable.circle_green -> R.drawable.circle_green_blind + R.drawable.circle_yellow -> R.drawable.circle_yellow_blind + R.drawable.circle_red -> R.drawable.circle_red_blind + else -> R.drawable.circle_white + } + else + when (ua.status) { + R.drawable.circle_green_blind -> R.drawable.circle_green + R.drawable.circle_yellow_blind -> R.drawable.circle_yellow + R.drawable.circle_red_blind -> R.drawable.circle_red + else -> R.drawable.circle_white + } + uas.value = updatedUas.toList() + uasStatus.value = statusMap() + } } fun statusMap(): Map {