diff --git a/android/app/src/main/java/updated/mysterium/vpn/core/MysteriumAndroidCoreService.kt b/android/app/src/main/java/updated/mysterium/vpn/core/MysteriumAndroidCoreService.kt index 8e993c09..15b62df1 100644 --- a/android/app/src/main/java/updated/mysterium/vpn/core/MysteriumAndroidCoreService.kt +++ b/android/app/src/main/java/updated/mysterium/vpn/core/MysteriumAndroidCoreService.kt @@ -89,18 +89,23 @@ class MysteriumAndroidCoreService : VpnService(), KoinComponent { } private fun startMobileProviderService(active: Boolean) { + mobileNode?.let { isProviderActive = active try { if (active) { + println("startMobileProviderService >>>> 1") it.startProvider() } else { + println("startMobileProviderService >>>> 0") it.stopProvider() } + } catch (e: Exception) { isProviderActive = !active println(e) } + println("startMobileProviderService >>>> >>>>") } } diff --git a/android/app/src/main/java/updated/mysterium/vpn/ui/provider/ProviderActivity.kt b/android/app/src/main/java/updated/mysterium/vpn/ui/provider/ProviderActivity.kt index 393d2097..31630542 100644 --- a/android/app/src/main/java/updated/mysterium/vpn/ui/provider/ProviderActivity.kt +++ b/android/app/src/main/java/updated/mysterium/vpn/ui/provider/ProviderActivity.kt @@ -46,7 +46,9 @@ class ProviderActivity : BaseActivity() { initToolbar(binding.manualConnectToolbar) viewModel.providerUpdate.observe(this) { + binding.providerModeSwitch.tag = true binding.providerModeSwitch.isChecked = it.active + binding.providerModeSwitch.tag = null } viewModel.providerServiceStatus.observe(this) { @@ -70,7 +72,11 @@ class ProviderActivity : BaseActivity() { finish() } binding.providerModeSwitch.setOnCheckedChangeListener { _, isChecked -> - viewModel.toggleProvider(isChecked) + val v = binding.providerModeSwitch.tag + println("setOnCheckedChangeListener >>>>>> $v") + if (binding.providerModeSwitch.tag == null) { + viewModel.toggleProvider(isChecked) + } } binding.buttonUI.setOnClickListener { try { diff --git a/android/app/src/main/java/updated/mysterium/vpn/ui/provider/ProviderViewModel.kt b/android/app/src/main/java/updated/mysterium/vpn/ui/provider/ProviderViewModel.kt index 82c9fa27..857091a3 100644 --- a/android/app/src/main/java/updated/mysterium/vpn/ui/provider/ProviderViewModel.kt +++ b/android/app/src/main/java/updated/mysterium/vpn/ui/provider/ProviderViewModel.kt @@ -40,6 +40,7 @@ class ProviderViewModel(useCaseProvider: UseCaseProvider) : ViewModel() { private lateinit var appNotificationManager: AppNotificationManager private var deferredNode = DeferredNode() + val handler = CoroutineExceptionHandler { _, exception -> Log.i(TAG, exception.localizedMessage ?: exception.toString()) } @@ -56,7 +57,7 @@ class ProviderViewModel(useCaseProvider: UseCaseProvider) : ViewModel() { startDeferredNode() val initialState = ProviderState( - active = getIsProvider(), + active = getIsProviderActive(), ) _providerUpdate.postValue(initialState) @@ -68,27 +69,42 @@ class ProviderViewModel(useCaseProvider: UseCaseProvider) : ViewModel() { "scraping" -> servicesState.active[2] = running } _providerServiceStatus.postValue(servicesState) + + // make provider switch state "false" if all services are disabled + var allDisabled = true + for (x in servicesState.active) { + if (x) { + allDisabled = false + } + } + if (allDisabled) { + _providerUpdate.postValue(ProviderState(false)) + } + } } } fun toggleProvider(isChecked: Boolean) { + println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> toggleProvider $isChecked") + CoroutineScope(Dispatchers.IO).launch { coreService?.let { if (isChecked) { - if (it.isProviderActive()) { - return@let - } +// if (it.isProviderActive()) { +// return@let +// } it.startProvider(true) it.startForegroundWithNotification( NotificationChannels.PROVIDER_NOTIFICATION, appNotificationManager.createProviderNotification() ) + } else { - if (!it.isProviderActive()) { - return@let - } +// if (!it.isProviderActive()) { +// return@let +// } it.startProvider(false) it.stopForeground() @@ -112,7 +128,7 @@ class ProviderViewModel(useCaseProvider: UseCaseProvider) : ViewModel() { connectionUseCase.getIdentity() } - private fun getIsProvider(): Boolean { + private fun getIsProviderActive(): Boolean { coreService?.let { return it.isProviderActive() }