X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FMainWebViewActivity.java;h=7f91e043e9aa648bf8699b22143f143b17295807;hp=d13c4d27bb0ff149fffeb19064b2564f7a2f3f3c;hb=3f3b7c8fbe988fe730a5fbb53169489566655595;hpb=de4e15cd445f2659165676e524a99f3c0f42033c diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java index d13c4d27..7f91e043 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -73,6 +73,7 @@ import android.webkit.CookieManager; import android.webkit.HttpAuthHandler; import android.webkit.SslErrorHandler; import android.webkit.ValueCallback; +import android.webkit.WebBackForwardList; import android.webkit.WebChromeClient; import android.webkit.WebResourceResponse; import android.webkit.WebSettings; @@ -129,6 +130,7 @@ import com.stoutner.privacybrowser.dialogs.DownloadLocationPermissionDialog; import com.stoutner.privacybrowser.dialogs.EditBookmarkDialog; import com.stoutner.privacybrowser.dialogs.EditBookmarkFolderDialog; import com.stoutner.privacybrowser.dialogs.HttpAuthenticationDialog; +import com.stoutner.privacybrowser.dialogs.PinnedMismatchDialog; import com.stoutner.privacybrowser.dialogs.SaveWebpageImageDialog; import com.stoutner.privacybrowser.dialogs.SslCertificateErrorDialog; import com.stoutner.privacybrowser.dialogs.StoragePermissionDialog; @@ -164,8 +166,8 @@ import java.util.Set; // AppCompatActivity from android.support.v7.app.AppCompatActivity must be used to have access to the SupportActionBar until the minimum API is >= 21. public class MainWebViewActivity extends AppCompatActivity implements CreateBookmarkDialog.CreateBookmarkListener, CreateBookmarkFolderDialog.CreateBookmarkFolderListener, DownloadFileDialog.DownloadFileListener, DownloadImageDialog.DownloadImageListener, DownloadLocationPermissionDialog.DownloadLocationPermissionDialogListener, EditBookmarkDialog.EditBookmarkListener, - EditBookmarkFolderDialog.EditBookmarkFolderListener, NavigationView.OnNavigationItemSelectedListener, PopulateBlocklists.PopulateBlocklistsListener, SaveWebpageImageDialog.SaveWebpageImageListener, - StoragePermissionDialog.StoragePermissionDialogListener, WebViewTabFragment.NewTabListener { + EditBookmarkFolderDialog.EditBookmarkFolderListener, NavigationView.OnNavigationItemSelectedListener, PinnedMismatchDialog.PinnedMismatchListener, PopulateBlocklists.PopulateBlocklistsListener, SaveWebpageImageDialog.SaveWebpageImageListener, + StoragePermissionDialog.StoragePermissionDialogListener, UrlHistoryDialog.NavigateHistoryListener, WebViewTabFragment.NewTabListener { // `orbotStatus` is public static so it can be accessed from `OrbotProxyHelper`. It is also used in `onCreate()`, `onResume()`, and `applyProxyThroughOrbot()`. public static String orbotStatus; @@ -433,7 +435,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook loadingNewIntent = true; // Add a new tab. - addNewTab(url); + addNewTab(url, true); } else { // Load the URL in the current tab. // Make it so. loadUrl(url); @@ -1803,11 +1805,17 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook case R.id.back: if (currentWebView.canGoBack()) { + // Get the current web back forward list. + WebBackForwardList webBackForwardList = currentWebView.copyBackForwardList(); + + // Get the previous entry URL. + String previousUrl = webBackForwardList.getItemAtIndex(webBackForwardList.getCurrentIndex() - 1).getUrl(); + // Reset the current domain name so that navigation works if third-party requests are blocked. currentWebView.resetCurrentDomainName(); - // Set navigating history so that the domain settings are applied when the new URL is loaded. - currentWebView.setNavigatingHistory(true); + // Apply the domain settings. + applyDomainSettings(currentWebView, previousUrl, false, false); // Load the previous website in the history. currentWebView.goBack(); @@ -1816,11 +1824,17 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook case R.id.forward: if (currentWebView.canGoForward()) { + // Get the current web back forward list. + WebBackForwardList webBackForwardList = currentWebView.copyBackForwardList(); + + // Get the next entry URL. + String nextUrl = webBackForwardList.getItemAtIndex(webBackForwardList.getCurrentIndex() + 1).getUrl(); + // Reset the current domain name so that navigation works if third-party requests are blocked. currentWebView.resetCurrentDomainName(); - // Set navigating history so that the domain settings are applied when the new URL is loaded. - currentWebView.setNavigatingHistory(true); + // Apply the domain settings. + applyDomainSettings(currentWebView, nextUrl, false, false); // Load the next website in the history. currentWebView.goForward(); @@ -2026,7 +2040,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Add an Open in New Tab entry. menu.add(R.string.open_in_new_tab).setOnMenuItemClickListener((MenuItem item) -> { // Load the link URL in a new tab. - addNewTab(linkUrl); + addNewTab(linkUrl, false); // Consume the event. return true; @@ -2151,9 +2165,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook menu.setHeaderTitle(imageUrl); // Add an Open in New Tab entry. - menu.add(R.string.open_in_new_tab).setOnMenuItemClickListener((MenuItem item) -> { - // Load the image URL in a new tab. - addNewTab(imageUrl); + menu.add(R.string.open_image_in_new_tab).setOnMenuItemClickListener((MenuItem item) -> { + // Load the image in a new tab. + addNewTab(imageUrl, false); // Consume the event. return true; @@ -2260,7 +2274,16 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Add an Open in New Tab entry. menu.add(R.string.open_in_new_tab).setOnMenuItemClickListener((MenuItem item) -> { // Load the link URL in a new tab. - addNewTab(linkUrl); + addNewTab(linkUrl, false); + + // Consume the event. + return true; + }); + + // Add an Open Image in New Tab entry. + menu.add(R.string.open_image_in_new_tab).setOnMenuItemClickListener((MenuItem item) -> { + // Load the image in a new tab. + addNewTab(imageUrl, false); // Consume the event. return true; @@ -2739,7 +2762,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook } } - // Override `onBackPressed` to handle the navigation drawer and and the WebView. + // Override `onBackPressed` to handle the navigation drawer and and the WebViews. @Override public void onBackPressed() { // Get a handle for the drawer layout and the tab layout. @@ -2761,11 +2784,17 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook loadBookmarksFolder(); } } else if (currentWebView.canGoBack()) { // There is at least one item in the current WebView history. + // Get the current web back forward list. + WebBackForwardList webBackForwardList = currentWebView.copyBackForwardList(); + + // Get the previous entry URL. + String previousUrl = webBackForwardList.getItemAtIndex(webBackForwardList.getCurrentIndex() - 1).getUrl(); + // Reset the current domain name so that navigation works if third-party requests are blocked. currentWebView.resetCurrentDomainName(); - // Set navigating history so that the domain settings are applied when the new URL is loaded. - currentWebView.setNavigatingHistory(true); + // Apply the domain settings. + applyDomainSettings(currentWebView, previousUrl, false, false); // Go back. currentWebView.goBack(); @@ -3585,6 +3614,36 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook bareWebView.destroy(); } + @Override + public void navigateHistory(String url, int steps) { + // Reset the current domain name so that navigation works if third-party requests are blocked. + currentWebView.resetCurrentDomainName(); + + // Apply the domain settings. + applyDomainSettings(currentWebView, url, false, false); + + // Load the history entry. + currentWebView.goBackOrForward(steps); + } + + @Override + public void pinnedErrorGoBack() { + // Get the current web back forward list. + WebBackForwardList webBackForwardList = currentWebView.copyBackForwardList(); + + // Get the previous entry URL. + String previousUrl = webBackForwardList.getItemAtIndex(webBackForwardList.getCurrentIndex() - 1).getUrl(); + + // Reset the current domain name so that navigation works if third-party requests are blocked. + currentWebView.resetCurrentDomainName(); + + // Apply the domain settings. + applyDomainSettings(currentWebView, previousUrl, false, false); + + // Go back. + currentWebView.goBack(); + } + // `reloadWebsite` is used if returning from the Domains activity. Otherwise JavaScript might not function correctly if it is newly enabled. @SuppressLint("SetJavaScriptEnabled") private boolean applyDomainSettings(NestedScrollWebView nestedScrollWebView, String url, boolean resetTab, boolean reloadWebsite) { @@ -4410,6 +4469,16 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook if (url.contains("&fbclid=")) { url = url.substring(0, url.indexOf("&fbclid=")); } + + // Remove `?fbadid=`. + if (url.contains("?fbadid=")) { + url = url.substring(0, url.indexOf("?fbadid=")); + } + + // Remove `&fbadid=`. + if (url.contains("&fbadid=")) { + url = url.substring(0, url.indexOf("&fbadid=")); + } } // Sanitize Twitter AMP redirects. @@ -4434,15 +4503,15 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook ultraPrivacy = combinedBlocklists.get(5); // Add the first tab. - addNewTab(""); + addNewTab("", true); } public void addTab(View view) { // Add a new tab with a blank URL. - addNewTab(""); + addNewTab("", true); } - private void addNewTab(String url) { + private void addNewTab(String url, boolean moveToTab) { // Sanitize the URL. url = sanitizeUrl(url); @@ -4466,7 +4535,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook newTab.setCustomView(R.layout.tab_custom_view); // Add the new WebView page. - webViewPagerAdapter.addPage(newTabNumber, webViewPager, url); + webViewPagerAdapter.addPage(newTabNumber, webViewPager, url, moveToTab); } public void closeTab(View view) { @@ -5782,20 +5851,6 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Get the IP addresses for the host. new GetHostIpAddresses(activity, getSupportFragmentManager(), nestedScrollWebView).execute(currentUri.getHost()); - // Apply any custom domain settings if the URL was loaded by navigating history. - if (nestedScrollWebView.getNavigatingHistory()) { - // Reset navigating history. - nestedScrollWebView.setNavigatingHistory(false); - - // Apply the domain settings. - boolean userAgentChanged = applyDomainSettings(nestedScrollWebView, url, true, false); - - // Manually load the URL if the user agent has changed, which will have caused the previous URL to be reloaded. - if (userAgentChanged) { - loadUrl(url); - } - } - // Replace Refresh with Stop if the options menu has been created. (The first WebView typically begins loading before the menu items are instantiated.) if (optionsMenu != null) { // Get a handle for the refresh menu item. @@ -5923,11 +5978,17 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook tabTitleTextView.setText(R.string.new_tab); } } else { // The WebView has loaded a webpage. - // Display the final URL. Getting the URL from the WebView instead of using the one provided by `onPageFinished()` makes websites like YouTube function correctly. - urlEditText.setText(currentUrl); + // Update the URL edit text if it is not currently being edited. + if (!urlEditText.hasFocus()) { + // Sanitize the current URL. This removes unwanted URL elements that were added by redirects, so that they won't be included if the URL is shared. + String sanitizedUrl = sanitizeUrl(currentUrl); - // Apply text highlighting to the URL. - highlightUrlText(); + // Display the final URL. Getting the URL from the WebView instead of using the one provided by `onPageFinished()` makes websites like YouTube function correctly. + urlEditText.setText(sanitizedUrl); + + // Apply text highlighting to the URL. + highlightUrlText(); + } // Only populate the title text view if the tab has been fully created. if (tab != null) {