From 47c1069d1e588c6c00a0a11d04593831ed51f65d Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 4 Sep 2020 00:05:27 +0800 Subject: Vibrate the phone if swiping distance exceeds threshold. --- app/src/main/AndroidManifest.xml | 1 + .../Adapter/PostRecyclerViewAdapter.java | 7 ----- .../infinityforreddit/Fragment/PostFragment.java | 34 ++++++++++++++++++++-- 3 files changed, 33 insertions(+), 9 deletions(-) (limited to 'app') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1a29d099..13339b1b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -6,6 +6,7 @@ + diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java index 13fa5d90..7c7d8f53 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -10,7 +10,6 @@ import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -511,12 +510,10 @@ public class PostRecyclerViewAdapter extends PagedListAdapter 0) { - if (dX > (itemView.getRight() - itemView.getLeft()) * 0.3) { + if (dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) { + if (!exceedThreshold) { + exceedThreshold = true; + if (v != null) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + v.vibrate(VibrationEffect.createOneShot(10, 175)); + } else { + //deprecated in API 26 + v.vibrate(50); + } + } + } backgroundLeft.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom()); } else { + exceedThreshold = false; backgroundLeft.setBounds(0, 0, 0, 0); } drawableLeft.setBounds(itemView.getLeft() + ((int) dX) - horizontalOffset - drawableLeft.getIntrinsicWidth(), (itemView.getBottom() - itemView.getTop() - drawableLeft.getIntrinsicHeight()) / 2, itemView.getLeft() + ((int) dX) - horizontalOffset, (itemView.getBottom() - itemView.getTop() + drawableLeft.getIntrinsicHeight()) / 2); backgroundLeft.draw(c); drawableLeft.draw(c); } else if (dX < 0) { - if (-dX > (itemView.getRight() - itemView.getLeft()) * 0.3) { + if (-dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) { + if (!exceedThreshold) { + exceedThreshold = true; + if (v != null) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + v.vibrate(VibrationEffect.createOneShot(10, 175)); + } else { + //deprecated in API 26 + v.vibrate(50); + } + } + } backgroundRight.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom()); } else { + exceedThreshold = false; backgroundRight.setBounds(0, 0, 0, 0); } drawableRight.setBounds(itemView.getRight() + ((int) dX) + horizontalOffset, (itemView.getBottom() - itemView.getTop() - drawableRight.getIntrinsicHeight()) / 2, itemView.getRight() + ((int) dX) + horizontalOffset + drawableRight.getIntrinsicWidth(), (itemView.getBottom() - itemView.getTop() + drawableRight.getIntrinsicHeight()) / 2); -- cgit v1.2.3