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

Commit 8743f187 authored by Harry Cutts's avatar Harry Cutts
Browse files

GestureConverter: add finger count axis for multi-finger swipes

Consumers of multi-finger swipes (i.e. SysUI) previously had to wait
until the first MOVE event to find out how many fingers were swiping,
even though in the framework we knew this when sending the DOWN event.
Adding this as an axis on the DOWN event should let SysUI simplify their
code.

Bug: 283093437
Test: atest inputflinger_tests:GestureConverterTest
Test: modify InputDispatcher to send multi-finger swipes to apps too,
      then check reported values in a test app
Change-Id: I34d1fdf096c49d7eb9b5d8ebd64427eb4e5db1f4
parent b2262b9d
Loading
Loading
Loading
Loading
+18 −1
Original line number Diff line number Diff line
@@ -781,6 +781,8 @@ enum {
     *
     * These values are relative to the state from the last event, not accumulated, so developers
     * should make sure to process this axis value for all batched historical events.
     *
     * This axis is only set on the first pointer in a motion event.
     */
    AMOTION_EVENT_AXIS_GESTURE_X_OFFSET = 48,
    /**
@@ -797,6 +799,8 @@ enum {
     *
     * These values are relative to the state from the last event, not accumulated, so developers
     * should make sure to process this axis value for all batched historical events.
     *
     * This axis is only set on the first pointer in a motion event.
     */
    AMOTION_EVENT_AXIS_GESTURE_SCROLL_X_DISTANCE = 50,
    /**
@@ -815,16 +819,29 @@ enum {
     *
     * These values are relative to the state from the last event, not accumulated, so developers
     * should make sure to process this axis value for all batched historical events.
     *
     * This axis is only set on the first pointer in a motion event.
     */
    AMOTION_EVENT_AXIS_GESTURE_PINCH_SCALE_FACTOR = 52,

    /**
     * Axis constant: the number of fingers being used in a multi-finger swipe gesture.
     *
     * - For a touch pad, reports the number of fingers being used in a multi-finger swipe gesture
     *   (with CLASSIFICATION_MULTI_FINGER_SWIPE).
     *
     * Since CLASSIFICATION_MULTI_FINGER_SWIPE is a hidden API, so is this axis. It is only set on
     * the first pointer in a motion event.
     */
    AMOTION_EVENT_AXIS_GESTURE_SWIPE_FINGER_COUNT = 53,

    /**
     * Note: This is not an "Axis constant". It does not represent any axis, nor should it be used
     * to represent any axis. It is a constant holding the value of the largest defined axis value,
     * to make some computations (like iterating through all possible axes) cleaner.
     * Please update the value accordingly if you add a new axis.
     */
    AMOTION_EVENT_MAXIMUM_VALID_AXIS_VALUE = AMOTION_EVENT_AXIS_GESTURE_PINCH_SCALE_FACTOR,
    AMOTION_EVENT_MAXIMUM_VALID_AXIS_VALUE = AMOTION_EVENT_AXIS_GESTURE_SWIPE_FINGER_COUNT,

    // NOTE: If you add a new axis here you must also add it to several other files.
    //       Refer to frameworks/base/core/java/android/view/MotionEvent.java for the full list.
+2 −1
Original line number Diff line number Diff line
@@ -404,7 +404,8 @@ namespace android {
    DEFINE_AXIS(GESTURE_Y_OFFSET), \
    DEFINE_AXIS(GESTURE_SCROLL_X_DISTANCE), \
    DEFINE_AXIS(GESTURE_SCROLL_Y_DISTANCE), \
    DEFINE_AXIS(GESTURE_PINCH_SCALE_FACTOR)
    DEFINE_AXIS(GESTURE_PINCH_SCALE_FACTOR), \
    DEFINE_AXIS(GESTURE_SWIPE_FINGER_COUNT)

// NOTE: If you add new LEDs here, you must also add them to Input.h
#define LEDS_SEQUENCE \
+3 −3
Original line number Diff line number Diff line
@@ -258,12 +258,12 @@ static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_GENERIC_13) == common
static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_GENERIC_14) == common::Axis::GENERIC_14);
static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_GENERIC_15) == common::Axis::GENERIC_15);
static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_GENERIC_16) == common::Axis::GENERIC_16);
// TODO(b/251196347): add GESTURE_{X,Y}_OFFSET, GESTURE_SCROLL_{X,Y}_DISTANCE, and
// GESTURE_PINCH_SCALE_FACTOR.
// TODO(b/251196347): add GESTURE_{X,Y}_OFFSET, GESTURE_SCROLL_{X,Y}_DISTANCE,
// GESTURE_PINCH_SCALE_FACTOR, and GESTURE_SWIPE_FINGER_COUNT.
// If you added a new axis, consider whether this should also be exposed as a HAL axis. Update the
// static_assert below and add the new axis here, or leave a comment summarizing your decision.
static_assert(static_cast<common::Axis>(AMOTION_EVENT_MAXIMUM_VALID_AXIS_VALUE) ==
              static_cast<common::Axis>(AMOTION_EVENT_AXIS_GESTURE_PINCH_SCALE_FACTOR));
              static_cast<common::Axis>(AMOTION_EVENT_AXIS_GESTURE_SWIPE_FINGER_COUNT));

