@Override public boolean onTouchEvent(MotionEvent event) { float mCurX; float mCurY; // API level 9 and above supports "Major" property on event which // gives // the size of the touch area at the point of contact // so for now we just hard code float TOUCH_AREA_SIZE = penWidth; int action = event.getAction(); if (action != MotionEvent.ACTION_UP && action != MotionEvent.ACTION_CANCEL) { int N = event.getHistorySize(); int P = event.getPointerCount(); for (int i = 0; i < N; i++) { for (int j = 0; j < P; j++) { mCurX = event.getHistoricalX(j, i); mCurY = event.getHistoricalY(j, i); drawPoint(mCurX, mCurY, event.getHistoricalPressure(j, i), TOUCH_AREA_SIZE); } } for (int j = 0; j < P; j++) { mCurX = event.getX(j); mCurY = event.getY(j); drawPoint(mCurX, mCurY, event.getPressure(j), TOUCH_AREA_SIZE); } } unsaved = true; return true; }
/** * Extracts the touch point data from a MotionEvent, converts each point into a marshallable * object and passes the set of points to the JNI layer to be transmitted to the remote host. * * @param event The event to send to the remote host for injection. NOTE: This object must be * updated to represent the remote machine's coordinate system before calling this function. */ public void sendTouchEvent(MotionEvent event) { int action = event.getActionMasked(); TouchEventData.EventType touchEventType = TouchEventData.EventType.fromMaskedAction(action); List<TouchEventData> touchEventList = new ArrayList<TouchEventData>(); if (action == MotionEvent.ACTION_MOVE) { // In order to process all of the events associated with an ACTION_MOVE event, we need // to walk the list of historical events in order and add each event to our list, then // retrieve the current move event data. int pointerCount = event.getPointerCount(); int historySize = event.getHistorySize(); for (int h = 0; h < historySize; ++h) { for (int p = 0; p < pointerCount; ++p) { touchEventList.add( new TouchEventData( event.getPointerId(p), event.getHistoricalX(p, h), event.getHistoricalY(p, h), event.getHistoricalSize(p, h), event.getHistoricalSize(p, h), event.getHistoricalOrientation(p, h), event.getHistoricalPressure(p, h))); } } for (int p = 0; p < pointerCount; p++) { touchEventList.add( new TouchEventData( event.getPointerId(p), event.getX(p), event.getY(p), event.getSize(p), event.getSize(p), event.getOrientation(p), event.getPressure(p))); } } else { // For all other events, we only want to grab the current/active pointer. The event // contains a list of every active pointer but passing all of of these to the host can // cause confusion on the remote OS side and result in broken touch gestures. int activePointerIndex = event.getActionIndex(); touchEventList.add( new TouchEventData( event.getPointerId(activePointerIndex), event.getX(activePointerIndex), event.getY(activePointerIndex), event.getSize(activePointerIndex), event.getSize(activePointerIndex), event.getOrientation(activePointerIndex), event.getPressure(activePointerIndex))); } if (!touchEventList.isEmpty()) { mInjector.sendTouchEvent(touchEventType, touchEventList.toArray(new TouchEventData[0])); } }