X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FMainWebViewActivity.java;h=2b8009f0d4d7b706d1dcfa5f28055fb965845416;hb=1f1fca70679a8bcf4b717fcca5dbf9e32cc56db5;hp=23e000a6b438c99a534c99f77c76f609db2afd34;hpb=26ce53c5262abc009c31441130d4a330ba0c267a;p=PrivacyBrowserAndroid.git 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 23e000a6..2b8009f0 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -135,6 +135,9 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // `orbotStatus` is public static so it can be accessed from `OrbotProxyHelper`. It is also used in `onCreate()`. public static String orbotStatus; + // `webViewTitle` is public static so it can be accessed from `CreateBookmarkDialog` and `CreateHomeScreenShortcutDialog`. It is also used in `onCreate()`. + public static String webViewTitle; + // `navigatingHistory` is used in `onCreate()` and `onNavigationItemSelected()`. private boolean navigatingHistory; @@ -206,6 +209,9 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // `proxyThroughOrbot` is used in `onCreate()` and `applySettings()` private boolean proxyThroughOrbot; + // `currentDomain` is used in `onCreate() and `applyDomainSettings()`. + private String currentDomain; + // `pendingUrl` is used in `onCreate()` and `applySettings()` private static String pendingUrl; @@ -289,10 +295,9 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // Set `waitingForOrbotHTMLString`. waitingForOrbotHTMLString = "

" + getString(R.string.waiting_for_orbot) + "

