Repository: thundernest/k-9
Issue: Cannot zoom HTML messages properly if at all

### Expected behavior
HTML mail should pinch zoom as it always has

### Actual behavior
In 5.201 I tried zooming a few HTML emails. Nether would pinch zoom (though double tapping made them a tiny bit bigger). At least once this led to a crash of the app.

I reverted to 5.010 and zoom works just fine with the same mail - in fact, it never had any such issue so I'm not upgrading until this is fixed.

### Steps to reproduce
Open html email e.g. from Xbox.com
Pinch zoom
Either nothing happens, it moves a tiny bit, or it ends up crashing.

### Environment
K-9 Mail version:
5.201 is broken for this. Works fine in 5.010

Android version:
7.0 on Nexus 6

IMAP


Changed files:
k9mail/src/main/java/com/fsck/k9/view/NonLockingScrollView.java

diff --git a/k9mail/src/main/java/com/fsck/k9/view/NonLockingScrollView.java b/k9mail/src/main/java/com/fsck/k9/view/NonLockingScrollView.java
index 5897c3b22..d97ec9ced 100644
--- a/k9mail/src/main/java/com/fsck/k9/view/NonLockingScrollView.java
+++ b/k9mail/src/main/java/com/fsck/k9/view/NonLockingScrollView.java
@@ -100,25 +100,14 @@ public class NonLockingScrollView extends ScrollView {
     @Override
     protected void onFinishInflate() {
         super.onFinishInflate();
-        excludeChildrenFromInterceptions(this);
+        setupDelegationOfTouchAndHierarchyChangeEvents();
     }
-
-    /**
-     * Traverses the view tree for {@link WebView}s so they can be excluded from touch
-     * interceptions and receive all events.
-     */
-    private void excludeChildrenFromInterceptions(View node) {
-        // If additional types of children should be excluded (e.g. horizontal scrolling banners),
-        // this needs to be modified accordingly.
-        if (node instanceof WebView) {
-            mChildrenNeedingAllTouches.add(node);
-        } else if (node instanceof ViewGroup) {
-            ViewGroup viewGroup = (ViewGroup) node;
-            final int childCount = viewGroup.getChildCount();
-            for (int i = 0; i < childCount; i++) {
-                final View child = viewGroup.getChildAt(i);
-                excludeChildrenFromInterceptions(child);
-            }
+    
+    private void setupDelegationOfTouchAndHierarchyChangeEvents() {
+        OnHierarchyChangeListener listener = new HierarchyTreeChangeListener();
+        setOnHierarchyChangeListener(listener);
+        for (int i = 0, childCount = getChildCount(); i < childCount; i++) {
+            listener.onChildViewAdded(this, getChildAt(i));
         }
     }
 
@@ -167,4 +156,32 @@ public class NonLockingScrollView extends ScrollView {
             super.requestChildFocus(child, focused);
         }
     }
+
+    class HierarchyTreeChangeListener implements OnHierarchyChangeListener {
+        @Override
+        public void onChildViewAdded(View parent, View child) {
+            if (child instanceof WebView) {
+                mChildrenNeedingAllTouches.add(child);                
+            } else if (child instanceof ViewGroup) {
+                ViewGroup childGroup = (ViewGroup) child;
+                childGroup.setOnHierarchyChangeListener(this);
+                for (int i = 0, childCount = childGroup.getChildCount(); i < childCount; i++) {
+                    onChildViewAdded(childGroup, childGroup.getChildAt(i));
+                }
+            }
+        }
+
+        @Override
+        public void onChildViewRemoved(View parent, View child) {
+            if (child instanceof WebView) {
+                mChildrenNeedingAllTouches.remove(child);
+            } else if (child instanceof ViewGroup) {
+                ViewGroup childGroup = (ViewGroup) child;
+                for (int i = 0, childCount = childGroup.getChildCount(); i < childCount; i++) {
+                    onChildViewRemoved(childGroup, childGroup.getChildAt(i));
+                }
+                childGroup.setOnHierarchyChangeListener(null);
+            }
+        }
+    }
 }