Update CursorOperation

This commit is contained in:
Victor 2014-03-05 17:34:05 +02:00
parent 2825ad74c3
commit 13763f7ed8

View File

@ -3,8 +3,10 @@ package com.annimon.socketfiletransfer.operations;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.widget.Button;
import com.annimon.socketfiletransfer.OperationListener; import com.annimon.socketfiletransfer.OperationListener;
import com.annimon.socketfiletransfer.R;
import com.annimon.socketfiletransfer.TouchpadView; import com.annimon.socketfiletransfer.TouchpadView;
import com.annimon.socketfiletransfer.util.ExceptionHandler; import com.annimon.socketfiletransfer.util.ExceptionHandler;
@ -15,6 +17,7 @@ import java.io.IOException;
*/ */
public class CursorOperation extends Operation { public class CursorOperation extends Operation {
private static final int DIVIDER = 3;
private static final int private static final int
NONE = -1, NONE = -1,
TYPE_MOVE = 1, TYPE_MOVE = 1,
@ -22,9 +25,13 @@ public class CursorOperation extends Operation {
TYPE_DRAG = 3, TYPE_DRAG = 3,
TYPE_RELEASED = 4, TYPE_RELEASED = 4,
TYPE_KEY_RELEASED = 5, TYPE_KEY_RELEASED = 5,
TYPE_MOVE_RELATIVE = 6,
TYPE_MOUSE_PRESSED = 7,
TYPE_MOUSE_RELEASED = 8,
STOP = 10; STOP = 10;
private boolean running; private boolean running;
private int startX, startY;
@Override @Override
public void startServerSide() throws Exception { public void startServerSide() throws Exception {
@ -34,13 +41,17 @@ public class CursorOperation extends Operation {
@Override @Override
public void startClientSide(Object... params) throws Exception { public void startClientSide(Object... params) throws Exception {
TouchpadView view = (TouchpadView) params[0]; TouchpadView view = (TouchpadView) params[0];
for (int i = 1; i <= 3; i++) {
// ((Button)params[i]).setOnClickListener(clickListener);
((Button)params[i]).setOnTouchListener(buttonTouchListener);
}
running = true; running = true;
dos.writeInt(OperationListener.MODE_CURSOR_CONTROL); dos.writeInt(OperationListener.MODE_CURSOR_CONTROL);
// Send view size // Android touchpad needs to send particular signal
dos.writeInt(view.getWidth()); dos.writeInt(-100);
dos.writeInt(view.getHeight()); dos.writeInt(-100);
view.requestFocus(); view.requestFocus();
view.setOnTouchListener(touchListener); view.setOnTouchListener(touchListener);
@ -53,38 +64,69 @@ public class CursorOperation extends Operation {
view.closeActivity(); 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 @Override
public boolean onTouch(View view, MotionEvent motionEvent) { public boolean onTouch(View view, MotionEvent motionEvent) {
if (!running) return true; if (!running) return true;
int x = (int) motionEvent.getX(); final int x = (int) motionEvent.getX();
int y = (int) motionEvent.getY(); final int y = (int) motionEvent.getY();
int action = motionEvent.getAction() & MotionEvent.ACTION_MASK; final int action = motionEvent.getAction() & MotionEvent.ACTION_MASK;
if (action == MotionEvent.ACTION_DOWN) { if (action == MotionEvent.ACTION_DOWN) {
try { startX = x;
dos.writeInt(TYPE_MOVE); startY = y;
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) { }
}
} else if ( (action == MotionEvent.ACTION_UP) || (action == MotionEvent.ACTION_CANCEL) ) { } else if ( (action == MotionEvent.ACTION_UP) || (action == MotionEvent.ACTION_CANCEL) ) {
try { try {
dos.writeInt(TYPE_RELEASED); dos.writeInt(TYPE_RELEASED);
} catch (IOException ex) { } } catch (IOException ex) { }
} else if (action == MotionEvent.ACTION_MOVE) { } else if (action == MotionEvent.ACTION_MOVE) {
try { try {
dos.writeInt(TYPE_DRAG); dos.writeInt(TYPE_MOVE_RELATIVE);
dos.writeInt(x); dos.writeInt((x - startX) / DIVIDER);
dos.writeInt(y); dos.writeInt((y - startY) / DIVIDER);
} catch (IOException ex) { } } 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 @Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) { public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {