diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-11-25 11:30:02 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-11-25 11:58:33 +0000 |
| commit | 525cb6745c070786750663f689fa5d0b1c4c0b4d (patch) | |
| tree | d829c3a18b9ab9a09c3fe4531139a288e88a1205 /android/src | |
| parent | 3f720c55e7fb7de341e8b4e21ca2985ee9ca0fb6 (diff) | |
| download | mullvadvpn-525cb6745c070786750663f689fa5d0b1c4c0b4d.tar.xz mullvadvpn-525cb6745c070786750663f689fa5d0b1c4c0b4d.zip | |
Refactor to use a `State` enumeration
Diffstat (limited to 'android/src')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt | 33 |
1 files changed, 16 insertions, 17 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 f9c2c467db..6920a85b4c 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt @@ -38,12 +38,16 @@ class MullvadVpnService : TalpidVpnService() { Disconnect, } + private enum class State { + Running, + Stopping, + Stopped, + } + private val binder = LocalBinder() private val serviceNotifier = EventNotifier<ServiceInstance?>(null) - private var hasStopped = false - private var isStopping = false - private var shouldStop = false + private var state = State.Running private var setUpDaemonJob: Job? = null @@ -127,13 +131,9 @@ class MullvadVpnService : TalpidVpnService() { } } - if (shouldStop && !quitCommand) { - shouldStop = false - - if (isStopping) { - restart() - isStopping = false - } + if (state == State.Stopping && !quitCommand) { + state = State.Running + restart() } return startResult @@ -150,9 +150,9 @@ class MullvadVpnService : TalpidVpnService() { Log.d(TAG, "Connection to service restored") isBound = true - if (isStopping) { + if (state == State.Stopping) { + state = State.Running restart() - isStopping = false } } @@ -164,7 +164,7 @@ class MullvadVpnService : TalpidVpnService() { Log.d(TAG, "Closed all connections to service") isBound = false - if (shouldStop) { + if (state != State.Running) { stop() } @@ -173,7 +173,7 @@ class MullvadVpnService : TalpidVpnService() { override fun onDestroy() { Log.d(TAG, "Service has stopped") - hasStopped = true + state = State.Stopped notificationManager.onDestroy() daemonInstance.onDestroy() super.onDestroy() @@ -208,7 +208,7 @@ class MullvadVpnService : TalpidVpnService() { Log.d(TAG, "Daemon has stopped") instance = null - if (!isStopping && !hasStopped) { + if (state == State.Running) { restart() } } @@ -248,8 +248,7 @@ class MullvadVpnService : TalpidVpnService() { private fun stop() { Log.d(TAG, "Stopping service") - isStopping = true - shouldStop = true + state = State.Stopping daemonInstance.stop() stopSelf() } |
