Commit 672d35a6 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Only access/change vars in onResume which are required here

parent 878b530e
...@@ -63,6 +63,7 @@ class MainFragment : Fragment() { ...@@ -63,6 +63,7 @@ class MainFragment : Fragment() {
private var proxyState:ProxyState = ProxyState.NOT_RUNNING private var proxyState:ProxyState = ProxyState.NOT_RUNNING
private var vpnStateReceiver: BroadcastReceiver? = null private var vpnStateReceiver: BroadcastReceiver? = null
private var latencyCheckJob:Job? = null private var latencyCheckJob:Job? = null
private var currentDisplayedServerHash:Int? = null
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
...@@ -74,25 +75,24 @@ class MainFragment : Fragment() { ...@@ -74,25 +75,24 @@ class MainFragment : Fragment() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
val previousProxyState = proxyState
proxyState = if(requireContext().isServiceRunning(DnsVpnService::class.java)) { proxyState = if(requireContext().isServiceRunning(DnsVpnService::class.java)) {
if(DnsVpnService.paused) ProxyState.PAUSED if(DnsVpnService.paused) ProxyState.PAUSED
else ProxyState.RUNNING else ProxyState.RUNNING
} else ProxyState.NOT_RUNNING } else ProxyState.NOT_RUNNING
updateVpnIndicators() if(proxyState != previousProxyState) {
context?.clearPreviousIptablesRedirect() updateVpnIndicators()
runLatencyCheck()
determineLatencyBounds()
displayServer(getPreferences().dnsServerConfig)
GlobalScope.launch {
val context = context
if (isAdded && !isDetached && context != null) {
updatePrivacyPolicyLink(getPreferences().dnsServerConfig)
}
} }
if(proxyState != previousProxyState || currentDisplayedServerHash == null) {
displayServer(getPreferences().dnsServerConfig)
}
runLatencyCheck()
} }
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
context?.clearPreviousIptablesRedirect()
determineLatencyBounds()
vpnStateReceiver = requireContext().registerLocalReceiver( vpnStateReceiver = requireContext().registerLocalReceiver(
listOf( listOf(
DnsVpnService.BROADCAST_VPN_ACTIVE, DnsVpnService.BROADCAST_VPN_ACTIVE,
...@@ -199,9 +199,16 @@ class MainFragment : Fragment() { ...@@ -199,9 +199,16 @@ class MainFragment : Fragment() {
} }
} }
} }
mainServerWrap.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
serverIndicator.updateLayoutParams {
height = mainServerWrap.measuredHeight
}
}
} }
private fun displayServer(config: DnsServerInformation<*>) { private fun displayServer(config: DnsServerInformation<*>) {
if(config.hashCode() == currentDisplayedServerHash) return
currentDisplayedServerHash = config.hashCode()
serverName.text = config.name serverName.text = config.name
serverURL.text = when(config.type) { serverURL.text = when(config.type) {
ServerType.DOH -> (config as HttpsDnsServerInformation).servers.firstOrNull()?.address?.getUrl( ServerType.DOH -> (config as HttpsDnsServerInformation).servers.firstOrNull()?.address?.getUrl(
...@@ -214,11 +221,7 @@ class MainFragment : Fragment() { ...@@ -214,11 +221,7 @@ class MainFragment : Fragment() {
} }
serverLatency.text = "-\nms" serverLatency.text = "-\nms"
serverIndicator.backgroundTintList = null serverIndicator.backgroundTintList = null
mainServerWrap.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> updatePrivacyPolicyLink(config)
serverIndicator.updateLayoutParams {
height = mainServerWrap.measuredHeight
}
}
} }
override fun onStop() { override fun onStop() {
...@@ -311,7 +314,7 @@ class MainFragment : Fragment() { ...@@ -311,7 +314,7 @@ class MainFragment : Fragment() {
private fun updatePrivacyPolicyLink(serverInfo: DnsServerInformation<*>) { private fun updatePrivacyPolicyLink(serverInfo: DnsServerInformation<*>) {
activity?.let { _ -> activity?.let { _ ->
if (!serverInfo.specification.privacyPolicyURL.isNullOrBlank()) { if (isAdded && !isDetached && !serverInfo.specification.privacyPolicyURL.isNullOrBlank()) {
launchWithLifecycle { launchWithLifecycle {
val url = URL(serverInfo.specification.privacyPolicyURL) val url = URL(serverInfo.specification.privacyPolicyURL)
launchUi { launchUi {
...@@ -338,6 +341,7 @@ class MainFragment : Fragment() { ...@@ -338,6 +341,7 @@ class MainFragment : Fragment() {
private var goodLatencyThreshold = 200 private var goodLatencyThreshold = 200
private var averageLatencyThreshold = 310 private var averageLatencyThreshold = 310
private fun runLatencyCheck() { private fun runLatencyCheck() {
latencyCheckJob?.cancel()
latencyCheckJob = launchWithLifecycle(cancelOn = setOf(Lifecycle.Event.ON_PAUSE)) { latencyCheckJob = launchWithLifecycle(cancelOn = setOf(Lifecycle.Event.ON_PAUSE)) {
if(isActive) { if(isActive) {
launchUi { launchUi {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment