Vibrate the phone if swiping distance exceeds threshold.

This commit is contained in:
Alex Ning 2020-09-04 00:05:27 +08:00
parent 98b324f190
commit 47c1069d1e
3 changed files with 33 additions and 9 deletions

View File

@ -6,6 +6,7 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="22" />

View File

@ -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<Post, RecyclerView
switch (post.getVoteType()) {
case 1:
//Upvoted
Log.i("asdasdf", "upvoted");
((PostBaseViewHolder) holder).upvoteButton.setColorFilter(mUpvotedColor, android.graphics.PorterDuff.Mode.SRC_IN);
((PostBaseViewHolder) holder).scoreTextView.setTextColor(mUpvotedColor);
break;
case -1:
Log.i("asdasdf", "downvoted");
//Downvoted
((PostBaseViewHolder) holder).downvoteButton.setColorFilter(mDownvotedColor, android.graphics.PorterDuff.Mode.SRC_IN);
((PostBaseViewHolder) holder).scoreTextView.setTextColor(mDownvotedColor);
@ -1527,14 +1524,12 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
if (previousVoteType != 1) {
//Not upvoted before
Log.i("asdasdf", "not 1 before");
post.setVoteType(1);
newVoteType = APIUtils.DIR_UPVOTE;
upvoteButton
.setColorFilter(mUpvotedColor, android.graphics.PorterDuff.Mode.SRC_IN);
scoreTextView.setTextColor(mUpvotedColor);
} else {
Log.i("asdasdf", "1 before");
//Upvoted before
post.setVoteType(0);
newVoteType = APIUtils.DIR_UNVOTE;
@ -1548,13 +1543,11 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
@Override
public void onVoteThingSuccess(int position1) {
if (newVoteType.equals(APIUtils.DIR_UPVOTE)) {
Log.i("asdasdf", "1");
post.setVoteType(1);
upvoteButton
.setColorFilter(mUpvotedColor, android.graphics.PorterDuff.Mode.SRC_IN);
scoreTextView.setTextColor(mUpvotedColor);
} else {
Log.i("asdasdf", "0");
post.setVoteType(0);
upvoteButton.setColorFilter(mPostIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN);
scoreTextView.setTextColor(mPostIconAndInfoColor);

View File

@ -14,6 +14,8 @@ import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
@ -621,11 +623,15 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
((ActivityToolbarInterface) activity).displaySortType();
}
Vibrator v = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
backgroundLeft = new ColorDrawable(customThemeWrapper.getDownvoted());
backgroundRight = new ColorDrawable(customThemeWrapper.getUpvoted());
drawableLeft = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_downward_black_24dp, null);
drawableRight = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_upward_black_24dp, null);
touchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
boolean exceedThreshold = false;
@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
@ -660,18 +666,42 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
View itemView = viewHolder.itemView;
int horizontalOffset = (int) Utils.convertDpToPixel(16, activity);
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);
}
}
}
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);