From c084b4e7508c3ae90baed18d736da0d86500cdc9 Mon Sep 17 00:00:00 2001 From: Saeed Mozaffari Date: Wed, 4 Oct 2017 11:30:27 +0330 Subject: [PATCH] Possibility to determine the edge touch level --- .../swipebackfragment/SwipeBackActivity.java | 8 +++ .../swipebackfragment/SwipeBackFragment.java | 14 ++++++ .../swipebackfragment/SwipeBackLayout.java | 50 ++++++++++++++++++- 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/swipeback/src/main/java/me/yokeyword/swipebackfragment/SwipeBackActivity.java b/swipeback/src/main/java/me/yokeyword/swipebackfragment/SwipeBackActivity.java index f9a235c..d313de7 100644 --- a/swipeback/src/main/java/me/yokeyword/swipebackfragment/SwipeBackActivity.java +++ b/swipeback/src/main/java/me/yokeyword/swipebackfragment/SwipeBackActivity.java @@ -48,6 +48,14 @@ void onActivityCreate() { mSwipeBackLayout.setLayoutParams(params); } + protected void setEdgeLevel(SwipeBackLayout.EdgeLevel edgeLevel) { + mSwipeBackLayout.setEdgeLevel(edgeLevel); + } + + protected void setEdgeLevel(int widthPixel) { + mSwipeBackLayout.setEdgeLevel(widthPixel); + } + public SwipeBackLayout getSwipeBackLayout() { return mSwipeBackLayout; } diff --git a/swipeback/src/main/java/me/yokeyword/swipebackfragment/SwipeBackFragment.java b/swipeback/src/main/java/me/yokeyword/swipebackfragment/SwipeBackFragment.java index 0b18058..525ffb5 100755 --- a/swipeback/src/main/java/me/yokeyword/swipebackfragment/SwipeBackFragment.java +++ b/swipeback/src/main/java/me/yokeyword/swipebackfragment/SwipeBackFragment.java @@ -69,6 +69,20 @@ protected View attachToSwipeBack(View view) { return mSwipeBackLayout; } + protected View attachToSwipeBack(View view, SwipeBackLayout.EdgeLevel edgeLevel) { + mSwipeBackLayout.attachToFragment(this, view); + mSwipeBackLayout.setEdgeLevel(edgeLevel); + return mSwipeBackLayout; + } + + protected void setEdgeLevel(SwipeBackLayout.EdgeLevel edgeLevel) { + mSwipeBackLayout.setEdgeLevel(edgeLevel); + } + + protected void setEdgeLevel(int widthPixel) { + mSwipeBackLayout.setEdgeLevel(widthPixel); + } + @Override public void onHiddenChanged(boolean hidden) { super.onHiddenChanged(hidden); diff --git a/swipeback/src/main/java/me/yokeyword/swipebackfragment/SwipeBackLayout.java b/swipeback/src/main/java/me/yokeyword/swipebackfragment/SwipeBackLayout.java index c17078e..a0dd626 100644 --- a/swipeback/src/main/java/me/yokeyword/swipebackfragment/SwipeBackLayout.java +++ b/swipeback/src/main/java/me/yokeyword/swipebackfragment/SwipeBackLayout.java @@ -11,13 +11,15 @@ import android.support.v4.view.ViewCompat; import android.support.v4.widget.ViewDragHelper; import android.util.AttributeSet; +import android.util.DisplayMetrics; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; +import android.view.WindowManager; import android.widget.FrameLayout; - import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; @@ -84,6 +86,13 @@ public class SwipeBackLayout extends FrameLayout { private boolean mEnable = true; private int mCurrentSwipeOrientation; + private Context context; + private EdgeLevel edgeLevel; + + public enum EdgeLevel { + MAX, MIN, MED + } + /** * The set of listeners to be sent events through. */ @@ -99,6 +108,7 @@ public SwipeBackLayout(Context context, AttributeSet attrs) { public SwipeBackLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); + this.context = context; init(); } @@ -140,6 +150,44 @@ public void setEdgeOrientation(int orientation) { } } + public void setEdgeLevel(EdgeLevel edgeLevel) { + this.edgeLevel = edgeLevel; + validateEdgeLevel(0, edgeLevel); + } + + public void setEdgeLevel(int widthPixel) { + validateEdgeLevel(widthPixel, null); + } + + public EdgeLevel getEdgeLevel() { + return edgeLevel; + } + + private void validateEdgeLevel(int widthPixel, EdgeLevel edgeLevel) { + try { + DisplayMetrics metrics = new DisplayMetrics(); + WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + windowManager.getDefaultDisplay().getMetrics(metrics); + Field mEdgeSize = mHelper.getClass().getDeclaredField("mEdgeSize"); + mEdgeSize.setAccessible(true); + if (widthPixel != 0) { + mEdgeSize.setInt(mHelper, widthPixel); + } else { + if (edgeLevel == EdgeLevel.MAX) { + mEdgeSize.setInt(mHelper, metrics.widthPixels); + } else if (edgeLevel == EdgeLevel.MED) { + mEdgeSize.setInt(mHelper, metrics.widthPixels / 2); + } else if (edgeLevel == EdgeLevel.MIN) { + mEdgeSize.setInt(mHelper, ((int) (20 * metrics.density + 0.5f))); + } + } + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + @IntDef({EDGE_LEFT, EDGE_RIGHT, EDGE_ALL}) @Retention(RetentionPolicy.SOURCE) public @interface EdgeOrientation {