File indexing completed on 2024-12-22 04:41:39
0001 /* 0002 * SPDX-FileCopyrightText: 2014 Ahmed I. Khalil <ahmedibrahimkhali@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 package org.kde.kdeconnect.Plugins.MousePadPlugin; 0008 0009 import android.view.MotionEvent; 0010 import android.view.ViewConfiguration; 0011 0012 0013 class MousePadGestureDetector { 0014 0015 private static final int TAP_TIMEOUT = ViewConfiguration.getTapTimeout() + 100; 0016 private final OnGestureListener mGestureListener; 0017 0018 private long mFirstDownTime = 0; 0019 0020 private boolean mIsGestureHandled; 0021 0022 public interface OnGestureListener { 0023 0024 boolean onTripleFingerTap(MotionEvent ev); 0025 0026 boolean onDoubleFingerTap(MotionEvent ev); 0027 } 0028 0029 MousePadGestureDetector(OnGestureListener gestureListener) { 0030 if (gestureListener == null) { 0031 throw new IllegalArgumentException("gestureListener cannot be null"); 0032 } 0033 mGestureListener = gestureListener; 0034 } 0035 0036 boolean onTouchEvent(MotionEvent event) { 0037 int action = event.getAction(); 0038 switch (action & MotionEvent.ACTION_MASK) { 0039 case MotionEvent.ACTION_DOWN: 0040 mIsGestureHandled = false; 0041 mFirstDownTime = event.getEventTime(); 0042 break; 0043 case MotionEvent.ACTION_POINTER_UP: 0044 int count = event.getPointerCount(); 0045 if (event.getEventTime() - mFirstDownTime <= TAP_TIMEOUT) { 0046 if (count == 3) { 0047 if (!mIsGestureHandled) { 0048 mIsGestureHandled = mGestureListener.onTripleFingerTap(event); 0049 } 0050 } else if (count == 2) { 0051 if (!mIsGestureHandled) { 0052 mIsGestureHandled = mGestureListener.onDoubleFingerTap(event); 0053 } 0054 } 0055 } 0056 mFirstDownTime = 0; 0057 break; 0058 } 0059 return mIsGestureHandled; 0060 } 0061 }