Bug 1116415 - 6. Provide fixed TouchDelegate class. r?sebastian draft
authorTom Klein <twointofive@gmail.com>
Wed, 21 Sep 2016 17:49:06 -0500
changeset 425504 9302e89df0fed6be96d88059976a8ad994e2e080
parent 425503 20ded81372507f3356c2922a47c299650cdba884
child 533929 a7fbabffea6147694b0ee9c3e15ce5ca7559e92b
push id32434
push userbmo:twointofive@gmail.com
push dateFri, 14 Oct 2016 20:21:22 +0000
reviewerssebastian
bugs1116415
milestone52.0a1
Bug 1116415 - 6. Provide fixed TouchDelegate class. r?sebastian The Android version of TouchDelegate never resets the value of its mDelegateTargeted, which means that once an event is delegated, future events can get delegated as well, regardless of whether the event occurs within the TouchDelegate's bounds. Because of the geometry this is more of an issue with the (future RecyclerView version of) TabsGridLayout, but it can occur on TabsLinearLayout as well where we use a TouchDelegate to increase the hit size of the close button on a tab - once a close button is hit by delegation and the tab is closed, the next time that tab is recycled it will continue to delegate ACTION_UP events to the close button (in which case they're silently dropped) even if the ACTION_DOWN was not in the close button's delegation area. This commit introduces TouchDelegateWithReset, which is simply an override copy of TouchDelegate with one extra block (commented in the new class) to reset mDelegateTargeted on each new gesture received. MozReview-Commit-ID: 5xrPBAdAK6D
mobile/android/base/java/org/mozilla/gecko/tabs/TabsLayoutItemView.java
mobile/android/base/java/org/mozilla/gecko/widget/TouchDelegateWithReset.java
mobile/android/base/moz.build
--- a/mobile/android/base/java/org/mozilla/gecko/tabs/TabsLayoutItemView.java
+++ b/mobile/android/base/java/org/mozilla/gecko/tabs/TabsLayoutItemView.java
@@ -1,28 +1,26 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.tabs;
 
-import org.mozilla.gecko.AppConstants;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.Tab;
 import org.mozilla.gecko.Tabs;
-import org.mozilla.gecko.util.HardwareUtils;
 import org.mozilla.gecko.widget.TabThumbnailWrapper;
+import org.mozilla.gecko.widget.TouchDelegateWithReset;
 import org.mozilla.gecko.widget.themed.ThemedRelativeLayout;
 
 import android.content.Context;
 import android.graphics.Rect;
 import android.graphics.drawable.Drawable;
 import android.util.AttributeSet;
 import android.util.TypedValue;
-import android.view.TouchDelegate;
 import android.view.View;
 import android.view.ViewTreeObserver;
 import android.widget.Checkable;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
 public class TabsLayoutItemView extends LinearLayout
@@ -111,17 +109,17 @@ public class TabsLayoutItemView extends 
                 final int targetHitArea = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());;
 
                 final Rect hitRect = new Rect();
                 hitRect.top = 0;
                 hitRect.right = getWidth();
                 hitRect.left = getWidth() - targetHitArea;
                 hitRect.bottom = targetHitArea;
 
