-
- // This must run asynchronously because it involves a network request. `String` declares the parameters. `Void` does not declare progress units. `SpannableStringBuilder` contains the results.
- private class GetIpAddresses constructor(activity: Activity, alertDialog: AlertDialog) : AsyncTask<String, Void?, SpannableStringBuilder>() {
- // Define the weak references.
- private val activityWeakReference: WeakReference<Activity> = WeakReference(activity)
- private val alertDialogWeakReference: WeakReference<AlertDialog> = WeakReference(alertDialog)
-
- override fun doInBackground(vararg domainName: String): SpannableStringBuilder {
- // Get handles for the activity and the alert dialog.
- val activity = activityWeakReference.get()
- val alertDialog = alertDialogWeakReference.get()
-
- // Abort if the activity or the dialog is gone.
- if (activity == null || activity.isFinishing || alertDialog == null) {
- return SpannableStringBuilder()
- }
-
- // Initialize an IP address string builder.
- val ipAddresses = StringBuilder()
-
- // Get an array with the IP addresses for the host.
- try {
- // Get an array with all the IP addresses for the domain.
- val inetAddressesArray = InetAddress.getAllByName(domainName[0])
-
- // Add each IP address to the string builder.
- for (inetAddress in inetAddressesArray) {
- // Check to see if this is not the first IP address.
- if (ipAddresses.isNotEmpty()) {
- // Add a line break to the string builder first.
- ipAddresses.append("\n")
- }
-
- // Add the IP Address to the string builder.
- ipAddresses.append(inetAddress.hostAddress)
- }
- } catch (exception: UnknownHostException) {
- // Do nothing.
- }
-
- // Set the label.
- val ipAddressesLabel = activity.getString(R.string.ip_addresses) + " "
-
- // Create a spannable string builder.
- val ipAddressesStringBuilder = SpannableStringBuilder(ipAddressesLabel + ipAddresses)
-
- // Create a blue color span according to the theme.
- val blueColorSpan = ForegroundColorSpan(activity.getColor(R.color.blue_text))
-
- // Set the string builder to display the certificate information in blue. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
- ipAddressesStringBuilder.setSpan(blueColorSpan, ipAddressesLabel.length, ipAddressesStringBuilder.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
-
- // Return the formatted string.
- return ipAddressesStringBuilder
- }
-
- // `onPostExecute()` operates on the UI thread.
- override fun onPostExecute(ipAddresses: SpannableStringBuilder) {
- // Get handles for the activity and the alert dialog.
- val activity = activityWeakReference.get()
- val alertDialog = alertDialogWeakReference.get()
-
- // Abort if the activity or the alert dialog is gone.
- if (activity == null || activity.isFinishing || alertDialog == null) {
- return
- }
-
- // Get a handle for the IP addresses text view.
- val ipAddressesTextView = alertDialog.findViewById<TextView>(R.id.ip_addresses)!!
-
- // Populate the IP addresses text view.
- ipAddressesTextView.text = ipAddresses
- }
- }
-}
\ No newline at end of file