X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fcoroutines%2FGetHostIpAddressesCoroutine.kt;h=4d3ed5ba2166b7589eb2283cc2d7805ba198d4a7;hp=985bc506c0c2229a0fc4024f78a3481c1b454715;hb=12042264a50769030361cf51b0ac197050209f0f;hpb=7ab6a9027175e6a8f72b3f3683f8c4e05643d8bc diff --git a/app/src/main/java/com/stoutner/privacybrowser/coroutines/GetHostIpAddressesCoroutine.kt b/app/src/main/java/com/stoutner/privacybrowser/coroutines/GetHostIpAddressesCoroutine.kt index 985bc506..4d3ed5ba 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/coroutines/GetHostIpAddressesCoroutine.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/coroutines/GetHostIpAddressesCoroutine.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019,2021-2022 Soren Stoutner . + * Copyright 2019,2021-2023 Soren Stoutner . * * This file is part of Privacy Browser Android . * @@ -19,7 +19,13 @@ package com.stoutner.privacybrowser.coroutines +import android.text.SpannableStringBuilder +import android.text.Spanned +import android.text.style.ForegroundColorSpan +import android.widget.TextView + import androidx.fragment.app.FragmentManager + import com.stoutner.privacybrowser.helpers.CheckPinnedMismatchHelper import com.stoutner.privacybrowser.views.NestedScrollWebView @@ -33,8 +39,50 @@ import java.net.InetAddress import java.net.UnknownHostException object GetHostIpAddressesCoroutine { + fun getAddresses(domainName: String, ipAddressesLabel: String, blueColorSpan: ForegroundColorSpan, ipAddressesTextView: TextView) { + // Get the IP addresses using a coroutine. + CoroutineScope(Dispatchers.Main).launch { + // Get the IP addresses on the IO thread. + withContext(Dispatchers.IO) { + // Get an array with the IP addresses for the host. + try { + // Initialize an IP address string builder. + val ipAddressesStringBuilder = StringBuilder() + + // Get an array with all the IP addresses for the domain. + val inetAddressesArray = InetAddress.getAllByName(domainName) + + // Add each IP address to the string builder. + for (inetAddress in inetAddressesArray) { + // Add a line break to the string builder if this is not the first IP address. + if (ipAddressesStringBuilder.isNotEmpty()) { + ipAddressesStringBuilder.append("\n") + } + + // Add the IP address to the string builder. + ipAddressesStringBuilder.append(inetAddress.hostAddress) + } + + // Create a spannable string builder. + val addressesStringBuilder = SpannableStringBuilder(ipAddressesLabel + ipAddressesStringBuilder) + + // Set the string builder to display the certificate information in blue. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction. + addressesStringBuilder.setSpan(blueColorSpan, ipAddressesLabel.length, addressesStringBuilder.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE) + + // Populate the IP addresses text view on the UI thread. + withContext(Dispatchers.Main) { + // Populate the IP addresses text view. + ipAddressesTextView.text = addressesStringBuilder + } + } catch (exception: UnknownHostException) { + // Do nothing. + } + } + } + } + @JvmStatic - fun getAddresses(domainName: String, nestedScrollWebView: NestedScrollWebView, supportFragmentManager: FragmentManager, pinnedMismatchString: String) { + fun checkPinnedMismatch(domainName: String, nestedScrollWebView: NestedScrollWebView, supportFragmentManager: FragmentManager, pinnedMismatchString: String) { // Get the IP addresses using a coroutine. CoroutineScope(Dispatchers.Main).launch { // Get the IP addresses on the IO thread. @@ -42,7 +90,7 @@ object GetHostIpAddressesCoroutine { // Get an array with the IP addresses for the host. try { // Initialize an IP address string builder. - val ipAddresses = StringBuilder() + val ipAddressesStringBuilder = StringBuilder() // Get an array with all the IP addresses for the domain. val inetAddressesArray = InetAddress.getAllByName(domainName) @@ -50,16 +98,16 @@ object GetHostIpAddressesCoroutine { // Add each IP address to the string builder. for (inetAddress in inetAddressesArray) { // Add a line break to the string builder if this is not the first IP address. - if (ipAddresses.isNotEmpty()) { - ipAddresses.append("\n") + if (ipAddressesStringBuilder.isNotEmpty()) { + ipAddressesStringBuilder.append("\n") } // Add the IP address to the string builder. - ipAddresses.append(inetAddress.hostAddress) + ipAddressesStringBuilder.append(inetAddress.hostAddress) } // Store the IP addresses. - nestedScrollWebView.currentIpAddresses = ipAddresses.toString() + nestedScrollWebView.currentIpAddresses = ipAddressesStringBuilder.toString() // Checked for pinned mismatches if there is pinned information and it is not ignored. This must be done on the UI thread because checking the pinned mismatch interacts with the WebView. withContext(Dispatchers.Main) {