-                setTouchDelegate(new TouchDelegate(hitRect, mCloseButton));
+                setTouchDelegate(new TouchDelegateWithReset(hitRect, mCloseButton));
 
                 return true;
             }
         });
     }
 
     protected void assignValues(Tab tab)  {
         if (tab == null) {
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/java/org/mozilla/gecko/widget/TouchDelegateWithReset.java
@@ -0,0 +1,134 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+package org.mozilla.gecko.widget;
+
+import android.graphics.Rect;
+import android.view.MotionEvent;
+import android.view.TouchDelegate;
+import android.view.View;
+import android.view.ViewConfiguration;
+
+/**
+ * This is a copy of TouchDelegate from
+ * https://github.com/android/platform_frameworks_base/blob/4b1a8f46d6ec55796bf77fd8921a5a242a219278/core/java/android/view/TouchDelegate.java
+ * with a fix to reset mDelegateTargeted on each new gesture - the sole substantive change is a new
+ * else leg in the ACTION_DOWN case of onTouchEvent marked by "START|END BUG FIX" comments.
+ */
+
+/**
+ * Helper class to handle situations where you want a view to have a larger touch area than its
+ * actual view bounds. The view whose touch area is changed is called the delegate view. This
+ * class should be used by an ancestor of the delegate. To use a TouchDelegate, first create an
+ * instance that specifies the bounds that should be mapped to the delegate and the delegate
+ * view itself.
+ * <p>
+ * The ancestor should then forward all of its touch events received in its
+ * {@link android.view.View#onTouchEvent(MotionEvent)} to {@link #onTouchEvent(MotionEvent)}.
+ * </p>
+ */
+public class TouchDelegateWithReset extends TouchDelegate {
+
+    /**
+     * View that should receive forwarded touch events
+     */
+    private View mDelegateView;
+
+    /**
+     * Bounds in local coordinates of the containing view that should be mapped to the delegate
+     * view. This rect is used for initial hit testing.
+     */
+    private Rect mBounds;
+
+    /**
+     * mBounds inflated to include some slop. This rect is to track whether the motion events
+     * should be considered to be be within the delegate view.
+     */
+    private Rect mSlopBounds;
+
+    /**
+     * True if the delegate had been targeted on a down event (intersected mBounds).
+     */
+    private boolean mDelegateTargeted;
+
+    private int mSlop;
+
+    /**
+     * Constructor
+     *
+     * @param bounds Bounds in local coordinates of the containing view that should be mapped to
+     *        the delegate view
+     * @param delegateView The view that should receive motion events
+     */
+    public TouchDelegateWithReset(Rect bounds, View delegateView) {
+        super(bounds, delegateView);
+
+        mBounds = bounds;
+
+        mSlop = ViewConfiguration.get(delegateView.getContext()).getScaledTouchSlop();
+        mSlopBounds = new Rect(bounds);
+        mSlopBounds.inset(-mSlop, -mSlop);
+        mDelegateView = delegateView;
+    }
+
+    /**
+     * Will forward touch events to the delegate view if the event is within the bounds
+     * specified in the constructor.
+     *
+     * @param event The touch event to forward
+     * @return True if the event was forwarded to the delegate, false otherwise.
+     */
+    @Override
+    public boolean onTouchEvent(MotionEvent event) {
+        int x = (int)event.getX();
+        int y = (int)event.getY();
+        boolean sendToDelegate = false;
+        boolean hit = true;
+        boolean handled = false;
+
+        switch (event.getAction()) {
+        case MotionEvent.ACTION_DOWN:
+            Rect bounds = mBounds;
+
+            if (bounds.contains(x, y)) {
+                mDelegateTargeted = true;
+                sendToDelegate = true;
+            } /* START BUG FIX */
+            else {
+                mDelegateTargeted = false;
+            }
+            /* END BUG FIX */
+            break;
+        case MotionEvent.ACTION_UP:
+        case MotionEvent.ACTION_MOVE:
+            sendToDelegate = mDelegateTargeted;
+            if (sendToDelegate) {
+                Rect slopBounds = mSlopBounds;
+                if (!slopBounds.contains(x, y)) {
+                    hit = false;
+                }
+            }
+            break;
+        case MotionEvent.ACTION_CANCEL:
+            sendToDelegate = mDelegateTargeted;
+            mDelegateTargeted = false;
+            break;
+        }
+        if (sendToDelegate) {
+            final View delegateView = mDelegateView;
+
+            if (hit) {
+                // Offset event coordinates to be inside the target view
+                event.setLocation(delegateView.getWidth() / 2, delegateView.getHeight() / 2);
+            } else {
+                // Offset event coordinates to be outside the target view (in case it does
+                // something like tracking pressed state)
+                int slop = mSlop;
+                event.setLocation(-(slop * 2), -(slop * 2));
+            }
+            handled = delegateView.dispatchTouchEvent(event);
+        }
+        return handled;
+    }
+}
\ No newline at end of file
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -753,16 +753,17 @@ gbjar.sources += ['java/org/mozilla/geck
     'widget/ResizablePathDrawable.java',
     'widget/RoundedCornerLayout.java',
     'widget/SiteLogins.java',
     'widget/SquaredImageView.java',
     'widget/SquaredRelativeLayout.java',
     'widget/SwipeDismissListViewTouchListener.java',
     'widget/TabThumbnailWrapper.java',
     'widget/ThumbnailView.java',
+    'widget/TouchDelegateWithReset.java',
     'widget/TwoWayView.java',
     'ZoomedView.java',
 ]]
 # The following sources are checked in to version control but
 # generated by a script (java/org/mozilla/gecko/widget/themed/generate_themed_views.py).
 # If you're editing this list, make sure to edit that script.
 gbjar.sources += ['java/org/mozilla/gecko/' + x for x in [
     'widget/themed/ThemedEditText.java',