Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c5b6e4fc authored by Annie Chin's avatar Annie Chin
Browse files

Fix touch handling in DragLayout.

Fixes: 33104074

-Only accept drags on HistoryFrame when isOpen()
-requestDisallowInterceptTouchEvent() in CalculatorResult to prevent
DragLayout from scrolling when a CalculatorResult is the target

Change-Id: If0dfbccc2fc05a90383a03cf397e6becd4496554
parent 1c4ad119
Loading
Loading
Loading
Loading
+4 −6
Original line number Diff line number Diff line
@@ -240,14 +240,12 @@ public class Calculator extends Activity
            // no-op
        }

        @Override
        public boolean allowDrag(MotionEvent event) {
            return isViewTarget(mHistoryFrame, event) || isViewTarget(mDisplayView, event);
        }

        @Override
        public boolean shouldInterceptTouchEvent(MotionEvent event) {
            return isViewTarget(mHistoryFrame, event) || isViewTarget(mDisplayView, event);
            if (!mDragLayout.isOpen()) {
                return isViewTarget(mDisplayView, event);
            }
            return true;
        }

        @Override
+27 −4
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.OverScroller;
import android.widget.Toast;

@@ -153,6 +154,10 @@ public class CalculatorResult extends AlignedTextView implements MenuItem.OnMenu
    private ActionMode.Callback mCopyActionModeCallback;
    private ContextMenu mContextMenu;

    // Used to determine whether a touch event should be intercepted.
    private float mInitialDownX;
    private float mInitialDownY;

    // The user requested that the result currently being evaluated should be stored to "memory".
    private boolean mStoreToMemoryRequested = false;

@@ -169,8 +174,8 @@ public class CalculatorResult extends AlignedTextView implements MenuItem.OnMenu
                    return true;
                }
                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2,
                                       float velocityX, float velocityY) {
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                        float velocityY) {
                    if (!mScroller.isFinished()) {
                        mCurrentPos = mScroller.getFinalX();
                    }
@@ -185,8 +190,8 @@ public class CalculatorResult extends AlignedTextView implements MenuItem.OnMenu
                    return true;
                }
                @Override
                public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                        float distanceX, float distanceY) {
                public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                        float distanceY) {
                    int distance = (int)distanceX;
                    if (!mScroller.isFinished()) {
                        mCurrentPos = mScroller.getFinalX();
@@ -216,6 +221,24 @@ public class CalculatorResult extends AlignedTextView implements MenuItem.OnMenu
        setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final int action = event.getActionMasked();

                final float x = event.getX();
                final float y = event.getY();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        mInitialDownX = x;
                        mInitialDownY = y;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        final float deltaX = Math.abs(x - mInitialDownX);
                        final float deltaY = Math.abs(y - mInitialDownY);
                        final int slop = ViewConfiguration.get(v.getContext()).getScaledTouchSlop();
                        if (deltaX > slop && deltaX > deltaY) {
                            // Prevent the DragLayout from intercepting horizontal scrolls.
                            getParent().requestDisallowInterceptTouchEvent(true);
                        }
                }
                return mGestureDetector.onTouchEvent(event);
            }
        });
+15 −20
Original line number Diff line number Diff line
@@ -108,11 +108,17 @@ public class DragLayout extends RelativeLayout {

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        // First verify that we don't have a large deltaX (that the user is not trying to
        // horizontally scroll).
        final int action = event.getActionMasked();

        // Always handle the case of the touch gesture being complete.
        if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
            // Release the scroll.
            mDragHelper.cancel();
            return false; // Do not intercept touch event, let the child handle it
        }

        final float x = event.getX();
        final float y = event.getY();
        final int action = event.getAction();

        switch (action) {
            case MotionEvent.ACTION_DOWN:
@@ -123,32 +129,24 @@ public class DragLayout extends RelativeLayout {
                final float deltaX = Math.abs(x - mInitialDownX);
                final float deltaY = Math.abs(y - mInitialDownY);
                final int slop = mDragHelper.getTouchSlop();
                if (deltaY > slop && deltaX > deltaY) {
                    mDragHelper.cancel();
                if (deltaY > slop && deltaY > deltaX) {
                    break;
                } else {
                    return false;
                }
        }

        boolean doDrag = true;
        for (DragCallback c : mDragCallbacks) {
            doDrag &= c.allowDrag(event);
            doDrag &= c.shouldInterceptTouchEvent(event);
        }
        return doDrag && mDragHelper.shouldInterceptTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean doIntercept = true;
        for (DragCallback c : mDragCallbacks) {
            doIntercept &= c.shouldInterceptTouchEvent(event);
        }
        if (doIntercept || isMoving()) {
        mDragHelper.processTouchEvent(event);
            return true;
        } else {
        return super.onTouchEvent(event);
    }
    }

    @Override
    public void computeScroll() {
@@ -215,9 +213,6 @@ public class DragLayout extends RelativeLayout {
        // Animate the RecyclerView text.
        void whileDragging(float yFraction);

        // Whether we should allow the drag to happen
        boolean allowDrag(MotionEvent event);

        // Whether we should intercept the touch event
        boolean shouldInterceptTouchEvent(MotionEvent event);

+1 −7
Original line number Diff line number Diff line
@@ -51,15 +51,9 @@ public class HistoryFragment extends Fragment {
                    mDragController.animateViews(yFraction, mRecyclerView);
                }

                @Override
                public boolean allowDrag(MotionEvent event) {
                    // Do not allow drag if the recycler view can move down more
                    return !mRecyclerView.canScrollVertically(1);
                }

                @Override
                public boolean shouldInterceptTouchEvent(MotionEvent event) {
                    return true;
                    return !mRecyclerView.canScrollVertically(1);
                }

                @Override