Java Code Examples for android.view.MotionEvent#PointerCoords

The following examples show how to use android.view.MotionEvent#PointerCoords . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MotionEventGenerator.java    From FirefoxReality with Mozilla Public License 2.0 6 votes vote down vote up
Device(final int aDevice) {
    mDevice = aDevice;
    mProperties = new MotionEvent.PointerProperties[1];
    mProperties[0] = new MotionEvent.PointerProperties();
    mProperties[0].id = 0;
    mProperties[0].toolType = MotionEvent.TOOL_TYPE_FINGER;
    mCoords = new MotionEvent.PointerCoords[1];
    mCoords[0] = new MotionEvent.PointerCoords();
    mMouseOutCoords = new MotionEvent.PointerCoords[1];
    for (MotionEvent.PointerCoords[] coords : Arrays.asList(mCoords, mMouseOutCoords)) {
        coords[0] = new MotionEvent.PointerCoords();
        coords[0].toolMajor = 2;
        coords[0].toolMinor = 2;
        coords[0].touchMajor = 2;
        coords[0].touchMinor = 2;
    }
    mMouseOutCoords[0].x = -10;
    mMouseOutCoords[0].y = -10;
}
 
Example 2
Source File: MotionEvents.java    From android-test with Apache License 2.0 6 votes vote down vote up
private static MotionEvent downPressICS(
    long downTime, float[] coordinates, float[] precision, int inputDevice, int buttonState) {
  MotionEvent.PointerCoords[] pointerCoords = {new MotionEvent.PointerCoords()};
  MotionEvent.PointerProperties[] pointerProperties = getPointerProperties(inputDevice);
  pointerCoords[0].clear();
  pointerCoords[0].x = coordinates[0];
  pointerCoords[0].y = coordinates[1];
  pointerCoords[0].pressure = 0;
  pointerCoords[0].size = 1;

  return MotionEvent.obtain(
      downTime,
      SystemClock.uptimeMillis(),
      MotionEvent.ACTION_DOWN,
      1, // pointerCount
      pointerProperties,
      pointerCoords,
      0, // metaState
      buttonState,
      precision[0],
      precision[1],
      0, // deviceId
      0, // edgeFlags
      inputDevice,
      0); // flags
}
 
Example 3
Source File: MotionEventTestUtils.java    From fresco with MIT License 6 votes vote down vote up
public static MotionEvent obtainMotionEvent(
    long downTime,
    long eventTime,
    int action,
    int id1,
    float x1,
    float y1,
    int id2,
    float x2,
    float y2) {
  int[] ids = new int[] {id1, id2};
  MotionEvent.PointerCoords[] coords =
      new MotionEvent.PointerCoords[] {createCoords(x1, y1), createCoords(x2, y2)};
  MotionEvent.PointerProperties[] properties = {createProperties(id1), createProperties(id2)};
  return MotionEvent.obtain(
      downTime, eventTime, action, 2, properties, coords, 0, 0, 1.0f, 1.0f, 0, 0, 0, 0);
}
 
Example 4
Source File: CatalystMultitouchHandlingTestCase.java    From react-native-GPay with MIT License 6 votes vote down vote up
private void dispatchEvent(
    final int action,
    final long start,
    final long when,
    final int pointerCount,
    final MotionEvent.PointerProperties[] pointerProps,
    final MotionEvent.PointerCoords[] pointerCoords) {
  getRootView().post(
      new Runnable() {
        @Override
        public void run() {
          MotionEvent event =
              MotionEvent.obtain(start, when, action, pointerCount, pointerProps, pointerCoords, 0, 0, 1.0f, 1.0f, 0, 0, 0, 0);
          getRootView().dispatchTouchEvent(event);
          event.recycle();
        }
      });
  getInstrumentation().waitForIdleSync();
}
 
Example 5
Source File: StickyGridHeadersGridView.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
private static MotionEvent.PointerCoords[] getPointerCoords(MotionEvent e) {
    int n = e.getPointerCount();
    MotionEvent.PointerCoords[] r = new MotionEvent.PointerCoords[n];
    for (int i = 0; i < n; i++) {
        r[i] = new MotionEvent.PointerCoords();
        e.getPointerCoords(i, r[i]);
    }
    return r;
}
 