static common::VideoFrame getHalVideoFrame(const TouchVideoFrame& frame) {
    common::VideoFrame out;
+3 −0
Original line number Diff line number Diff line
@@ -385,6 +385,8 @@ NotifyMotionArgs GestureConverter::endScroll(nsecs_t when, nsecs_t readTime) {
        }

        mDownTime = when;
        mFakeFingerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_GESTURE_SWIPE_FINGER_COUNT,
                                          fingerCount);
        out.push_back(makeMotionArgs(when, readTime, AMOTION_EVENT_ACTION_DOWN,
                                     /* actionButton= */ 0, mButtonState, /* pointerCount= */ 1,
                                     mFingerProps.data(), mFakeFingerCoords.data(), xCursorPosition,
@@ -441,6 +443,7 @@ NotifyMotionArgs GestureConverter::endScroll(nsecs_t when, nsecs_t readTime) {
                                 /* actionButton= */ 0, mButtonState, /* pointerCount= */ 1,
                                 mFingerProps.data(), mFakeFingerCoords.data(), xCursorPosition,
                                 yCursorPosition));
    mFakeFingerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_GESTURE_SWIPE_FINGER_COUNT, 0);
    mCurrentClassification = MotionClassification::NONE;
    mSwipeFingerCount = 0;
    return out;
+21 −16
Original line number Diff line number Diff line
@@ -395,7 +395,7 @@ TEST_F(GestureConverterTest, ThreeFingerSwipe_ClearsClassificationAfterGesture)
                WithMotionClassification(MotionClassification::NONE));
}

TEST_F(GestureConverterTest, ThreeFingerSwipe_ClearsOffsetsAfterGesture) {
TEST_F(GestureConverterTest, ThreeFingerSwipe_ClearsGestureAxesAfterGesture) {
    InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
    GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);

@@ -412,7 +412,8 @@ TEST_F(GestureConverterTest, ThreeFingerSwipe_ClearsOffsetsAfterGesture) {
                         GESTURES_ZOOM_START);
    args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, pinchGesture);
    ASSERT_FALSE(args.empty());
    EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()), WithGestureOffset(0, 0, EPSILON));
    EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
                AllOf(WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(0)));
}

TEST_F(GestureConverterTest, ThreeFingerSwipe_Vertical) {
@@ -433,6 +434,7 @@ TEST_F(GestureConverterTest, ThreeFingerSwipe_Vertical) {
    NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front());
    ASSERT_THAT(arg,
                AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON),
                      WithGestureSwipeFingerCount(3),
                      WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
                      WithPointerCount(1u), WithToolType(ToolType::FINGER)));
    PointerCoords finger0Start = arg.pointerCoords[0];
@@ -441,7 +443,7 @@ TEST_F(GestureConverterTest, ThreeFingerSwipe_Vertical) {
    ASSERT_THAT(arg,
                AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
                                       1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
                      WithGestureOffset(0, 0, EPSILON),
                      WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(3),
                      WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
                      WithPointerCount(2u), WithToolType(ToolType::FINGER)));
    PointerCoords finger1Start = arg.pointerCoords[1];
@@ -450,7 +452,7 @@ TEST_F(GestureConverterTest, ThreeFingerSwipe_Vertical) {
    ASSERT_THAT(arg,
                AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
                                       2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
                      WithGestureOffset(0, 0, EPSILON),
                      WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(3),
                      WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
                      WithPointerCount(3u), WithToolType(ToolType::FINGER)));
    PointerCoords finger2Start = arg.pointerCoords[2];
@@ -459,7 +461,7 @@ TEST_F(GestureConverterTest, ThreeFingerSwipe_Vertical) {
    arg = std::get<NotifyMotionArgs>(args.front());
    ASSERT_THAT(arg,
                AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
                      WithGestureOffset(0, -0.01, EPSILON),
                      WithGestureOffset(0, -0.01, EPSILON), WithGestureSwipeFingerCount(3),
                      WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
                      WithPointerCount(3u), WithToolType(ToolType::FINGER)));
    EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX());
@@ -476,7 +478,7 @@ TEST_F(GestureConverterTest, ThreeFingerSwipe_Vertical) {
    arg = std::get<NotifyMotionArgs>(args.front());
    ASSERT_THAT(arg,
                AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
                      WithGestureOffset(0, -0.005, EPSILON),
                      WithGestureOffset(0, -0.005, EPSILON), WithGestureSwipeFingerCount(3),
                      WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
                      WithPointerCount(3u), WithToolType(ToolType::FINGER)));
    EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX());
@@ -492,19 +494,20 @@ TEST_F(GestureConverterTest, ThreeFingerSwipe_Vertical) {
    ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
                AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
                                       2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
                      WithGestureOffset(0, 0, EPSILON),
                      WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(3),
                      WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
                      WithPointerCount(3u), WithToolType(ToolType::FINGER)));
    args.pop_front();
    ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
                AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
                                       1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
                      WithGestureOffset(0, 0, EPSILON),
                      WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(3),
                      WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
                      WithPointerCount(2u), WithToolType(ToolType::FINGER)));
    args.pop_front();
    ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
                AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithGestureOffset(0, 0, EPSILON),
                      WithGestureSwipeFingerCount(3),
                      WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
                      WithPointerCount(1u), WithToolType(ToolType::FINGER)));
}
@@ -600,6 +603,7 @@ TEST_F(GestureConverterTest, FourFingerSwipe_Horizontal) {
    NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front());
    ASSERT_THAT(arg,
                AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON),
                      WithGestureSwipeFingerCount(4),
                      WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
                      WithPointerCount(1u), WithToolType(ToolType::FINGER)));
    PointerCoords finger0Start = arg.pointerCoords[0];
@@ -608,7 +612,7 @@ TEST_F(GestureConverterTest, FourFingerSwipe_Horizontal) {
    ASSERT_THAT(arg,
                AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
                                       1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
                      WithGestureOffset(0, 0, EPSILON),
                      WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(4),
                      WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
                      WithPointerCount(2u), WithToolType(ToolType::FINGER)));
    PointerCoords finger1Start = arg.pointerCoords[1];
@@ -617,7 +621,7 @@ TEST_F(GestureConverterTest, FourFingerSwipe_Horizontal) {
    ASSERT_THAT(arg,
                AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
                                       2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
                      WithGestureOffset(0, 0, EPSILON),
                      WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(4),
                      WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
                      WithPointerCount(3u), WithToolType(ToolType::FINGER)));
    PointerCoords finger2Start = arg.pointerCoords[2];
@@ -626,7 +630,7 @@ TEST_F(GestureConverterTest, FourFingerSwipe_Horizontal) {
    ASSERT_THAT(arg,
                AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
                                       3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
                      WithGestureOffset(0, 0, EPSILON),
                      WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(4),
                      WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
                      WithPointerCount(4u), WithToolType(ToolType::FINGER)));
    PointerCoords finger3Start = arg.pointerCoords[3];
@@ -635,7 +639,7 @@ TEST_F(GestureConverterTest, FourFingerSwipe_Horizontal) {
    arg = std::get<NotifyMotionArgs>(args.front());
    ASSERT_THAT(arg,
                AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
                      WithGestureOffset(0.01, 0, EPSILON),
                      WithGestureOffset(0.01, 0, EPSILON), WithGestureSwipeFingerCount(4),
                      WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
                      WithPointerCount(4u), WithToolType(ToolType::FINGER)));
    EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() + 10);
@@ -654,7 +658,7 @@ TEST_F(GestureConverterTest, FourFingerSwipe_Horizontal) {
    arg = std::get<NotifyMotionArgs>(args.front());
    ASSERT_THAT(arg,
                AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
                      WithGestureOffset(0.005, 0, EPSILON),
                      WithGestureOffset(0.005, 0, EPSILON), WithGestureSwipeFingerCount(4),
                      WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
                      WithPointerCount(4u), WithToolType(ToolType::FINGER)));
    EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() + 15);
@@ -672,26 +676,27 @@ TEST_F(GestureConverterTest, FourFingerSwipe_Horizontal) {
    ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
                AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
                                       3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
                      WithGestureOffset(0, 0, EPSILON),
                      WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(4),
                      WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
                      WithPointerCount(4u), WithToolType(ToolType::FINGER)));
    args.pop_front();
    ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
                AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
                                       2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
                      WithGestureOffset(0, 0, EPSILON),
                      WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(4),
                      WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
                      WithPointerCount(3u), WithToolType(ToolType::FINGER)));
    args.pop_front();
    ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
                AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
                                       1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
                      WithGestureOffset(0, 0, EPSILON),
                      WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(4),
                      WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
                      WithPointerCount(2u), WithToolType(ToolType::FINGER)));
    args.pop_front();
    ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
                AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithGestureOffset(0, 0, EPSILON),
                      WithGestureSwipeFingerCount(4),
                      WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
                      WithPointerCount(1u), WithToolType(ToolType::FINGER)));
}
Loading