Tracy Zhou | d248ee7 | 2023-01-23 22:06:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Tracy Zhou | a33fb69 | 2023-03-20 10:57:02 -0700 | [diff] [blame] | 17 | package com.android.launcher3; |
Tracy Zhou | d248ee7 | 2023-01-23 22:06:19 -0800 | [diff] [blame] | 18 | |
| 19 | import static android.view.MotionEvent.CLASSIFICATION_TWO_FINGER_SWIPE; |
| 20 | |
| 21 | import static com.android.launcher3.config.FeatureFlags.ENABLE_TRACKPAD_GESTURE; |
| 22 | |
| 23 | import android.annotation.TargetApi; |
| 24 | import android.os.Build; |
| 25 | import android.view.MotionEvent; |
| 26 | |
| 27 | /** Handles motion events from trackpad. */ |
| 28 | public class MotionEventsUtils { |
| 29 | |
| 30 | /** {@link MotionEvent#CLASSIFICATION_MULTI_FINGER_SWIPE} is hidden. */ |
| 31 | public static final int CLASSIFICATION_MULTI_FINGER_SWIPE = 4; |
| 32 | |
Tracy Zhou | 48ac5a0 | 2023-05-27 11:23:31 -0700 | [diff] [blame] | 33 | /** {@link MotionEvent#AXIS_GESTURE_SWIPE_FINGER_COUNT} is hidden. */ |
| 34 | private static final int AXIS_GESTURE_SWIPE_FINGER_COUNT = 53; |
| 35 | |
Tracy Zhou | d248ee7 | 2023-01-23 22:06:19 -0800 | [diff] [blame] | 36 | @TargetApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) |
| 37 | public static boolean isTrackpadScroll(MotionEvent event) { |
| 38 | return ENABLE_TRACKPAD_GESTURE.get() |
| 39 | && event.getClassification() == CLASSIFICATION_TWO_FINGER_SWIPE; |
| 40 | } |
| 41 | |
| 42 | @TargetApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) |
| 43 | public static boolean isTrackpadMultiFingerSwipe(MotionEvent event) { |
| 44 | return ENABLE_TRACKPAD_GESTURE.get() |
| 45 | && event.getClassification() == CLASSIFICATION_MULTI_FINGER_SWIPE; |
| 46 | } |
| 47 | |
Tracy Zhou | 7dc9e3a | 2023-03-15 01:12:30 -0700 | [diff] [blame] | 48 | public static boolean isTrackpadThreeFingerSwipe(MotionEvent event) { |
Tracy Zhou | 48ac5a0 | 2023-05-27 11:23:31 -0700 | [diff] [blame] | 49 | return isTrackpadMultiFingerSwipe(event) && event.getAxisValue( |
| 50 | AXIS_GESTURE_SWIPE_FINGER_COUNT) == 3; |
Tracy Zhou | 7dc9e3a | 2023-03-15 01:12:30 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | public static boolean isTrackpadFourFingerSwipe(MotionEvent event) { |
Tracy Zhou | 48ac5a0 | 2023-05-27 11:23:31 -0700 | [diff] [blame] | 54 | return isTrackpadMultiFingerSwipe(event) && event.getAxisValue( |
| 55 | AXIS_GESTURE_SWIPE_FINGER_COUNT) == 4; |
Tracy Zhou | 7dc9e3a | 2023-03-15 01:12:30 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Tracy Zhou | d248ee7 | 2023-01-23 22:06:19 -0800 | [diff] [blame] | 58 | public static boolean isTrackpadMotionEvent(MotionEvent event) { |
| 59 | return isTrackpadScroll(event) || isTrackpadMultiFingerSwipe(event); |
| 60 | } |
| 61 | } |