summaryrefslogtreecommitdiffhomepage
path: root/android/src
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2021-04-01 21:56:53 +0000
committerAleksandr Granin <aleksandr@mullvad.net>2021-04-08 13:26:19 +0200
commit26c034a746bbebaa519663ba41fdf65406ede4ed (patch)
treeba277f6dd2a9ee99311ecaedc2a02d1a13ef57ea /android/src
parent65be6de695768007bafab2b8afb77e1603a94fd2 (diff)
downloadmullvadvpn-26c034a746bbebaa519663ba41fdf65406ede4ed.tar.xz
mullvadvpn-26c034a746bbebaa519663ba41fdf65406ede4ed.zip
Separate persistence into a new class
Diffstat (limited to 'android/src')
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt5
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/ServiceEndpoint.kt8
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/SplitTunneling.kt37
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/service/persistence/SplitTunnelingPersistence.kt35
4 files changed, 49 insertions, 36 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt
index cd35ecd9c1..bca3f4956d 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt
@@ -16,6 +16,7 @@ import kotlinx.coroutines.launch
import net.mullvad.mullvadvpn.model.Settings
import net.mullvad.mullvadvpn.service.endpoint.ServiceEndpoint
import net.mullvad.mullvadvpn.service.notifications.AccountExpiryNotification
+import net.mullvad.mullvadvpn.service.persistence.SplitTunnelingPersistence
import net.mullvad.mullvadvpn.service.tunnelstate.TunnelStateUpdater
import net.mullvad.mullvadvpn.ui.MainActivity
import net.mullvad.talpid.TalpidVpnService
@@ -104,10 +105,10 @@ class MullvadVpnService : TalpidVpnService() {
tunnelStateUpdater = TunnelStateUpdater(this, serviceNotifier)
endpoint = ServiceEndpoint(
- this,
Looper.getMainLooper(),
daemonInstance.intermittentDaemon,
- connectivityListener
+ connectivityListener,
+ SplitTunnelingPersistence(this)
)
notificationManager =
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/ServiceEndpoint.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/ServiceEndpoint.kt
index 7d4a0cf614..8e11e1e1cd 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/ServiceEndpoint.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/ServiceEndpoint.kt
@@ -1,6 +1,5 @@
package net.mullvad.mullvadvpn.service.endpoint
-import android.content.Context
import android.os.DeadObjectException
import android.os.Looper
import android.os.Messenger
@@ -15,14 +14,15 @@ import net.mullvad.mullvadvpn.ipc.DispatchingHandler
import net.mullvad.mullvadvpn.ipc.Event
import net.mullvad.mullvadvpn.ipc.Request
import net.mullvad.mullvadvpn.service.MullvadDaemon
+import net.mullvad.mullvadvpn.service.persistence.SplitTunnelingPersistence
import net.mullvad.mullvadvpn.util.Intermittent
import net.mullvad.talpid.ConnectivityListener
class ServiceEndpoint(
- context: Context,
looper: Looper,
internal val intermittentDaemon: Intermittent<MullvadDaemon>,
- val connectivityListener: ConnectivityListener
+ val connectivityListener: ConnectivityListener,
+ splitTunnelingPersistence: SplitTunnelingPersistence
) {
private val listeners = mutableSetOf<Messenger>()
private val registrationQueue: SendChannel<Messenger> = startRegistrator()
@@ -38,7 +38,7 @@ class ServiceEndpoint(
val accountCache = AccountCache(this)
val keyStatusListener = KeyStatusListener(this)
val locationInfoCache = LocationInfoCache(this)
- val splitTunneling = SplitTunneling(context, this)
+ val splitTunneling = SplitTunneling(splitTunnelingPersistence, this)
init {
dispatcher.registerHandler(Request.RegisterListener::class) { request ->
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/SplitTunneling.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/SplitTunneling.kt
index 2a704c74a2..f9b77704c6 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/SplitTunneling.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/SplitTunneling.kt
@@ -1,36 +1,22 @@
package net.mullvad.mullvadvpn.service.endpoint
-import android.content.Context
-import java.io.File
import kotlin.properties.Delegates.observable
import net.mullvad.mullvadvpn.ipc.Event
import net.mullvad.mullvadvpn.ipc.Request
+import net.mullvad.mullvadvpn.service.persistence.SplitTunnelingPersistence
import net.mullvad.talpid.util.EventNotifier
-// The spelling of the shared preferences location can't be changed to American English without
-// either having users lose their preferences on update or implementing some migration code.
-private const val SHARED_PREFERENCES = "split_tunnelling"
-private const val KEY_ENABLED = "enabled"
+class SplitTunneling(persistence: SplitTunnelingPersistence, endpoint: ServiceEndpoint) {
+ private val excludedApps = persistence.excludedApps.toMutableSet()
-class SplitTunneling(context: Context, endpoint: ServiceEndpoint) {
- // The spelling of the app list file name can't be changed to American English without either
- // having users lose their preferences on update or implementing some migration code.
- private val appListFile = File(context.filesDir, "split-tunnelling.txt")
- private val excludedApps = HashSet<String>()
- private val preferences = context.getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE)
-
- private var enabled by observable(preferences.getBoolean(KEY_ENABLED, false)) { _, _, _ ->
- enabledChanged()
+ private var enabled by observable(persistence.enabled) { _, _, isEnabled ->
+ persistence.enabled = isEnabled
+ update()
}
val onChange = EventNotifier<List<String>?>(null)
init {
- if (appListFile.exists()) {
- excludedApps.addAll(appListFile.readLines())
- update()
- }
-
onChange.subscribe(this) { excludedApps ->
endpoint.sendEvent(Event.SplitTunnelingUpdate(excludedApps))
}
@@ -51,7 +37,7 @@ class SplitTunneling(context: Context, endpoint: ServiceEndpoint) {
}
registerHandler(Request.PersistExcludedApps::class) { _ ->
- appListFile.writeText(excludedApps.joinToString(separator = "\n"))
+ persistence.excludedApps = excludedApps
}
}
}
@@ -60,15 +46,6 @@ class SplitTunneling(context: Context, endpoint: ServiceEndpoint) {
onChange.unsubscribeAll()
}
- private fun enabledChanged() {
- preferences.edit().apply {
- putBoolean(KEY_ENABLED, enabled)
- apply()
- }
-
- update()
- }
-
private fun update() {
if (enabled) {
onChange.notify(excludedApps.toList())
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/persistence/SplitTunnelingPersistence.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/persistence/SplitTunnelingPersistence.kt
new file mode 100644
index 0000000000..425aec8836
--- /dev/null
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/persistence/SplitTunnelingPersistence.kt
@@ -0,0 +1,35 @@
+package net.mullvad.mullvadvpn.service.persistence
+
+import android.content.Context
+import java.io.File
+import kotlin.properties.Delegates.observable
+
+// The spelling of the shared preferences location can't be changed to American English without
+// either having users lose their preferences on update or implementing some migration code.
+private const val SHARED_PREFERENCES = "split_tunnelling"
+private const val KEY_ENABLED = "enabled"
+
+class SplitTunnelingPersistence(context: Context) {
+ // The spelling of the app list file name can't be changed to American English without either
+ // having users lose their preferences on update or implementing some migration code.
+ private val appListFile = File(context.filesDir, "split-tunnelling.txt")
+ private val preferences = context.getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE)
+
+ var enabled by observable(preferences.getBoolean(KEY_ENABLED, false)) { _, _, isEnabled ->
+ preferences.edit().apply {
+ putBoolean(KEY_ENABLED, isEnabled)
+ apply()
+ }
+ }
+
+ var excludedApps by observable(loadExcludedApps()) { _, _, excludedAppsSet ->
+ appListFile.writeText(excludedAppsSet.joinToString(separator = "\n"))
+ }
+
+ private fun loadExcludedApps(): Set<String> {
+ return when {
+ appListFile.exists() -> appListFile.readLines().toSet()
+ else -> emptySet()
+ }
+ }
+}