]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java
Remove AsyncTask from SSLCertificateErrorDialog. https://redmine.stoutner.com/issues/987
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / MainWebViewActivity.java
index 162a1c8fcee84c3dd5519f10f3c4f9c58519ef8c..229ab3b6392233e6c9737fd797c88b967c81a3c5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015-2022 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2015-2023 Soren Stoutner <soren@stoutner.com>.
  *
  * Download cookie code contributed 2017 Hendrik Knackstedt.  Copyright assigned to Soren Stoutner <soren@stoutner.com>.
  *
@@ -127,11 +127,11 @@ import com.google.android.material.tabs.TabLayout;
 
 import com.stoutner.privacybrowser.R;
 import com.stoutner.privacybrowser.adapters.WebViewPagerAdapter;
-import com.stoutner.privacybrowser.asynctasks.SaveUrl;
-import com.stoutner.privacybrowser.asynctasks.SaveWebpageImage;
 import com.stoutner.privacybrowser.coroutines.GetHostIpAddressesCoroutine;
 import com.stoutner.privacybrowser.coroutines.PopulateBlocklistsCoroutine;
 import com.stoutner.privacybrowser.coroutines.PrepareSaveDialogCoroutine;
+import com.stoutner.privacybrowser.coroutines.SaveUrlCoroutine;
+import com.stoutner.privacybrowser.coroutines.SaveWebpageImageCoroutine;
 import com.stoutner.privacybrowser.dataclasses.PendingDialogDataClass;
 import com.stoutner.privacybrowser.dialogs.CreateBookmarkDialog;
 import com.stoutner.privacybrowser.dialogs.CreateBookmarkFolderDialog;
@@ -363,7 +363,12 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
                 public void onActivityResult(Uri fileUri) {
                     // Only save the URL if the file URI is not null, which happens if the user exited the file picker by pressing back.
                     if (fileUri != null) {
-                        new SaveUrl(getApplicationContext(), resultLauncherActivityHandle, fileUri, currentWebView.getSettings().getUserAgentString(), currentWebView.getAcceptCookies()).execute(saveUrlString);
+                        // Instantiate the save URL coroutine.
+                        SaveUrlCoroutine saveUrlCoroutine = new SaveUrlCoroutine();
+
+                        // Save the URL.
+                        saveUrlCoroutine.save(getApplicationContext(), resultLauncherActivityHandle, saveUrlString, fileUri, currentWebView.getSettings().getUserAgentString(),
+                                currentWebView.getAcceptCookies());
                     }
 
                     // Reset the save URL string.
@@ -456,8 +461,11 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
                 public void onActivityResult(Uri fileUri) {
                     // Only save the webpage image if the file URI is not null, which happens if the user exited the file picker by pressing back.
                     if (fileUri != null) {
+                        // Instantiate the save webpage image coroutine.
+                        SaveWebpageImageCoroutine saveWebpageImageCoroutine = new SaveWebpageImageCoroutine();
+
                         // Save the webpage image.
-                        new SaveWebpageImage(resultLauncherActivityHandle, fileUri, currentWebView).execute();
+                        saveWebpageImageCoroutine.save(resultLauncherActivityHandle, fileUri, currentWebView);
                     }
                 }
             });
@@ -751,6 +759,19 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
                 }
             }
         } else {  // The app has been restarted.
+            // Get the information from the intent.
+            String intentAction = intent.getAction();
+            Uri intentUriData = intent.getData();
+            String intentStringExtra = intent.getStringExtra(Intent.EXTRA_TEXT);
+
+            // Determine if this is a web search.
+            boolean isWebSearch = ((intentAction != null) && intentAction.equals(Intent.ACTION_WEB_SEARCH));
+
+            // If the new intent will open a new tab, set the saved tab position to be the size of the saved state array list.
+            // The tab position is 0 based, meaning the at the new tab will be the tab position that is restored.
+            if (intentUriData != null || intentStringExtra != null || isWebSearch)
+                savedTabPosition = savedStateArrayList.size();
+
             // Replace the intent that started the app with this one.  This will load the tab after the others have been restored.
             setIntent(intent);
         }
@@ -2600,7 +2621,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
                         startActivity(emailIntent);
                     } catch (ActivityNotFoundException exception) {
                         // Display a snackbar.
-                        Snackbar.make(currentWebView, getString(R.string.error) + "  " + exception, Snackbar.LENGTH_INDEFINITE).show();
+                        Snackbar.make(currentWebView, getString(R.string.error) + exception, Snackbar.LENGTH_INDEFINITE).show();
                     }
 
                     // Consume the event.
@@ -2929,7 +2950,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
                 currentWebView.loadUrl(temporaryMhtFile.toString());
             } catch (Exception exception) {
                 // Display a snackbar.
-                Snackbar.make(currentWebView, getString(R.string.error) + "  " + exception, Snackbar.LENGTH_INDEFINITE).show();
+                Snackbar.make(currentWebView, getString(R.string.error) + exception, Snackbar.LENGTH_INDEFINITE).show();
             }
         } else {  // Let the WebView handle opening of the file.
             // Open the file.
@@ -4305,7 +4326,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
             startActivity(openWithAppIntent);
         } catch (ActivityNotFoundException exception) {  // There are no apps available to open the URL.
             // Show a snackbar with the error.
-            Snackbar.make(currentWebView, getString(R.string.error) + "  " + exception, Snackbar.LENGTH_INDEFINITE).show();
+            Snackbar.make(currentWebView, getString(R.string.error) + exception, Snackbar.LENGTH_INDEFINITE).show();
         }
     }
 
