package edu.berkeley.cs.amplab.carat.android;

import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class SwipeListener extends BaseSwipeListener {

	public static SwipeListener instance = new SwipeListener();

	public boolean handleUp(View v, MotionEvent ev) {
		float w = v.getWidth();
		float x = ev.getX();
		float y = ev.getY();

		boolean left = true;
		float xDiff = x - oldX;
		if (x < oldX) {
			// finger moves left, ui moves right
			xDiff = oldX - x;
			left = false;
		}
		
		float yDiff = y - oldY;
		if (y < oldY) {
			yDiff = oldY - y;
		}

		if (xDiff > w / 3.0 && xDiff > yDiff) {
			// moved horizontally. Lets change tabs to the right direction:
			if (left && currentTab > 0) {
				CaratMainActivity.changeTab(currentTab - 1);
				return true;
			} else if (!left && currentTab + 1 < CaratMainActivity.tabHost.getTabWidget().getTabCount()) {
				CaratMainActivity.changeTab(currentTab + 1);
				return true;
			} else
				return false;
		}
		return false;
	}
}

abstract class BaseSwipeListener implements OnTouchListener {

	float oldX = 0;
	float oldY = 0;
	
	int currentTab = 0;

	@Override
	public boolean onTouch(View v, MotionEvent ev) {
		int action = ev.getActionMasked();
		if (action == MotionEvent.ACTION_DOWN) {
			currentTab = CaratMainActivity.tabHost.getCurrentTab();
			oldX = ev.getX();
			oldY = ev.getY();
			// Fix swipe not working on fake bugs/hogs screens:
			if (currentTab == 3 || currentTab == 4)
				return true;
			return false;
		} else if (action == MotionEvent.ACTION_UP) {
			currentTab = CaratMainActivity.tabHost.getCurrentTab();
			return handleUp(v, ev);
		}
		return false;
	}

	public abstract boolean handleUp(View v, MotionEvent ev);
}

class BackSwipeListener extends BaseSwipeListener {

	Activity a = null;

	public BackSwipeListener(Activity a) {
		this.a = a;
	}

	@Override
	public boolean handleUp(View v, MotionEvent ev) {
		int w = v.getWidth();
		float x = ev.getX();
		float y = ev.getY();

		boolean left = true;
		float xDiff = x - oldX;
		if (x < oldX) {
			xDiff = oldX - x;
			left = false;
		}
		
		float yDiff = y - oldY;
		if (y < oldY) {
			yDiff = oldY - y;
		}

		if (xDiff > w / 3.0 && xDiff > yDiff) {
			// moved horizontally. Lets change tabs to the right direction:
			if (left) {
				a.finish();
				return true;
			}
		}
		return false;
	}
}

@@ -169,16 +169,51 @@
 istener;
+%0Aimport android.widget.ViewFlipper;
 %0A%0Apublic
@@ -2425,28 +2425,890 @@
 %09%7D%0A%09%09%7D%0A%09%09return false;%0A%09%7D%0A%7D%0A
+%0Aclass FlipperBackListener extends BaseSwipeListener %7B%0A%0A%09ViewFlipper vf = null;%0A%09int backViewIndex = 0;%0A%0A%09public FlipperBackListener(ViewFlipper vf, int backViewIndex) %7B%0A%09%09this.vf = vf;%0A%09%09this.backViewIndex = backViewIndex;%0A%09%7D%0A%0A%09@Override%0A%09public boolean handleUp(View v, MotionEvent ev) %7B%0A%09%09int w = v.getWidth();%0A%09%09float x = ev.getX();%0A%09%09float y = ev.getY();%0A%0A%09%09boolean left = true;%0A%09%09float xDiff = x - oldX;%0A%09%09if (x %3C oldX) %7B%0A%09%09%09xDiff = oldX - x;%0A%09%09%09left = false;%0A%09%09%7D%0A%09%09%0A%09%09float yDiff = y - oldY;%0A%09%09if (y %3C oldY) %7B%0A%09%09%09yDiff = oldY - y;%0A%09%09%7D%0A%0A%09%09if (xDiff %3E w / 3.0 && xDiff %3E yDiff) %7B%0A%09%09%09// moved horizontally. Lets change tabs to the right direction:%0A%09%09%09if (left) %7B%0A%09%09%09%09vf.setOutAnimation(CaratMainActivity.outtoRight);%0A%09%09%09%09vf.setInAnimation(CaratMainActivity.inFromLeft);%0A%09%09%09%09vf.setDisplayedChild(backViewIndex);%0A%09%09%09%09return true;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return false;%0A%09%7D%0A%7D%0A

Allow back swipe for ViewFlipper type views.