From d79c8f09e6edffb382441cc76e7e3888b09b6a70 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Mon, 8 Jun 2020 13:56:52 +0200 Subject: [PATCH] Added tunnel settings --- .../viewmodels/TunnelSettingsViewModel.kt | 112 +++++++++++++++++- .../res/layout/settings_tunnel_fragment.xml | 55 ++++++++- app/src/main/res/values/strings.xml | 15 +++ 3 files changed, 180 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/linphone/activities/main/settings/viewmodels/TunnelSettingsViewModel.kt b/app/src/main/java/org/linphone/activities/main/settings/viewmodels/TunnelSettingsViewModel.kt index ef4688fb1..7b38dd8ae 100644 --- a/app/src/main/java/org/linphone/activities/main/settings/viewmodels/TunnelSettingsViewModel.kt +++ b/app/src/main/java/org/linphone/activities/main/settings/viewmodels/TunnelSettingsViewModel.kt @@ -19,6 +19,116 @@ */ package org.linphone.activities.main.settings.viewmodels +import androidx.lifecycle.MutableLiveData +import org.linphone.R +import org.linphone.activities.main.settings.SettingListenerStub +import org.linphone.core.Factory +import org.linphone.core.Tunnel +import org.linphone.core.TunnelConfig + class TunnelSettingsViewModel : GenericSettingsViewModel() { - // TODO + val hostnameUrlListener = object : SettingListenerStub() { + override fun onTextValueChanged(newValue: String) { + val config = getTunnelConfig() + config.host = newValue + updateTunnelConfig(config) + } + } + val hostnameUrl = MutableLiveData() + + val portListener = object : SettingListenerStub() { + override fun onTextValueChanged(newValue: String) { + if (newValue.isNotEmpty()) { + val config = getTunnelConfig() + config.port = newValue.toInt() + updateTunnelConfig(config) + } + } + } + val port = MutableLiveData() + + val useDualModeListener = object : SettingListenerStub() { + override fun onBoolValueChanged(newValue: Boolean) { + val tunnel = core.tunnel + tunnel.enableDualMode(newValue) + } + } + val useDualMode = MutableLiveData() + + val hostnameUrl2Listener = object : SettingListenerStub() { + override fun onTextValueChanged(newValue: String) { + val config = getTunnelConfig() + config.host2 = newValue + updateTunnelConfig(config) + } + } + val hostnameUrl2 = MutableLiveData() + + val port2Listener = object : SettingListenerStub() { + override fun onTextValueChanged(newValue: String) { + if (newValue.isNotEmpty()) { + val config = getTunnelConfig() + config.port2 = newValue.toInt() + updateTunnelConfig(config) + } + } + } + val port2 = MutableLiveData() + + val modeListener = object : SettingListenerStub() { + override fun onListValueChanged(position: Int) { + core.tunnel.mode = when (position) { + 0 -> Tunnel.Mode.Disable + 1 -> Tunnel.Mode.Enable + else -> Tunnel.Mode.Auto + } + } + } + val modeIndex = MutableLiveData() + val modeLabels = MutableLiveData>() + + init { + val tunnel = core.tunnel + val config = getTunnelConfig() + + hostnameUrl.value = config.host + port.value = config.port + useDualMode.value = tunnel.dualModeEnabled() + hostnameUrl2.value = config.host2 + port2.value = config.port2 + + initModeList() + } + + private fun getTunnelConfig(): TunnelConfig { + val tunnel = core.tunnel + val configs = tunnel.servers + return if (configs.isNotEmpty()) { + configs[0] + } else { + Factory.instance().createTunnelConfig() + } + } + + private fun updateTunnelConfig(config: TunnelConfig) { + val tunnel = core.tunnel + tunnel.cleanServers() + if (config.host?.isNotEmpty() == true) { + tunnel.addServer(config) + } + } + + private fun initModeList() { + val labels = arrayListOf() + labels.add(prefs.getString(R.string.tunnel_settings_disabled_mode)) + labels.add(prefs.getString(R.string.tunnel_settings_always_mode)) + labels.add(prefs.getString(R.string.tunnel_settings_auto_mode)) + modeLabels.value = labels + + modeIndex.value = when (core.tunnel.mode) { + Tunnel.Mode.Disable -> 0 + Tunnel.Mode.Enable -> 1 + else -> 2 + } + } } diff --git a/app/src/main/res/layout/settings_tunnel_fragment.xml b/app/src/main/res/layout/settings_tunnel_fragment.xml index 15e33e4c9..aec5b5525 100644 --- a/app/src/main/res/layout/settings_tunnel_fragment.xml +++ b/app/src/main/res/layout/settings_tunnel_fragment.xml @@ -1,8 +1,10 @@ - + + @@ -63,6 +65,57 @@ android:layout_height="wrap_content" android:orientation="vertical"> + + + + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cdb0491fd..e28bd39f0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -429,6 +429,21 @@ Android app settings + Hostname + + Port + + Dual mode enabled + Will use one server for upload and another one for download + Second server hostname + Required in dual mode + Second server port + Required in dual mode + Mode + + Disabled + Always + Auto Username