Example 6
Source File: StickyGridHeadersGridView.java    From StickyGridHeaders with Apache License 2.0 5 votes vote down vote up
private static MotionEvent.PointerCoords[] getPointerCoords(MotionEvent e) {
    int n = e.getPointerCount();
    MotionEvent.PointerCoords[] r = new MotionEvent.PointerCoords[n];
    for (int i = 0; i < n; i++) {
        r[i] = new MotionEvent.PointerCoords();
        e.getPointerCoords(i, r[i]);
    }
    return r;
}
 
Example 7
Source File: MotionEventTestUtils.java    From fresco with MIT License 5 votes vote down vote up
public static MotionEvent obtainMotionEvent(
    long downTime, long eventTime, int action, int id1, float x1, float y1) {
  int[] ids = new int[] {id1};
  MotionEvent.PointerCoords[] coords = new MotionEvent.PointerCoords[] {createCoords(x1, y1)};
  MotionEvent.PointerProperties[] properties = {createProperties(id1)};
  return MotionEvent.obtain(
      downTime, eventTime, action, 1, properties, coords, 0, 0, 1.0f, 1.0f, 0, 0, 0, 0);
}
 
Example 8
Source File: ActionsExecutor.java    From appium-uiautomator2-server with Apache License 2.0 5 votes vote down vote up
private static MotionEvent.PointerCoords[] filterPointerCoordinates(
        final List<MotionInputEventParams> motionEventsParams, final boolean shouldHovering) {
    final List<MotionEvent.PointerCoords> result = new ArrayList<>();
    for (final MotionInputEventParams eventParams : motionEventsParams) {
        if (shouldHovering && HOVERING_ACTIONS.contains(eventParams.actionCode) &&
                eventParams.properties.toolType == MotionEvent.TOOL_TYPE_MOUSE) {
            result.add(eventParams.coordinates);
        } else if (!shouldHovering && !HOVERING_ACTIONS.contains(eventParams.actionCode)) {
            result.add(eventParams.coordinates);
        }
    }
    return result.toArray(new MotionEvent.PointerCoords[0]);
}
 
Example 9
Source File: MotionInputEventParams.java    From appium-uiautomator2-server with Apache License 2.0 5 votes vote down vote up
public MotionInputEventParams(long startDelta, int actionCode, MotionEvent.PointerCoords coordinates,
                              int button, MotionEvent.PointerProperties properties) {
    super();
    this.startDelta = startDelta;
    this.actionCode = actionCode;
    this.coordinates = coordinates;
    this.button = button;
    this.properties = properties;
}
 
Example 10
Source File: StickyGridHeadersGridView.java    From StickyGridHeaders with Apache License 2.0 5 votes vote down vote up
private MotionEvent transformEvent(MotionEvent e, int headerPosition) {
    if (headerPosition == MATCHED_STICKIED_HEADER) {
        return e;
    }

    long downTime = e.getDownTime();
    long eventTime = e.getEventTime();
    int action = e.getAction();
    int pointerCount = e.getPointerCount();
    int[] pointerIds = getPointerIds(e);
    MotionEvent.PointerCoords[] pointerCoords = getPointerCoords(e);
    int metaState = e.getMetaState();
    float xPrecision = e.getXPrecision();
    float yPrecision = e.getYPrecision();
    int deviceId = e.getDeviceId();
    int edgeFlags = e.getEdgeFlags();
    int source = e.getSource();
    int flags = e.getFlags();

    View headerHolder = getChildAt(headerPosition);
    for (int i = 0; i < pointerCount;i++) {
        pointerCoords[i].y -= headerHolder.getTop();
    }
    MotionEvent n = MotionEvent.obtain(downTime, eventTime, action,
            pointerCount, pointerIds, pointerCoords, metaState, xPrecision,
            yPrecision, deviceId, edgeFlags, source, flags);
    return n;
}
 
