]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/views/NestedScrollWebView.java
c7122cb8c83c4c34cad3ff4ff5a7a549cc2dc059
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / views / NestedScrollWebView.java
1 /*
2  * Copyright © 2019 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
6  * Privacy Browser is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacybrowser.views;
21
22 import android.content.Context;
23 import android.graphics.Bitmap;
24 import android.graphics.drawable.BitmapDrawable;
25 import android.graphics.drawable.Drawable;
26 import android.util.AttributeSet;
27 import android.view.MotionEvent;
28 import android.webkit.HttpAuthHandler;
29 import android.webkit.SslErrorHandler;
30 import android.webkit.WebView;
31
32 import androidx.annotation.NonNull;
33 import androidx.core.content.ContextCompat;
34 import androidx.core.view.NestedScrollingChild2;
35 import androidx.core.view.NestedScrollingChildHelper;
36 import androidx.core.view.ViewCompat;
37
38 import com.stoutner.privacybrowser.R;
39
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.Date;
43 import java.util.List;
44
45 // NestedScrollWebView extends WebView to handle nested scrolls (scrolling the app bar off the screen).
46 public class NestedScrollWebView extends WebView implements NestedScrollingChild2 {
47     // These constants identify the blocklists.
48     public final static int BLOCKED_REQUESTS = 0;
49     public final static int EASYLIST = 1;
50     public final static int EASYPRIVACY = 2;
51     public final static int FANBOYS_ANNOYANCE_LIST = 3;
52     public final static int FANBOYS_SOCIAL_BLOCKING_LIST = 4;
53     public final static int ULTRALIST = 5;
54     public final static int ULTRAPRIVACY = 6;
55     public final static int THIRD_PARTY_REQUESTS = 7;
56
57     // Keep a copy of the WebView fragment ID.
58     private long webViewFragmentId;
59
60     // Store the handlers.
61     private SslErrorHandler sslErrorHandler;
62     private HttpAuthHandler httpAuthHandler;
63
64     // Track if domain settings are applied to this nested scroll WebView and, if so, the database ID.
65     private boolean domainSettingsApplied;
66     private int domainSettingsDatabaseId;
67
68     // Keep track of the current URL.  This is used to not block resource requests to the main URL.
69     private String currentUrl;
70
71     // Keep track of when the domain name changes so that domain settings can be reapplied.  This should never be null.
72     private String currentDomainName = "";
73
74     // Track the status of first-party cookies.
75     private boolean acceptFirstPartyCookies;
76
77     // Track the domain settings JavaScript status.  This can be removed once night mode does not require JavaScript.
78     private boolean domainSettingsJavaScriptEnabled;
79
80     // Track the resource requests.
81     private List<String[]> resourceRequests = Collections.synchronizedList(new ArrayList<>());  // Using a synchronized list makes adding resource requests thread safe.
82     private boolean easyListEnabled;
83     private boolean easyPrivacyEnabled;
84     private boolean fanboysAnnoyanceListEnabled;
85     private boolean fanboysSocialBlockingListEnabled;
86     private boolean ultraListEnabled;
87     private boolean ultraPrivacyEnabled;
88     private boolean blockAllThirdPartyRequests;
89     private int blockedRequests;
90     private int easyListBlockedRequests;
91     private int easyPrivacyBlockedRequests;
92     private int fanboysAnnoyanceListBlockedRequests;
93     private int fanboysSocialBlockingListBlockedRequests;
94     private int ultraListBlockedRequests;
95     private int ultraPrivacyBlockedRequests;
96     private int thirdPartyBlockedRequests;
97
98     // The pinned SSL certificate variables.
99     private boolean hasPinnedSslCertificate;
100     private String pinnedSslIssuedToCName;
101     private String pinnedSslIssuedToOName;
102     private String pinnedSslIssuedToUName;
103     private String pinnedSslIssuedByCName;
104     private String pinnedSslIssuedByOName;
105     private String pinnedSslIssuedByUName;
106     private Date pinnedSslStartDate;
107     private Date pinnedSslEndDate;
108
109     // The current IP addresses variables.
110     private boolean hasCurrentIpAddresses;
111     private String currentIpAddresses;
112
113     // The pinned IP addresses variables.
114     private boolean hasPinnedIpAddresses;
115     private String pinnedIpAddresses;
116
117     // The ignore pinned domain information tracker.  This is set when a user proceeds past a pinned mismatch dialog to prevent the dialog from showing again until after the domain changes.
118     private boolean ignorePinnedDomainInformation;
119
120     // Track navigation of history.
121     private boolean navigatingHistory;
122
123     // The default or favorite icon.
124     private Bitmap favoriteOrDefaultIcon;
125
126     // Track night mode.
127     private boolean nightMode;
128
129     // Track swipe to refresh.
130     private boolean swipeToRefresh;
131
132     // The nested scrolling child helper is used throughout the class.
133     private NestedScrollingChildHelper nestedScrollingChildHelper;
134
135     // The previous Y position needs to be tracked between motion events.
136     private int previousYPosition;
137
138
139
140     // The basic constructor.
141     public NestedScrollWebView(Context context) {
142         // Roll up to the next constructor.
143         this(context, null);
144     }
145
146     // The intermediate constructor.
147     public NestedScrollWebView(Context context, AttributeSet attributeSet) {
148         // Roll up to the next constructor.
149         this(context, attributeSet, android.R.attr.webViewStyle);
150     }
151
152     // The full constructor.
153     public NestedScrollWebView(Context context, AttributeSet attributeSet, int defaultStyle) {
154         // Run the default commands.
155         super(context, attributeSet, defaultStyle);
156
157         // Initialize the nested scrolling child helper.
158         nestedScrollingChildHelper = new NestedScrollingChildHelper(this);
159
160         // Enable nested scrolling by default.
161         nestedScrollingChildHelper.setNestedScrollingEnabled(true);
162     }
163
164
165
166     // WebView Fragment ID.
167     public void setWebViewFragmentId(long webViewFragmentId) {
168         // Store the WebView fragment ID.
169         this.webViewFragmentId = webViewFragmentId;
170     }
171
172     public long getWebViewFragmentId() {
173         // Return the WebView fragment ID.
174         return webViewFragmentId;
175     }
176
177
178     // SSL error handler.
179     public void setSslErrorHandler(SslErrorHandler sslErrorHandler) {
180         // Store the current SSL error handler.
181         this.sslErrorHandler = sslErrorHandler;
182     }
183
184     public SslErrorHandler getSslErrorHandler() {
185         // Return the current SSL error handler.
186         return sslErrorHandler;
187     }
188
189     public void resetSslErrorHandler() {
190         // Reset the current SSL error handler.
191         sslErrorHandler = null;
192     }
193
194
195     // HTTP authentication handler.
196     public void setHttpAuthHandler(HttpAuthHandler httpAuthHandler) {
197         // Store the current HTTP authentication handler.
198         this.httpAuthHandler = httpAuthHandler;
199     }
200
201     public HttpAuthHandler getHttpAuthHandler() {
202         // Return the current HTTP authentication handler.
203         return httpAuthHandler;
204     }
205
206     public void resetHttpAuthHandler() {
207         // Reset the current HTTP authentication handler.
208         httpAuthHandler = null;
209     }
210
211
212     // Domain settings.
213     public void setDomainSettingsApplied(boolean applied) {
214         // Store the domain settings applied status.
215         domainSettingsApplied = applied;
216     }
217
218     public boolean getDomainSettingsApplied() {
219         // Return the domain settings applied status.
220         return domainSettingsApplied;
221     }
222
223
224     // Domain settings database ID.
225     public void setDomainSettingsDatabaseId(int databaseId) {
226         // Store the domain settings database ID.
227         domainSettingsDatabaseId = databaseId;
228     }
229
230     public int getDomainSettingsDatabaseId() {
231         // Return the domain settings database ID.
232         return domainSettingsDatabaseId;
233     }
234
235
236     // Current URL.
237     public void setCurrentUrl(String url) {
238         // Store the current URL.
239         currentUrl = url;
240     }
241
242     public String getCurrentUrl() {
243         // Return the current URL.
244         return currentUrl;
245     }
246
247
248     // Current domain name.  To function well when called, the domain name should never be allowed to be null.
249     public void setCurrentDomainName(@NonNull String domainName) {
250         // Store the current domain name.
251         currentDomainName = domainName;
252     }
253
254     public void resetCurrentDomainName() {
255         // Reset the current domain name.
256         currentDomainName = "";
257     }
258
259     public String getCurrentDomainName() {
260         // Return the current domain name.
261         return currentDomainName;
262     }
263
264
265     // First-party cookies.
266     public void setAcceptFirstPartyCookies(boolean status) {
267         // Store the accept first-party cookies status.
268         acceptFirstPartyCookies = status;
269     }
270
271     public boolean getAcceptFirstPartyCookies() {
272         // Return the accept first-party cookies status.
273         return acceptFirstPartyCookies;
274     }
275
276
277     // Domain settings JavaScript enabled.  This can be removed once night mode does not require JavaScript.
278     public void setDomainSettingsJavaScriptEnabled(boolean status) {
279         // Store the domain settings JavaScript status.
280         domainSettingsJavaScriptEnabled = status;
281     }
282
283     public boolean getDomainSettingsJavaScriptEnabled() {
284         // Return the domain settings JavaScript status.
285         return domainSettingsJavaScriptEnabled;
286     }
287
288
289     // Resource requests.
290     public void addResourceRequest(String[] resourceRequest) {
291         // Add the resource request to the list.
292         resourceRequests.add(resourceRequest);
293     }
294
295     public List<String[]> getResourceRequests() {
296         // Return the list of resource requests as an array list.
297         return resourceRequests;
298     }
299
300     public void clearResourceRequests() {
301         // Clear the resource requests.
302         resourceRequests.clear();
303     }
304
305
306     // Blocklists.
307     public void enableBlocklist(int blocklist, boolean status) {
308         // Update the status of the indicated blocklist.
309         switch (blocklist) {
310             case EASYLIST:
311                 // Update the status of the blocklist.
312                 easyListEnabled = status;
313                 break;
314
315             case EASYPRIVACY:
316                 // Update the status of the blocklist.
317                 easyPrivacyEnabled = status;
318                 break;
319
320             case FANBOYS_ANNOYANCE_LIST:
321                 // Update the status of the blocklist.
322                 fanboysAnnoyanceListEnabled = status;
323                 break;
324
325             case FANBOYS_SOCIAL_BLOCKING_LIST:
326                 // Update the status of the blocklist.
327                 fanboysSocialBlockingListEnabled = status;
328                 break;
329
330             case ULTRALIST:
331                 // Update the status of the blocklist.
332                 ultraListEnabled = status;
333                 break;
334
335             case ULTRAPRIVACY:
336                 // Update the status of the blocklist.
337                 ultraPrivacyEnabled = status;
338                 break;
339
340             case THIRD_PARTY_REQUESTS:
341                 // Update the status of the blocklist.
342                 blockAllThirdPartyRequests = status;
343                 break;
344         }
345     }
346
347     public boolean isBlocklistEnabled(int blocklist) {
348         // Get the status of the indicated blocklist.
349         switch (blocklist) {
350             case EASYLIST:
351                 // Return the status of the blocklist.
352                 return easyListEnabled;
353
354             case EASYPRIVACY:
355                 // Return the status of the blocklist.
356                 return easyPrivacyEnabled;
357
358             case FANBOYS_ANNOYANCE_LIST:
359                 // Return the status of the blocklist.
360                 return fanboysAnnoyanceListEnabled;
361
362             case FANBOYS_SOCIAL_BLOCKING_LIST:
363                 // Return the status of the blocklist.
364                 return fanboysSocialBlockingListEnabled;
365
366             case ULTRALIST:
367                 // Return the status of the blocklist.
368                 return ultraListEnabled;
369
370             case ULTRAPRIVACY:
371                 // Return the status of the blocklist.
372                 return ultraPrivacyEnabled;
373
374             case THIRD_PARTY_REQUESTS:
375                 // Return the status of the blocklist.
376                 return blockAllThirdPartyRequests;
377
378             default:
379                 // The default value is required but should never be used.
380                 return false;
381         }
382     }
383
384
385     // Resource request counters.
386     public void resetRequestsCounters() {
387         // Reset all the resource request counters.
388         blockedRequests = 0;
389         easyListBlockedRequests = 0;
390         easyPrivacyBlockedRequests = 0;
391         fanboysAnnoyanceListBlockedRequests = 0;
392         fanboysSocialBlockingListBlockedRequests = 0;
393         ultraListBlockedRequests = 0;
394         ultraPrivacyBlockedRequests = 0;
395         thirdPartyBlockedRequests = 0;
396     }
397
398     public void incrementRequestsCount(int blocklist) {
399         // Increment the count of the indicated blocklist.
400         switch (blocklist) {
401             case BLOCKED_REQUESTS:
402                 // Increment the blocked requests count.
403                 blockedRequests++;
404                 break;
405
406             case EASYLIST:
407                 // Increment the EasyList blocked requests count.
408                 easyListBlockedRequests++;
409                 break;
410
411             case EASYPRIVACY:
412                 // Increment the EasyPrivacy blocked requests count.
413                 easyPrivacyBlockedRequests++;
414                 break;
415
416             case FANBOYS_ANNOYANCE_LIST:
417                 // Increment the Fanboy's Annoyance List blocked requests count.
418                 fanboysAnnoyanceListBlockedRequests++;
419                 break;
420
421             case FANBOYS_SOCIAL_BLOCKING_LIST:
422                 // Increment the Fanboy's Social Blocking List blocked requests count.
423                 fanboysSocialBlockingListBlockedRequests++;
424                 break;
425
426             case ULTRALIST:
427                 // Increment the UltraList blocked requests count.
428                 ultraListBlockedRequests++;
429                 break;
430
431             case ULTRAPRIVACY:
432                 // Increment the UltraPrivacy blocked requests count.
433                 ultraPrivacyBlockedRequests++;
434                 break;
435
436             case THIRD_PARTY_REQUESTS:
437                 // Increment the Third Party blocked requests count.
438                 thirdPartyBlockedRequests++;
439                 break;
440         }
441     }
442
443     public int getRequestsCount(int blocklist) {
444         // Get the count of the indicated blocklist.
445         switch (blocklist) {
446             case BLOCKED_REQUESTS:
447                 // Return the blocked requests count.
448                 return blockedRequests;
449
450             case EASYLIST:
451                 // Return the EasyList blocked requests count.
452                 return easyListBlockedRequests;
453
454             case EASYPRIVACY:
455                 // Return the EasyPrivacy blocked requests count.
456                 return easyPrivacyBlockedRequests;
457
458             case FANBOYS_ANNOYANCE_LIST:
459                 // Return the Fanboy's Annoyance List blocked requests count.
460                 return fanboysAnnoyanceListBlockedRequests;
461
462             case FANBOYS_SOCIAL_BLOCKING_LIST:
463                 // Return the Fanboy's Social Blocking List blocked requests count.
464                 return fanboysSocialBlockingListBlockedRequests;
465
466             case ULTRALIST:
467                 // Return the UltraList blocked requests count.
468                 return ultraListBlockedRequests;
469
470             case ULTRAPRIVACY:
471                 // Return the UltraPrivacy blocked requests count.
472                 return ultraPrivacyBlockedRequests;
473
474             case THIRD_PARTY_REQUESTS:
475                 // Return the Third Party blocked requests count.
476                 return thirdPartyBlockedRequests;
477
478             default:
479                 // Return 0.  This should never end up being called.
480                 return 0;
481         }
482     }
483
484
485     // Pinned SSL certificates.
486     public boolean hasPinnedSslCertificate() {
487         // Return the status of the pinned SSL certificate.
488         return hasPinnedSslCertificate;
489     }
490
491     public void setPinnedSslCertificate(String issuedToCName, String issuedToOName, String issuedToUName, String issuedByCName, String issuedByOName, String issuedByUName, Date startDate, Date endDate) {
492         // Store the pinned SSL certificate information.
493         pinnedSslIssuedToCName = issuedToCName;
494         pinnedSslIssuedToOName = issuedToOName;
495         pinnedSslIssuedToUName = issuedToUName;
496         pinnedSslIssuedByCName = issuedByCName;
497         pinnedSslIssuedByOName = issuedByOName;
498         pinnedSslIssuedByUName = issuedByUName;
499         pinnedSslStartDate = startDate;
500         pinnedSslEndDate = endDate;
501
502         // Set the pinned SSL certificate tracker.
503         hasPinnedSslCertificate = true;
504     }
505
506     public ArrayList<Object> getPinnedSslCertificate() {
507         // Initialize an array list.
508         ArrayList<Object> arrayList = new ArrayList<>();
509
510         // Create the SSL certificate string array.
511         String[] sslCertificateStringArray = new String[] {pinnedSslIssuedToCName, pinnedSslIssuedToOName, pinnedSslIssuedToUName, pinnedSslIssuedByCName, pinnedSslIssuedByOName, pinnedSslIssuedByUName};
512
513         // Create the SSL certificate date array.
514         Date[] sslCertificateDateArray = new Date[] {pinnedSslStartDate, pinnedSslEndDate};
515
516         // Add the arrays to the array list.
517         arrayList.add(sslCertificateStringArray);
518         arrayList.add(sslCertificateDateArray);
519
520         // Return the pinned SSL certificate array list.
521         return arrayList;
522     }
523
524     public void clearPinnedSslCertificate() {
525         // Clear the pinned SSL certificate.
526         pinnedSslIssuedToCName = null;
527         pinnedSslIssuedToOName = null;
528         pinnedSslIssuedToUName = null;
529         pinnedSslIssuedByCName = null;
530         pinnedSslIssuedByOName = null;
531         pinnedSslIssuedByUName = null;
532         pinnedSslStartDate = null;
533         pinnedSslEndDate = null;
534
535         // Clear the pinned SSL certificate tracker.
536         hasPinnedSslCertificate = false;
537     }
538
539
540     // Current IP addresses.
541     public boolean hasCurrentIpAddresses() {
542         // Return the status of the current IP addresses.
543         return hasCurrentIpAddresses;
544     }
545
546     public void setCurrentIpAddresses(String ipAddresses) {
547         // Store the current IP addresses.
548         currentIpAddresses = ipAddresses;
549
550         // Set the current IP addresses tracker.
551         hasCurrentIpAddresses = true;
552     }
553
554     public String getCurrentIpAddresses() {
555         // Return the current IP addresses.
556         return currentIpAddresses;
557     }
558
559     public void clearCurrentIpAddresses() {
560         // Clear the current IP addresses.
561         currentIpAddresses = null;
562
563         // Clear the current IP addresses tracker.
564         hasCurrentIpAddresses = false;
565     }
566
567
568     // Pinned IP addresses.
569     public boolean hasPinnedIpAddresses() {
570         // Return the status of the pinned IP addresses.
571         return hasPinnedIpAddresses;
572     }
573
574     public void setPinnedIpAddresses(String ipAddresses) {
575         // Store the pinned IP addresses.
576         pinnedIpAddresses = ipAddresses;
577
578         // Set the pinned IP addresses tracker.
579         hasPinnedIpAddresses = true;
580     }
581
582     public String getPinnedIpAddresses() {
583         // Return the pinned IP addresses.
584         return pinnedIpAddresses;
585     }
586
587     public void clearPinnedIpAddresses() {
588         // Clear the pinned IP addresses.
589         pinnedIpAddresses = null;
590
591         // Clear the pinned IP addresses tracker.
592         hasPinnedIpAddresses = false;
593     }
594
595
596     // Ignore pinned information.
597     public void setIgnorePinnedDomainInformation(boolean status) {
598         // Set the status of the ignore pinned domain information tracker.
599         ignorePinnedDomainInformation = status;
600     }
601
602     // The syntax looks better as written, even if it is always inverted.
603     @SuppressWarnings("BooleanMethodIsAlwaysInverted")
604     public boolean ignorePinnedDomainInformation() {
605         // Return the status of the ignore pinned domain information tracker.
606         return ignorePinnedDomainInformation;
607     }
608
609
610     // Navigating history.
611     public void setNavigatingHistory(boolean status) {
612         // Set the status of navigating history.
613         navigatingHistory = status;
614     }
615
616     public boolean getNavigatingHistory() {
617         // Return the status of navigating history.
618         return navigatingHistory;
619     }
620
621
622     // Favorite or default icon.
623     public void initializeFavoriteIcon() {
624         // Get the default favorite icon drawable.  `ContextCompat` must be used until API >= 21.
625         Drawable favoriteIconDrawable = ContextCompat.getDrawable(getContext(), R.drawable.world);
626
627         // Cast the favorite icon drawable to a bitmap drawable.
628         BitmapDrawable favoriteIconBitmapDrawable = (BitmapDrawable) favoriteIconDrawable;
629
630         // Remove the incorrect warning below that the favorite icon bitmap drawable might be null.
631         assert favoriteIconBitmapDrawable != null;
632
633         // Store the default icon bitmap.
634         favoriteOrDefaultIcon = favoriteIconBitmapDrawable.getBitmap();
635     }
636
637     public void setFavoriteOrDefaultIcon(Bitmap icon) {
638         // Scale the favorite icon bitmap down if it is larger than 256 x 256.  Filtering uses bilinear interpolation.
639         if ((icon.getHeight() > 256) || (icon.getWidth() > 256)) {
640             favoriteOrDefaultIcon = Bitmap.createScaledBitmap(icon, 256, 256, true);
641         } else {
642             // Store the icon as presented.
643             favoriteOrDefaultIcon = icon;
644         }
645     }
646
647     public Bitmap getFavoriteOrDefaultIcon() {
648         // Return the favorite or default icon.
649         return favoriteOrDefaultIcon;
650     }
651
652
653     // Night mode.
654     public void setNightMode(boolean status) {
655         // Store the night mode status.
656         nightMode = status;
657     }
658
659     public boolean getNightMode() {
660         // Return the night mode status.
661         return nightMode;
662     }
663
664
665     // Swipe to refresh.
666     public void setSwipeToRefresh(boolean status) {
667         // Store the swipe to refresh status.
668         swipeToRefresh = status;
669     }
670
671     public boolean getSwipeToRefresh() {
672         // Return the swipe to refresh status.
673         return swipeToRefresh;
674     }
675
676
677     // Scroll range.
678     public int getHorizontalScrollRange() {
679         // Return the horizontal scroll range.
680         return computeHorizontalScrollRange();
681     }
682
683     public int getVerticalScrollRange() {
684         // Return the vertical scroll range.
685         return computeVerticalScrollRange();
686     }
687
688
689
690     @Override
691     public boolean onTouchEvent(MotionEvent motionEvent) {
692         // Initialize a tracker to return if this motion event is handled.
693         boolean motionEventHandled;
694
695         // Run the commands for the given motion event action.
696         switch (motionEvent.getAction()) {
697             case MotionEvent.ACTION_DOWN:
698                 // Start nested scrolling along the vertical axis.  `ViewCompat` must be used until the minimum API >= 21.
699                 startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
700
701                 // Save the current Y position.  Action down will not be called again until a new motion starts.
702                 previousYPosition = (int) motionEvent.getY();
703
704                 // Run the default commands.
705                 motionEventHandled = super.onTouchEvent(motionEvent);
706                 break;
707
708             case MotionEvent.ACTION_MOVE:
709                 // Get the current Y position.
710                 int currentYMotionPosition = (int) motionEvent.getY();
711
712                 // Calculate the pre-scroll delta Y.
713                 int preScrollDeltaY = previousYPosition - currentYMotionPosition;
714
715                 // Initialize a variable to track how much of the scroll is consumed.
716                 int[] consumedScroll = new int[2];
717
718                 // Initialize a variable to track the offset in the window.
719                 int[] offsetInWindow = new int[2];
720
721                 // Get the WebView Y position.
722                 int webViewYPosition = getScrollY();
723
724                 // Set the scroll delta Y to initially be the same as the pre-scroll delta Y.
725                 int scrollDeltaY = preScrollDeltaY;
726
727                 // Dispatch the nested pre-school.  This scrolls the app bar if it needs it.  `offsetInWindow` will be returned with an updated value.
728                 if (dispatchNestedPreScroll(0, preScrollDeltaY, consumedScroll, offsetInWindow)) {
729                     // Update the scroll delta Y if some of it was consumed.
730                     // There is currently a bug in Android where if scrolling up at a certain slow speed the input can lock the pre scroll and continue to consume it after the app bar is fully displayed.
731                     scrollDeltaY = preScrollDeltaY - consumedScroll[1];
732                 }
733
734                 // Check to see if the WebView is at the top and and the scroll action is downward.
735                 if ((webViewYPosition == 0) && (scrollDeltaY < 0)) {  // Swipe to refresh is being engaged.
736                     // Stop the nested scroll so that swipe to refresh has complete control.  This way releasing the scroll to refresh circle doesn't scroll the WebView at the same time.
737                     stopNestedScroll();
738                 } else {  // Swipe to refresh is not being engaged.
739                     // Start the nested scroll so that the app bar can scroll off the screen.
740                     startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
741
742                     // 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.
743                     dispatchNestedScroll(0, scrollDeltaY, 0, 0, offsetInWindow);
744
745                     // Store the current Y position for use in the next action move.
746                     previousYPosition = previousYPosition - scrollDeltaY;
747                 }
748
749                 // Run the default commands.
750                 motionEventHandled = super.onTouchEvent(motionEvent);
751                 break;
752
753
754             default:
755                 // Stop nested scrolling.
756                 stopNestedScroll();
757
758                 // Run the default commands.
759                 motionEventHandled = super.onTouchEvent(motionEvent);
760         }
761
762         // Perform a click.  This is required by the Android accessibility guidelines.
763         performClick();
764
765         // Return the status of the motion event.
766         return motionEventHandled;
767     }
768
769     // The Android accessibility guidelines require overriding `performClick()` and calling it from `onTouchEvent()`.
770     @Override
771     public boolean performClick() {
772         return super.performClick();
773     }
774
775
776     // Method from NestedScrollingChild.
777     @Override
778     public void setNestedScrollingEnabled(boolean status) {
779         // Set the status of the nested scrolling.
780         nestedScrollingChildHelper.setNestedScrollingEnabled(status);
781     }
782
783     // Method from NestedScrollingChild.
784     @Override
785     public boolean isNestedScrollingEnabled() {
786         // Return the status of nested scrolling.
787         return nestedScrollingChildHelper.isNestedScrollingEnabled();
788     }
789
790
791     // Method from NestedScrollingChild.
792     @Override
793     public boolean startNestedScroll(int axes) {
794         // Start a nested scroll along the indicated axes.
795         return nestedScrollingChildHelper.startNestedScroll(axes);
796     }
797
798     // Method from NestedScrollingChild2.
799     @Override
800     public boolean startNestedScroll(int axes, int type) {
801         // Start a nested scroll along the indicated axes for the given type of input which caused the scroll event.
802         return nestedScrollingChildHelper.startNestedScroll(axes, type);
803     }
804
805
806     // Method from NestedScrollingChild.
807     @Override
808     public void stopNestedScroll() {
809         // Stop the nested scroll.
810         nestedScrollingChildHelper.stopNestedScroll();
811     }
812
813     // Method from NestedScrollingChild2.
814     @Override
815     public void stopNestedScroll(int type) {
816         // Stop the nested scroll of the given type of input which caused the scroll event.
817         nestedScrollingChildHelper.stopNestedScroll(type);
818     }
819
820
821     // Method from NestedScrollingChild.
822     @Override
823     public boolean hasNestedScrollingParent() {
824         // Return the status of the nested scrolling parent.
825         return nestedScrollingChildHelper.hasNestedScrollingParent();
826     }
827
828     // Method from NestedScrollingChild2.
829     @Override
830     public boolean hasNestedScrollingParent(int type) {
831         // return the status of the nested scrolling parent for the given type of input which caused the scroll event.
832         return nestedScrollingChildHelper.hasNestedScrollingParent(type);
833     }
834
835
836     // Method from NestedScrollingChild.
837     @Override
838     public boolean dispatchNestedPreScroll(int deltaX, int deltaY, int[] consumed, int[] offsetInWindow) {
839         // Dispatch a nested pre-scroll with the specified deltas, which lets a parent to consume some of the scroll if desired.
840         return nestedScrollingChildHelper.dispatchNestedPreScroll(deltaX, deltaY, consumed, offsetInWindow);
841     }
842
843     // Method from NestedScrollingChild2.
844     @Override
845     public boolean dispatchNestedPreScroll(int deltaX, int deltaY, int[] consumed, int[] offsetInWindow, int type) {
846         // Dispatch a nested pre-scroll with the specified deltas for the given type of input which caused the scroll event, which lets a parent to consume some of the scroll if desired.
847         return nestedScrollingChildHelper.dispatchNestedPreScroll(deltaX, deltaY, consumed, offsetInWindow, type);
848     }
849
850
851     // Method from NestedScrollingChild.
852     @Override
853     public boolean dispatchNestedScroll(int deltaXConsumed, int deltaYConsumed, int deltaXUnconsumed, int deltaYUnconsumed, int[] offsetInWindow) {
854         // Dispatch a nested scroll with the specified deltas.
855         return nestedScrollingChildHelper.dispatchNestedScroll(deltaXConsumed, deltaYConsumed, deltaXUnconsumed, deltaYUnconsumed, offsetInWindow);
856     }
857
858     // Method from NestedScrollingChild2.
859     @Override
860     public boolean dispatchNestedScroll(int deltaXConsumed, int deltaYConsumed, int deltaXUnconsumed, int deltaYUnconsumed, int[] offsetInWindow, int type) {
861         // Dispatch a nested scroll with the specified deltas for the given type of input which caused the scroll event.
862         return nestedScrollingChildHelper.dispatchNestedScroll(deltaXConsumed, deltaYConsumed, deltaXUnconsumed, deltaYUnconsumed, offsetInWindow, type);
863     }
864
865
866     // Method from NestedScrollingChild.
867     @Override
868     public boolean dispatchNestedPreFling(float velocityX, float velocityY) {
869         // Dispatch a nested pre-fling with the specified velocity, which lets a parent consume the fling if desired.
870         return nestedScrollingChildHelper.dispatchNestedPreFling(velocityX, velocityY);
871     }
872
873     // Method from NestedScrollingChild.
874     @Override
875     public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
876         // Dispatch a nested fling with the specified velocity.
877         return nestedScrollingChildHelper.dispatchNestedFling(velocityX, velocityY, consumed);
878     }
879 }