From 19544c1b3d90801205691ce4ccdde80a1d64aa65 Mon Sep 17 00:00:00 2001 From: pokkst Date: Sat, 9 Dec 2023 11:58:45 -0600 Subject: [PATCH] More bug fixes --- .../wallet/fragment/home/HomeFragment.kt | 31 +++++++- .../fragment/onboarding/OnboardingFragment.kt | 9 +++ .../onboarding/OnboardingViewModel.kt | 2 +- .../fragment/settings/SettingsFragment.kt | 77 +++++++++++-------- .../fragment/settings/SettingsViewModel.kt | 5 ++ .../net/mynero/wallet/service/ProxyService.kt | 17 +--- 6 files changed, 90 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/net/mynero/wallet/fragment/home/HomeFragment.kt b/app/src/main/java/net/mynero/wallet/fragment/home/HomeFragment.kt index 32fb6b0..1f751ed 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/home/HomeFragment.kt +++ b/app/src/main/java/net/mynero/wallet/fragment/home/HomeFragment.kt @@ -24,6 +24,7 @@ import net.mynero.wallet.MainActivity import net.mynero.wallet.R import net.mynero.wallet.adapter.TransactionInfoAdapter import net.mynero.wallet.adapter.TransactionInfoAdapter.TxInfoAdapterListener +import net.mynero.wallet.model.EnumTorState import net.mynero.wallet.model.TransactionInfo import net.mynero.wallet.model.WalletManager import net.mynero.wallet.service.BalanceService @@ -79,9 +80,10 @@ class HomeFragment : Fragment(), TxInfoAdapterListener { ProxyService.instance?.proxyChangeEvents?.observe(viewLifecycleOwner) { proxy -> lifecycleScope.launch(Dispatchers.IO) { - Log.d("HomeFragment", "Updating proxy:: $proxy") - WalletManager.instance?.setProxy(proxy) - WalletManager.instance?.wallet?.setProxy(proxy) + Log.d("HomeFragment", "Updating proxy, restarting wallet. proxy=$proxy") + val finalProxy = if(proxy.isNotEmpty() && ProxyService.instance?.usingProxy == true) proxy else "" + WalletManager.instance?.setProxy(finalProxy) + WalletManager.instance?.wallet?.setProxy(finalProxy) WalletManager.instance?.wallet?.init(0) WalletManager.instance?.wallet?.startRefresh() } @@ -89,7 +91,7 @@ class HomeFragment : Fragment(), TxInfoAdapterListener { DaemonService.instance?.daemonChangeEvents?.observe(viewLifecycleOwner) { daemon -> lifecycleScope.launch(Dispatchers.IO) { - Log.d("HomeFragment", "Updating daemon:: $daemon") + Log.d("HomeFragment", "Updating daemon, restarting wallet. daemon=$daemon") WalletManager.instance?.setDaemon(daemon) WalletManager.instance?.wallet?.init(0) WalletManager.instance?.wallet?.setTrustedDaemon(daemon.trusted) @@ -170,6 +172,27 @@ class HomeFragment : Fragment(), TxInfoAdapterListener { ) } } + + val samouraiTorManager = ProxyService.instance?.samouraiTorManager + samouraiTorManager?.getTorStateLiveData()?.observe(viewLifecycleOwner) { + samouraiTorManager.getProxy()?.address()?.let { socketAddress -> + if(socketAddress.toString().isEmpty()) return@let + if(ProxyService.instance?.usingProxy == true && ProxyService.instance?.useBundledTor == true) { + val proxyString = socketAddress.toString().substring(1) + val address = proxyString.split(":")[0] + val port = proxyString.split(":")[1] + refreshProxy(address, port) + } + } + } + } + + private fun refreshProxy(proxyAddress: String, proxyPort: String) { + val cachedProxyAddress = ProxyService.instance?.proxyAddress + val cachedProxyPort = ProxyService.instance?.proxyPort + if((proxyAddress != cachedProxyAddress) || (proxyPort != cachedProxyPort)) { + ProxyService.instance?.updateProxy(proxyAddress, proxyPort) + } } override fun onClickTransaction(txInfo: TransactionInfo?) { diff --git a/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingFragment.kt b/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingFragment.kt index 025565d..168dee2 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingFragment.kt +++ b/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingFragment.kt @@ -172,6 +172,10 @@ class OnboardingFragment : Fragment(), NodeSelectionDialogListener, AddNodeListe if(mViewModel?.useProxy?.value == true && mViewModel?.useBundledTor?.value == true) { torIcon?.visibility = View.VISIBLE indicatorCircle?.visibility = View.INVISIBLE + val proxyString = socketAddress.toString().substring(1) + val address = proxyString.split(":")[0] + val port = proxyString.split(":")[1] + updateProxy(address, port) } } @@ -192,6 +196,11 @@ class OnboardingFragment : Fragment(), NodeSelectionDialogListener, AddNodeListe } } + private fun updateProxy(address: String, port: String) { + walletProxyPortEditText?.setText(port) + walletProxyAddressEditText?.setText(address) + } + private fun bindListeners() { // Disable onBack click val onBackPressedCallback: OnBackPressedCallback = object : OnBackPressedCallback(true) { diff --git a/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingViewModel.kt b/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingViewModel.kt index a453cfa..2bb2727 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingViewModel.kt +++ b/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingViewModel.kt @@ -58,7 +58,7 @@ class OnboardingViewModel : ViewModel() { if(seedType == SeedType.POLYSEED && (passphrase.isEmpty() || confirmedPassphrase.isEmpty())) return@combineLiveDatas false if(useProxy && (proxyAddress.isEmpty() || proxyPort.isEmpty()) && !useBundledTor) return@combineLiveDatas false val progress = torState?.progressIndicator ?: 0 - if(useBundledTor && progress < 100) return@combineLiveDatas false + if(useBundledTor && progress < 100 && useProxy) return@combineLiveDatas false return@combineLiveDatas true } diff --git a/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsFragment.kt b/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsFragment.kt index bca0148..b3fbc50 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsFragment.kt +++ b/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsFragment.kt @@ -15,6 +15,7 @@ import android.widget.Toast import androidx.activity.OnBackPressedCallback import androidx.appcompat.widget.SwitchCompat import androidx.constraintlayout.widget.ConstraintLayout +import androidx.core.widget.addTextChangedListener import androidx.fragment.app.Fragment import androidx.lifecycle.ViewModelProvider import androidx.navigation.fragment.NavHostFragment @@ -77,21 +78,23 @@ class SettingsFragment : Fragment(), PasswordListener, NodeSelectionDialogListen walletProxyPortEditText = view.findViewById(R.id.wallet_proxy_port_edittext) useBundledTor = view.findViewById(R.id.bundled_tor_checkbox) - useBundledTor?.isChecked = ProxyService.instance?.useBundledTor == true - walletProxyPortEditText?.visibility = if (useBundledTor?.isChecked == true) View.GONE else View.VISIBLE - walletProxyAddressEditText?.visibility = if (useBundledTor?.isChecked == true) View.GONE else View.VISIBLE + val cachedProxyAddress = ProxyService.instance?.proxyAddress ?: return + val cachedProxyPort = ProxyService.instance?.proxyPort ?: return + val cachedUsingProxy = ProxyService.instance?.usingProxy == true + val cachedUsingBundledTor = ProxyService.instance?.useBundledTor == true + + val useProxyVisibility = if (cachedUsingProxy) View.VISIBLE else View.GONE + walletProxyPortEditText?.visibility = if (cachedUsingBundledTor) View.GONE else useProxyVisibility + walletProxyAddressEditText?.visibility = if (cachedUsingBundledTor) View.GONE else useProxyVisibility + proxySettingsLayout?.visibility = useProxyVisibility + streetModeSwitch?.isChecked = PrefService.instance?.getBoolean(Constants.PREF_STREET_MODE, false) == true monerochanSwitch?.isChecked = PrefService.instance?.getBoolean(Constants.PREF_MONEROCHAN, true) == true donationSwitch?.isChecked = PrefService.instance?.getBoolean(Constants.PREF_DONATE_PER_TX, false) == true + useBundledTor?.isChecked = cachedUsingBundledTor + torSwitch?.isChecked = cachedUsingProxy + updateProxy(cachedProxyAddress, cachedProxyPort) - val usesProxy = ProxyService.instance?.usingProxy == true - val cachedProxyAddress = ProxyService.instance?.proxyAddress ?: return - val cachedProxyPort = ProxyService.instance?.proxyPort ?: return - if (ProxyService.instance?.hasProxySet() == true) { - initProxyStuff(cachedProxyAddress, cachedProxyPort) - } - torSwitch?.isChecked = usesProxy - proxySettingsLayout?.visibility = if (usesProxy) View.VISIBLE else View.GONE val node = PrefService.instance?.node // shouldn't use default value here selectNodeButton?.text = getString(R.string.node_button_text, node?.address) @@ -154,26 +157,32 @@ class SettingsFragment : Fragment(), PasswordListener, NodeSelectionDialogListen torSwitch?.setOnCheckedChangeListener { _: CompoundButton?, b: Boolean -> mViewModel?.setUseProxy(b) } + + walletProxyPortEditText?.addTextChangedListener { + mViewModel?.edited = true + } + walletProxyAddressEditText?.addTextChangedListener { + mViewModel?.edited = true + } } private fun bindObservers() { mViewModel?.useProxy?.observe(viewLifecycleOwner) { b -> - if (b) { - if (ProxyService.instance?.hasProxySet() == true) { - val proxyAddress = ProxyService.instance?.proxyAddress ?: return@observe - val proxyPort = ProxyService.instance?.proxyPort ?: return@observe - initProxyStuff(proxyAddress, proxyPort) - } - proxySettingsLayout?.visibility = View.VISIBLE - } else { - proxySettingsLayout?.visibility = View.GONE - } + val visibility = if(b) View.VISIBLE else View.GONE + proxySettingsLayout?.visibility = visibility + + val useBundledTor = mViewModel?.useBundledTor?.value == true + walletProxyPortEditText?.visibility = if (useBundledTor) View.GONE else visibility + walletProxyAddressEditText?.visibility = if (useBundledTor) View.GONE else visibility + refreshProxy() } mViewModel?.useBundledTor?.observe(viewLifecycleOwner) { isChecked -> - walletProxyPortEditText?.visibility = if (isChecked) View.GONE else View.VISIBLE - walletProxyAddressEditText?.visibility = if (isChecked) View.GONE else View.VISIBLE + val usingProxy = mViewModel?.useProxy?.value == true + val visibility = if(usingProxy) View.VISIBLE else View.GONE + walletProxyPortEditText?.visibility = if (isChecked) View.GONE else visibility + walletProxyAddressEditText?.visibility = if (isChecked) View.GONE else visibility } val samouraiTorManager = ProxyService.instance?.samouraiTorManager @@ -186,6 +195,10 @@ class SettingsFragment : Fragment(), PasswordListener, NodeSelectionDialogListen if(mViewModel?.useProxy?.value == true && mViewModel?.useBundledTor?.value == true) { torIcon?.visibility = View.VISIBLE indicatorCircle?.visibility = View.INVISIBLE + val proxyString = socketAddress.toString().substring(1) + val address = proxyString.split(":")[0] + val port = proxyString.split(":")[1] + updateProxy(address, port) } } @@ -206,10 +219,18 @@ class SettingsFragment : Fragment(), PasswordListener, NodeSelectionDialogListen } } + private fun updateProxy(address: String, port: String) { + walletProxyPortEditText?.setText(port) + walletProxyAddressEditText?.setText(address) + refreshProxy() + } + private fun refreshProxy() { val proxyAddress = walletProxyAddressEditText?.text.toString() val proxyPort = walletProxyPortEditText?.text.toString() - ProxyService.instance?.updateProxy(proxyAddress, proxyPort) + + if(mViewModel?.edited == true) + ProxyService.instance?.updateProxy(proxyAddress, proxyPort) } private fun displaySeedDialog(password: String) { @@ -228,14 +249,6 @@ class SettingsFragment : Fragment(), PasswordListener, NodeSelectionDialogListen Toast.makeText(context, R.string.bad_password, Toast.LENGTH_SHORT).show() } - private fun initProxyStuff(proxyAddress: String, proxyPort: String) { - val validIpAddress = Patterns.IP_ADDRESS.matcher(proxyAddress).matches() - if (validIpAddress) { - walletProxyAddressEditText?.setText(proxyAddress) - walletProxyPortEditText?.setText(proxyPort) - } - } - override fun onNodeSelected() { val node = PrefService.instance?.node selectNodeButton?.text = getString(R.string.node_button_text, node?.address) diff --git a/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsViewModel.kt b/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsViewModel.kt index b236c02..581d6ea 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsViewModel.kt +++ b/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsViewModel.kt @@ -15,6 +15,7 @@ class SettingsViewModel : ViewModel() { val useProxy: LiveData = _useProxy private val _useBundledTor = MutableLiveData(false) val useBundledTor: LiveData = _useBundledTor + var edited = false init { _useProxy.value = ProxyService.instance?.usingProxy @@ -31,6 +32,8 @@ class SettingsViewModel : ViewModel() { } else { samouraiTorManager?.stop() } + + edited = true } fun setUseBundledTor(use: Boolean) { @@ -43,5 +46,7 @@ class SettingsViewModel : ViewModel() { } else { samouraiTorManager?.stop() } + + edited = true } } \ No newline at end of file diff --git a/app/src/main/java/net/mynero/wallet/service/ProxyService.kt b/app/src/main/java/net/mynero/wallet/service/ProxyService.kt index f0bf048..07ec855 100644 --- a/app/src/main/java/net/mynero/wallet/service/ProxyService.kt +++ b/app/src/main/java/net/mynero/wallet/service/ProxyService.kt @@ -15,17 +15,6 @@ class ProxyService(application: Application) : ServiceBase(null) { instance = this samouraiTorManager = SamouraiTorManager(application, TorKmpManager(application)) - samouraiTorManager?.getTorStateLiveData()?.observeForever { - samouraiTorManager?.getProxy()?.address()?.let { socketAddress -> - if(socketAddress.toString().isEmpty()) return@let - val proxyString = socketAddress.toString().substring(1) - val address = proxyString.split(":")[0] - val port = proxyString.split(":")[1] - if(usingProxy && useBundledTor) - updateProxy(address, port) - } - } - if(useBundledTor && usingProxy) { samouraiTorManager?.start() } @@ -54,7 +43,7 @@ class ProxyService(application: Application) : ServiceBase(null) { } } - fun hasProxySet(): Boolean { + private fun hasProxySet(): Boolean { val proxyString = proxy return proxyString?.contains(":") == true } @@ -70,7 +59,7 @@ class ProxyService(application: Application) : ServiceBase(null) { val proxyAddress: String get() { - if (hasProxySet()) { + if (hasProxySet() && usingProxy) { val proxyString = proxy return proxyString?.split(":".toRegex())?.dropLastWhile { it.isEmpty() } ?.toTypedArray() @@ -80,7 +69,7 @@ class ProxyService(application: Application) : ServiceBase(null) { } val proxyPort: String get() { - if (hasProxySet()) { + if (hasProxySet() && usingProxy) { val proxyString = proxy return proxyString?.split(":".toRegex())?.dropLastWhile { it.isEmpty() } ?.toTypedArray()