Example 11
Source File: StickyGridHeadersGridView.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
private MotionEvent transformEvent(MotionEvent e, int headerPosition) {
    if (headerPosition == MATCHED_STICKIED_HEADER) {
        return e;
    }

    long downTime = e.getDownTime();
    long eventTime = e.getEventTime();
    int action = e.getAction();
    int pointerCount = e.getPointerCount();
    int[] pointerIds = getPointerIds(e);
    MotionEvent.PointerCoords[] pointerCoords = getPointerCoords(e);
    int metaState = e.getMetaState();
    float xPrecision = e.getXPrecision();
    float yPrecision = e.getYPrecision();
    int deviceId = e.getDeviceId();
    int edgeFlags = e.getEdgeFlags();
    int source = e.getSource();
    int flags = e.getFlags();

    View headerHolder = getChildAt(headerPosition);
    for (int i = 0; i < pointerCount;i++) {
        pointerCoords[i].y -= headerHolder.getTop();
    }
    MotionEvent n = MotionEvent.obtain(downTime, eventTime, action,
            pointerCount, pointerIds, pointerCoords, metaState, xPrecision,
            yPrecision, deviceId, edgeFlags, source, flags);
    return n;
}
 
Example 12
Source File: StickyGridHeadersGridView.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
private static MotionEvent.PointerCoords[] getPointerCoords(MotionEvent e) {
    int n = e.getPointerCount();
    MotionEvent.PointerCoords[] r = new MotionEvent.PointerCoords[n];
    for (int i = 0; i < n; i++) {
        r[i] = new MotionEvent.PointerCoords();
        e.getPointerCoords(i, r[i]);
    }
    return r;
}
 
Example 13
Source File: ServoPanZoomController.java    From FirefoxReality with Mozilla Public License 2.0 5 votes vote down vote up
private boolean handleScrollEvent(MotionEvent event) {
    if (!mAttached) {
        mQueuedEvents.add(new Pair(EVENT_SOURCE_SCROLL, event));
        return false;
    }

    if (event.getPointerCount() <= 0) {
        return false;
    }

    final MotionEvent.PointerCoords coords = new MotionEvent.PointerCoords();
    event.getPointerCoords(0, coords);

    mSession.getSurfaceBounds(mTempRect);
    final float x = coords.x - mTempRect.left;
    final float y = coords.y - mTempRect.top;

    final float hScroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL) * mPointerScrollFactor;
    final float vScroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL) * mPointerScrollFactor;

    if (!mIsScrolling) {
        mSession.scrollStart((int) hScroll, (int) vScroll, (int) x, (int) y);
    } else {
        mSession.scroll((int) hScroll, (int) vScroll, (int) x, (int) y);
    }
    mIsScrolling = true;
    return true;
}
 
Example 14
Source File: ServoPanZoomController.java    From FirefoxReality with Mozilla Public License 2.0 5 votes vote down vote up
private boolean handleMotionEvent(MotionEvent event) {
    if (!mAttached) {
        mQueuedEvents.add(new Pair(EVENT_SOURCE_MOTION, event));
        return false;
    }

    if (event.getPointerCount() <= 0) {
        return false;
    }

    final int action = event.getActionMasked();

    if (action == MotionEvent.ACTION_DOWN) {
        mLastDownTime = event.getDownTime();
    } else if (mLastDownTime != event.getDownTime()) {
        return false;
    }

    final MotionEvent.PointerCoords coords = new MotionEvent.PointerCoords();
    event.getPointerCoords(0, coords);

    if (action == MotionEvent.ACTION_UP) {
        mSession.click((int) coords.x, (int) coords.y);
    }

    return true;
}
 
Example 15
Source File: StickyGridHeadersGridView.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
private MotionEvent transformEvent(MotionEvent e, int headerPosition) {
    if (headerPosition == MATCHED_STICKIED_HEADER) {
        return e;
    }

    long downTime = e.getDownTime();
    long eventTime = e.getEventTime();
    int action = e.getAction();
    int pointerCount = e.getPointerCount();
    int[] pointerIds = getPointerIds(e);
    MotionEvent.PointerCoords[] pointerCoords = getPointerCoords(e);
    int metaState = e.getMetaState();
    float xPrecision = e.getXPrecision();
    float yPrecision = e.getYPrecision();
    int deviceId = e.getDeviceId();
    int edgeFlags = e.getEdgeFlags();
    int source = e.getSource();
    int flags = e.getFlags();

    View headerHolder = getChildAt(headerPosition);
    for (int i = 0; i < pointerCount;i++) {
        pointerCoords[i].y -= headerHolder.getTop();
    }
    MotionEvent n = MotionEvent.obtain(downTime, eventTime, action,
            pointerCount, pointerIds, pointerCoords, metaState, xPrecision,
            yPrecision, deviceId, edgeFlags, source, flags);
    return n;
}
 
Example 16
Source File: InAppInputManager.java    From CatVision-io-SDK-Android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void injectTouchEvent(int buttonId, int event, int x, int y)
{
	final View view = obtainTargetView();
	if (view == null) return;
	final Activity activity = obtainActivity();
	if (activity == null) return;

	int viewLocation[] = new int[2];
	view.getLocationOnScreen(viewLocation);

	MotionEvent.PointerProperties pp = new MotionEvent.PointerProperties();
	pp.toolType = MotionEvent.TOOL_TYPE_FINGER;
	pp.id = 0;
	MotionEvent.PointerProperties[] pps = new MotionEvent.PointerProperties[]{pp};

	MotionEvent.PointerCoords pc = new MotionEvent.PointerCoords();
	pc.size = 1;
	pc.pressure = 1;
	pc.x = x - viewLocation[0];
	pc.y = y - viewLocation[1];
	MotionEvent.PointerCoords[] pcs = new MotionEvent.PointerCoords[]{pc};

	long t = SystemClock.uptimeMillis();

	final MotionEvent e = MotionEvent.obtain(
			t,          // long downTime
			t + 100,    // long eventTime
			event,      // int action
			pps.length, // int pointerCount
			pps,        // MotionEvent.PointerProperties[] pointerProperties
			pcs,        // MotionEvent.PointerCoords[] pointerCoords
			0,          // int metaState
			0,          // int buttonState
			1,          // float xPrecision
			1,          // float yPrecision
			1,          // int deviceId
			0,          // int edgeFlags
			InputDevice.SOURCE_TOUCHSCREEN, //int source
			0           // int flags
	);

	activity.runOnUiThread(new Runnable() {
		public void run() {

			view.dispatchTouchEvent(e);
		}
	});
}
 
Example 17
Source File: ActionsTokenizer.java    From appium-uiautomator2-server with Apache License 2.0 4 votes vote down vote up
private static MotionEvent.PointerCoords extractCoordinates(final String actionId,
                                                            final List<W3CGestureModel> gestures,
                                                            final int itemIdx) {
    if (itemIdx < 0) {
        throw new ActionsParseException(String.format(
                "The first item of action '%s' cannot define HOVER move, " +
                        "because its start coordinates are not set", actionId));
    }
    final W3CGestureModel gesture = gestures.get(itemIdx);
    if (!ACTION_ITEM_TYPE_POINTER_MOVE.equals(gesture.type)) {
        if (itemIdx > 0) {
            return extractCoordinates(actionId, gestures, itemIdx - 1);
        }
        throw new ActionsParseException(String.format(
                "Action item '%s' of action '%s' should be preceded with at least one item " +
                        "with coordinates", gesture, actionId));
    }
    Object origin = gesture.origin == null ? ACTION_ITEM_ORIGIN_VIEWPORT : gesture.origin;
    final MotionEvent.PointerCoords result = new MotionEvent.PointerCoords();
    result.size = gesture.size == null ? 1 : gesture.size.floatValue();
    result.pressure = gesture.pressure == null ? 1 : gesture.pressure.floatValue();
    if (origin instanceof String) {
        if (origin.equals(ACTION_ITEM_ORIGIN_VIEWPORT)) {
            if (gesture.x == null || gesture.y == null) {
                throw new ActionsParseException(String.format(
                        "Both coordinates must be be set for action item '%s' of action '%s'",
                        gesture, actionId));
            }
            result.x = gesture.x.floatValue();
            result.y = gesture.y.floatValue();
            return result;
        } else if (origin.equals(ACTION_ITEM_ORIGIN_POINTER)) {
            if (itemIdx > 0) {
                final MotionEvent.PointerCoords recentCoords = extractCoordinates(actionId, gestures, itemIdx - 1);
                result.x = recentCoords.x;
                result.y = recentCoords.y;
                if (gesture.x != null) {
                    result.x += gesture.x.floatValue();
                }
                if (gesture.y != null) {
                    result.y += gesture.y.floatValue();
                }
                return result;
            }
            throw new ActionsParseException(String.format(
                    "Action item '%s' of action '%s' should be preceded with at least one item " +
                            "containing absolute coordinates", gesture, actionId));
        }
    }
    return extractElementCoordinates(actionId, gesture, origin);
}
 