@@ -4325,7 +4346,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
             startActivity(openWithBrowserIntent);
         } catch (ActivityNotFoundException exception) {  // There are no browsers available to open the URL.
             // Show a snackbar with the error.
-            Snackbar.make(currentWebView, getString(R.string.error) + "  " + exception, Snackbar.LENGTH_INDEFINITE).show();
+            Snackbar.make(currentWebView, getString(R.string.error) + exception, Snackbar.LENGTH_INDEFINITE).show();
         }
     }
 
@@ -4356,7 +4377,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
             // Add the first tab.
             addNewTab("", true);
         } else {  // The activity has been restarted.
-            // Restore each tab.  Once the minimum API >= 24, a `forEach()` command can be used.
+            // Restore each tab.
             for (int i = 0; i < savedStateArrayList.size(); i++) {
                 // Add a new tab.
                 tabLayout.addTab(tabLayout.newTab());
@@ -4382,7 +4403,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
             if (savedTabPosition == 0) {  // The first tab is selected.
                 // Set the first page as the current WebView.
                 setCurrentWebView(0);
-            } else {  // the first tab is not selected.
+            } else {  // The first tab is not selected.
                 // Move to the selected tab.
                 webViewPager.setCurrentItem(savedTabPosition);
             }
@@ -4841,7 +4862,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
                 // Remove any background on the URL relative layout.
                 urlRelativeLayout.setBackground(ResourcesCompat.getDrawable(getResources(), R.color.transparent, null));
             }
-        } else {  // The fragment has not been populated.  Try again in 100 milliseconds.
+        } else if (pageNumber == savedTabPosition){  // The app is being restored but the saved tab position fragment has not been populated yet.  Try again in 100 milliseconds.
             // Create a handler to set the current WebView.
             Handler setCurrentWebViewHandler = new Handler();
 
@@ -5330,9 +5351,11 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
 
         nestedScrollWebView.setWebViewClient(new WebViewClient() {
             // `shouldOverrideUrlLoading` makes this WebView the default handler for URLs inside the app, so that links are not kicked out to other apps.
-            // The deprecated `shouldOverrideUrlLoading` must be used until API >= 24.
             @Override
-            public boolean shouldOverrideUrlLoading(WebView view, String url) {
+            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest webResourceRequest) {
+                // Get the URL from the web resource request.
+                String url = webResourceRequest.getUrl().toString();
+
                 // Sanitize the url.
                 url = sanitizeUrl(url);
 
@@ -5359,7 +5382,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
                         startActivity(emailIntent);
                     } catch (ActivityNotFoundException exception) {
                         // Display a snackbar.
-                        Snackbar.make(currentWebView, getString(R.string.error) + "  " + exception, Snackbar.LENGTH_INDEFINITE).show();
+                        Snackbar.make(currentWebView, getString(R.string.error) + exception, Snackbar.LENGTH_INDEFINITE).show();
                     }
 
 
@@ -5380,7 +5403,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
                         startActivity(dialIntent);
                     } catch (ActivityNotFoundException exception) {
                         // Display a snackbar.
-                        Snackbar.make(currentWebView, getString(R.string.error) + "  " + exception, Snackbar.LENGTH_INDEFINITE).show();
+                        Snackbar.make(currentWebView, getString(R.string.error) + exception, Snackbar.LENGTH_INDEFINITE).show();
                     }
 
                     // Returning true indicates Privacy Browser is handling the URL by creating an intent.
@@ -5402,7 +5425,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
                     try {
                         startActivity(genericIntent);
                     } catch (ActivityNotFoundException exception) {
-                        Snackbar.make(nestedScrollWebView, getString(R.string.unrecognized_url) + "  " + url, Snackbar.LENGTH_SHORT).show();
+                        Snackbar.make(nestedScrollWebView, getString(R.string.unrecognized_url) + url, Snackbar.LENGTH_SHORT).show();
                     }
 
                     // Returning true indicates Privacy Browser is handling the URL by creating an intent.
@@ -5829,7 +5852,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
 
                 if ((currentDomainName != null) && !currentDomainName.isEmpty()) {
                     // Get the IP addresses for the current URI.
-                    GetHostIpAddressesCoroutine.getAddresses(currentDomainName, nestedScrollWebView, getSupportFragmentManager(), getString(R.string.pinned_mismatch));
+                    GetHostIpAddressesCoroutine.checkPinnedMismatch(currentDomainName, nestedScrollWebView, getSupportFragmentManager(), getString(R.string.pinned_mismatch));
                 }
 
                 // Replace Refresh with Stop if the options menu has been created.  (The first WebView typically begins loading before the menu items are instantiated.)