"; - // Initialize `pendingUrl`. + // Initialize `currentDomain`, `pendingUrl`, and `orbotStatus`. + currentDomain = ""; pendingUrl = ""; - - // Set the initial Orbot status. orbotStatus = "unknown"; // Create an Orbot status `BroadcastReceiver`. @@ -679,7 +684,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // Set the favorite icon when it changes. @Override public void onReceivedIcon(WebView view, Bitmap icon) { - // Save a copy of the favorite icon for use if a shortcut is added to the home screen. + // Save a copy of the favorite icon. favoriteIcon = icon; // Place the favorite icon in the appBar. @@ -687,6 +692,13 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation imageViewFavoriteIcon.setImageBitmap(Bitmap.createScaledBitmap(icon, 64, 64, true)); } + // Save a copy of the title when it changes. + @Override + public void onReceivedTitle(WebView view, String title) { + // Save a copy of the title. + webViewTitle = title; + } + // Enter full screen video @Override public void onShowCustomView(View view, CustomViewCallback callback) { @@ -790,8 +802,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation domStorageEnabled = false; saveFormDataEnabled = false; - // Apply the settings from the shared preferences. - applySettings(); + // Apply the app settings from the shared preferences. + applyAppSettings(); // Load `formattedUrlString` if we are not proxying through Orbot and waiting for Orbot to connect. if (!(proxyThroughOrbot && !orbotStatus.equals("ON"))) { @@ -1140,6 +1152,14 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation mainWebView.getSettings().setTextZoom(200); return true; + case R.id.share: + Intent shareIntent = new Intent(); + shareIntent.setAction(Intent.ACTION_SEND); + shareIntent.putExtra(Intent.EXTRA_TEXT, urlTextBox.getText().toString()); + shareIntent.setType("text/plain"); + startActivity(Intent.createChooser(shareIntent, "Share URL")); + return true; + case R.id.find_on_page: // Hide the URL app bar. supportAppBar.setVisibility(View.GONE); @@ -1163,20 +1183,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation }, 200); return true; - case R.id.share: - Intent shareIntent = new Intent(); - shareIntent.setAction(Intent.ACTION_SEND); - shareIntent.putExtra(Intent.EXTRA_TEXT, urlTextBox.getText().toString()); - shareIntent.setType("text/plain"); - startActivity(Intent.createChooser(shareIntent, "Share URL")); - return true; - - case R.id.addToHomescreen: - // Show the `CreateHomeScreenShortcutDialog` `AlertDialog` and name this instance `R.string.create_shortcut`. - AppCompatDialogFragment createHomeScreenShortcutDialogFragment = new CreateHomeScreenShortcutDialog(); - createHomeScreenShortcutDialogFragment.show(getSupportFragmentManager(), getResources().getString(R.string.create_shortcut)); - - //Everything else will be handled by `CreateHomeScreenShortcutDialog` and the associated listener below. + case R.id.refresh: + mainWebView.reload(); return true; case R.id.print: @@ -1190,8 +1198,12 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation printManager.print(getResources().getString(R.string.privacy_browser_web_page), printDocumentAdapter, null); return true; - case R.id.refresh: - mainWebView.reload(); + case R.id.addToHomescreen: + // Show the `CreateHomeScreenShortcutDialog` `AlertDialog` and name this instance `R.string.create_shortcut`. + AppCompatDialogFragment createHomeScreenShortcutDialogFragment = new CreateHomeScreenShortcutDialog(); + createHomeScreenShortcutDialogFragment.show(getSupportFragmentManager(), getResources().getString(R.string.create_shortcut)); + + //Everything else will be handled by `CreateHomeScreenShortcutDialog` and the associated listener below. return true; default: @@ -1706,6 +1718,10 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation } else { // Load the previous URL if available. if (mainWebView.canGoBack()) { + // Set `navigatingHistory` so that the domain settings are applied when the new URL is loaded. + navigatingHistory = true; + + // Go back. mainWebView.goBack(); } else { // Pass `onBackPressed()` to the system. @@ -1751,7 +1767,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation super.onRestart(); // Apply the settings from shared preferences, which might have been changed in `SettingsActivity`. - applySettings(); + applyAppSettings(); // Update the privacy icon. `true` runs `invalidateOptionsMenu` as the last step. updatePrivacyIcons(true); @@ -1824,42 +1840,49 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // Extract the domain from `uri`. String hostname = uri.getHost(); - // Initialize the database handler. `this` specifies the context. The two `nulls` do not specify the database name or a `CursorFactory`. - // The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`. - DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(this, null, null, 0); + // Only apply the domain settings if `hostname` is not the same as `currentDomain`. This allows the user to set temporary settings for JavaScript, Cookies, DOM Storage, etc. + if (!hostname.equals(currentDomain)) { + // Set the new `hostname` as the `currentDomain`. + currentDomain = hostname; - // Get a full cursor from `domainsDatabaseHelper`. - Cursor domainNameCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain(); + // Initialize the database handler. `this` specifies the context. The two `nulls` do not specify the database name or a `CursorFactory`. + // The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`. + DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(this, null, null, 0); - // Initialize `domainSettingsSet`. - Set domainSettingsSet = new HashSet<>(); + // Get a full cursor from `domainsDatabaseHelper`. + Cursor domainNameCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain(); - // Get the domain name column index. - int domainNameColumnIndex = domainNameCursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME); + // Initialize `domainSettingsSet`. + Set domainSettingsSet = new HashSet<>(); - // Populate `domainSettingsSet`. - for (int i=0; i= 21. - if (Build.VERSION.SDK_INT >= 21) { - cookieManager.setAcceptThirdPartyCookies(mainWebView, thirdPartyCookiesEnabled); - } + FrameLayout urlAppBarFrameLayout = (FrameLayout) findViewById(R.id.url_app_bar_framelayout); - // Set the user agent. - if (userAgentString.equals("WebView default user agent")) { - // Set the user agent to `""`, which uses the default value. - mainWebView.getSettings().setUserAgentString(""); - } else { - // Use the selected user agent. - mainWebView.getSettings().setUserAgentString(userAgentString); - } + if (hostHasDomainSettings) { // The url we are loading has custom domain settings. + // Get a cursor for the current host and move it to the first position. + Cursor currentHostDomainSettingsCursor = domainsDatabaseHelper.getCursorForDomainName(domainNameInDatabase); + currentHostDomainSettingsCursor.moveToFirst(); - // Set a green background on `urlTextBox` to indicate that custom domain settings are being used. We have to use the deprecated `.getColor()` until the minimum API >= 23. - urlAppBarFrameLayout.setBackgroundColor(getResources().getColor(R.color.green_100)); - } else { // The URL we are loading does not have custom domain settings. Load the defaults. - // Get the shared preference values. `this` references the current context. - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); - - // Store the values from `sharedPreferences` in variables. - javaScriptEnabled = sharedPreferences.getBoolean("javascript_enabled", false); - firstPartyCookiesEnabled = sharedPreferences.getBoolean("first_party_cookies_enabled", false); - thirdPartyCookiesEnabled = sharedPreferences.getBoolean("third_party_cookies_enabled", false); - domStorageEnabled = sharedPreferences.getBoolean("dom_storage_enabled", false); - saveFormDataEnabled = sharedPreferences.getBoolean("save_form_data_enabled", false); - String userAgentString = sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0"); - String customUserAgentString = sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"); - String defaultFontSizeString = sharedPreferences.getString("default_font_size", "100"); - - // Apply the default settings. - mainWebView.getSettings().setJavaScriptEnabled(javaScriptEnabled); - cookieManager.setAcceptCookie(firstPartyCookiesEnabled); - mainWebView.getSettings().setDomStorageEnabled(domStorageEnabled); - mainWebView.getSettings().setSaveFormData(saveFormDataEnabled); - mainWebView.getSettings().setTextZoom(Integer.valueOf(defaultFontSizeString)); - - // Set third-party cookies status if API >= 21. - if (Build.VERSION.SDK_INT >= 21) { - cookieManager.setAcceptThirdPartyCookies(mainWebView, thirdPartyCookiesEnabled); - } + // Get the settings from the cursor. + javaScriptEnabled = (currentHostDomainSettingsCursor.getInt(currentHostDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_JAVASCRIPT)) == 1); + firstPartyCookiesEnabled = (currentHostDomainSettingsCursor.getInt(currentHostDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FIRST_PARTY_COOKIES)) == 1); + thirdPartyCookiesEnabled = (currentHostDomainSettingsCursor.getInt(currentHostDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_THIRD_PARTY_COOKIES)) == 1); + domStorageEnabled = (currentHostDomainSettingsCursor.getInt(currentHostDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_DOM_STORAGE)) == 1); + saveFormDataEnabled = (currentHostDomainSettingsCursor.getInt(currentHostDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FORM_DATA)) == 1); + String userAgentString = (currentHostDomainSettingsCursor.getString(currentHostDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.USER_AGENT))); + int fontSize = (currentHostDomainSettingsCursor.getInt(currentHostDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.FONT_SIZE))); - // Set the default user agent. - switch (userAgentString) { - case "WebView default user agent": - // Set the user agent to `""`, which uses the default value. - mainWebView.getSettings().setUserAgentString(""); - break; + // Close `currentHostDomainSettingsCursor`. + currentHostDomainSettingsCursor.close(); + + // Apply the domain settings. + mainWebView.getSettings().setJavaScriptEnabled(javaScriptEnabled); + cookieManager.setAcceptCookie(firstPartyCookiesEnabled); + mainWebView.getSettings().setDomStorageEnabled(domStorageEnabled); + mainWebView.getSettings().setSaveFormData(saveFormDataEnabled); + mainWebView.getSettings().setTextZoom(fontSize); - case "Custom user agent": - // Set the custom user agent. - mainWebView.getSettings().setUserAgentString(customUserAgentString); - break; + // Set third-party cookies status if API >= 21. + if (Build.VERSION.SDK_INT >= 21) { + cookieManager.setAcceptThirdPartyCookies(mainWebView, thirdPartyCookiesEnabled); + } - default: + // Set the user agent. + if (userAgentString.equals("WebView default user agent")) { + // Set the user agent to `""`, which uses the default value. + mainWebView.getSettings().setUserAgentString(""); + } else { // Use the selected user agent. mainWebView.getSettings().setUserAgentString(userAgentString); - break; - } + } - // Set a transparent background on `urlTextBox`. We have to use the deprecated `.getColor()` until the minimum API >= 23. - urlAppBarFrameLayout.setBackgroundColor(getResources().getColor(R.color.transparent)); - } + // Set a green background on `urlTextBox` to indicate that custom domain settings are being used. We have to use the deprecated `.getColor()` until the minimum API >= 23. + urlAppBarFrameLayout.setBackgroundColor(getResources().getColor(R.color.green_100)); + } else { // The URL we are loading does not have custom domain settings. Load the defaults. + // Get the shared preference values. `this` references the current context. + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); + + // Store the values from `sharedPreferences` in variables. + javaScriptEnabled = sharedPreferences.getBoolean("javascript_enabled", false); + firstPartyCookiesEnabled = sharedPreferences.getBoolean("first_party_cookies_enabled", false); + thirdPartyCookiesEnabled = sharedPreferences.getBoolean("third_party_cookies_enabled", false); + domStorageEnabled = sharedPreferences.getBoolean("dom_storage_enabled", false); + saveFormDataEnabled = sharedPreferences.getBoolean("save_form_data_enabled", false); + String userAgentString = sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0"); + String customUserAgentString = sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"); + String defaultFontSizeString = sharedPreferences.getString("default_font_size", "100"); + + // Apply the default settings. + mainWebView.getSettings().setJavaScriptEnabled(javaScriptEnabled); + cookieManager.setAcceptCookie(firstPartyCookiesEnabled); + mainWebView.getSettings().setDomStorageEnabled(domStorageEnabled); + mainWebView.getSettings().setSaveFormData(saveFormDataEnabled); + mainWebView.getSettings().setTextZoom(Integer.valueOf(defaultFontSizeString)); - // Close `domainsDatabaseHelper`. - domainsDatabaseHelper.close(); + // Set third-party cookies status if API >= 21. + if (Build.VERSION.SDK_INT >= 21) { + cookieManager.setAcceptThirdPartyCookies(mainWebView, thirdPartyCookiesEnabled); + } + + // Set the default user agent. + switch (userAgentString) { + case "WebView default user agent": + // Set the user agent to `""`, which uses the default value. + mainWebView.getSettings().setUserAgentString(""); + break; + + case "Custom user agent": + // Set the custom user agent. + mainWebView.getSettings().setUserAgentString(customUserAgentString); + break; + + default: + // Use the selected user agent. + mainWebView.getSettings().setUserAgentString(userAgentString); + break; + } - // Update the privacy icons, but only if `mainMenu` has already been populated. - if (mainMenu != null) { - updatePrivacyIcons(true); + // Set a transparent background on `urlTextBox`. We have to use the deprecated `.getColor()` until the minimum API >= 23. + urlAppBarFrameLayout.setBackgroundColor(getResources().getColor(R.color.transparent)); + } + + // Close `domainsDatabaseHelper`. + domainsDatabaseHelper.close(); + + // Update the privacy icons, but only if `mainMenu` has already been populated. + if (mainMenu != null) { + updatePrivacyIcons(true); + } } } @@ -1995,7 +2018,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0); } - private void applySettings() { + private void applyAppSettings() { // Get the shared preference values. `this` references the current context. SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);