diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/LoginScreen.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/LoginScreen.kt index b4287aaeef90..113ef4b020b5 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/LoginScreen.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/LoginScreen.kt @@ -295,6 +295,7 @@ private fun LoginState.supportingText(): String? { when (loginError) { LoginError.InvalidCredentials -> R.string.login_fail_description LoginError.UnableToCreateAccount -> R.string.failed_to_create_account + LoginError.NoInternetConnection -> R.string.no_internet_connection is LoginError.Unknown -> R.string.error_occurred null -> return null } diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/state/LoginUiState.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/state/LoginUiState.kt index bcbc181b85ab..82f69e538008 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/state/LoginUiState.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/state/LoginUiState.kt @@ -37,4 +37,6 @@ sealed class LoginError { data object InvalidCredentials : LoginError() data class Unknown(val reason: String) : LoginError() + + data object NoInternetConnection : LoginError() } diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/di/UiModule.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/di/UiModule.kt index 220097a7313a..9e35e67823a6 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/di/UiModule.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/di/UiModule.kt @@ -24,6 +24,7 @@ import net.mullvad.mullvadvpn.ui.serviceconnection.RelayListListener import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionManager import net.mullvad.mullvadvpn.ui.serviceconnection.SplitTunneling import net.mullvad.mullvadvpn.usecase.AccountExpiryNotificationUseCase +import net.mullvad.mullvadvpn.usecase.ConnectivityUseCase import net.mullvad.mullvadvpn.usecase.EmptyPaymentUseCase import net.mullvad.mullvadvpn.usecase.NewDeviceNotificationUseCase import net.mullvad.mullvadvpn.usecase.PaymentUseCase @@ -100,6 +101,7 @@ val uiModule = module { single { NewDeviceNotificationUseCase(get()) } single { PortRangeUseCase(get()) } single { RelayListUseCase(get(), get()) } + single { ConnectivityUseCase(get()) } single { InAppNotificationController(get(), get(), get(), get(), MainScope()) } @@ -130,7 +132,7 @@ val uiModule = module { viewModel { ConnectViewModel(get(), get(), get(), get(), get(), get(), get()) } viewModel { DeviceListViewModel(get(), get()) } viewModel { DeviceRevokedViewModel(get(), get()) } - viewModel { LoginViewModel(get(), get(), get()) } + viewModel { LoginViewModel(get(), get(), get(), get()) } viewModel { PrivacyDisclaimerViewModel(get()) } viewModel { SelectLocationViewModel(get(), get(), get()) } viewModel { SettingsViewModel(get(), get()) } diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/ConnectivityUseCase.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/ConnectivityUseCase.kt new file mode 100644 index 000000000000..35983a86c0e9 --- /dev/null +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/ConnectivityUseCase.kt @@ -0,0 +1,17 @@ +package net.mullvad.mullvadvpn.usecase + +import android.content.Context +import android.net.ConnectivityManager +import android.net.NetworkCapabilities + +class ConnectivityUseCase(val context: Context) { + fun isInternetAvailable(): Boolean { + val connectivityManager = + context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + + val network = connectivityManager.activeNetwork + val capabilities = connectivityManager.getNetworkCapabilities(network) + + return capabilities?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) == true + } +} diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/LoginViewModel.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/LoginViewModel.kt index 87174f2063bc..34648f1d539b 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/LoginViewModel.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/LoginViewModel.kt @@ -17,7 +17,9 @@ import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import net.mullvad.mullvadvpn.compose.state.LoginError import net.mullvad.mullvadvpn.compose.state.LoginState -import net.mullvad.mullvadvpn.compose.state.LoginState.* +import net.mullvad.mullvadvpn.compose.state.LoginState.Idle +import net.mullvad.mullvadvpn.compose.state.LoginState.Loading +import net.mullvad.mullvadvpn.compose.state.LoginState.Success import net.mullvad.mullvadvpn.compose.state.LoginUiState import net.mullvad.mullvadvpn.constant.LOGIN_TIMEOUT_MILLIS import net.mullvad.mullvadvpn.model.AccountCreationResult @@ -25,6 +27,7 @@ import net.mullvad.mullvadvpn.model.AccountToken import net.mullvad.mullvadvpn.model.LoginResult import net.mullvad.mullvadvpn.repository.AccountRepository import net.mullvad.mullvadvpn.repository.DeviceRepository +import net.mullvad.mullvadvpn.usecase.ConnectivityUseCase import net.mullvad.mullvadvpn.usecase.NewDeviceNotificationUseCase import net.mullvad.mullvadvpn.util.awaitWithTimeoutOrNull @@ -42,6 +45,7 @@ class LoginViewModel( private val accountRepository: AccountRepository, private val deviceRepository: DeviceRepository, private val newDeviceNotificationUseCase: NewDeviceNotificationUseCase, + private val connectivityUseCase: ConnectivityUseCase, private val dispatcher: CoroutineDispatcher = Dispatchers.IO ) : ViewModel() { private val _loginState = MutableStateFlow(LoginUiState.INITIAL.loginState) @@ -75,6 +79,10 @@ class LoginViewModel( } fun login(accountToken: String) { + if (!isInternetAvailable()) { + _loginState.value = Idle(LoginError.NoInternetConnection) + return + } _loginState.value = Loading.LoggingIn viewModelScope.launch(dispatcher) { // Ensure we always take at least MINIMUM_LOADING_SPINNER_TIME_MILLIS to show the @@ -135,4 +143,8 @@ class LoginViewModel( Idle(LoginError.UnableToCreateAccount) } } + + private fun isInternetAvailable(): Boolean { + return connectivityUseCase.isInternetAvailable() + } } diff --git a/android/lib/resource/src/main/res/values/strings.xml b/android/lib/resource/src/main/res/values/strings.xml index 5b33b45f575e..0b690671ace7 100644 --- a/android/lib/resource/src/main/res/values/strings.xml +++ b/android/lib/resource/src/main/res/values/strings.xml @@ -35,6 +35,7 @@ Voucher code has already been used. An error occurred. Settings + No internet connection Account less than a day left less than a minute ago