Skip to content

Commit

Permalink
Merge pull request #16 from saeedmozaffari/master
Browse files Browse the repository at this point in the history
Possibility to determine the edge touch level
  • Loading branch information
YoKeyword authored Oct 9, 2017
2 parents 5b79e7f + c084b4e commit 29058e2
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*/
Expand All @@ -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();
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 29058e2

Please sign in to comment.