diff --git a/SocketFileTransfer/src/main/java/com/annimon/socketfiletransfer/operations/CursorOperation.java b/SocketFileTransfer/src/main/java/com/annimon/socketfiletransfer/operations/CursorOperation.java index fac2d56..974c4bc 100644 --- a/SocketFileTransfer/src/main/java/com/annimon/socketfiletransfer/operations/CursorOperation.java +++ b/SocketFileTransfer/src/main/java/com/annimon/socketfiletransfer/operations/CursorOperation.java @@ -3,8 +3,10 @@ package com.annimon.socketfiletransfer.operations; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; +import android.widget.Button; import com.annimon.socketfiletransfer.OperationListener; +import com.annimon.socketfiletransfer.R; import com.annimon.socketfiletransfer.TouchpadView; import com.annimon.socketfiletransfer.util.ExceptionHandler; @@ -15,6 +17,7 @@ import java.io.IOException; */ public class CursorOperation extends Operation { + private static final int DIVIDER = 3; private static final int NONE = -1, TYPE_MOVE = 1, @@ -22,9 +25,13 @@ public class CursorOperation extends Operation { TYPE_DRAG = 3, TYPE_RELEASED = 4, TYPE_KEY_RELEASED = 5, + TYPE_MOVE_RELATIVE = 6, + TYPE_MOUSE_PRESSED = 7, + TYPE_MOUSE_RELEASED = 8, STOP = 10; private boolean running; + private int startX, startY; @Override public void startServerSide() throws Exception { @@ -34,13 +41,17 @@ public class CursorOperation extends Operation { @Override public void startClientSide(Object... params) throws Exception { TouchpadView view = (TouchpadView) params[0]; + for (int i = 1; i <= 3; i++) { +// ((Button)params[i]).setOnClickListener(clickListener); + ((Button)params[i]).setOnTouchListener(buttonTouchListener); + } running = true; dos.writeInt(OperationListener.MODE_CURSOR_CONTROL); - // Send view size - dos.writeInt(view.getWidth()); - dos.writeInt(view.getHeight()); + // Android touchpad needs to send particular signal + dos.writeInt(-100); + dos.writeInt(-100); view.requestFocus(); view.setOnTouchListener(touchListener); @@ -53,38 +64,69 @@ public class CursorOperation extends Operation { view.closeActivity(); } - private View.OnTouchListener touchListener = new View.OnTouchListener() { + private final View.OnTouchListener buttonTouchListener = new View.OnTouchListener() { + + @Override + public boolean onTouch(View v, MotionEvent event) { + int buttonMask = 0; + switch (v.getId()) { + case R.id.left_button: + buttonMask = 16; + break; + case R.id.middle_button: + buttonMask = 8; + break; + case R.id.right_button: + buttonMask = 4; + break; + default: return true; + } + + final int action = event.getAction() & MotionEvent.ACTION_MASK; + int type = 0; + switch (action) { + case MotionEvent.ACTION_DOWN: + type = TYPE_MOUSE_PRESSED; + break; + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_CANCEL: + type = TYPE_MOUSE_RELEASED; + break; + default: return true; + } + + try { + dos.writeInt(type); + dos.writeInt(buttonMask); + } catch (IOException e) { + e.printStackTrace(); + } + + return true; + } + }; + + private final View.OnTouchListener touchListener = new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (!running) return true; - int x = (int) motionEvent.getX(); - int y = (int) motionEvent.getY(); - int action = motionEvent.getAction() & MotionEvent.ACTION_MASK; + final int x = (int) motionEvent.getX(); + final int y = (int) motionEvent.getY(); + final int action = motionEvent.getAction() & MotionEvent.ACTION_MASK; if (action == MotionEvent.ACTION_DOWN) { - try { - dos.writeInt(TYPE_MOVE); - dos.writeInt(x); - dos.writeInt(y); - } catch (IOException ex) { } - // Click event - if (motionEvent.getPointerCount() > 1) { - try { - dos.writeInt(TYPE_CLICK); - int mask = motionEvent.getPointerCount() - 2; - dos.writeInt(16 >> mask); - } catch (IOException ex) { } - } + startX = x; + startY = y; } else if ( (action == MotionEvent.ACTION_UP) || (action == MotionEvent.ACTION_CANCEL) ) { try { dos.writeInt(TYPE_RELEASED); } catch (IOException ex) { } } else if (action == MotionEvent.ACTION_MOVE) { try { - dos.writeInt(TYPE_DRAG); - dos.writeInt(x); - dos.writeInt(y); + dos.writeInt(TYPE_MOVE_RELATIVE); + dos.writeInt((x - startX) / DIVIDER); + dos.writeInt((y - startY) / DIVIDER); } catch (IOException ex) { } } @@ -92,7 +134,7 @@ public class CursorOperation extends Operation { } }; - private View.OnKeyListener keyListener = new View.OnKeyListener() { + private final View.OnKeyListener keyListener = new View.OnKeyListener() { @Override public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {