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