- // This must run asynchronously because it involves a network request. `String` declares the parameters. `Void` does not declare progress units. `String` contains the results.
- private static class GetHostIpAddresses extends AsyncTask<String, Void, String> {
- // The weak references are used to determine if the activity have disappeared while the AsyncTask is running.
- private final WeakReference<Activity> activityWeakReference;
-
- GetHostIpAddresses(Activity activity) {
- // Populate the weak references.
- activityWeakReference = new WeakReference<>(activity);
- }
-
- // `onPreExecute()` operates on the UI thread.
- @Override
- protected void onPreExecute() {
- // Get a handle for the activity.
- Activity activity = activityWeakReference.get();
-
- // Abort if the activity is gone.
- if ((activity == null) || activity.isFinishing()) {
- return;
- }
-
- // Set the getting IP addresses tracker.
- gettingIpAddresses = true;
- }
-
-
- @Override
- protected String doInBackground(String... domainName) {
- // Get a handle for the activity.
- Activity activity = activityWeakReference.get();
-
- // Abort if the activity is gone.
- if ((activity == null) || activity.isFinishing()) {
- // Return an empty spannable string builder.
- return "";
- }
-
- // Initialize an IP address string builder.
- StringBuilder ipAddresses = new StringBuilder();
-
- // Get an array with the IP addresses for the host.
- try {
- // Get an array with all the IP addresses for the domain.
- InetAddress[] inetAddressesArray = InetAddress.getAllByName(domainName[0]);
-
- // Add each IP address to the string builder.
- for (InetAddress inetAddress : inetAddressesArray) {
- if (ipAddresses.length() == 0) { // This is the first IP address.
- // Add the IP address to the string builder.
- ipAddresses.append(inetAddress.getHostAddress());
- } else { // This is not the first IP address.
- // Add a line break to the string builder first.
- ipAddresses.append("\n");
-
- // Add the IP address to the string builder.
- ipAddresses.append(inetAddress.getHostAddress());
- }
- }
- } catch (UnknownHostException exception) {
- // Do nothing.
- }
-
- // Return the string.
- return ipAddresses.toString();
- }
-
- // `onPostExecute()` operates on the UI thread.
- @Override
- protected void onPostExecute(String ipAddresses) {
- // Get a handle for the activity.
- Activity activity = activityWeakReference.get();
-
- // Abort if the activity is gone.
- if ((activity == null) || activity.isFinishing()) {
- return;
- }
-
- // Store the IP addresses.
- currentHostIpAddresses = ipAddresses;
-
- if (!urlIsLoading) {
- checkPinnedMismatch();
- }
-
- // Reset the getting IP addresses tracker.
- gettingIpAddresses = false;
- }
- }
-
- private class WebViewPagerAdapter extends FragmentPagerAdapter {
- // The WebView fragments list contains all the WebViews.
- private LinkedList<WebViewTabFragment> webViewFragmentsList = new LinkedList<>();
-
- // Define the constructor.
- private WebViewPagerAdapter(FragmentManager fragmentManager){
- // Run the default commands.
- super(fragmentManager);
- }
-
- @Override
- public int getCount() {
- // Return the number of pages.
- return webViewFragmentsList.size();
- }
-
- @Override
- public int getItemPosition(@NonNull Object object) {
- //noinspection SuspiciousMethodCalls
- if (webViewFragmentsList.contains(object)) {
- // The tab has not been deleted.
- return POSITION_UNCHANGED;
- } else {
- // The tab has been deleted.
- return POSITION_NONE;
- }
- }
-
- @Override
- public Fragment getItem(int pageNumber) {
- // Get a WebView for a particular page. Page numbers are 0 indexed.
- return webViewFragmentsList.get(pageNumber);
- }
-
- private void addPage() {
- // Add a new page. The pages and tabs are 0 indexed, so the size of the current list equals the number of the next page.
- webViewFragmentsList.add(WebViewTabFragment.createTab(webViewFragmentsList.size()));
-
- // Update the view pager.
- notifyDataSetChanged();
- }
-
- private void deletePage(int pageNumber) {
- // Get a handle for the tab layout.
- TabLayout tabLayout = findViewById(R.id.tablayout);
-
- // TODO always move to the next tab if possible.
- // Select a tab that is not being deleted.
- if (pageNumber == 0) { // The first tab is being deleted.
- // Get a handle for the second tab. The tabs are 0 indexed.
- TabLayout.Tab secondTab = tabLayout.getTabAt(1);
-
- // Remove the incorrect lint warning below that the second tab might be null.
- assert secondTab != null;
-
- // Select the second tab.
- secondTab.select();
- } else { // The first tab is not being deleted.
- // Get a handle for the previous tab.
- TabLayout.Tab previousTab = tabLayout.getTabAt(pageNumber - 1);