2 * Copyright © 2019 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui;
20 import android.content.Context;
21 import android.view.GestureDetector;
22 import android.view.GestureDetector.SimpleOnGestureListener;
23 import android.view.MotionEvent;
24 import android.view.View;
26 import static net.ktnx.mobileledger.utils.Logger.debug;
28 public abstract class OnSwipeTouchListener implements View.OnTouchListener {
29 public final GestureDetector gestureDetector;
31 protected OnSwipeTouchListener(Context ctx) {
32 gestureDetector = new GestureDetector(ctx, new GestureListener() );
35 private final class GestureListener extends SimpleOnGestureListener {
36 private static final int SWIPE_THRESHOLD = 100;
37 private static final int SWIPE_VELOCITY_THRESHOLD = 100;
40 public boolean onDown(MotionEvent e) {
41 debug("sw-l", "onDown");
46 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
47 boolean result = false;
49 debug("sw-l", "onFling");
52 float diffX = e2.getX() - e1.getX();
53 float diffY = e2.getY() - e1.getY();
54 if (Math.abs(diffX) > Math.abs(diffY)) {
55 if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
57 debug("sw-l", "calling onSwipeRight");
61 debug("sw-l", "calling onSwipeLeft");
67 else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
85 public void onSwipeRight() {}
86 public void onSwipeLeft() {
87 debug("sw-l", "LEFT");
89 public void onSwipeUp() {}
90 public void onSwipeDown() {}