Example 18
Source File: MotionEventTestUtils.java    From fresco with MIT License 4 votes vote down vote up
public static MotionEvent.PointerCoords createCoords(float x, float y) {
  MotionEvent.PointerCoords pointerCoords = new MotionEvent.PointerCoords();
  pointerCoords.x = x;
  pointerCoords.y = y;
  return pointerCoords;
}
 
Example 19
Source File: CatalystMultitouchHandlingTestCase.java    From react-native-GPay with MIT License 4 votes vote down vote up
private MotionEvent.PointerCoords createPointerCoords(float x, float y) {
  MotionEvent.PointerCoords pointerCoords = new MotionEvent.PointerCoords();
  pointerCoords.x = x;
  pointerCoords.y = y;
  return pointerCoords;
}
 
Example 20
Source File: VideoStreamManager.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
List<MotionEvent> convertTouchEvent(OnTouchEvent onTouchEvent){
	List<MotionEvent> motionEventList = new ArrayList<MotionEvent>();

	List<TouchEvent> touchEventList = onTouchEvent.getEvent();
	if (touchEventList == null || touchEventList.size() == 0) return null;

	TouchType touchType = onTouchEvent.getType();
	if (touchType == null) { return null; }

	if(sdlMotionEvent == null) {
		if (touchType == TouchType.BEGIN) {
			sdlMotionEvent = new SdlMotionEvent();
		} else{
			return null;
		}
	}

	SdlMotionEvent.Pointer pointer;
	MotionEvent motionEvent;

	for (TouchEvent touchEvent : touchEventList) {
		if (touchEvent == null || touchEvent.getId() == null) {
			continue;
		}

		List<TouchCoord> touchCoordList = touchEvent.getTouchCoordinates();
		if (touchCoordList == null || touchCoordList.size() == 0) {
			continue;
		}

		TouchCoord touchCoord = touchCoordList.get(touchCoordList.size() - 1);
		if (touchCoord == null) {
			continue;
		}

		int motionEventAction = sdlMotionEvent.getMotionEventAction(touchType, touchEvent);
		long downTime = sdlMotionEvent.downTime;
		long eventTime = sdlMotionEvent.eventTime;
		pointer = sdlMotionEvent.getPointerById(touchEvent.getId());
		if (pointer != null) {
			pointer.setCoords(touchCoord.getX() * touchScalar[0], touchCoord.getY() * touchScalar[1]);
		}

		MotionEvent.PointerProperties[] pointerProperties = new MotionEvent.PointerProperties[sdlMotionEvent.pointers.size()];
		MotionEvent.PointerCoords[] pointerCoords = new MotionEvent.PointerCoords[sdlMotionEvent.pointers.size()];

		for (int i = 0; i < sdlMotionEvent.pointers.size(); i++) {
			pointerProperties[i] = new MotionEvent.PointerProperties();
			pointerProperties[i].id = sdlMotionEvent.getPointerByIndex(i).id;
			pointerProperties[i].toolType = MotionEvent.TOOL_TYPE_FINGER;

			pointerCoords[i] = new MotionEvent.PointerCoords();
			pointerCoords[i].x = sdlMotionEvent.getPointerByIndex(i).x;
			pointerCoords[i].y = sdlMotionEvent.getPointerByIndex(i).y;
			pointerCoords[i].orientation = 0;
			pointerCoords[i].pressure = 1.0f;
			pointerCoords[i].size = 1;
		}

		motionEvent = MotionEvent.obtain(downTime, eventTime, motionEventAction,
				sdlMotionEvent.pointers.size(), pointerProperties, pointerCoords, 0, 0, 1,
				1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0);
		motionEventList.add(motionEvent);

		if(motionEventAction == MotionEvent.ACTION_UP || motionEventAction == MotionEvent.ACTION_CANCEL){
			//If the motion event should be finished we should clear our reference
			sdlMotionEvent.pointers.clear();
			sdlMotionEvent = null;
			break;
		} else if((motionEventAction & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_UP){
			sdlMotionEvent.removePointerById(touchEvent.getId());
		}
	}

	return motionEventList;
}