1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 18 package com.android.email.view; 19 20 import android.content.Context; 21 import android.graphics.Rect; 22 import android.util.AttributeSet; 23 import android.view.MotionEvent; 24 import android.view.View; 25 import android.view.ViewGroup; 26 import android.webkit.WebView; 27 import android.widget.ScrollView; 28 29 import java.util.ArrayList; 30 31 /** 32 * A {@link ScrollView} that will never lock scrolling in a particular direction. 33 * 34 * Usually ScrollView will capture all touch events once a drag has begun. In some cases, 35 * we want to delegate those touches to children as normal, even in the middle of a drag. This is 36 * useful when there are childviews like a WebView tha handles scrolling in the horizontal direction 37 * even while the ScrollView drags vertically. 38 * 39 * This is only tested to work for ScrollViews where the content scrolls in one direction. 40 */ 41 public class NonLockingScrollView extends ScrollView { 42 public NonLockingScrollView(Context context) { 43 super(context); 44 } 45 public NonLockingScrollView(Context context, AttributeSet attrs) { 46 super(context, attrs); 47 } 48 public NonLockingScrollView(Context context, AttributeSet attrs, int defStyle) { 49 super(context, attrs, defStyle); 50 } 51 52 /** 53 * Whether or not the contents of this view is being dragged by one of the children in 54 * {@link #mChildrenNeedingAllTouches}. 55 */ 56 private boolean mInCustomDrag = false; 57 58 /** 59 * The list of children who should always receive touch events, and not have them intercepted. 60 */ 61 private final ArrayList<View> mChildrenNeedingAllTouches = new ArrayList<View>(); 62 63 @Override 64 public boolean onInterceptTouchEvent(MotionEvent ev) { 65 final int action = ev.getActionMasked(); 66 final boolean isUp = action == MotionEvent.ACTION_UP; 67 68 if (isUp && mInCustomDrag) { 69 // An up event after a drag should be intercepted so that child views don't handle 70 // click events falsely after a drag. 71 mInCustomDrag = false; 72 onTouchEvent(ev); 73 return true; 74 } 75 76 if (!mInCustomDrag && !isEventOverChild(ev, mChildrenNeedingAllTouches)) { 77 return super.onInterceptTouchEvent(ev); 78 } 79 80 // Note the normal scrollview implementation is to intercept all touch events after it has 81 // detected a drag starting. We will handle this ourselves. 82 mInCustomDrag = super.onInterceptTouchEvent(ev); 83 if (mInCustomDrag) { 84 onTouchEvent(ev); 85 } 86 87 // Don't intercept events - pass them on to children as normal. 88 return false; 89 } 90 91 @Override 92 protected void onFinishInflate() { 93 super.onFinishInflate(); 94 excludeChildrenFromInterceptions(this); 95 } 96 97 /** 98 * Traverses the view tree for {@link WebView}s so they can be excluded from touch 99 * interceptions and receive all events. 100 */ 101 private void excludeChildrenFromInterceptions(View node) { 102 // If additional types of children should be excluded (e.g. horizontal scrolling banners), 103 // this needs to be modified accordingly. 104 if (node instanceof WebView) { 105 mChildrenNeedingAllTouches.add(node); 106 } else if (node instanceof ViewGroup) { 107 ViewGroup viewGroup = (ViewGroup) node; 108 final int childCount = viewGroup.getChildCount(); 109 for (int i = 0; i < childCount; i++) { 110 final View child = viewGroup.getChildAt(i); 111 excludeChildrenFromInterceptions(child); 112 } 113 } 114 } 115 116 private static final Rect sHitFrame = new Rect(); 117 private static boolean isEventOverChild(MotionEvent ev, ArrayList<View> children) { 118 final int actionIndex = ev.getActionIndex(); 119 final float x = ev.getX(actionIndex); 120 final float y = ev.getY(actionIndex); 121 122 for (View child : children) { 123 if (!canViewReceivePointerEvents(child)) { 124 continue; 125 } 126 child.getHitRect(sHitFrame); 127 128 // child can receive the motion event. 129 if (sHitFrame.contains((int) x, (int) y)) { 130 return true; 131 } 132 } 133 return false; 134 } 135 136 private static boolean canViewReceivePointerEvents(View child) { 137 return child.getVisibility() == VISIBLE || (child.getAnimation() != null); 138 } 139 } 140