Teach resource requests to be tab aware.
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / views / NestedScrollWebView.java
index ea94571aa834a6924e4d79b7f1b6c40fe2b789d4..c4f54de9ee2488dc985a6ecdb0ba9fa2b599ccb1 100644 (file)
@@ -28,14 +28,38 @@ import androidx.core.view.NestedScrollingChild2;
 import androidx.core.view.NestedScrollingChildHelper;
 import androidx.core.view.ViewCompat;
 
+import java.util.ArrayList;
+
 // NestedScrollWebView extends WebView to handle nested scrolls (scrolling the app bar off the screen).
 public class NestedScrollWebView extends WebView implements NestedScrollingChild2 {
+    // These constants identify the blocklists.
+    public final static int BLOCKED_REQUESTS = 0;
+    public final static int EASY_LIST_BLOCKED_REQUESTS = 1;
+    public final static int EASY_PRIVACY_BLOCKED_REQUESTS = 2;
+    public final static int FANBOYS_ANNOYANCE_LIST_BLOCKED_REQUESTS = 3;
+    public final static int FANBOYS_SOCIAL_BLOCKING_LIST_BLOCKED_REQUESTS = 4;
+    public final static int ULTRA_PRIVACY_BLOCKED_REQUESTS = 5;
+    public final static int THIRD_PARTY_BLOCKED_REQUESTS = 6;
+
     // The nested scrolling child helper is used throughout the class.
     private NestedScrollingChildHelper nestedScrollingChildHelper;
 
     // The previous Y position needs to be tracked between motion events.
     private int previousYPosition;
 
+    // Track if domain settings are applied to this nested scroll WebView and, if so, the database ID.
+    private boolean domainSettingsApplied;
+    private int domainSettingsDatabaseId;
+
+    // Track the resource requests.
+    private ArrayList<String[]> resourceRequests = new ArrayList<>();
+    private int blockedRequests;
+    private int easyListBlockedRequests;
+    private int easyPrivacyBlockedRequests;
+    private int fanboysAnnoyanceListBlockedRequests;
+    private int fanboysSocialBlockingListBlockedRequests;
+    private int ultraPrivacyBlockedRequests;
+    private int thirdPartyBlockedRequests;
 
     // Basic constructor.
     public NestedScrollWebView(Context context) {
@@ -61,6 +85,157 @@ public class NestedScrollWebView extends WebView implements NestedScrollingChild
         nestedScrollingChildHelper.setNestedScrollingEnabled(true);
     }
 
+    public void setDomainSettingsApplied(boolean applied) {
+        // Store the domain settings applied status.
+        domainSettingsApplied = applied;
+    }
+
+    public boolean getDomainSettingsApplied() {
+        // Return the domain settings applied status.
+        return domainSettingsApplied;
+    }
+
+    public void setDomainSettingsDatabaseId(int databaseId) {
+        // Store the domain settings database ID.
+        domainSettingsDatabaseId = databaseId;
+    }
+
+    public int getDomainSettingsDatabaseId() {
+        // Return the domain settings database ID.
+        return domainSettingsDatabaseId;
+    }
+
+    public void addResourceRequest(String[] resourceRequest) {
+        // Add the resource request to the list.
+        resourceRequests.add(resourceRequest);
+    }
+
+    public ArrayList<String[]> getResourceRequests() {
+        // Return the list of resource requests.
+        return resourceRequests;
+    }
+
+    public void clearResourceRequests() {
+        // Clear the resource requests.
+        resourceRequests.clear();
+    }
+
+    public void resetRequestsCount(int list) {
+        // Run the command on the indicated list.
+        switch (list) {
+            case BLOCKED_REQUESTS:
+                // Reset the blocked requests count.
+                blockedRequests = 0;
+                break;
+
+            case EASY_LIST_BLOCKED_REQUESTS:
+                // Reset the EasyList blocked requests count.
+                easyListBlockedRequests = 0;
+                break;
+
+            case EASY_PRIVACY_BLOCKED_REQUESTS:
+                // Reset the EasyPrivacy blocked requests count.
+                easyPrivacyBlockedRequests = 0;
+                break;
+
+            case FANBOYS_ANNOYANCE_LIST_BLOCKED_REQUESTS:
+                // Reset the Fanboy's Annoyance List blocked requests count.
+                fanboysAnnoyanceListBlockedRequests = 0;
+                break;
+
+            case FANBOYS_SOCIAL_BLOCKING_LIST_BLOCKED_REQUESTS:
+                // Reset the Fanboy's Social Blocking List blocked requests count.
+                fanboysSocialBlockingListBlockedRequests = 0;
+                break;
+
+            case ULTRA_PRIVACY_BLOCKED_REQUESTS:
+                // Reset the UltraPrivacy blocked requests count.
+                ultraPrivacyBlockedRequests = 0;
+                break;
+
+            case THIRD_PARTY_BLOCKED_REQUESTS:
+                // Reset the Third Party blocked requests count.
+                thirdPartyBlockedRequests = 0;
+                break;
+        }
+    }
+
+    public void incrementRequestsCount(int list) {
+        // Run the command on the indicated list.
+        switch (list) {
+            case BLOCKED_REQUESTS:
+                // Increment the blocked requests count.
+                blockedRequests++;
+                break;
+
+            case EASY_LIST_BLOCKED_REQUESTS:
+                // Increment the EasyList blocked requests count.
+                easyListBlockedRequests++;
+                break;
+
+            case EASY_PRIVACY_BLOCKED_REQUESTS:
+                // Increment the EasyPrivacy blocked requests count.
+                easyPrivacyBlockedRequests++;
+                break;
+
+            case FANBOYS_ANNOYANCE_LIST_BLOCKED_REQUESTS:
+                // Increment the Fanboy's Annoyance List blocked requests count.
+                fanboysAnnoyanceListBlockedRequests++;
+                break;
+
+            case FANBOYS_SOCIAL_BLOCKING_LIST_BLOCKED_REQUESTS:
+                // Increment the Fanboy's Social Blocking List blocked requests count.
+                fanboysSocialBlockingListBlockedRequests++;
+                break;
+
+            case ULTRA_PRIVACY_BLOCKED_REQUESTS:
+                // Increment the UltraPrivacy blocked requests count.
+                ultraPrivacyBlockedRequests++;
+                break;
+
+            case THIRD_PARTY_BLOCKED_REQUESTS:
+                // Increment the Third Party blocked requests count.
+                thirdPartyBlockedRequests++;
+                break;
+        }
+    }
+
+    public int getRequestsCount(int list) {
+        // Run the command on the indicated list.
+        switch (list) {
+            case BLOCKED_REQUESTS:
+                // Return the blocked requests count.
+                return blockedRequests;
+
+            case EASY_LIST_BLOCKED_REQUESTS:
+                // Return the EasyList blocked requests count.
+                return easyListBlockedRequests;
+
+            case EASY_PRIVACY_BLOCKED_REQUESTS:
+                // Return the EasyPrivacy blocked requests count.
+                return easyPrivacyBlockedRequests;
+
+            case FANBOYS_ANNOYANCE_LIST_BLOCKED_REQUESTS:
+                // Return the Fanboy's Annoyance List blocked requests count.
+                return fanboysAnnoyanceListBlockedRequests;
+
+            case FANBOYS_SOCIAL_BLOCKING_LIST_BLOCKED_REQUESTS:
+                // Return the Fanboy's Social Blocking List blocked requests count.
+                return fanboysSocialBlockingListBlockedRequests;
+
+            case ULTRA_PRIVACY_BLOCKED_REQUESTS:
+                // Return the UltraPrivacy blocked requests count.
+                return ultraPrivacyBlockedRequests;
+
+            case THIRD_PARTY_BLOCKED_REQUESTS:
+                // Return the Third Party blocked requests count.
+                return thirdPartyBlockedRequests;
+
+            default:
+                // Return 0.  This should never end up being called.
+                return 0;
+        }
+    }
 
     @Override
     public boolean onTouchEvent(MotionEvent motionEvent) {
@@ -82,19 +257,43 @@ public class NestedScrollWebView extends WebView implements NestedScrollingChild
 
             case MotionEvent.ACTION_MOVE:
                 // Get the current Y position.
-                int currentYPosition = (int) motionEvent.getY();
+                int currentYMotionPosition = (int) motionEvent.getY();
+
+                // Calculate the pre-scroll delta Y.
+                int preScrollDeltaY = previousYPosition - currentYMotionPosition;
 
-                // Calculate the delta Y.
-                int deltaY = previousYPosition - currentYPosition;
+                // Initialize a variable to track how much of the scroll is consumed.
+                int[] consumedScroll = new int[2];
 
-                // Store the current Y position for use in the next action move.
-                previousYPosition = currentYPosition;
+                // Initialize a variable to track the offset in the window.
+                int[] offsetInWindow = new int[2];
 
-                // Dispatch the nested pre-school.
-                dispatchNestedPreScroll(0, deltaY, null, null);
+                // Get the WebView Y position.
+                int webViewYPosition = getScrollY();
 
-                // Dispatch the nested scroll.
-                dispatchNestedScroll(0, deltaY, 0, 0, null);
+                // Set the scroll delta Y to initially be the same as the pre-scroll delta Y.
+                int scrollDeltaY = preScrollDeltaY;
+
+                // Dispatch the nested pre-school.  This scrolls the app bar if it needs it.  `offsetInWindow` will be returned with an updated value.
+                if (dispatchNestedPreScroll(0, preScrollDeltaY, consumedScroll, offsetInWindow)) {
+                    // Update the scroll delta Y if some of it was consumed.
+                    scrollDeltaY = preScrollDeltaY - consumedScroll[1];
+                }
+
+                // Check to see if the WebView is at the top and and the scroll action is downward.
+                if ((webViewYPosition == 0) && (scrollDeltaY < 0)) {  // Swipe to refresh is being engaged.
+                    // Stop the nested scroll so that swipe to refresh has complete control.
+                    stopNestedScroll();
+                } else {  // Swipe to refresh is not being engaged.
+                    // Start the nested scroll so that the app bar can scroll off the screen.
+                    startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
+
+                    // Dispatch the nested scroll.  This scrolls the WebView.  The delta Y unconsumed normally controls the swipe refresh layout, but that is handled with the `if` statement above.
+                    dispatchNestedScroll(0, scrollDeltaY, 0, 0, offsetInWindow);
+
+                    // Store the current Y position for use in the next action move.
+                    previousYPosition = previousYPosition - scrollDeltaY;
+                }
 
                 // Run the default commands.
                 motionEventHandled = super.onTouchEvent(motionEvent);
@@ -109,10 +308,19 @@ public class NestedScrollWebView extends WebView implements NestedScrollingChild
                 motionEventHandled = super.onTouchEvent(motionEvent);
         }
 
+        // Perform a click.  This is required by the Android accessibility guidelines.
+        performClick();
+
         // Return the status of the motion event.
         return motionEventHandled;
     }
 
+    // The Android accessibility guidelines require overriding `performClick()` and calling it from `onTouchEvent()`.
+    @Override
+    public boolean performClick() {
+        return super.performClick();
+    }
+
 
     // Method from NestedScrollingChild.
     @Override