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

Commit 198ac9ca authored by Jorge Gil's avatar Jorge Gil Committed by Android (Google) Code Review
Browse files

Merge "Add delay between motion events in hold-to-drag gesture" into main

parents fc4963a6 36b5470c
Loading
Loading
Loading
Loading
+1 −5
Original line number Diff line number Diff line
@@ -109,11 +109,7 @@ open class DesktopModeAppHelper(private val innerHelper: IStandardAppHelper) :
        if (motionEventHelper.inputMethod == TOUCH
            && Flags.enableHoldToDragAppHandle()) {
            // Touch requires hold-to-drag.
            val downTime = SystemClock.uptimeMillis()
            motionEventHelper.actionDown(startX, startY, time = downTime)
            SystemClock.sleep(100L) // hold for 100ns before starting the move.
            motionEventHelper.actionMove(startX, startY, startX, endY, 100, downTime = downTime)
            motionEventHelper.actionUp(startX, endY, downTime = downTime)
            motionEventHelper.holdToDrag(startX, startY, startX, endY, steps = 100)
        } else {
            device.drag(startX, startY, startX, endY, 100)
        }
+38 −1
Original line number Diff line number Diff line
@@ -54,7 +54,15 @@ class MotionEventHelper(
        injectMotionEvent(ACTION_UP, x, y, downTime = downTime)
    }

    fun actionMove(startX: Int, startY: Int, endX: Int, endY: Int, steps: Int, downTime: Long) {
    fun actionMove(
        startX: Int,
        startY: Int,
        endX: Int,
        endY: Int,
        steps: Int,
        downTime: Long,
        withMotionEventInjectDelay: Boolean = false
    ) {
        val incrementX = (endX - startX).toFloat() / (steps - 1)
        val incrementY = (endY - startY).toFloat() / (steps - 1)

@@ -65,9 +73,33 @@ class MotionEventHelper(

            val moveEvent = getMotionEvent(downTime, time, ACTION_MOVE, x, y)
            injectMotionEvent(moveEvent)
            if (withMotionEventInjectDelay) {
                SystemClock.sleep(MOTION_EVENT_INJECTION_DELAY_MILLIS)
            }
        }
    }

    /**
     * Drag from [startX], [startY] to [endX], [endY] with a "hold" period after touching down
     * and before moving.
     */
    fun holdToDrag(startX: Int, startY: Int, endX: Int, endY: Int, steps: Int) {
        val downTime = SystemClock.uptimeMillis()
        actionDown(startX, startY, time = downTime)
        SystemClock.sleep(100L) // Hold before dragging.
        actionMove(
            startX,
            startY,
            endX,
            endY,
            steps,
            downTime,
            withMotionEventInjectDelay = true
        )
        SystemClock.sleep(REGULAR_CLICK_LENGTH)
        actionUp(startX, endX, downTime)
    }

    private fun injectMotionEvent(
        action: Int,
        x: Int,
@@ -120,4 +152,9 @@ class MotionEventHelper(
        event.displayId = 0
        return event
    }

    companion object {
        private const val MOTION_EVENT_INJECTION_DELAY_MILLIS = 5L
        private const val REGULAR_CLICK_LENGTH = 100L
    }
}
 No newline at end of file