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);
    }

}
@@ -626,25 +626,24 @@
 ityY) %7B%0A
-%0A
         
 // Grab 
@@ -638,1052 +638,115 @@
     
-// Grab two events located on the plane at e1=(x1, y1) and e2=(x2, y2)%0A        // Let e1 be the initial event%0A        // e2 can be located at 4 different positions, consider the following diagram%0A        // (Assume that lines are separated by 90 degrees.)%0A        //%0A        //%0A        //         %5C A  /%0A        //          %5C  /%0A        //       D   e1   B%0A        //          /  %5C%0A        //         / C  %5C%0A        //%0A        // So if (x2,y2) falls in region:%0A        //  A =%3E it's an UP swipe%0A        //  B =%3E it's a RIGHT swipe%0A        //  C =%3E it's a DOWN swipe%0A        //  D =%3E it's a LEFT swipe%0A        //%0A%0A        float x1 = e1.getX();%0A        float y1 = e1.getY();%0A%0A        float x2 = e2.getX();%0A        float y2 = e2.getY();%0A%0A        double angle = getAngle(x1, y1, x2, y2);%0A        if(inRange(angle, 45, 135))%7B%0A            return onSwipeUp();%0A        %7D%0A        else if(inRange(angle, 0,45) %7C%7C inRange(angle, 315, 360))%7B%0A            return onSwipeRight();%0A        %7D%0A        else if(inRange(angle, 225, 315))%7B%0A            return
+if (Math.abs(velocityX) %3E Math.abs(velocityY)) %7B%0A            return velocityX %3C 0 ? onSwipeLeft() :
  onSwipe
 Down
@@ -741,20 +741,21 @@
  onSwipe
-Down
+Right
 ();%0A    
@@ -763,21 +763,14 @@
    %7D
-%0A       
  else
+ 
 %7B%0A  
@@ -790,1009 +790,62 @@
 urn 
-onSwipeLeft();%0A        %7D%0A    %7D%0A%0A    /**%0A     *%0A     * Finds the angle between two points in the plane (x1,y1) and (x2, y2)%0A     * The angle is measured with 0/360 being the X-axis to the right, angles%0A     * increase counter clockwise.%0A     *%0A     * @param x1 the x position of the first point%0A     * @param y1 the y position of the first point%0A     * @param x2 the x position of the second point%0A     * @param y2 the y position of the second point%0A     * @return the angle between two points%0A     */%0A    private static double getAngle(float x1, float y1, float x2, float y2) %7B%0A        double rad = Math.atan2(y1-y2,x2-x1) + Math.PI;%0A        return (rad*180/Math.PI + 180)%25360;%0A    %7D%0A%0A    /**%0A     * @param angle an angle%0A     * @param init the initial bound%0A     * @param end the final bound%0A     * @return returns true if the given angle is in the interval %5Binit, end).%0A     */%0A    private static boolean inRange(double angle, float init, float end)%7B%0A        return (angle %3E= init) && (angle %3C end);
+velocityY %3C 0 ? onSwipeUp() : onSwipeDown();%0A        %7D
 %0A   

Fix possible NPE in OnSwipeListener