mirror of
https://invent.kde.org/network/kdeconnect-android.git
synced 2025-12-22 12:13:52 +01:00
Migrate MousePadGestureDetector to Kotlin
This commit is contained in:
committed by
Albert Vaca Cintora
parent
5f5c487afc
commit
519abe01b7
@@ -1,61 +0,0 @@
|
|||||||
/*
|
|
||||||
* SPDX-FileCopyrightText: 2014 Ahmed I. Khalil <ahmedibrahimkhali@gmail.com>
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.kde.kdeconnect.Plugins.MousePadPlugin;
|
|
||||||
|
|
||||||
import android.view.MotionEvent;
|
|
||||||
import android.view.ViewConfiguration;
|
|
||||||
|
|
||||||
|
|
||||||
class MousePadGestureDetector {
|
|
||||||
|
|
||||||
private static final int TAP_TIMEOUT = ViewConfiguration.getTapTimeout() + 100;
|
|
||||||
private final OnGestureListener mGestureListener;
|
|
||||||
|
|
||||||
private long mFirstDownTime = 0;
|
|
||||||
|
|
||||||
private boolean mIsGestureHandled;
|
|
||||||
|
|
||||||
public interface OnGestureListener {
|
|
||||||
|
|
||||||
boolean onTripleFingerTap(MotionEvent ev);
|
|
||||||
|
|
||||||
boolean onDoubleFingerTap(MotionEvent ev);
|
|
||||||
}
|
|
||||||
|
|
||||||
MousePadGestureDetector(OnGestureListener gestureListener) {
|
|
||||||
if (gestureListener == null) {
|
|
||||||
throw new IllegalArgumentException("gestureListener cannot be null");
|
|
||||||
}
|
|
||||||
mGestureListener = gestureListener;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean onTouchEvent(MotionEvent event) {
|
|
||||||
int action = event.getAction();
|
|
||||||
switch (action & MotionEvent.ACTION_MASK) {
|
|
||||||
case MotionEvent.ACTION_DOWN:
|
|
||||||
mIsGestureHandled = false;
|
|
||||||
mFirstDownTime = event.getEventTime();
|
|
||||||
break;
|
|
||||||
case MotionEvent.ACTION_POINTER_UP:
|
|
||||||
int count = event.getPointerCount();
|
|
||||||
if (event.getEventTime() - mFirstDownTime <= TAP_TIMEOUT) {
|
|
||||||
if (count == 3) {
|
|
||||||
if (!mIsGestureHandled) {
|
|
||||||
mIsGestureHandled = mGestureListener.onTripleFingerTap(event);
|
|
||||||
}
|
|
||||||
} else if (count == 2) {
|
|
||||||
if (!mIsGestureHandled) {
|
|
||||||
mIsGestureHandled = mGestureListener.onDoubleFingerTap(event);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mFirstDownTime = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return mIsGestureHandled;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2014 Ahmed I. Khalil <ahmedibrahimkhali@gmail.com>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
||||||
|
*/
|
||||||
|
package org.kde.kdeconnect.Plugins.MousePadPlugin
|
||||||
|
|
||||||
|
import android.view.MotionEvent
|
||||||
|
import android.view.ViewConfiguration
|
||||||
|
|
||||||
|
class MousePadGestureDetector {
|
||||||
|
private val tapTimeout = ViewConfiguration.getTapTimeout() + 100
|
||||||
|
private val gestureListener: OnGestureListener
|
||||||
|
private var firstDownTime = 0L
|
||||||
|
private var isGestureHandled = false
|
||||||
|
|
||||||
|
interface OnGestureListener {
|
||||||
|
fun onTripleFingerTap(ev: MotionEvent): Boolean
|
||||||
|
|
||||||
|
fun onDoubleFingerTap(ev: MotionEvent): Boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(gestureListener: OnGestureListener) {
|
||||||
|
this@MousePadGestureDetector.gestureListener = gestureListener
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onTouchEvent(event: MotionEvent): Boolean {
|
||||||
|
val action = event.action
|
||||||
|
when (action and MotionEvent.ACTION_MASK) {
|
||||||
|
MotionEvent.ACTION_DOWN -> {
|
||||||
|
isGestureHandled = false
|
||||||
|
firstDownTime = event.eventTime
|
||||||
|
}
|
||||||
|
MotionEvent.ACTION_POINTER_UP -> {
|
||||||
|
val count = event.pointerCount
|
||||||
|
if (event.eventTime - firstDownTime <= tapTimeout) {
|
||||||
|
if (count == 3) {
|
||||||
|
if (!isGestureHandled) {
|
||||||
|
isGestureHandled = gestureListener.onTripleFingerTap(event)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (count == 2) {
|
||||||
|
if (!isGestureHandled) {
|
||||||
|
isGestureHandled = gestureListener.onDoubleFingerTap(event)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
firstDownTime = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return isGestureHandled
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user