+ // We have to use the deprecated `.getDrawable()` until the minimum API >= 21.
+ @SuppressWarnings("deprecation")
+ private void applyDomainSettings(String url) {
+ // Reset `navigatingHistory`.
+ navigatingHistory = false;
+
+ // Parse the URL into a URI.
+ Uri uri = Uri.parse(url);
+
+ // Extract the domain from `uri`.
+ String hostName = uri.getHost();
+
+ // Initialize `loadingNewDomainName`.
+ boolean loadingNewDomainName;
+
+ // If either `hostName` or `currentDomainName` are `null`, run the options for loading a new domain name.
+ // The lint suggestion to simplify the `if` statement is incorrect, because `hostName.equals(currentDomainName)` can produce a `null object reference.`
+ //noinspection SimplifiableIfStatement
+ if ((hostName == null) || (currentDomainName == null)) {
+ loadingNewDomainName = true;
+ } else { // Determine if `hostName` equals `currentDomainName`.
+ loadingNewDomainName = !hostName.equals(currentDomainName);
+ }
+
+ // Only apply the domain settings if we are loading a new domain. This allows the user to set temporary settings for JavaScript, cookies, DOM storage, etc.
+ if (loadingNewDomainName) {
+ // Set the new `hostname` as the `currentDomainName`.
+ currentDomainName = hostName;
+
+ // Reset `ignorePinnedSslCertificate`.
+ ignorePinnedSslCertificate = false;
+
+ // Reset `favoriteIconBitmap` and display it in the `appbar`.
+ favoriteIconBitmap = favoriteIconDefaultBitmap;
+ favoriteIconImageView.setImageBitmap(Bitmap.createScaledBitmap(favoriteIconBitmap, 64, 64, true));
+
+ // 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);
+
+ // Get a full cursor from `domainsDatabaseHelper`.
+ Cursor domainNameCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
+
+ // Initialize `domainSettingsSet`.
+ Set<String> domainSettingsSet = new HashSet<>();
+
+ // Get the domain name column index.
+ int domainNameColumnIndex = domainNameCursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME);
+
+ // Populate `domainSettingsSet`.
+ for (int i = 0; i < domainNameCursor.getCount(); i++) {
+ // Move `domainsCursor` to the current row.
+ domainNameCursor.moveToPosition(i);
+
+ // Store the domain name in `domainSettingsSet`.
+ domainSettingsSet.add(domainNameCursor.getString(domainNameColumnIndex));
+ }
+
+ // Close `domainNameCursor.
+ domainNameCursor.close();
+
+ // Initialize variables to track if domain settings will be applied and, if so, under which name.
+ domainSettingsApplied = false;
+ String domainNameInDatabase = null;
+
+ // Check the hostname.
+ if (domainSettingsSet.contains(hostName)) {
+ domainSettingsApplied = true;
+ domainNameInDatabase = hostName;
+ }
+
+ // If `hostName` is not `null`, check all the subdomains of `hostName` against wildcard domains in `domainCursor`.
+ if (hostName != null) {
+ while (hostName.contains(".") && !domainSettingsApplied) { // Stop checking if we run out of `.` or if we already know that `domainSettingsApplied` is `true`.
+ if (domainSettingsSet.contains("*." + hostName)) { // Check the host name prepended by `*.`.
+ domainSettingsApplied = true;
+ domainNameInDatabase = "*." + hostName;
+ }
+
+ // Strip out the lowest subdomain of `host`.
+ hostName = hostName.substring(hostName.indexOf(".") + 1);
+ }
+ }
+
+ // Get a handle for the shared preference. `this` references the current context.
+ SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
+
+ // Store the general preference information.
+ String defaultFontSizeString = sharedPreferences.getString("default_font_size", "100");
+ String defaultUserAgentString = sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0");
+ String defaultCustomUserAgentString = sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0");
+ nightMode = sharedPreferences.getBoolean("night_mode", false);
+
+ if (domainSettingsApplied) { // 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();
+
+ // Get the settings from the cursor.
+ domainSettingsDatabaseId = (currentHostDomainSettingsCursor.getInt(currentHostDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper._ID)));
+ 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));
+ displayWebpageImagesInt = currentHostDomainSettingsCursor.getInt(currentHostDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.DISPLAY_IMAGES));
+ int nightModeInt = currentHostDomainSettingsCursor.getInt(currentHostDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.NIGHT_MODE));
+ pinnedDomainSslCertificate = (currentHostDomainSettingsCursor.getInt(currentHostDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.PINNED_SSL_CERTIFICATE)) == 1);
+ pinnedDomainSslIssuedToCNameString = currentHostDomainSettingsCursor.getString(currentHostDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_TO_COMMON_NAME));
+ pinnedDomainSslIssuedToONameString = currentHostDomainSettingsCursor.getString(currentHostDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_TO_ORGANIZATION));
+ pinnedDomainSslIssuedToUNameString = currentHostDomainSettingsCursor.getString(currentHostDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_TO_ORGANIZATIONAL_UNIT));
+ pinnedDomainSslIssuedByCNameString = currentHostDomainSettingsCursor.getString(currentHostDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_BY_COMMON_NAME));
+ pinnedDomainSslIssuedByONameString = currentHostDomainSettingsCursor.getString(currentHostDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_BY_ORGANIZATION));
+ pinnedDomainSslIssuedByUNameString = currentHostDomainSettingsCursor.getString(currentHostDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_BY_ORGANIZATIONAL_UNIT));
+
+ // Set `nightMode` according to `nightModeInt`. If `nightModeInt` is `DomainsDatabaseHelper.NIGHT_MODE_SYSTEM_DEFAULT` the current setting from `sharedPreferences` will be used.
+ switch (nightModeInt) {
+ case DomainsDatabaseHelper.NIGHT_MODE_ENABLED:
+ nightMode = true;
+ break;
+
+ case DomainsDatabaseHelper.NIGHT_MODE_DISABLED:
+ nightMode = false;
+ break;
+ }
+
+ // Set `javaScriptEnabled` to be `true` if `night_mode` is `true`.
+ if (nightMode) {
+ javaScriptEnabled = true;
+ }
+
+ // Set the pinned SSL certificate start date to `null` if the saved date `long` is 0.
+ if (currentHostDomainSettingsCursor.getLong(currentHostDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_START_DATE)) == 0) {
+ pinnedDomainSslStartDate = null;
+ } else {
+ pinnedDomainSslStartDate = new Date(currentHostDomainSettingsCursor.getLong(currentHostDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_START_DATE)));
+ }
+
+ // Set the pinned SSL certificate end date to `null` if the saved date `long` is 0.
+ if (currentHostDomainSettingsCursor.getLong(currentHostDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_END_DATE)) == 0) {
+ pinnedDomainSslEndDate = null;
+ } else {
+ pinnedDomainSslEndDate = new Date(currentHostDomainSettingsCursor.getLong(currentHostDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_END_DATE)));
+ }
+
+ // Close `currentHostDomainSettingsCursor`.
+ currentHostDomainSettingsCursor.close();
+
+ // Apply the domain settings.
+ mainWebView.getSettings().setJavaScriptEnabled(javaScriptEnabled);
+ cookieManager.setAcceptCookie(firstPartyCookiesEnabled);
+ mainWebView.getSettings().setDomStorageEnabled(domStorageEnabled);
+ mainWebView.getSettings().setSaveFormData(saveFormDataEnabled);
+
+ // Apply the font size.
+ if (fontSize == 0) { // Apply the default font size.
+ mainWebView.getSettings().setTextZoom(Integer.valueOf(defaultFontSizeString));
+ } else { // Apply the specified font size.
+ mainWebView.getSettings().setTextZoom(fontSize);
+ }
+
+ // Set third-party cookies status if API >= 21.
+ if (Build.VERSION.SDK_INT >= 21) {
+ cookieManager.setAcceptThirdPartyCookies(mainWebView, thirdPartyCookiesEnabled);
+ }
+
+ // Only set the user agent if the webpage is not currently loading. Otherwise, changing the user agent on redirects can cause the original website to reload. <https://redmine.stoutner.com/issues/160>
+ if (!urlIsLoading) {
+ switch (userAgentString) {
+ case "System default user agent":
+ // Set the user agent according to the system default.
+ switch (defaultUserAgentString) {
+ 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(defaultCustomUserAgentString);
+ break;
+
+ default:
+ // Use the selected user agent.
+ mainWebView.getSettings().setUserAgentString(defaultUserAgentString);
+ }
+ break;
+
+ case "WebView default user agent":
+ // Set the user agent to `""`, which uses the default value.
+ mainWebView.getSettings().setUserAgentString("");
+ break;
+
+ default:
+ // Use the selected user agent.
+ mainWebView.getSettings().setUserAgentString(userAgentString);
+ }
+ }
+
+ // Set a green background on `urlTextBox` to indicate that custom domain settings are being used. We have to use the deprecated `.getDrawable()` until the minimum API >= 21.
+ if (darkTheme) {
+ urlAppBarRelativeLayout.setBackground(getResources().getDrawable(R.drawable.url_bar_background_dark_blue));
+ } else {
+ urlAppBarRelativeLayout.setBackground(getResources().getDrawable(R.drawable.url_bar_background_light_green));
+ }
+ } else { // The URL we are loading does not have custom domain settings. Load the defaults.
+ // 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);
+
+ // Set `javaScriptEnabled` to be `true` if `night_mode` is `true`.
+ if (nightMode) {
+ javaScriptEnabled = true;
+ }
+
+ // 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));
+
+ // Reset the pinned SSL certificate information.
+ domainSettingsDatabaseId = -1;
+ pinnedDomainSslCertificate = false;
+ pinnedDomainSslIssuedToCNameString = "";
+ pinnedDomainSslIssuedToONameString = "";
+ pinnedDomainSslIssuedToUNameString = "";
+ pinnedDomainSslIssuedByCNameString = "";
+ pinnedDomainSslIssuedByONameString = "";
+ pinnedDomainSslIssuedByUNameString = "";
+ pinnedDomainSslStartDate = null;
+ pinnedDomainSslEndDate = null;
+
+ // Set third-party cookies status if API >= 21.
+ if (Build.VERSION.SDK_INT >= 21) {
+ cookieManager.setAcceptThirdPartyCookies(mainWebView, thirdPartyCookiesEnabled);
+ }
+
+ // Only set the user agent if the webpage is not currently loading. Otherwise, changing the user agent on redirects can cause the original website to reload. <https://redmine.stoutner.com/issues/160>
+ if (!urlIsLoading) {
+ switch (defaultUserAgentString) {
+ 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(defaultCustomUserAgentString);
+ break;
+
+ default:
+ // Use the selected user agent.
+ mainWebView.getSettings().setUserAgentString(defaultUserAgentString);
+ }
+ }
+
+ // Set a transparent background on `urlTextBox`. We have to use the deprecated `.getDrawable()` until the minimum API >= 21.
+ urlAppBarRelativeLayout.setBackgroundDrawable(getResources().getDrawable(R.color.transparent));
+ }
+
+ // Close `domainsDatabaseHelper`.
+ domainsDatabaseHelper.close();
+
+ // Remove the `onTheFlyDisplayImagesSet` flag and set the display webpage images mode. `true` indicates that custom domain settings are applied.
+ onTheFlyDisplayImagesSet = false;
+ setDisplayWebpageImages();
+
+ // Update the privacy icons, but only if `mainMenu` has already been populated.
+ if (mainMenu != null) {
+ updatePrivacyIcons(true);
+ }
+ }
+ }
+
+ private void setDisplayWebpageImages() {
+ if (!onTheFlyDisplayImagesSet) {
+ if (domainSettingsApplied) { // Custom domain settings are applied.
+ switch (displayWebpageImagesInt) {
+ case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_SYSTEM_DEFAULT:
+ mainWebView.getSettings().setLoadsImagesAutomatically(displayWebpageImagesBoolean);
+ break;
+
+ case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_ENABLED:
+ mainWebView.getSettings().setLoadsImagesAutomatically(true);
+ break;
+
+ case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_DISABLED:
+ mainWebView.getSettings().setLoadsImagesAutomatically(false);
+ break;
+ }
+ } else { // Default settings are applied.
+ mainWebView.getSettings().setLoadsImagesAutomatically(displayWebpageImagesBoolean);
+ }
+ }
+ }
+