package com.naman14.timber.utils; import android.support.annotation.NonNull; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import com.naman14.timber.MusicPlayer; /** * Created by nv95 on 02.11.16. */ public class SlideTrackSwitcher implements View.OnTouchListener { private static final int SWIPE_THRESHOLD = 200; private static final int SWIPE_VELOCITY_THRESHOLD = 100; private GestureDetector mDetector; private View mView; public SlideTrackSwitcher() { } public void attach(@NonNull View v) { mView = v; mDetector = new GestureDetector(v.getContext(), new SwipeListener()); v.setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { return mDetector.onTouchEvent(event); } private class SwipeListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { boolean result = false; try { float diffY = e2.getY() - e1.getY(); float diffX = e2.getX() - e1.getX(); if (Math.abs(diffX) > Math.abs(diffY)) { if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { if (diffX > 0) { onSwipeRight(); } else { onSwipeLeft(); } } result = true; } else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { if (diffY > 0) { onSwipeBottom(); } else { onSwipeTop(); } } result = true; } catch (Exception exception) { exception.printStackTrace(); } return result; } @Override public boolean onDown(MotionEvent e) { return true; } @Override public boolean onDoubleTap(MotionEvent e) { MusicPlayer.playOrPause(); return true; } @Override public boolean onSingleTapConfirmed(MotionEvent e) { onClick(); return super.onSingleTapConfirmed(e); } } public void onSwipeRight() { MusicPlayer.previous(mView.getContext(), true); } public void onSwipeLeft() { MusicPlayer.next(); } public void onSwipeTop() { } public void onSwipeBottom() { } public void onClick() { } }