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

Commit 8e367265 authored by Justin Klaassen's avatar Justin Klaassen
Browse files

Intercept touches from secondary pointers

Fixes: 33480687
Test: manually verified secondary pointers are intercepted
Change-Id: Ie70a3cdb2ca79483add5ed07d214f98d3959e322
parent c4dce219
Loading
Loading
Loading
Loading
+27 −19
Original line number Diff line number Diff line
@@ -188,38 +188,46 @@ public class CalculatorPadViewPager extends ViewPager {
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // Always intercept touch events when a11y focused since otherwise they will be
        // incorrectly offset by a11y before being dispatched to children.
        boolean shouldIntercept = isAccessibilityFocused() || super.onInterceptTouchEvent(ev);
        if (isAccessibilityFocused() || super.onInterceptTouchEvent(ev)) {
            return true;
        }

        // Only allow the current item to receive touch events.
        if (!shouldIntercept && ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
            final int x = (int) ev.getX() + getScrollX();
            final int y = (int) ev.getY() + getScrollY();
        final int action = ev.getActionMasked();
        if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN) {
            // If a child is a11y focused then we must always intercept the touch event
            // since it will be incorrectly offset by a11y.
            final int childCount = getChildCount();
            for (int childIndex = childCount - 1; childIndex >= 0; --childIndex) {
                if (getChildAt(childIndex).isAccessibilityFocused()) {
                    mClickedItemIndex = childIndex;
                    return true;
                }
            }

            // Reset the previously clicked item index.
            if (action == MotionEvent.ACTION_DOWN) {
                mClickedItemIndex = -1;
            }

            final int childCount = getChildCount();
            // Otherwise if touch is on a non-current item then intercept.
            final int actionIndex = ev.getActionIndex();
            final float x = ev.getX(actionIndex) + getScrollX();
            final float y = ev.getY(actionIndex) + getScrollY();
            for (int i = childCount - 1; i >= 0; --i) {
                final int childIndex = getChildDrawingOrder(childCount, i);
                final View child = getChildAt(childIndex);
                if (child.isAccessibilityFocused()) {
                    // If a child is a11y focused then we must always intercept the touch event
                    // since it will be incorrectly offset by a11y.
                    shouldIntercept = true;
                    mClickedItemIndex = childIndex;
                    break;
                } else if (mClickedItemIndex == -1
                        && child.getVisibility() == VISIBLE
                if (child.getVisibility() == VISIBLE
                        && x >= child.getLeft() && x < child.getRight()
                        && y >= child.getTop() && y < child.getBottom()) {
                    shouldIntercept = childIndex != getCurrentItem();
                    if (action == MotionEvent.ACTION_DOWN) {
                        mClickedItemIndex = childIndex;
                    // continue; since another child may be a11y focused.
                    }
                    return childIndex != getCurrentItem();
                }
            }
        }

        return shouldIntercept;
        return false;
    }

    @Override