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=b0cd103a68203752c29cafd546a9341d2ddd3725;hp=d397a619e3c0cd9a35a32aa8ef9c7a37f2fcaef6;hb=0c842fcba25551727cc26a7edc55bd5d5bf262ee;hpb=c0f2dcca77fffd804c90cee5103795fc3849112b 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 d397a619..b0cd103a 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -1144,6 +1144,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook MenuItem blockAllThirdPartyRequestsMenuItem = menu.findItem(R.id.block_all_third_party_requests); MenuItem fontSizeMenuItem = menu.findItem(R.id.font_size); MenuItem swipeToRefreshMenuItem = menu.findItem(R.id.swipe_to_refresh); + MenuItem wideViewportMenuItem = menu.findItem(R.id.wide_viewport); MenuItem displayImagesMenuItem = menu.findItem(R.id.display_images); MenuItem nightModeMenuItem = menu.findItem(R.id.night_mode); MenuItem proxyThroughOrbotMenuItem = menu.findItem(R.id.proxy_through_orbot); @@ -1180,6 +1181,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook ultraPrivacyMenuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.ULTRA_PRIVACY)); blockAllThirdPartyRequestsMenuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.THIRD_PARTY_REQUESTS)); swipeToRefreshMenuItem.setChecked(currentWebView.getSwipeToRefresh()); + wideViewportMenuItem.setChecked(currentWebView.getSettings().getUseWideViewPort()); displayImagesMenuItem.setChecked(currentWebView.getSettings().getLoadsImagesAutomatically()); nightModeMenuItem.setChecked(currentWebView.getNightMode()); @@ -1911,6 +1913,11 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook } return true; + case R.id.wide_viewport: + // Toggle the viewport. + currentWebView.getSettings().setUseWideViewPort(!currentWebView.getSettings().getUseWideViewPort()); + return true; + case R.id.display_images: if (currentWebView.getSettings().getLoadsImagesAutomatically()) { // Images are currently loaded automatically. // Disable loading of images. @@ -3214,8 +3221,8 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook newHostName = ""; } - // Only apply the domain settings if a new domain is being loaded. This allows the user to set temporary settings for JavaScript, cookies, DOM storage, etc. - if (!nestedScrollWebView.getCurrentDomainName().equals(newHostName)) { + // Apply the domain settings if a new domain is being loaded or if the new domain is blank. This allows the user to set temporary settings for JavaScript, cookies, DOM storage, etc. + if (!nestedScrollWebView.getCurrentDomainName().equals(newHostName) || newHostName.equals("")) { // Set the new host name as the current domain name. nestedScrollWebView.setCurrentDomainName(newHostName); @@ -3321,8 +3328,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook String defaultFontSizeString = sharedPreferences.getString("font_size", getString(R.string.font_size_default_value)); String defaultUserAgentName = sharedPreferences.getString("user_agent", getString(R.string.user_agent_default_value)); boolean defaultSwipeToRefresh = sharedPreferences.getBoolean("swipe_to_refresh", true); - boolean displayWebpageImages = sharedPreferences.getBoolean("display_webpage_images", true); boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false); + boolean wideViewport = sharedPreferences.getBoolean("wide_viewport", true); + boolean displayWebpageImages = sharedPreferences.getBoolean("display_webpage_images", true); // Get a handle for the cookie manager. CookieManager cookieManager = CookieManager.getInstance(); @@ -3364,6 +3372,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook int fontSize = currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.FONT_SIZE)); int swipeToRefreshInt = currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SWIPE_TO_REFRESH)); int nightModeInt = currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.NIGHT_MODE)); + int wideViewportInt = currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.WIDE_VIEWPORT)); int displayWebpageImagesInt = currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.DISPLAY_IMAGES)); boolean pinnedSslCertificate = (currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.PINNED_SSL_CERTIFICATE)) == 1); String pinnedSslIssuedToCName = currentDomainSettingsCursor.getString(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_TO_COMMON_NAME)); @@ -3406,17 +3415,17 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Set night mode according to the night mode int. switch (nightModeInt) { - case DomainsDatabaseHelper.NIGHT_MODE_SYSTEM_DEFAULT: + case DomainsDatabaseHelper.SYSTEM_DEFAULT: // Set night mode according to the current default. nestedScrollWebView.setNightMode(sharedPreferences.getBoolean("night_mode", false)); break; - case DomainsDatabaseHelper.NIGHT_MODE_ENABLED: + case DomainsDatabaseHelper.ENABLED: // Enable night mode. nestedScrollWebView.setNightMode(true); break; - case DomainsDatabaseHelper.NIGHT_MODE_DISABLED: + case DomainsDatabaseHelper.DISABLED: // Disable night mode. nestedScrollWebView.setNightMode(false); break; @@ -3502,7 +3511,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Set swipe to refresh. switch (swipeToRefreshInt) { - case DomainsDatabaseHelper.SWIPE_TO_REFRESH_SYSTEM_DEFAULT: + case DomainsDatabaseHelper.SYSTEM_DEFAULT: // Store the swipe to refresh status in the nested scroll WebView. nestedScrollWebView.setSwipeToRefresh(defaultSwipeToRefresh); @@ -3510,7 +3519,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook swipeRefreshLayout.setEnabled(defaultSwipeToRefresh); break; - case DomainsDatabaseHelper.SWIPE_TO_REFRESH_ENABLED: + case DomainsDatabaseHelper.ENABLED: // Store the swipe to refresh status in the nested scroll WebView. nestedScrollWebView.setSwipeToRefresh(true); @@ -3518,7 +3527,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook swipeRefreshLayout.setEnabled(true); break; - case DomainsDatabaseHelper.SWIPE_TO_REFRESH_DISABLED: + case DomainsDatabaseHelper.DISABLED: // Store the swipe to refresh status in the nested scroll WebView. nestedScrollWebView.setSwipeToRefresh(false); @@ -3526,17 +3535,32 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook swipeRefreshLayout.setEnabled(false); } + // Set the viewport. + switch (wideViewportInt) { + case DomainsDatabaseHelper.SYSTEM_DEFAULT: + nestedScrollWebView.getSettings().setUseWideViewPort(wideViewport); + break; + + case DomainsDatabaseHelper.ENABLED: + nestedScrollWebView.getSettings().setUseWideViewPort(true); + break; + + case DomainsDatabaseHelper.DISABLED: + nestedScrollWebView.getSettings().setUseWideViewPort(false); + break; + } + // Set the loading of webpage images. switch (displayWebpageImagesInt) { - case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_SYSTEM_DEFAULT: + case DomainsDatabaseHelper.SYSTEM_DEFAULT: nestedScrollWebView.getSettings().setLoadsImagesAutomatically(displayWebpageImages); break; - case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_ENABLED: + case DomainsDatabaseHelper.ENABLED: nestedScrollWebView.getSettings().setLoadsImagesAutomatically(true); break; - case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_DISABLED: + case DomainsDatabaseHelper.DISABLED: nestedScrollWebView.getSettings().setLoadsImagesAutomatically(false); break; } @@ -3619,6 +3643,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook nestedScrollWebView.getSettings().setUserAgentString(userAgentDataArray[userAgentArrayPosition]); } + // Set the viewport. + nestedScrollWebView.getSettings().setUseWideViewPort(wideViewport); + // Set the loading of webpage images. nestedScrollWebView.getSettings().setLoadsImagesAutomatically(displayWebpageImages); @@ -4395,9 +4422,6 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook nestedScrollWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_NEVER_ALLOW); } - // Set the WebView to use a wide viewport. Otherwise, some web pages will be scrunched and some content will render outside the screen. - nestedScrollWebView.getSettings().setUseWideViewPort(true); - // Set the WebView to load in overview mode (zoomed out to the maximum width). nestedScrollWebView.getSettings().setLoadWithOverviewMode(true); @@ -4678,8 +4702,14 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Get the title text view from the tab. TextView tabTitleTextView = tabView.findViewById(R.id.title_textview); - // Set the title as the tab text. - tabTitleTextView.setText(title); + // Set the title according to the URL. + if (title.equals("about:blank")) { + // Set the title to indicate a new tab. + tabTitleTextView.setText(R.string.new_tab); + } else { + // Set the title as the tab text. + tabTitleTextView.setText(title); + } } } @@ -5281,7 +5311,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook } } - // Replace Refresh with Stop if the options menu has been created. (The WebView typically begins loading before the menu items are instantiated.) + // 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. MenuItem refreshMenuItem = optionsMenu.findItem(R.id.refresh); @@ -5306,12 +5336,6 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook @Override public void onPageFinished(WebView view, String url) { - // Reset the wide view port if it has been turned off by the waiting for Orbot message. - if (!waitingForOrbot) { - // Only use a wide view port if the URL starts with `http`, not for `file://` and `content://`. - nestedScrollWebView.getSettings().setUseWideViewPort(url.startsWith("http")); - } - // Flush any cookies to persistent storage. The cookie manager has become very lazy about flushing cookies in recent versions. if (nestedScrollWebView.getAcceptFirstPartyCookies() && Build.VERSION.SDK_INT >= 21) { CookieManager.getInstance().flush(); @@ -5378,6 +5402,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Get the current URL from the nested scroll WebView. This is more accurate than using the URL passed into the method, which is sometimes not the final one. String currentUrl = nestedScrollWebView.getUrl(); + // Get the current tab. + TabLayout.Tab tab = tabLayout.getTabAt(currentPagePosition); + // Update the URL text bar if the page is currently selected and the user is not currently typing in the URL edit text. // Crash records show that, in some crazy way, it is possible for the current URL to be blank at this point. // Probably some sort of race condition when Privacy Browser is being resumed. @@ -5393,36 +5420,45 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Display the keyboard. inputMethodManager.showSoftInput(urlEditText, 0); - // Hide the WebView, which causes the default background color to be displayed according to the theme. - nestedScrollWebView.setVisibility(View.INVISIBLE); - // Apply the domain settings. This clears any settings from the previous domain. applyDomainSettings(nestedScrollWebView, "", true, false); + + // Only populate the title text view if the tab has been fully created. + if (tab != null) { + // Get the custom view from the tab. + View tabView = tab.getCustomView(); + + // Remove the incorrect warning below that the current tab view might be null. + assert tabView != null; + + // Get the title text view from the tab. + TextView tabTitleTextView = tabView.findViewById(R.id.title_textview); + + // Set the title as the tab text. + 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); // Apply text highlighting to the URL. highlightUrlText(); - } - } - // Get the current tab. - TabLayout.Tab tab = tabLayout.getTabAt(currentPagePosition); + // Only populate the title text view if the tab has been fully created. + if (tab != null) { + // Get the custom view from the tab. + View tabView = tab.getCustomView(); - // Only populate the title text view if the tab has been fully created. - if (tab != null) { - // Get the custom view from the tab. - View tabView = tab.getCustomView(); - - // Remove the incorrect warning below that the current tab view might be null. - assert tabView != null; + // Remove the incorrect warning below that the current tab view might be null. + assert tabView != null; - // Get the title text view from the tab. - TextView tabTitleTextView = tabView.findViewById(R.id.title_textview); + // Get the title text view from the tab. + TextView tabTitleTextView = tabView.findViewById(R.id.title_textview); - // Set the title as the tab text. Sometimes `onReceivedTitle()` is not called, especially when navigating history. - tabTitleTextView.setText(nestedScrollWebView.getTitle()); + // Set the title as the tab text. Sometimes `onReceivedTitle()` is not called, especially when navigating history. + tabTitleTextView.setText(nestedScrollWebView.getTitle()); + } + } } } } @@ -5526,9 +5562,22 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Load the URL. nestedScrollWebView.loadUrl(url, customHeaders); - // Display the keyboard if the URL is blank. + // Set the focus and display the keyboard if the URL is blank. if (url.equals("")) { - inputMethodManager.showSoftInput(urlEditText, 0); + // Request focus for the URL text box. + urlEditText.requestFocus(); + + // Create a display keyboard handler. + Handler displayKeyboardHandler = new Handler(); + + // Create a display keyboard runnable. + Runnable displayKeyboardRunnable = () -> { + // Display the keyboard. + inputMethodManager.showSoftInput(urlEditText, 0); + }; + + // Display the keyboard after 100 milliseconds, which leaves enough time for the tab to transition. + displayKeyboardHandler.postDelayed(displayKeyboardRunnable, 100); } } }