summaryrefslogtreecommitdiffhomepage
path: root/android/src/main
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-11-17 00:32:26 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-12-10 12:55:06 +0000
commita1e567295c218e48dc2ed46340709cfa460cdafb (patch)
treeae58d4958e9f9604b08bf29fbc2add945a685afb /android/src/main
parent221ed6b52a6df797b64deb3ceed4113f43575df0 (diff)
downloadmullvadvpn-a1e567295c218e48dc2ed46340709cfa460cdafb.tar.xz
mullvadvpn-a1e567295c218e48dc2ed46340709cfa460cdafb.zip
Synchronize custom DNS server list changes
Diffstat (limited to 'android/src/main')
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/ui/customdns/CustomDnsAdapter.kt89
1 files changed, 58 insertions, 31 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/customdns/CustomDnsAdapter.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/customdns/CustomDnsAdapter.kt
index 3eff101a3b..2993bd71c3 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/customdns/CustomDnsAdapter.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/customdns/CustomDnsAdapter.kt
@@ -5,6 +5,8 @@ import android.view.LayoutInflater
import android.view.ViewGroup
import java.net.InetAddress
import kotlin.properties.Delegates.observable
+import kotlinx.coroutines.sync.Mutex
+import kotlinx.coroutines.sync.withLock
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.service.CustomDns
import net.mullvad.mullvadvpn.util.JobTracker
@@ -18,6 +20,7 @@ class CustomDnsAdapter(val customDns: CustomDns) : Adapter<CustomDnsItemHolder>(
FOOTER,
}
+ private val customDnsServersLock = Mutex()
private val inetAddressValidator = InetAddressValidator.getInstance()
private val jobTracker = JobTracker()
@@ -54,13 +57,17 @@ class CustomDnsAdapter(val customDns: CustomDns) : Adapter<CustomDnsItemHolder>(
customDns.apply {
onDnsServersChanged.subscribe(this) { dnsServers ->
jobTracker.newUiJob("updateDnsServers") {
- activeCustomDnsServers = dnsServers
+ customDnsServersLock.withLock {
+ activeCustomDnsServers = dnsServers
+ }
}
}
onEnabledChanged.subscribe(this) { value ->
jobTracker.newUiJob("updateEnabled") {
- enabled = value
+ customDnsServersLock.withLock {
+ enabled = value
+ }
}
}
}
@@ -132,50 +139,68 @@ class CustomDnsAdapter(val customDns: CustomDns) : Adapter<CustomDnsItemHolder>(
}
fun newDnsServer() {
- if (enabled) {
- val count = getItemCount()
+ jobTracker.newUiJob("newDnsServer") {
+ customDnsServersLock.withLock {
+ if (enabled) {
+ val count = getItemCount()
- editDnsServerAt(count - 2)
+ editDnsServerAt(count - 2)
+ }
+ }
}
}
fun saveDnsServer(address: String, errorCallback: () -> Unit) {
jobTracker.newUiJob("saveDnsServer $address") {
- editingPosition?.let { position ->
- var validAddress: Boolean
-
- if (position >= cachedCustomDnsServers.size) {
- validAddress = addDnsServer(address)
- } else {
- validAddress = replaceDnsServer(address, position)
- }
+ customDnsServersLock.withLock {
+ editingPosition?.let { position ->
+ var validAddress: Boolean
- if (!validAddress) {
- errorCallback()
+ if (position >= cachedCustomDnsServers.size) {
+ validAddress = addDnsServer(address)
+ } else {
+ validAddress = replaceDnsServer(address, position)
+ }
+
+ if (!validAddress) {
+ errorCallback()
+ }
}
}
}
}
fun editDnsServer(address: InetAddress) {
- if (enabled) {
- val position = cachedCustomDnsServers.indexOf(address)
+ jobTracker.newUiJob("editDnsServer $address") {
+ customDnsServersLock.withLock {
+ if (enabled) {
+ val position = cachedCustomDnsServers.indexOf(address)
- editDnsServerAt(position)
+ editDnsServerAt(position)
+ }
+ }
}
}
fun stopEditing() {
- if (enabled) {
- editDnsServerAt(null)
+ jobTracker.newUiJob("stopEditing") {
+ customDnsServersLock.withLock {
+ if (enabled) {
+ editDnsServerAt(null)
+ }
+ }
}
}
fun stopEditing(address: InetAddress) {
- if (enabled) {
- editingPosition?.let { position ->
- if (cachedCustomDnsServers.getOrNull(position) == address) {
- editDnsServerAt(null)
+ jobTracker.newUiJob("stopEditing $address") {
+ customDnsServersLock.withLock {
+ if (enabled) {
+ editingPosition?.let { position ->
+ if (cachedCustomDnsServers.getOrNull(position) == address) {
+ editDnsServerAt(null)
+ }
+ }
}
}
}
@@ -183,16 +208,18 @@ class CustomDnsAdapter(val customDns: CustomDns) : Adapter<CustomDnsItemHolder>(
fun removeDnsServer(address: InetAddress) {
jobTracker.newUiJob("removeDnsServer $address") {
- val position = jobTracker.runOnBackground {
- val index = cachedCustomDnsServers.indexOf(address)
+ customDnsServersLock.withLock {
+ val position = jobTracker.runOnBackground {
+ val index = cachedCustomDnsServers.indexOf(address)
- cachedCustomDnsServers.removeAt(index)
- customDns.removeDnsServer(address)
+ cachedCustomDnsServers.removeAt(index)
+ customDns.removeDnsServer(address)
- index
- }
+ index
+ }
- notifyItemRemoved(position)
+ notifyItemRemoved(position)
+ }
}
}