From ba55296a448efc2a3789ef6f3a7d2e438513454e Mon Sep 17 00:00:00 2001 From: John Li Date: Fri, 11 Dec 2020 16:13:24 +0800 Subject: [PATCH] Haptic effect on swiping the notification item Haptic once the swiping on the notification item is going to snap in either directions. The snap-in scenario is about the notification item when there is a "snap back point" i.e. if swiping the item back till a certain point it just snaps back to initial state but once it goes past a certain location it snaps into the new location. - screenshot, https://screenshot.googleplex.com/6A8Gxs7yRwqAVk2 Bug: 175364588 Test: manual Change-Id: I7e2ed19bfb7f863502e10233e3e23ee5d434b3b4 --- .../NotificationHistoryRecyclerView.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/com/android/settings/notification/history/NotificationHistoryRecyclerView.java b/src/com/android/settings/notification/history/NotificationHistoryRecyclerView.java index 5a6ff7e781d..06c74bc3432 100644 --- a/src/com/android/settings/notification/history/NotificationHistoryRecyclerView.java +++ b/src/com/android/settings/notification/history/NotificationHistoryRecyclerView.java @@ -1,9 +1,14 @@ package com.android.settings.notification.history; +import static android.view.HapticFeedbackConstants.CLOCK_TICK; + import android.annotation.Nullable; import android.content.Context; +import android.graphics.Canvas; import android.util.AttributeSet; +import android.view.View; +import androidx.annotation.NonNull; import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.LinearLayoutManager; @@ -15,6 +20,9 @@ public class NotificationHistoryRecyclerView extends RecyclerView { private OnItemSwipeDeleteListener listener; + /** The amount of horizontal displacement caused by user's action, used to track the swiping. */ + private float dXLast; + public NotificationHistoryRecyclerView(Context context) { this(context, null); } @@ -57,6 +65,27 @@ public class NotificationHistoryRecyclerView extends RecyclerView { listener.onItemSwipeDeleted(viewHolder.getAdapterPosition()); } } + + /** Performs haptic effect once the swiping goes past a certain location. */ + @Override + public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, + @NonNull ViewHolder viewHolder, float dX, float dY, int actionState, + boolean isCurrentlyActive) { + super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); + if (isCurrentlyActive) { + View view = viewHolder.itemView; + float swipeThreshold = getSwipeThreshold(viewHolder); + float snapOffset = swipeThreshold * view.getWidth(); + boolean snapIntoNewLocation = dX < -snapOffset || dX > snapOffset; + boolean snapIntoNewLocationLast = dXLast < -snapOffset || dXLast > snapOffset; + if (snapIntoNewLocation != snapIntoNewLocationLast) { + view.performHapticFeedback(CLOCK_TICK); + } + dXLast = dX; + } else { + dXLast = 0; + } + } } public interface OnItemSwipeDeleteListener {