]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/coroutines/GetHostIpAddressesCoroutine.kt
Remove AsyncTask from SSLCertificateErrorDialog. https://redmine.stoutner.com/issues/987
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / coroutines / GetHostIpAddressesCoroutine.kt
index 985bc506c0c2229a0fc4024f78a3481c1b454715..4d3ed5ba2166b7589eb2283cc2d7805ba198d4a7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2019,2021-2022 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2019,2021-2023 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
  *
 
 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) {