Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4916fb62 authored by Beverly's avatar Beverly Committed by Beverly Tai
Browse files

Add optional longPressDuration to LongPressHandlingView

If no longpressDuration is specified, use the default
ViewConfiguration longpress time.

Test: atest LongPressHandlingViewInteractionHandlerTest.kt
Bug: 305234447
Flag: NONE
Change-Id: I4d21e93720e2421b1334d9a3c2ea26d273a676b0
parent 25fde473
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import com.android.systemui.shade.TouchLogger
import kotlin.math.pow
import kotlin.math.sqrt
@@ -36,11 +37,18 @@ import kotlinx.coroutines.DisposableHandle
class LongPressHandlingView(
    context: Context,
    attrs: AttributeSet?,
    private val longPressDuration: () -> Long,
) :
    View(
        context,
        attrs,
    ) {

    constructor(
        context: Context,
        attrs: AttributeSet?,
    ) : this(context, attrs, { ViewConfiguration.getLongPressTimeout().toLong() })

    interface Listener {
        /** Notifies that a long-press has been detected by the given view. */
        fun onLongPressDetected(
@@ -77,6 +85,7 @@ class LongPressHandlingView(
                )
            },
            onSingleTapDetected = { listener?.onSingleTapDetected(this@LongPressHandlingView) },
            longPressDuration = longPressDuration,
        )
    }

+4 −2
Original line number Diff line number Diff line
@@ -33,6 +33,8 @@ class LongPressHandlingViewInteractionHandler(
    private val onLongPressDetected: (x: Int, y: Int) -> Unit,
    /** Callback reporting the a single tap gesture was detected at the given coordinates. */
    private val onSingleTapDetected: () -> Unit,
    /** Time for the touch to be considered a long-press in ms */
    private val longPressDuration: () -> Long,
) {
    sealed class MotionEventModel {
        object Other : MotionEventModel()
@@ -77,7 +79,7 @@ class LongPressHandlingViewInteractionHandler(
                cancelScheduledLongPress()
                if (
                    event.distanceMoved <= ViewConfiguration.getTouchSlop() &&
                        event.gestureDuration < ViewConfiguration.getLongPressTimeout()
                        event.gestureDuration < longPressDuration()
                ) {
                    dispatchSingleTap()
                }
@@ -103,7 +105,7 @@ class LongPressHandlingViewInteractionHandler(
                        y = y,
                    )
                },
                ViewConfiguration.getLongPressTimeout().toLong(),
                longPressDuration(),
            )
    }

+1 −0
Original line number Diff line number Diff line
@@ -67,6 +67,7 @@ class LongPressHandlingViewInteractionHandlerTest : SysuiTestCase() {
                isAttachedToWindow = { isAttachedToWindow },
                onLongPressDetected = onLongPressDetected,
                onSingleTapDetected = onSingleTapDetected,
                longPressDuration = { ViewConfiguration.getLongPressTimeout().toLong() }
            )
        underTest.isLongPressHandlingEnabled = true
    }