From d2d463d0f22ba610a9b458213373c161d6e17c91 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Thu, 30 Mar 2017 17:18:07 +0300 Subject: [PATCH] Fix #3053 --- .../MapContextMenuFragment.java | 7 ++- .../controls/HorizontalSwipeConfirm.java | 57 +++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/views/controls/HorizontalSwipeConfirm.java diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java index 72b1695559..02e101526b 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java @@ -18,8 +18,6 @@ import android.view.VelocityTracker; import android.view.View; import android.view.ViewGroup; import android.view.ViewTreeObserver; -import android.view.animation.Animation; -import android.view.animation.AnimationUtils; import android.view.animation.DecelerateInterpolator; import android.widget.Button; import android.widget.FrameLayout; @@ -47,6 +45,7 @@ import net.osmand.plus.mapcontextmenu.MenuController.TitleProgressController; import net.osmand.plus.mapcontextmenu.other.MapRouteInfoMenu; import net.osmand.plus.views.AnimateDraggingMapThread; import net.osmand.plus.views.OsmandMapTileView; +import net.osmand.plus.views.controls.HorizontalSwipeConfirm; import net.osmand.plus.views.controls.SingleTapConfirm; import net.osmand.util.Algorithms; @@ -241,6 +240,7 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents { runLayoutListener(); final GestureDetector singleTapDetector = new GestureDetector(view.getContext(), new SingleTapConfirm()); + final GestureDetector swipeDetector = new GestureDetector(view.getContext(), new HorizontalSwipeConfirm(true)); final View.OnTouchListener slideTouchListener = new View.OnTouchListener() { private float dy; @@ -285,6 +285,9 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents { } if (menu.isLandscapeLayout()) { + if (swipeDetector.onTouchEvent(event)) { + menu.close(); + } return true; } diff --git a/OsmAnd/src/net/osmand/plus/views/controls/HorizontalSwipeConfirm.java b/OsmAnd/src/net/osmand/plus/views/controls/HorizontalSwipeConfirm.java new file mode 100644 index 0000000000..2f72050648 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/views/controls/HorizontalSwipeConfirm.java @@ -0,0 +1,57 @@ +package net.osmand.plus.views.controls; + +import android.view.GestureDetector; +import android.view.MotionEvent; + +public class HorizontalSwipeConfirm implements GestureDetector.OnGestureListener { + + private static final int SWIPE_MIN_DISTANCE = 120; + private static final int SWIPE_MAX_OFF_PATH = 250; + private static final int SWIPE_THRESHOLD_VELOCITY = 200; + + private boolean rightToLeftSwipe; + + public HorizontalSwipeConfirm(boolean rightToLeftSwipe) { + this.rightToLeftSwipe = rightToLeftSwipe; + } + + @Override + public boolean onDown(MotionEvent e) { + return false; + } + + @Override + public void onShowPress(MotionEvent e) { + } + + @Override + public boolean onSingleTapUp(MotionEvent e) { + return false; + } + + @Override + public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { + return false; + } + + @Override + public void onLongPress(MotionEvent e) { + } + + @Override + public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { + + if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) { + return false; + } + // right to left swipe + if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { + return rightToLeftSwipe; + } + // left to right swipe + else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { + return !rightToLeftSwipe; + } + return false; + } +} \ No newline at end of file