diff options
Diffstat (limited to 'android')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/ui/WireguardKeyFragment.kt | 72 | ||||
| -rw-r--r-- | android/src/main/res/values/strings.xml | 4 |
2 files changed, 34 insertions, 42 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/WireguardKeyFragment.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/WireguardKeyFragment.kt index 79a3e8e012..ccf083f3b7 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/WireguardKeyFragment.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/WireguardKeyFragment.kt @@ -9,7 +9,6 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView -import android.widget.Toast import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.Job @@ -31,10 +30,10 @@ import org.joda.time.format.DateTimeFormat val RFC3339_FORMAT = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss.SSSSSSSSSS z") class WireguardKeyFragment : ServiceDependentFragment(OnNoService.GoToLaunchScreen) { - enum class ActionState { - Idle, - Generating, - Verifying; + sealed class ActionState { + class Idle(val verified: Boolean) : ActionState() + class Generating() : ActionState() + class Verifying() : ActionState() } private val jobTracker = JobTracker() @@ -46,7 +45,7 @@ class WireguardKeyFragment : ServiceDependentFragment(OnNoService.GoToLaunchScre private var tunnelState: TunnelState = TunnelState.Disconnected() private lateinit var urlController: BlockingController - private var actionState = ActionState.Idle + private var actionState: ActionState = ActionState.Idle(false) set(value) { if (field != value) { field = value @@ -160,7 +159,7 @@ class WireguardKeyFragment : ServiceDependentFragment(OnNoService.GoToLaunchScre synchronized(this@WireguardKeyFragment) { tunnelState = uiState - if (actionState == ActionState.Generating) { + if (actionState is ActionState.Generating) { reconnectionExpected = !(tunnelState is TunnelState.Disconnected) } else if (tunnelState is TunnelState.Connected) { reconnectionExpected = false @@ -182,10 +181,13 @@ class WireguardKeyFragment : ServiceDependentFragment(OnNoService.GoToLaunchScre connectionProxy.onUiStateChange.unsubscribe(listener) } + if (!(actionState is ActionState.Idle)) { + actionState = ActionState.Idle(false) + } + keyStatusListener.onKeyStatusChange = null currentJob?.cancel() resetReconnectionExpectedJob?.cancel() - actionState = ActionState.Idle urlController.onPause() jobTracker.cancelAllJobs() } @@ -213,16 +215,16 @@ class WireguardKeyFragment : ServiceDependentFragment(OnNoService.GoToLaunchScre private fun updateStatus() { jobTracker.newUiJob("updateStatus") { verifyingKeySpinner.visibility = when (actionState) { - ActionState.Verifying -> View.VISIBLE + is ActionState.Verifying -> View.VISIBLE else -> View.GONE } - when (actionState) { - ActionState.Generating -> statusMessage.visibility = View.GONE - ActionState.Verifying -> statusMessage.visibility = View.GONE - ActionState.Idle -> { + when (val state = actionState) { + is ActionState.Generating -> statusMessage.visibility = View.GONE + is ActionState.Verifying -> statusMessage.visibility = View.GONE + is ActionState.Idle -> { if (hasConnectivity) { - updateKeyStatus(keyStatus) + updateKeyStatus(state.verified, keyStatus) } else { updateOfflineStatus() } @@ -239,29 +241,35 @@ class WireguardKeyFragment : ServiceDependentFragment(OnNoService.GoToLaunchScre } } - private fun updateKeyStatus(keyStatus: KeygenEvent?) { + private fun updateKeyStatus(verificationWasDone: Boolean, keyStatus: KeygenEvent?) { if (keyStatus is KeygenEvent.NewKey) { if (keyStatus.replacementFailure != null) { showKeygenFailure(keyStatus.replacementFailure) } else { - updateKeyIsValid(keyStatus.verified) + updateKeyIsValid(verificationWasDone, keyStatus.verified) } } else { statusMessage.visibility = View.GONE } } - private fun updateKeyIsValid(verified: Boolean?) { + private fun updateKeyIsValid(verificationWasDone: Boolean, verified: Boolean?) { when (verified) { true -> setStatusMessage(R.string.wireguard_key_valid, R.color.green) false -> setStatusMessage(R.string.wireguard_key_invalid, R.color.red) - null -> statusMessage.visibility = View.GONE + null -> { + if (verificationWasDone) { + setStatusMessage(R.string.wireguard_key_verification_failure, R.color.red) + } else { + statusMessage.visibility = View.GONE + } + } } } private fun updateButtons() { jobTracker.newUiJob("updateButtons") { - val isIdle = actionState == ActionState.Idle + val isIdle = actionState is ActionState.Idle generateKeyButton.setEnabled(isIdle && hasConnectivity) verifyKeyButton.setEnabled(isIdle && hasConnectivity) @@ -299,40 +307,24 @@ class WireguardKeyFragment : ServiceDependentFragment(OnNoService.GoToLaunchScre currentJob = GlobalScope.launch(Dispatchers.Default) { synchronized(this) { - actionState = ActionState.Generating + actionState = ActionState.Generating() reconnectionExpected = !(tunnelState is TunnelState.Disconnected) } keyStatus = null keyStatusListener.generateKey().join() - actionState = ActionState.Idle + actionState = ActionState.Idle(false) } } private fun onValidateKeyPress() { currentJob?.cancel() - actionState = ActionState.Verifying - - currentJob = GlobalScope.launch(Dispatchers.Main) { - statusMessage.visibility = View.GONE - verifyingKeySpinner.visibility = View.VISIBLE + currentJob = GlobalScope.launch(Dispatchers.Default) { + actionState = ActionState.Verifying() keyStatusListener.verifyKey().join() - - verifyingKeySpinner.visibility = View.GONE - statusMessage.visibility = View.VISIBLE - actionState = ActionState.Idle - - when (val state = keyStatus) { - is KeygenEvent.NewKey -> { - if (state.verified == null) { - Toast.makeText(parentActivity, - R.string.wireguard_key_verification_failure, - Toast.LENGTH_SHORT).show() - } - } - } + actionState = ActionState.Idle(true) } } diff --git a/android/src/main/res/values/strings.xml b/android/src/main/res/values/strings.xml index eb94bed4f1..cbef0fe68c 100644 --- a/android/src/main/res/values/strings.xml +++ b/android/src/main/res/values/strings.xml @@ -165,8 +165,8 @@ manage keys while in a blocked state</string> <string name="wireguard_key_valid">Key is valid</string> <string name="wireguard_key_invalid">Key is invalid</string> - <string name="wireguard_key_verification_failure">Failed to - validate key</string> + <string name="wireguard_key_verification_failure">Key verification + failed</string> <string name="wireguard_public_key">WireGuard public key </string> <string name="copied_wireguard_public_key">Copied WireGuard |
