package com.hilton.gitatouille;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.webkit.WebView;

public class CustomizedWebView extends WebView {
    private static final float EPISODE = 5.0f;
    private OnScrollListener mOnScrollListener;
    
    public CustomizedWebView(Context context) {
        super(context);
        initialize();
    }

    public CustomizedWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public CustomizedWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initialize();
    }
    
    public void setScrollListener(OnScrollListener l) {
        mOnScrollListener = l;
    }
    
    private float mStartY;
    private float mStartX;
    private boolean mScrollingDown;
    private boolean mScrollingUp;
    private boolean mScrollingLeft;
    private boolean mScrollingRight;
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final float currentX = event.getX();
        final float currentY = event.getY();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mStartX = event.getX();
            mStartY = event.getY();
            break;
        case MotionEvent.ACTION_MOVE: {
            if ((currentX - mStartX) > EPISODE) {
                if (!mScrollingLeft) {
                    mScrollingLeft = true;
                    mScrollingRight = false;
                    mScrollingUp = false;
                    mScrollingDown = false;
                    mOnScrollListener.scrollLeft();
                }
                break;
            } else if ((mStartX - currentX) > EPISODE) {
                if (!mScrollingRight) {
                    mScrollingRight = true;
                    mScrollingLeft = false;
                    mScrollingDown = false;
                    mScrollingUp = false;
                    mOnScrollListener.scrollRight();
                }
                break;
            }
            if ((currentY - mStartY) > EPISODE) {
                if (!mScrollingDown) {
                    mScrollingDown = true;
                    mScrollingUp = false;
                    mScrollingRight = false;
                    mScrollingLeft = false;
                    mOnScrollListener.scrollUp();
                }
                break;
            } else if ((mStartY - currentY) > EPISODE) {
                if (!mScrollingUp) {
                    mScrollingUp = true;
                    mScrollingDown = false;
                    mScrollingRight = false;
                    mScrollingLeft = false;
                    mOnScrollListener.scrollDown();
                }
                break;
            }
            break;
        }
        case MotionEvent.ACTION_UP:
            initialize();
            break;
        }
        return super.onTouchEvent(event);
    }

    private void initialize() {
        mScrollingDown = false;
        mScrollingUp = false;
        mScrollingLeft = false;
        mScrollingRight = false;
        mStartY = Float.NaN;
        mStartX = Float.NaN;
    }
    
    public interface OnScrollListener {
        public void scrollDown();
        public void scrollUp();
        public void scrollLeft();
        public void scrollRight();
    }
}
@@ -237,16 +237,18 @@
 l float 
+Y_
 EPISODE 
@@ -253,60 +253,169 @@
 E = 
-5.0f;%0A    private OnScrollListener mOnScrollListener
+10.0f;%0A    private static final float X_EPISODE = 30.0f;%0A    private OnScrollListener mOnScrollListener;%0A    private float mXEpisode;%0A    private float mYEpisode
 ;%0A  
@@ -1481,42 +1481,181 @@
     
-if ((currentX - mStartX) %3E EPISODE
+final float dx = Math.abs(currentX - mStartX);%0A            final float dy = Math.abs(currentY - mStartY);%0A            if ((currentX - mStartX) %3E mXEpisode && dy %3C= mYEpisode
 ) %7B%0A
@@ -2002,31 +2002,52 @@
 currentX) %3E 
-EPISODE
+mXEpisode && dy %3C= mYEpisode
 ) %7B%0A        
@@ -2395,31 +2395,52 @@
  mStartY) %3E 
-EPISODE
+mYEpisode && dx %3C= mXEpisode
 ) %7B%0A        
@@ -2781,23 +2781,44 @@
 entY) %3E 
-EPISODE
+mYEpisode && dx %3C= mXEpisode
 ) %7B%0A    
@@ -3513,24 +3513,202 @@
  Float.NaN;%0A
+        mXEpisode = getContext().getResources().getDisplayMetrics().density * X_EPISODE;%0A        mYEpisode = getContext().getResources().getDisplayMetrics().density * Y_EPISODE;%0A
     %7D%0A    %0A 

Improve the gesture experiences.