diff options
Diffstat (limited to 'android/app')
4 files changed, 66 insertions, 2 deletions
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 c3eb19b270..13b5e7a2db 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 @@ -105,8 +105,9 @@ val uiModule = module { androidContext().getSharedPreferences(APP_PREFERENCES_NAME, Context.MODE_PRIVATE) ) } - single { SettingsRepository(get()) } + single { SettingsRepository(get(), get()) } single { MullvadProblemReport(get()) } + single { RelayOverridesRepository(get(), get()) } single { CustomListsRepository(get(), get(), get()) } single { AccountExpiryNotificationUseCase(get()) } diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/RelayOverridesRepository.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/RelayOverridesRepository.kt new file mode 100644 index 0000000000..835cab4710 --- /dev/null +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/RelayOverridesRepository.kt @@ -0,0 +1,44 @@ +package net.mullvad.mullvadvpn.repository + +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.SharingStarted +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.flow.mapNotNull +import kotlinx.coroutines.flow.onStart +import kotlinx.coroutines.flow.stateIn +import net.mullvad.mullvadvpn.lib.ipc.MessageHandler +import net.mullvad.mullvadvpn.lib.ipc.Request +import net.mullvad.mullvadvpn.model.RelayOverride +import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionManager +import net.mullvad.mullvadvpn.ui.serviceconnection.settingsListener +import net.mullvad.mullvadvpn.util.callbackFlowFromNotifier +import net.mullvad.mullvadvpn.util.flatMapReadyConnectionOrDefault + +class RelayOverridesRepository( + private val serviceConnectionManager: ServiceConnectionManager, + private val messageHandler: MessageHandler, + dispatcher: CoroutineDispatcher = Dispatchers.IO, +) { + fun clearAllOverrides() { + messageHandler.trySendRequest(Request.ClearAllRelayOverrides) + } + + val relayOverrides: StateFlow<List<RelayOverride>?> = + serviceConnectionManager.connectionState + .flatMapReadyConnectionOrDefault(flowOf()) { state -> + callbackFlowFromNotifier(state.container.settingsListener.settingsNotifier) + } + .mapNotNull { it?.relayOverrides?.toList() } + .onStart { + serviceConnectionManager + .settingsListener() + ?.settingsNotifier + ?.latestEvent + ?.relayOverrides + ?.toList() + } + .stateIn(CoroutineScope(dispatcher), SharingStarted.Eagerly, null) +} diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/SettingsRepository.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/SettingsRepository.kt index 81c4b85b88..7d61feaf0c 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/SettingsRepository.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/SettingsRepository.kt @@ -4,11 +4,18 @@ import java.net.InetAddress import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.flow.stateIn +import kotlinx.coroutines.withContext +import net.mullvad.mullvadvpn.lib.ipc.Event.ApplyJsonSettingsResult +import net.mullvad.mullvadvpn.lib.ipc.MessageHandler +import net.mullvad.mullvadvpn.lib.ipc.Request +import net.mullvad.mullvadvpn.lib.ipc.events import net.mullvad.mullvadvpn.model.CustomDnsOptions import net.mullvad.mullvadvpn.model.DefaultDnsOptions import net.mullvad.mullvadvpn.model.DnsOptions @@ -24,7 +31,8 @@ import net.mullvad.mullvadvpn.util.flatMapReadyConnectionOrDefault class SettingsRepository( private val serviceConnectionManager: ServiceConnectionManager, - dispatcher: CoroutineDispatcher = Dispatchers.IO + private val messageHandler: MessageHandler, + private val dispatcher: CoroutineDispatcher = Dispatchers.IO, ) { val settingsUpdates: StateFlow<Settings?> = serviceConnectionManager.connectionState @@ -92,4 +100,11 @@ class SettingsRepository( fun setLocalNetworkSharing(isEnabled: Boolean) { serviceConnectionManager.settingsListener()?.allowLan = isEnabled } + + suspend fun applySettingsPatch(json: String) = + withContext(dispatcher) { + val deferred = async { messageHandler.events<ApplyJsonSettingsResult>().first() } + messageHandler.trySendRequest(Request.ApplyJsonSettings(json)) + deferred.await() + } } diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/serviceconnection/SettingsListener.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/serviceconnection/SettingsListener.kt index d996c432ad..e2ccc2e470 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/serviceconnection/SettingsListener.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/serviceconnection/SettingsListener.kt @@ -68,4 +68,8 @@ class SettingsListener(private val connection: Messenger, eventDispatcher: Event settings = newSettings } + + fun applySettingsPatch(json: String) { + connection.send(Request.ApplyJsonSettings(json).message) + } } |
