0d8874d7ea1b3b42be0b7a7758a7ca9ef501f286
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / OnSwipeTouchListener.java
1 package com.owncloud.android.ui;
2
3 import android.content.Context;
4 import android.util.Log;
5 import android.view.GestureDetector;
6 import android.view.GestureDetector.SimpleOnGestureListener;
7 import android.view.MotionEvent;
8 import android.view.View;
9 import android.view.View.OnTouchListener;
10 import android.widget.Toast;
11
12 public class OnSwipeTouchListener implements OnTouchListener {
13
14 private final Context mContext;
15 private final GestureDetector mGestureDetector;
16
17 public OnSwipeTouchListener(Context context) {
18 mContext = context;
19 mGestureDetector = new GestureDetector(context, new GestureListener());
20 }
21
22 public boolean onTouch(final View v, final MotionEvent event) {
23 //super.onTouch(v, event);
24 Log.d("SWIPE", "Swipe listener touched");
25 return mGestureDetector.onTouchEvent(event);
26 }
27
28 private final class GestureListener extends SimpleOnGestureListener {
29
30 private static final int SWIPE_THRESHOLD = 100;
31 private static final int SWIPE_VELOCITY_THRESHOLD = 100;
32
33 @Override
34 public boolean onDown(MotionEvent e) {
35 return true;
36 }
37
38 @Override
39 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
40 boolean result = false;
41 try {
42 float diffY = e2.getY() - e1.getY();
43 float diffX = e2.getX() - e1.getX();
44 if (Math.abs(diffX) > Math.abs(diffY)) {
45 if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
46 if (diffX > 0) {
47 onSwipeRight();
48 } else {
49 onSwipeLeft();
50 }
51 }
52 } else {
53 if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
54 if (diffY > 0) {
55 onSwipeBottom();
56 } else {
57 onSwipeTop();
58 }
59 }
60 }
61 } catch (Exception exception) {
62 exception.printStackTrace();
63 }
64 return result;
65 }
66 }
67
68 public void onSwipeTop() {
69 Toast.makeText(mContext, "top", Toast.LENGTH_SHORT).show();
70 }
71 public void onSwipeRight() {
72 Toast.makeText(mContext, "right", Toast.LENGTH_SHORT).show();
73 }
74 public void onSwipeLeft() {
75 Toast.makeText(mContext, "left", Toast.LENGTH_SHORT).show();
76 }
77 public void onSwipeBottom() {
78 Toast.makeText(mContext, "bottom", Toast.LENGTH_SHORT).show();
79 }
80
81 }
82