From 5a385f0ba12a7af005761e30155f7fd18767d2f9 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Thu, 12 Jul 2018 19:11:13 -0700 Subject: [PATCH] Add on-the-fly blocklist controls. https://redmine.stoutner.com/issues/279 --- .../activities/MainWebViewActivity.java | 77 ++++++++++++++++--- .../activities/RequestsActivity.java | 21 ++++- .../helpers/OrbotProxyHelper.java | 12 ++- .../main/res/menu/webview_options_menu.xml | 75 +++++++++++++----- app/src/main/res/values/strings.xml | 10 ++- app/src/main/res/xml/preferences.xml | 8 +- 6 files changed, 159 insertions(+), 44 deletions(-) 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 e7d7c661..26a6a403 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -283,7 +283,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // `fullScreenVideoFrameLayout` is used in `onCreate()` and `onConfigurationChanged()`. private FrameLayout fullScreenVideoFrameLayout; - // `swipeRefreshLayout` is used in `onCreate()`, `onPrepareOptionsMenu()`, `onOptionsMenuSelected()`, and `onRestart()`. + // `swipeRefreshLayout` is used in `onCreate()`, `onPrepareOptionsMenu()`, `onOptionsItemSelected()`, and `onRestart()`. private SwipeRefreshLayout swipeRefreshLayout; // `urlAppBarRelativeLayout` is used in `onCreate()` and `applyDomainSettings()`. @@ -325,7 +325,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // `searchURL` is used in `loadURLFromTextBox()` and `applyAppSettings()`. private String searchURL; - // The block list variables are used in `onCreate()` and `applyAppSettings()`. + // The block list variables are used in `onCreate()`, `onPrepareOptionsMenu()`, `onOptionsItemSelected()`, and `applyAppSettings()`. private boolean easyListEnabled; private boolean easyPrivacyEnabled; private boolean fanboysAnnoyanceListEnabled; @@ -1691,6 +1691,10 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook MenuItem clearCookiesMenuItem = menu.findItem(R.id.clear_cookies); MenuItem clearDOMStorageMenuItem = menu.findItem(R.id.clear_dom_storage); MenuItem clearFormDataMenuItem = menu.findItem(R.id.clear_form_data); // Form data can be removed once the minimum API >= 26. + MenuItem easyListMenuItem = menu.findItem(R.id.easylist); + MenuItem easyPrivacyMenuItem = menu.findItem(R.id.easyprivacy); + MenuItem fanboysAnnoyanceListMenuItem = menu.findItem(R.id.fanboys_annoyance_list); + MenuItem fanboysSocialBlockingListMenuItem = menu.findItem(R.id.fanboys_social_blocking_list); MenuItem fontSizeMenuItem = menu.findItem(R.id.font_size); MenuItem swipeToRefreshMenuItem = menu.findItem(R.id.swipe_to_refresh); MenuItem displayImagesMenuItem = menu.findItem(R.id.display_images); @@ -1707,44 +1711,51 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook toggleThirdPartyCookiesMenuItem.setChecked(thirdPartyCookiesEnabled); toggleDomStorageMenuItem.setChecked(domStorageEnabled); toggleSaveFormDataMenuItem.setChecked(saveFormDataEnabled); // Form data can be removed once the minimum API >= 26. + easyListMenuItem.setChecked(easyListEnabled); + easyPrivacyMenuItem.setChecked(easyPrivacyEnabled); + fanboysAnnoyanceListMenuItem.setChecked(fanboysAnnoyanceListEnabled); + fanboysSocialBlockingListMenuItem.setChecked(fanboysSocialBlockingListEnabled); swipeToRefreshMenuItem.setChecked(swipeRefreshLayout.isEnabled()); displayImagesMenuItem.setChecked(mainWebView.getSettings().getLoadsImagesAutomatically()); // Enable third-party cookies if first-party cookies are enabled. toggleThirdPartyCookiesMenuItem.setEnabled(firstPartyCookiesEnabled); - // Enable `DOM Storage` if JavaScript is enabled. + // Enable DOM Storage if JavaScript is enabled. toggleDomStorageMenuItem.setEnabled(javaScriptEnabled); - // Enable `Clear Cookies` if there are any. + // Enable Clear Cookies if there are any. clearCookiesMenuItem.setEnabled(cookieManager.hasCookies()); - // Get a count of the number of files in the `Local Storage` directory. + // Get a count of the number of files in the Local Storage directory. File localStorageDirectory = new File (privateDataDirectoryString + "/app_webview/Local Storage/"); int localStorageDirectoryNumberOfFiles = 0; if (localStorageDirectory.exists()) { localStorageDirectoryNumberOfFiles = localStorageDirectory.list().length; } - // Get a count of the number of files in the `IndexedDB` directory. + // Get a count of the number of files in the IndexedDB directory. File indexedDBDirectory = new File (privateDataDirectoryString + "/app_webview/IndexedDB"); int indexedDBDirectoryNumberOfFiles = 0; if (indexedDBDirectory.exists()) { indexedDBDirectoryNumberOfFiles = indexedDBDirectory.list().length; } - // Enable `Clear DOM Storage` if there is any. + // Enable Clear DOM Storage if there is any. clearDOMStorageMenuItem.setEnabled(localStorageDirectoryNumberOfFiles > 0 || indexedDBDirectoryNumberOfFiles > 0); - // Enable `Clear Form Data` is there is any. This can be removed once the minimum API >= 26. + // Enable Clear Form Data is there is any. This can be removed once the minimum API >= 26. if (Build.VERSION.SDK_INT < 26) { WebViewDatabase mainWebViewDatabase = WebViewDatabase.getInstance(this); clearFormDataMenuItem.setEnabled(mainWebViewDatabase.hasFormData()); } - // Enable `Clear Data` if any of the submenu items are enabled. + // Enable Clear Data if any of the submenu items are enabled. clearDataMenuItem.setEnabled(clearCookiesMenuItem.isEnabled() || clearDOMStorageMenuItem.isEnabled() || clearFormDataMenuItem.isEnabled()); + // Disable Fanboy's Social Blocking List if Fanboy's Annoyance List is checked. + fanboysSocialBlockingListMenuItem.setEnabled(!fanboysAnnoyanceListEnabled); + // Initialize font size variables. int fontSize = mainWebView.getSettings().getTextZoom(); String fontSizeTitle; @@ -2132,6 +2143,54 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook startActivity(viewSourceIntent); return true; + case R.id.easylist: + // Toggle the EasyList status. + easyListEnabled = !easyListEnabled; + + // Update the menu checkbox. + menuItem.setChecked(easyListEnabled); + + // Reload the main WebView. + mainWebView.reload(); + return true; + + case R.id.easyprivacy: + // Toggle the EasyPrivacy status. + easyPrivacyEnabled = !easyPrivacyEnabled; + + // Update the menu checkbox. + menuItem.setChecked(easyPrivacyEnabled); + + // Reload the main WebView. + mainWebView.reload(); + return true; + + case R.id.fanboys_annoyance_list: + // Toggle Fanboy's Annoyance List status. + fanboysAnnoyanceListEnabled = !fanboysAnnoyanceListEnabled; + + // Update the menu checkbox. + menuItem.setChecked(fanboysAnnoyanceListEnabled); + + // Update the staus of Fanboy's Social Blocking List. + MenuItem fanboysSocialBlockingListMenuItem = mainMenu.findItem(R.id.fanboys_social_blocking_list); + fanboysSocialBlockingListMenuItem.setEnabled(!fanboysAnnoyanceListEnabled); + + // Reload the main WebView. + mainWebView.reload(); + return true; + + case R.id.fanboys_social_blocking_list: + // Toggle Fanboy's Social Blocking List status. + fanboysSocialBlockingListEnabled = !fanboysSocialBlockingListEnabled; + + // Update teh menu checkbox. + menuItem.setChecked(fanboysSocialBlockingListEnabled); + + // Reload the main WebView. + mainWebView.reload(); + return true; + case R.id.share: // Setup the share string. String shareString = webViewTitle + " – " + urlTextBox.getText().toString(); diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/RequestsActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/RequestsActivity.java index 492697fd..dcf64129 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/RequestsActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/RequestsActivity.java @@ -82,7 +82,8 @@ public class RequestsActivity extends AppCompatActivity implements ViewRequestDi appBar.setCustomView(R.layout.requests_spinner); appBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP); - // Initialize the resource array lists. + // Initialize the resource array lists. A list is needed for all the resource requests, or the activity can crash if `MainWebViewActivity.resourceRequests` is modified after the activity loads. + List allResourceRequests = new ArrayList<>(); List defaultResourceRequests = new ArrayList<>(); List allowedResourceRequests = new ArrayList<>(); List blockedResourceRequests = new ArrayList<>(); @@ -91,14 +92,26 @@ public class RequestsActivity extends AppCompatActivity implements ViewRequestDi for (String[] request : MainWebViewActivity.resourceRequests) { switch (Integer.valueOf(request[MainWebViewActivity.REQUEST_DISPOSITION])) { case MainWebViewActivity.REQUEST_DEFAULT: + // Add the request to the list of all requests. + allResourceRequests.add(request); + + // Add the request to the list of default requests. defaultResourceRequests.add(request); break; case MainWebViewActivity.REQUEST_ALLOWED: + // Add the request to the list of all requests. + allResourceRequests.add(request); + + // Add the request to the list of allowed requests. allowedResourceRequests.add(request); break; case MainWebViewActivity.REQUEST_BLOCKED: + // Add the request to the list of all requests. + allResourceRequests.add(request); + + // Add the request to the list of blocked requests. blockedResourceRequests.add(request); break; } @@ -106,7 +119,7 @@ public class RequestsActivity extends AppCompatActivity implements ViewRequestDi // Setup a matrix cursor for the resource lists. MatrixCursor spinnerCursor = new MatrixCursor(new String[]{"_id", "Requests"}); - spinnerCursor.addRow(new Object[]{0, getString(R.string.all) + " - " + MainWebViewActivity.resourceRequests.size()}); + spinnerCursor.addRow(new Object[]{0, getString(R.string.all) + " - " + allResourceRequests.size()}); spinnerCursor.addRow(new Object[]{1, getString(R.string.default_label) + " - " + defaultResourceRequests.size()}); spinnerCursor.addRow(new Object[]{2, getString(R.string.allowed_plural) + " - " + allowedResourceRequests.size()}); spinnerCursor.addRow(new Object[]{3, getString(R.string.blocked_plural) + " - " + blockedResourceRequests.size()}); @@ -137,7 +150,7 @@ public class RequestsActivity extends AppCompatActivity implements ViewRequestDi switch (position) { case 0: // All requests. // Get an adapter for all the request. - ArrayAdapter allResourceRequestsArrayAdapter = new RequestsArrayAdapter(getApplicationContext(), MainWebViewActivity.resourceRequests); + ArrayAdapter allResourceRequestsArrayAdapter = new RequestsArrayAdapter(getApplicationContext(), allResourceRequests); // Display the adapter in the list view. resourceRequestsListView.setAdapter(allResourceRequestsArrayAdapter); @@ -176,7 +189,7 @@ public class RequestsActivity extends AppCompatActivity implements ViewRequestDi }); // Create an array adapter with the list of the resource requests. - ArrayAdapter resourceRequestsArrayAdapter = new RequestsArrayAdapter(getApplicationContext(), MainWebViewActivity.resourceRequests); + ArrayAdapter resourceRequestsArrayAdapter = new RequestsArrayAdapter(getApplicationContext(), allResourceRequests); // Populate the list view with the resource requests adapter. resourceRequestsListView.setAdapter(resourceRequestsArrayAdapter); diff --git a/app/src/main/java/com/stoutner/privacybrowser/helpers/OrbotProxyHelper.java b/app/src/main/java/com/stoutner/privacybrowser/helpers/OrbotProxyHelper.java index 4f6cff18..a3607dad 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/helpers/OrbotProxyHelper.java +++ b/app/src/main/java/com/stoutner/privacybrowser/helpers/OrbotProxyHelper.java @@ -40,10 +40,14 @@ import java.lang.reflect.Method; public class OrbotProxyHelper { public static void setProxy(Context privacyBrowserContext, Activity parentActivity, String proxyHost, String proxyPort) { // Set the proxy values - System.setProperty("http.proxyHost", proxyHost); - System.setProperty("http.proxyPort", proxyPort); - System.setProperty("https.proxyHost", proxyHost); - System.setProperty("https.proxyPort", proxyPort); + System.setProperty("proxyHost", proxyHost); + System.setProperty("proxyPort", proxyPort); + + // These entries shouldn't be needed if the above general settings are applied. But I leave them in here for troubleshooting just in case. + //System.setProperty("http.proxyHost", proxyHost); + //System.setProperty("http.proxyPort", proxyPort); + //System.setProperty("https.proxyHost", proxyHost); + //System.setProperty("https.proxyPort", proxyPort); // Use reflection to apply the new proxy values. try { diff --git a/app/src/main/res/menu/webview_options_menu.xml b/app/src/main/res/menu/webview_options_menu.xml index ba51a600..862539e0 100644 --- a/app/src/main/res/menu/webview_options_menu.xml +++ b/app/src/main/res/menu/webview_options_menu.xml @@ -91,17 +91,54 @@ + + + + + + + + + + + + + @@ -109,49 +146,49 @@ @@ -160,21 +197,21 @@ @@ -182,35 +219,35 @@ \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a9ebd4c7..5bbcecbc 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -131,6 +131,8 @@ Clear Cookies Clear DOM Storage Clear Form Data + Fanboy’s Annoyance List + Fanboy’s Social Blocking List Layout Font Size 25% @@ -383,10 +385,10 @@ Main ad blocking list. EasyPrivacy Main tracker blocking list. - Fanboy’s annoyance list - Block annoying popups and links. Includes Fanboy’s social blocking lists. - Fanboy’s social blocking list - Blocks third-party social media content. + Fanboy’s annoyance list + Block annoying popups and links. Includes Fanboy’s social blocking lists. + Fanboy’s social blocking list + Blocks third-party social media content. Tor Proxy through Orbot Proxy all web traffic through Orbot on localhost:8118. diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 55547577..c3687d0c 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -106,14 +106,14 @@ -- 2.45.2