2 * Copyright © 2019 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
6 * Privacy Browser is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser.asynctasks;
22 import android.app.Activity;
23 import android.os.AsyncTask;
25 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
27 import java.lang.ref.WeakReference;
28 import java.net.InetAddress;
29 import java.net.UnknownHostException;
31 // This must run asynchronously because it involves a network request. `String` declares the parameters. `Void` does not declare progress units. `String` contains the results.
32 public class GetHostIpAddresses extends AsyncTask<String, Void, String> {
33 // The weak references are used to determine if the activity have disappeared while the AsyncTask is running.
34 private final WeakReference<Activity> activityWeakReference;
35 private final int domainSettingsDatabaseId;
37 public GetHostIpAddresses(Activity activity, int domainSettingsDatabaseId) {
38 // Populate the weak activity reference.
39 activityWeakReference = new WeakReference<>(activity);
41 // Populate the domain settings database ID.
42 this.domainSettingsDatabaseId = domainSettingsDatabaseId;
45 // `onPreExecute()` operates on the UI thread.
47 protected void onPreExecute() {
48 // Get a handle for the activity.
49 Activity activity = activityWeakReference.get();
51 // Abort if the activity is gone.
52 if ((activity == null) || activity.isFinishing()) {
56 // Set the getting IP addresses tracker.
57 MainWebViewActivity.gettingIpAddresses = true;
62 protected String doInBackground(String... domainName) {
63 // Get a handle for the activity.
64 Activity activity = activityWeakReference.get();
66 // Abort if the activity is gone.
67 if ((activity == null) || activity.isFinishing()) {
68 // Return an empty spannable string builder.
72 // Initialize an IP address string builder.
73 StringBuilder ipAddresses = new StringBuilder();
75 // Get an array with the IP addresses for the host.
77 // Get an array with all the IP addresses for the domain.
78 InetAddress[] inetAddressesArray = InetAddress.getAllByName(domainName[0]);
80 // Add each IP address to the string builder.
81 for (InetAddress inetAddress : inetAddressesArray) {
82 if (ipAddresses.length() == 0) { // This is the first IP address.
83 // Add the IP address to the string builder.
84 ipAddresses.append(inetAddress.getHostAddress());
85 } else { // This is not the first IP address.
86 // Add a line break to the string builder first.
87 ipAddresses.append("\n");
89 // Add the IP address to the string builder.
90 ipAddresses.append(inetAddress.getHostAddress());
93 } catch (UnknownHostException exception) {
98 return ipAddresses.toString();
101 // `onPostExecute()` operates on the UI thread.
103 protected void onPostExecute(String ipAddresses) {
104 // Get a handle for the activity.
105 Activity activity = activityWeakReference.get();
107 // Abort if the activity is gone.
108 if ((activity == null) || activity.isFinishing()) {
112 // Store the IP addresses.
113 MainWebViewActivity.currentHostIpAddresses = ipAddresses;
115 if (!MainWebViewActivity.urlIsLoading) {
116 MainWebViewActivity.checkPinnedMismatch(domainSettingsDatabaseId);
119 // Reset the getting IP addresses tracker.
120 MainWebViewActivity.gettingIpAddresses = false;