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

Commit e2236797 authored by Justin Klaassen's avatar Justin Klaassen
Browse files

Track pointer index in AlarmActivity#onTouchEvent

Bug: 25566916

We need to track the initial pointer index used to initiate the snooze
or dismiss gesture, otherwise we risk performing the wrong action if
another pointer is present when the user finishes the gesture.

Change-Id: I2f3a0a11fc5e4571fc1dcbe741de432b91815ba5
parent f8e562f6
Loading
Loading
Loading
Loading
+53 −37
Original line number Diff line number Diff line
@@ -143,6 +143,8 @@ public class AlarmActivity extends AppCompatActivity
    private ValueAnimator mDismissAnimator;
    private ValueAnimator mPulseAnimator;

    private int mInitialPointerIndex = MotionEvent.INVALID_POINTER_ID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
@@ -340,17 +342,43 @@ public class AlarmActivity extends AppCompatActivity
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
    public boolean onTouch(View view, MotionEvent event) {
        if (mAlarmHandled) {
            LogUtils.v(LOGTAG, "onTouch ignored: %s", motionEvent);
            LogUtils.v(LOGTAG, "onTouch ignored: %s", event);
            return false;
        }

        final int action = event.getActionMasked();
        if (action == MotionEvent.ACTION_DOWN) {
            LogUtils.v(LOGTAG, "onTouch started: %s", event);

            // Track the pointer that initiated the touch sequence.
            mInitialPointerIndex = event.getPointerId(event.getActionIndex());

            // Stop the pulse, allowing the last pulse to finish.
            mPulseAnimator.setRepeatCount(0);
        } else if (action == MotionEvent.ACTION_CANCEL) {
            LogUtils.v(LOGTAG, "onTouch canceled: %s", event);

            // Clear the pointer index.
            mInitialPointerIndex = MotionEvent.INVALID_POINTER_ID;

            // Reset everything.
            resetAnimations();
        }

        final int actionIndex = event.getActionIndex();
        if (mInitialPointerIndex == MotionEvent.INVALID_POINTER_ID
                || mInitialPointerIndex != event.getPointerId(actionIndex)) {
            // Ignore any pointers other than the initial one, bail early.
            return true;
        }

        final int[] contentLocation = {0, 0};
        mContentView.getLocationOnScreen(contentLocation);

        final float x = motionEvent.getRawX() - contentLocation[0];
        final float y = motionEvent.getRawY() - contentLocation[1];
        final float x = event.getRawX() - contentLocation[0];
        final float y = event.getRawY() - contentLocation[1];

        final int alarmLeft = mAlarmButton.getLeft() + mAlarmButton.getPaddingLeft();
        final int alarmRight = mAlarmButton.getRight() - mAlarmButton.getPaddingRight();
@@ -365,16 +393,10 @@ public class AlarmActivity extends AppCompatActivity
        }
        setAnimatedFractions(snoozeFraction, dismissFraction);

        switch (motionEvent.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                LogUtils.v(LOGTAG, "onTouch started: %s", motionEvent);

                // Stop the pulse, allowing the last pulse to finish.
                mPulseAnimator.setRepeatCount(0);
                break;
            case MotionEvent.ACTION_UP:
                LogUtils.v(LOGTAG, "onTouch ended: %s", motionEvent);
        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP) {
            LogUtils.v(LOGTAG, "onTouch ended: %s", event);

            mInitialPointerIndex = event.INVALID_POINTER_ID;
            if (snoozeFraction == 1.0f) {
                snooze();
            } else if (dismissFraction == 1.0f) {
@@ -384,7 +406,7 @@ public class AlarmActivity extends AppCompatActivity
                    // Animate back to the initial state.
                    AnimatorUtils.reverse(mAlarmAnimator, mSnoozeAnimator, mDismissAnimator);
                } else if (mAlarmButton.getTop() <= y && y <= mAlarmButton.getBottom()) {
                        // User touched the alarm button, hint the dismiss action
                    // User touched the alarm button, hint the dismiss action.
                    hintDismiss();
                }

@@ -394,12 +416,6 @@ public class AlarmActivity extends AppCompatActivity
                    mPulseAnimator.start();
                }
            }
                break;
            case MotionEvent.ACTION_CANCEL:
                resetAnimations();
                break;
            default:
                break;
        }

        return true;