From 3b750c78ab28b88ef1c2f07d15cd730073bf339b Mon Sep 17 00:00:00 2001 From: "Ahmed I. Khalil" Date: Tue, 1 Jul 2014 18:28:39 +0200 Subject: [PATCH] Added Triple and Double finger gestures. For right and middle clicking. REVIEW: 119072 --- .../MousePadPlugin/MousePadActivity.java | 73 +++++++++++++------ .../MousePadGestureDetector.java | 59 +++++++++++++++ 2 files changed, 109 insertions(+), 23 deletions(-) create mode 100644 src/main/java/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadGestureDetector.java diff --git a/src/main/java/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadActivity.java b/src/main/java/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadActivity.java index 6cab94bd..31985083 100644 --- a/src/main/java/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadActivity.java +++ b/src/main/java/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadActivity.java @@ -12,8 +12,7 @@ import org.kde.kdeconnect.BackgroundService; import org.kde.kdeconnect.Device; import org.kde.kdeconnect_tp.R; -public class MousePadActivity extends Activity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener { - +public class MousePadActivity extends Activity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener, MousePadGestureDetector.OnGestureListener { private final static float MinDistanceToSendScroll = 2.5f; private float mPrevX; @@ -29,12 +28,15 @@ public class MousePadActivity extends Activity implements GestureDetector.OnGest private GestureDetector mDetector; + private MousePadGestureDetector mMousePadGestureDetector; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mousepad); deviceId = getIntent().getStringExtra("deviceId"); mDetector = new GestureDetector(this, this); + mMousePadGestureDetector = new MousePadGestureDetector(this, this); mDetector.setOnDoubleTapListener(this); } @@ -49,33 +51,22 @@ public class MousePadActivity extends Activity implements GestureDetector.OnGest public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_right_click: - BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() { - @Override - public void onServiceStart(BackgroundService service) { - Device device = service.getDevice(deviceId); - MousePadPlugin mousePadPlugin = (MousePadPlugin)device.getPlugin("plugin_mousepad"); - if (mousePadPlugin == null) return; - mousePadPlugin.sendRightClick(); - } - }); - break; + sendRightClick(); + return true; case R.id.menu_middle_click: - BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() { - @Override - public void onServiceStart(BackgroundService service) { - Device device = service.getDevice(deviceId); - MousePadPlugin mousePadPlugin = (MousePadPlugin)device.getPlugin("plugin_mousepad"); - if (mousePadPlugin == null) return; - mousePadPlugin.sendMiddleClick(); - } - }); - break; + sendMiddleClick(); + return true; + default: + return super.onOptionsItemSelected(item); } - return true; } @Override public boolean onTouchEvent(MotionEvent event) { + if (mMousePadGestureDetector.onTouchEvent(event)) { + return true; + } + if ( mDetector.onTouchEvent(event) ) { return true; } @@ -201,4 +192,40 @@ public class MousePadActivity extends Activity implements GestureDetector.OnGest public boolean onDoubleTapEvent(MotionEvent e) { return false; } + + @Override + public boolean onTripleFingerTap(MotionEvent ev) { + sendMiddleClick(); + return true; + } + + @Override + public boolean onDoubleFingerTap(MotionEvent ev) { + sendRightClick(); + return true; + } + + private void sendMiddleClick() { + BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() { + @Override + public void onServiceStart(BackgroundService service) { + Device device = service.getDevice(deviceId); + MousePadPlugin mousePadPlugin = (MousePadPlugin)device.getPlugin("plugin_mousepad"); + if (mousePadPlugin == null) return; + mousePadPlugin.sendMiddleClick(); + } + }); + } + + private void sendRightClick() { + BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() { + @Override + public void onServiceStart(BackgroundService service) { + Device device = service.getDevice(deviceId); + MousePadPlugin mousePadPlugin = (MousePadPlugin)device.getPlugin("plugin_mousepad"); + if (mousePadPlugin == null) return; + mousePadPlugin.sendRightClick(); + } + }); + } } diff --git a/src/main/java/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadGestureDetector.java b/src/main/java/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadGestureDetector.java new file mode 100644 index 00000000..912e0862 --- /dev/null +++ b/src/main/java/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadGestureDetector.java @@ -0,0 +1,59 @@ +package org.kde.kdeconnect.Plugins.MousePadPlugin; + +import android.content.Context; +import android.view.MotionEvent; +import android.view.ViewConfiguration; + + +public class MousePadGestureDetector { + + private static final int TAP_TIMEOUT = ViewConfiguration.getTapTimeout() + 100; + private OnGestureListener mGestureListener; + + private Context mCtx; + + private long mFirstDownTime = 0; + + private boolean mIsGestureHandled; + + public interface OnGestureListener { + + boolean onTripleFingerTap(MotionEvent ev); + + boolean onDoubleFingerTap(MotionEvent ev); + } + + public MousePadGestureDetector(Context ctx, OnGestureListener gestureListener) { + if (gestureListener == null) { + throw new IllegalArgumentException("gestureListener cannot be null"); + } + mGestureListener = gestureListener; + mCtx = ctx; + } + + public 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; + } +}