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=353a8d16407ddb6f182921d15dc22916b250e951;hp=60ff281448bded819d6ae54f51c92b23b3843b02;hb=f82135d919d64d4909c37c79a18e14ceba802579;hpb=70c5de02b748297473611a048b35a0f9f9f993ee 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 60ff2814..353a8d16 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2017 Soren Stoutner . + * Copyright © 2015-2017 Soren Stoutner . * * Download cookie code contributed 2017 Hendrik Knackstedt. Copyright assigned to Soren Stoutner . * @@ -194,11 +194,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // 'homepage' is used in `onCreate()`, `onNavigationItemSelected()`, and `applyAppSettings()`. private String homepage; - // `javaScriptDisabledSearchURL` is used in `loadURLFromTextBox()` and `applyAppSettings()`. - private String javaScriptDisabledSearchURL; - - // `javaScriptEnabledSearchURL` is used in `loadURLFromTextBox()` and `applyAppSettings()`. - private String javaScriptEnabledSearchURL; + // `searchURL` is used in `loadURLFromTextBox()` and `applyAppSettings()`. + private String searchURL; // `adBlockerEnabled` is used in `onCreate()` and `applyAppSettings()`. private boolean adBlockerEnabled; @@ -571,8 +568,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation @SuppressWarnings("deprecation") @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { - // Use an external email program if the link begins with `mailto:`. - if (url.startsWith("mailto:")) { + if (url.startsWith("mailto:")) { // Load the URL in an external email program because it begins with `mailto:`. // We use `ACTION_SENDTO` instead of `ACTION_SEND` so that only email programs are launched. Intent emailIntent = new Intent(Intent.ACTION_SENDTO); @@ -584,10 +580,15 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // Make it so. startActivity(emailIntent); + + // Returning `true` indicates the application is handling the URL. return true; } else { // Load the URL in Privacy Browser. - loadUrl(url); - return true; + // Apply the domain settings for the new URL. + applyDomainSettings(url); + + // Returning `false` causes the current `WebView` to handle the URL and prevents it from adding redirects to the history list. + return false; } } @@ -666,7 +667,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation privacyBrowserRuntime.exec("rm -rf " + privateDataDirectoryString + "/cache"); // Delete the `app_webview` folder, which contains an additional `WebView` cache. See `https://code.google.com/p/android/issues/detail?id=233826&thanks=233826&ts=1486670530`. - privacyBrowserRuntime.exec("rm -rf " + privacyBrowserRuntime + "/app_webview"); + privacyBrowserRuntime.exec("rm -rf " + privateDataDirectoryString + "/app_webview"); } catch (IOException e) { // Do nothing if an error is thrown. } @@ -825,6 +826,12 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // Hide zoom controls. mainWebView.getSettings().setDisplayZoomControls(false); + // Set `mainWebView` to use a wide viewport. Otherwise, some web pages will be scrunched and some content will render outside the screen. + mainWebView.getSettings().setUseWideViewPort(true); + + // Set `mainWebView` to load in overview mode (zoomed out to the maximum width). + mainWebView.getSettings().setLoadWithOverviewMode(true); + // Initialize cookieManager. cookieManager = CookieManager.getInstance(); @@ -987,6 +994,11 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // Prepare the font size title and current size menu item. switch (fontSize) { + case 25: + fontSizeTitle = getResources().getString(R.string.font_size) + " - " + getResources().getString(R.string.twenty_five_percent); + selectedFontSizeMenuItem = menu.findItem(R.id.fontSizeTwentyFivePercent); + break; + case 50: fontSizeTitle = getResources().getString(R.string.font_size) + " - " + getResources().getString(R.string.fifty_percent); selectedFontSizeMenuItem = menu.findItem(R.id.fontSizeFiftyPercent); @@ -1191,6 +1203,10 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation Snackbar.make(findViewById(R.id.main_webview), R.string.form_data_deleted, Snackbar.LENGTH_SHORT).show(); return true; + case R.id.fontSizeTwentyFivePercent: + mainWebView.getSettings().setTextZoom(25); + return true; + case R.id.fontSizeFiftyPercent: mainWebView.getSettings().setTextZoom(50); return true; @@ -1220,10 +1236,21 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation return true; case R.id.share: + // Setup the share string. + String shareString; + if (webViewTitle != null) { + shareString = webViewTitle + " – " + urlTextBox.getText().toString(); + } else { + shareString = urlTextBox.getText().toString(); + } + + // Create the share intent. Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); - shareIntent.putExtra(Intent.EXTRA_TEXT, urlTextBox.getText().toString()); + shareIntent.putExtra(Intent.EXTRA_TEXT, shareString); shareIntent.setType("text/plain"); + + // Make it so. startActivity(Intent.createChooser(shareIntent, "Share URL")); return true; @@ -1408,7 +1435,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation privacyBrowserRuntime.exec("rm -rf " + privateDataDirectoryString + "/cache"); // Delete the `app_webview` folder, which contains an additional `WebView` cache. See `https://code.google.com/p/android/issues/detail?id=233826&thanks=233826&ts=1486670530`. - privacyBrowserRuntime.exec("rm -rf " + privacyBrowserRuntime + "/app_webview"); + privacyBrowserRuntime.exec("rm -rf " + privateDataDirectoryString + "/app_webview"); } catch (IOException e) { // Do nothing if an error is thrown. } @@ -1854,7 +1881,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation String unformattedUrlString = urlTextBox.getText().toString().trim(); // Check to see if `unformattedUrlString` is a valid URL. Otherwise, convert it into a search. - if ((Patterns.WEB_URL.matcher(unformattedUrlString).matches()) || (unformattedUrlString.contains("localhost"))) { + if ((Patterns.WEB_URL.matcher(unformattedUrlString).matches()) || (unformattedUrlString.startsWith("http://")) || (unformattedUrlString.startsWith("https://"))) { // Add `http://` at the beginning if it is missing. Otherwise the app will segfault. if (!unformattedUrlString.startsWith("http")) { unformattedUrlString = "http://" + unformattedUrlString; @@ -1887,12 +1914,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // Sanitize the search input and convert it to a search. final String encodedUrlString = URLEncoder.encode(unformattedUrlString, "UTF-8"); - // Use the correct search URL. - if (javaScriptEnabled) { // JavaScript is enabled. - formattedUrlString = javaScriptEnabledSearchURL + encodedUrlString; - } else { // JavaScript is disabled. - formattedUrlString = javaScriptDisabledSearchURL + encodedUrlString; - } + // Add the base search URL. + formattedUrlString = searchURL + encodedUrlString; } loadUrl(formattedUrlString); @@ -2121,16 +2144,12 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); // Store the values from `sharedPreferences` in variables. - String javaScriptDisabledSearchString = sharedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q="); - String javaScriptDisabledSearchCustomURLString = sharedPreferences.getString("javascript_disabled_search_custom_url", ""); - String javaScriptEnabledSearchString = sharedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q="); - String javaScriptEnabledSearchCustomURLString = sharedPreferences.getString("javascript_enabled_search_custom_url", ""); String homepageString = sharedPreferences.getString("homepage", "https://duckduckgo.com"); String torHomepageString = sharedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"); - String torJavaScriptDisabledSearchString = sharedPreferences.getString("tor_javascript_disabled_search", "https://3g2upl4pq6kufc4m.onion/html/?q="); - String torJavaScriptDisabledSearchCustomURLString = sharedPreferences.getString("tor_javascript_disabled_search_custom_url", ""); - String torJavaScriptEnabledSearchString = sharedPreferences.getString("tor_javascript_enabled_search", "https://3g2upl4pq6kufc4m.onion/?q="); - String torJavaScriptEnabledSearchCustomURLString = sharedPreferences.getString("tor_javascript_enabled_search_custom_url", ""); + String torSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q="); + String torSearchCustomURLString = sharedPreferences.getString("tor_search_custom_url", ""); + String searchString = sharedPreferences.getString("search", "https://duckduckgo.com/html/?q="); + String searchCustomURLString = sharedPreferences.getString("search_custom_url", ""); adBlockerEnabled = sharedPreferences.getBoolean("block_ads", true); incognitoModeEnabled = sharedPreferences.getBoolean("incognito_mode", false); boolean doNotTrackEnabled = sharedPreferences.getBoolean("do_not_track", false); @@ -2150,18 +2169,11 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation formattedUrlString = homepage; } - // Set JavaScript disabled search. - if (torJavaScriptDisabledSearchString.equals("Custom URL")) { // Get the custom URL string. - javaScriptDisabledSearchURL = torJavaScriptDisabledSearchCustomURLString; + // Set the search URL. + if (torSearchString.equals("Custom URL")) { // Get the custom URL string. + searchURL = torSearchCustomURLString; } else { // Use the string from the pre-built list. - javaScriptDisabledSearchURL = torJavaScriptDisabledSearchString; - } - - // Set JavaScript enabled search. - if (torJavaScriptEnabledSearchString.equals("Custom URL")) { // Get the custom URL string. - javaScriptEnabledSearchURL = torJavaScriptEnabledSearchCustomURLString; - } else { // Use the string from the pre-built list. - javaScriptEnabledSearchURL = torJavaScriptEnabledSearchString; + searchURL = torSearchString; } // Set the proxy. `this` refers to the current activity where an `AlertDialog` might be displayed. @@ -2184,18 +2196,11 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation formattedUrlString = homepage; } - // Set JavaScript disabled search. - if (javaScriptDisabledSearchString.equals("Custom URL")) { // Get the custom URL string. - javaScriptDisabledSearchURL = javaScriptDisabledSearchCustomURLString; + // Set the search URL. + if (searchString.equals("Custom URL")) { // Get the custom URL string. + searchURL = searchCustomURLString; } else { // Use the string from the pre-built list. - javaScriptDisabledSearchURL = javaScriptDisabledSearchString; - } - - // Set JavaScript enabled search. - if (javaScriptEnabledSearchString.equals("Custom URL")) { // Get the custom URL string. - javaScriptEnabledSearchURL = javaScriptEnabledSearchCustomURLString; - } else { // Use the string from the pre-built list. - javaScriptEnabledSearchURL = javaScriptEnabledSearchString; + searchURL = searchString; } // Reset the proxy to default. The host is `""` and the port is `"0"`. @@ -2248,8 +2253,10 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // Reset `inFullScreenBrowsingMode` to `false`. inFullScreenBrowsingMode = false; - // Show the `appBar`. - appBar.show(); + // Show the `appBar` if `findOnPageLinearLayout` is not visible. + if (findOnPageLinearLayout.getVisibility() == View.GONE) { + appBar.show(); + } // Show the `BannerAd` in the free flavor. if (BuildConfig.FLAVOR.contentEquals("free")) {