package uk.org.ngo.squeezer.widget;

import android.view.GestureDetector;
import android.view.MotionEvent;

public class OnSwipeListener extends GestureDetector.SimpleOnGestureListener {

    public boolean onSwipeUp(){
        return false;
    }

    public boolean onSwipeDown(){
        return false;
    }

    public boolean onSwipeLeft(){
        return false;
    }

    public boolean onSwipeRight(){
        return false;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

        // Grab two events located on the plane at e1=(x1, y1) and e2=(x2, y2)
        // Let e1 be the initial event
        // e2 can be located at 4 different positions, consider the following diagram
        // (Assume that lines are separated by 90 degrees.)
        //
        //
        //         \ A  /
        //          \  /
        //       D   e1   B
        //          /  \
        //         / C  \
        //
        // So if (x2,y2) falls in region:
        //  A => it's an UP swipe
        //  B => it's a RIGHT swipe
        //  C => it's a DOWN swipe
        //  D => it's a LEFT swipe
        //

        float x1 = e1.getX();
        float y1 = e1.getY();

        float x2 = e2.getX();
        float y2 = e2.getY();

        double angle = getAngle(x1, y1, x2, y2);
        if(inRange(angle, 45, 135)){
            return onSwipeUp();
        }
        else if(inRange(angle, 0,45) || inRange(angle, 315, 360)){
            return onSwipeRight();
        }
        else if(inRange(angle, 225, 315)){
            return onSwipeDown();
        }
        else{
            return onSwipeLeft();
        }
    }

    /**
     *
     * Finds the angle between two points in the plane (x1,y1) and (x2, y2)
     * The angle is measured with 0/360 being the X-axis to the right, angles
     * increase counter clockwise.
     *
     * @param x1 the x position of the first point
     * @param y1 the y position of the first point
     * @param x2 the x position of the second point
     * @param y2 the y position of the second point
     * @return the angle between two points
     */
    private static double getAngle(float x1, float y1, float x2, float y2) {
        double rad = Math.atan2(y1-y2,x2-x1) + Math.PI;
        return (rad*180/Math.PI + 180)%360;
    }

    /**
     * @param angle an angle
     * @param init the initial bound
     * @param end the final bound
     * @return returns true if the given angle is in the interval [init, end).
     */
    private static boolean inRange(double angle, float init, float end){
        return (angle >= init) && (angle < end);
    }

}
package uk.org.ngo.squeezer.widget;

import android.view.GestureDetector;
import android.view.MotionEvent;

public class OnSwipeListener extends GestureDetector.SimpleOnGestureListener {

    public boolean onSwipeUp(){
        return false;
    }

    public boolean onSwipeDown(){
        return false;
    }

    public boolean onSwipeLeft(){
        return false;
    }

    public boolean onSwipeRight(){
        return false;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if (Math.abs(velocityX) > Math.abs(velocityY)) {
            return velocityX < 0 ? onSwipeLeft() : onSwipeRight();
        } else {
            return velocityY < 0 ? onSwipeUp() : onSwipeDown();
        }
    }

}
Fix possible NPE in OnSwipeListener