Java Code Examples for android.view.MotionEvent#TOOL_TYPE_FINGER

The following examples show how to use android.view.MotionEvent#TOOL_TYPE_FINGER . 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: ActionsTokenizer.java    From appium-uiautomator2-server with Apache License 2.0 6 votes vote down vote up
private static int actionToToolType(final W3CItemModel item) {
    if (item.parameters != null && item.parameters.pointerType != null) {
        switch (item.parameters.pointerType) {
            case POINTER_TYPE_MOUSE:
                return MotionEvent.TOOL_TYPE_MOUSE;
            case POINTER_TYPE_PEN:
                return MotionEvent.TOOL_TYPE_STYLUS;
            case POINTER_TYPE_TOUCH:
                return MotionEvent.TOOL_TYPE_FINGER;
            default:
                // use default
                break;
        }
    }
    return MotionEvent.TOOL_TYPE_FINGER;
}
 
Example 3
Source File: MotionEvents.java    From android-test with Apache License 2.0 6 votes vote down vote up
private static MotionEvent.PointerProperties[] getPointerProperties(int inputDevice) {
  MotionEvent.PointerProperties[] pointerProperties = {new MotionEvent.PointerProperties()};
  pointerProperties[0].clear();
  pointerProperties[0].id = 0;
  switch (inputDevice) {
    case InputDevice.SOURCE_MOUSE:
      pointerProperties[0].toolType = MotionEvent.TOOL_TYPE_MOUSE;
      break;
    case InputDevice.SOURCE_STYLUS:
      pointerProperties[0].toolType = MotionEvent.TOOL_TYPE_STYLUS;
      break;
    case InputDevice.SOURCE_TOUCHSCREEN:
      pointerProperties[0].toolType = MotionEvent.TOOL_TYPE_FINGER;
      break;
    default:
      pointerProperties[0].toolType = MotionEvent.TOOL_TYPE_UNKNOWN;
      break;
  }
  return pointerProperties;
}
 
Example 4
Source File: GenericTouchGesture.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
TouchPointer(int startX, int startY, int deltaX, int deltaY,
        int id, float scale, int scaledTouchSlop) {
    mStartX = startX * scale;
    mStartY = startY * scale;

    float scaledDeltaX = deltaX * scale;
    float scaledDeltaY = deltaY * scale;

    if (scaledDeltaX != 0 || scaledDeltaY != 0) {
        // The touch handler only considers a pointer as moving once
        // it's been moved by more than scaledTouchSlop pixels. We
        // thus increase the delta distance so the move is actually
        // registered as covering the specified distance.
        float distance = (float)Math.sqrt(scaledDeltaX * scaledDeltaX +
                scaledDeltaY * scaledDeltaY);
        mDeltaX = scaledDeltaX * (1 + scaledTouchSlop / distance);
        mDeltaY = scaledDeltaY * (1 + scaledTouchSlop / distance);
    }
    else {
        mDeltaX = scaledDeltaX;
        mDeltaY = scaledDeltaY;
    }

    if (deltaX != 0 || deltaY != 0) {
        mStepX = mDeltaX / Math.abs(mDeltaX + mDeltaY);
        mStepY = mDeltaY / Math.abs(mDeltaX + mDeltaY);
    } else {
        mStepX = 0;
        mStepY = 0;
    }

    mProperties = new PointerProperties();
    mProperties.id = id;
    mProperties.toolType = MotionEvent.TOOL_TYPE_FINGER;

    mCoords = new PointerCoords();
    mCoords.x = mStartX;
    mCoords.y = mStartY;
    mCoords.pressure = 1.0f;
}
 
Example 5
Source File: GestureController.java    From za-Farmer with MIT License 5 votes vote down vote up
public Pointer(int id, Point point) {
    prop = new PointerProperties();
    prop.id = id;
    prop.toolType = MotionEvent.TOOL_TYPE_FINGER;
    coords = new PointerCoords();
    coords.pressure = 1;
    coords.size = 1;
    coords.x = point.x;
    coords.y = point.y;
}
 
Example 6
Source File: GenericTouchGesture.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
TouchPointer(int startX, int startY, int deltaX, int deltaY,
        int id, float scale, int scaledTouchSlop) {
    mStartX = startX * scale;
    mStartY = startY * scale;

    float scaledDeltaX = deltaX * scale;
    float scaledDeltaY = deltaY * scale;

    if (scaledDeltaX != 0 || scaledDeltaY != 0) {
        // The touch handler only considers a pointer as moving once
        // it's been moved by more than scaledTouchSlop pixels. We
        // thus increase the delta distance so the move is actually
        // registered as covering the specified distance.
        float distance = (float)Math.sqrt(scaledDeltaX * scaledDeltaX +
                scaledDeltaY * scaledDeltaY);
        mDeltaX = scaledDeltaX * (1 + scaledTouchSlop / distance);
        mDeltaY = scaledDeltaY * (1 + scaledTouchSlop / distance);
    }
    else {
        mDeltaX = scaledDeltaX;
        mDeltaY = scaledDeltaY;
    }

    if (deltaX != 0 || deltaY != 0) {
        mStepX = mDeltaX / Math.abs(mDeltaX + mDeltaY);
        mStepY = mDeltaY / Math.abs(mDeltaX + mDeltaY);
    } else {
        mStepX = 0;
        mStepY = 0;
    }

    mProperties = new PointerProperties();
    mProperties.id = id;
    mProperties.toolType = MotionEvent.TOOL_TYPE_FINGER;

    mCoords = new PointerCoords();
    mCoords.x = mStartX;
    mCoords.y = mStartY;
    mCoords.pressure = 1.0f;
}
 
Example 7
Source File: GameWebView.java    From kcanotify_h5-master with GNU General Public License v3.0 5 votes vote down vote up
public void handleTouch(MotionEvent event) {
    Log.d("touchEvent", event.getToolType(0) + ":" + event.getActionMasked());
    if(event.getToolType(0) == MotionEvent.TOOL_TYPE_FINGER) {
        if(event.getAction() == MotionEvent.ACTION_MOVE) {
            buildMoveEvent(event);
        } else if(event.getAction() == MotionEvent.ACTION_DOWN && changeTouchEvent){
            buildMoveEvent(event);
        } else if(event.getAction() == MotionEvent.ACTION_UP && changeTouchEvent){
            buildMoveEvent(event);
        }
    }
}
 
Example 8
Source File: GestureController.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
public Pointer(int id, Point point) {
    prop = new PointerProperties();
    prop.id = id;
    prop.toolType = MotionEvent.TOOL_TYPE_FINGER;
    coords = new PointerCoords();
    coords.pressure = 1;
    coords.size = 1;
    coords.x = point.x;
    coords.y = point.y;
}
 
Example 9
Source File: GestureListener.java    From input-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a human-readable string describing the type of touch that triggered a MotionEvent.
 */

private static String getTouchType(MotionEvent e){

    String touchTypeDescription = " ";
    int touchType = e.getToolType(0);

    switch (touchType) {
        case MotionEvent.TOOL_TYPE_FINGER:
            touchTypeDescription += "(finger)";
            break;
        case MotionEvent.TOOL_TYPE_STYLUS:
            touchTypeDescription += "(stylus, ";
            //Get some additional information about the stylus touch
            float stylusPressure = e.getPressure();
            touchTypeDescription += "pressure: " + stylusPressure;

            if(Build.VERSION.SDK_INT >= 21) {
                touchTypeDescription += ", buttons pressed: " + getButtonsPressed(e);
            }

            touchTypeDescription += ")";
            break;
        case MotionEvent.TOOL_TYPE_ERASER:
            touchTypeDescription += "(eraser)";
            break;
        case MotionEvent.TOOL_TYPE_MOUSE:
            touchTypeDescription += "(mouse)";
            break;
        default:
            touchTypeDescription += "(unknown tool)";
            break;
    }

    return touchTypeDescription;
}
 
Example 10
Source File: GestureListener.java    From android-BasicGestureDetect with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a human-readable string describing the type of touch that triggered a MotionEvent.
 */

private static String getTouchType(MotionEvent e){

    String touchTypeDescription = " ";
    int touchType = e.getToolType(0);

    switch (touchType) {
        case MotionEvent.TOOL_TYPE_FINGER:
            touchTypeDescription += "(finger)";
            break;
        case MotionEvent.TOOL_TYPE_STYLUS:
            touchTypeDescription += "(stylus, ";
            //Get some additional information about the stylus touch
            float stylusPressure = e.getPressure();
            touchTypeDescription += "pressure: " + stylusPressure;

            if(Build.VERSION.SDK_INT >= 21) {
                touchTypeDescription += ", buttons pressed: " + getButtonsPressed(e);
            }

            touchTypeDescription += ")";
            break;
        case MotionEvent.TOOL_TYPE_ERASER:
            touchTypeDescription += "(eraser)";
            break;
        case MotionEvent.TOOL_TYPE_MOUSE:
            touchTypeDescription += "(mouse)";
            break;
        default:
            touchTypeDescription += "(unknown tool)";
            break;
    }

    return touchTypeDescription;
}
 
Example 11
Source File: Swiper.java    From AndroidRipper with GNU Affero General Public License v3.0 4 votes vote down vote up
public void generateSwipeGesture(PointF startPoint1, PointF startPoint2,
        PointF endPoint1, PointF endPoint2)
{

    long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis();

    float startX1 = startPoint1.x;
    float startY1 = startPoint1.y;
    float startX2 = startPoint2.x;
    float startY2 = startPoint2.y;

    float endX1 = endPoint1.x;
    float endY1 = endPoint1.y;
    float endX2 = endPoint2.x;
    float endY2 = endPoint2.y;

    // pointer 1
    float x1 = startX1;
    float y1 = startY1;

    // pointer 2
    float x2 = startX2;
    float y2 = startY2;

    PointerCoords[] pointerCoords = new PointerCoords[2];
    PointerCoords pc1 = new PointerCoords();
    PointerCoords pc2 = new PointerCoords();
    pc1.x = x1;
    pc1.y = y1;
    pc1.pressure = 1;
    pc1.size = 1;
    pc2.x = x2;
    pc2.y = y2;
    pc2.pressure = 1;
    pc2.size = 1;
    pointerCoords[0] = pc1;
    pointerCoords[1] = pc2;

    PointerProperties[] pointerProperties = new PointerProperties[2];
    PointerProperties pp1 = new PointerProperties();
    PointerProperties pp2 = new PointerProperties();
    pp1.id = 0;
    pp1.toolType = MotionEvent.TOOL_TYPE_FINGER;
    pp2.id = 1;
    pp2.toolType = MotionEvent.TOOL_TYPE_FINGER;
    pointerProperties[0] = pp1;
    pointerProperties[1] = pp2;

    MotionEvent event;
    // send the initial touches
    event = MotionEvent.obtain(downTime, eventTime,
            MotionEvent.ACTION_DOWN, 1, pointerProperties, pointerCoords,
            0, 0, // metaState, buttonState
            1, // x precision
            1, // y precision
            0, 0, 0, 0); // deviceId, edgeFlags, source, flags
    _instrument.sendPointerSync(event);

    event = MotionEvent.obtain(downTime, eventTime,
            MotionEvent.ACTION_POINTER_DOWN
                    + (pp2.id << MotionEvent.ACTION_POINTER_INDEX_SHIFT),
            2, pointerProperties, pointerCoords, 0, 0, 1, 1, 0, 0, 0, 0);
    _instrument.sendPointerSync(event);

    int numMoves = GESTURE_DURATION_MS / EVENT_TIME_INTERVAL_MS;

    float stepX1 = (endX1 - startX1) / numMoves;
    float stepY1 = (endY1 - startY1) / numMoves;
    float stepX2 = (endX2 - startX2) / numMoves;
    float stepY2 = (endY2 - startY2) / numMoves;

    // send the zoom
    for (int i = 0; i < numMoves; i++)
    {
        eventTime += EVENT_TIME_INTERVAL_MS;
        pointerCoords[0].x += stepX1;
        pointerCoords[0].y += stepY1;
        pointerCoords[1].x += stepX2;
        pointerCoords[1].y += stepY2;

        event = MotionEvent.obtain(downTime, eventTime,
                MotionEvent.ACTION_MOVE, 2, pointerProperties,
                pointerCoords, 0, 0, 1, 1, 0, 0, 0, 0);
        _instrument.sendPointerSync(event);
    }
}
 
Example 12
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;
}
 
Example 13
Source File: MyInteractionController.java    From PUMA with Apache License 2.0 4 votes vote down vote up
/**
 * Performs a multi-touch gesture
 *
 * Takes a series of touch coordinates for at least 2 pointers. Each pointer must have
 * all of its touch steps defined in an array of {@link PointerCoords}. By having the ability
 * to specify the touch points along the path of a pointer, the caller is able to specify
 * complex gestures like circles, irregular shapes etc, where each pointer may take a
 * different path.
 *
 * To create a single point on a pointer's touch path
 * <code>
 *       PointerCoords p = new PointerCoords();
 *       p.x = stepX;
 *       p.y = stepY;
 *       p.pressure = 1;
 *       p.size = 1;
 * </code>
 * @param touches each array of {@link PointerCoords} constitute a single pointer's touch path.
 *        Multiple {@link PointerCoords} arrays constitute multiple pointers, each with its own
 *        path. Each {@link PointerCoords} in an array constitute a point on a pointer's path.
 * @return <code>true</code> if all points on all paths are injected successfully, <code>false
 *        </code>otherwise
 * @since API Level 18
 */
public boolean performMultiPointerGesture(PointerCoords[]... touches) {
	boolean ret = true;
	if (touches.length < 2) {
		throw new IllegalArgumentException("Must provide coordinates for at least 2 pointers");
	}

	// Get the pointer with the max steps to inject.
	int maxSteps = 0;
	for (int x = 0; x < touches.length; x++)
		maxSteps = (maxSteps < touches[x].length) ? touches[x].length : maxSteps;

	// specify the properties for each pointer as finger touch
	PointerProperties[] properties = new PointerProperties[touches.length];
	PointerCoords[] pointerCoords = new PointerCoords[touches.length];
	for (int x = 0; x < touches.length; x++) {
		PointerProperties prop = new PointerProperties();
		prop.id = x;
		prop.toolType = MotionEvent.TOOL_TYPE_FINGER;
		properties[x] = prop;

		// for each pointer set the first coordinates for touch down
		pointerCoords[x] = touches[x][0];
	}

	// Touch down all pointers
	long downTime = SystemClock.uptimeMillis();
	MotionEvent event;
	event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 1, properties, pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0);
	ret &= injectEventSync(event);

	for (int x = 1; x < touches.length; x++) {
		event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), getPointerAction(MotionEvent.ACTION_POINTER_DOWN, x), x + 1, properties, pointerCoords, 0, 0, 1, 1, 0, 0,
				InputDevice.SOURCE_TOUCHSCREEN, 0);
		ret &= injectEventSync(event);
	}

	// Move all pointers
	for (int i = 1; i < maxSteps - 1; i++) {
		// for each pointer
		for (int x = 0; x < touches.length; x++) {
			// check if it has coordinates to move
			if (touches[x].length > i)
				pointerCoords[x] = touches[x][i];
			else
				pointerCoords[x] = touches[x][touches[x].length - 1];
		}

		event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, touches.length, properties, pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0);

		ret &= injectEventSync(event);
		SystemClock.sleep(MOTION_EVENT_INJECTION_DELAY_MILLIS);
	}

	// For each pointer get the last coordinates
	for (int x = 0; x < touches.length; x++)
		pointerCoords[x] = touches[x][touches[x].length - 1];

	// touch up
	for (int x = 1; x < touches.length; x++) {
		event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), getPointerAction(MotionEvent.ACTION_POINTER_UP, x), x + 1, properties, pointerCoords, 0, 0, 1, 1, 0, 0,
				InputDevice.SOURCE_TOUCHSCREEN, 0);
		ret &= injectEventSync(event);
	}

	Log.i(LOG_TAG, "x " + pointerCoords[0].x);
	// first to touch down is last up
	event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 1, properties, pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0);
	ret &= injectEventSync(event);
	return ret;
}
 
Example 14
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 15
Source File: Tapper.java    From AndroidRipper with GNU Affero General Public License v3.0 4 votes vote down vote up
public void generateTapGesture(int numTaps, PointF... points)
{
    MotionEvent event;

    long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis();

    // pointer 1
    float x1 = points[0].x;
    float y1 = points[0].y;

    float x2 = 0;
    float y2 = 0;
    if (points.length == 2)
    {
        // pointer 2
        x2 = points[1].x;
        y2 = points[1].y;
    }

    PointerCoords[] pointerCoords = new PointerCoords[points.length];
    PointerCoords pc1 = new PointerCoords();
    pc1.x = x1;
    pc1.y = y1;
    pc1.pressure = 1;
    pc1.size = 1;
    pointerCoords[0] = pc1;
    PointerCoords pc2 = new PointerCoords();
    if (points.length == 2)
    {
        pc2.x = x2;
        pc2.y = y2;
        pc2.pressure = 1;
        pc2.size = 1;
        pointerCoords[1] = pc2;
    }

    PointerProperties[] pointerProperties = new PointerProperties[points.length];
    PointerProperties pp1 = new PointerProperties();
    pp1.id = 0;
    pp1.toolType = MotionEvent.TOOL_TYPE_FINGER;
    pointerProperties[0] = pp1;
    PointerProperties pp2 = new PointerProperties();
    if (points.length == 2)
    {
        pp2.id = 1;
        pp2.toolType = MotionEvent.TOOL_TYPE_FINGER;
        pointerProperties[1] = pp2;
    }

    int i = 0;
    while (i != numTaps)
    {
        event = MotionEvent.obtain(downTime, eventTime,
                MotionEvent.ACTION_DOWN, points.length, pointerProperties,
                pointerCoords, 0, 0, 1, 1, 0, 0,
                InputDevice.SOURCE_TOUCHSCREEN, 0);
        _instrument.sendPointerSync(event);

        if (points.length == 2)
        {
            event = MotionEvent
                    .obtain(downTime,
                            eventTime,
                            MotionEvent.ACTION_POINTER_DOWN
                                    + (pp2.id << MotionEvent.ACTION_POINTER_INDEX_SHIFT),
                            points.length, pointerProperties,
                            pointerCoords, 0, 0, 1, 1, 0, 0,
                            InputDevice.SOURCE_TOUCHSCREEN, 0);
            _instrument.sendPointerSync(event);

            eventTime += EVENT_TIME_INTERVAL_MS;
            event = MotionEvent
                    .obtain(downTime,
                            eventTime,
                            MotionEvent.ACTION_POINTER_UP
                                    + (pp2.id << MotionEvent.ACTION_POINTER_INDEX_SHIFT),
                            points.length, pointerProperties,
                            pointerCoords, 0, 0, 1, 1, 0, 0,
                            InputDevice.SOURCE_TOUCHSCREEN, 0);
            _instrument.sendPointerSync(event);
        }

        eventTime += EVENT_TIME_INTERVAL_MS;
        event = MotionEvent.obtain(downTime, eventTime,
                MotionEvent.ACTION_UP, points.length, pointerProperties,
                pointerCoords, 0, 0, 1, 1, 0, 0,
                InputDevice.SOURCE_TOUCHSCREEN, 0);
        _instrument.sendPointerSync(event);

        i++;
    }
}
 
Example 16
Source File: MotionEvents.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
static boolean isFingerEvent(@NonNull MotionEvent e) {
    return e.getToolType(0) == MotionEvent.TOOL_TYPE_FINGER;
}
 
Example 17
Source File: InteractionController.java    From za-Farmer with MIT License 4 votes vote down vote up
/**
 * Performs a multi-touch gesture
 *
 * Takes a series of touch coordinates for at least 2 pointers. Each pointer must have
 * all of its touch steps defined in an array of {@link PointerCoords}. By having the ability
 * to specify the touch points along the path of a pointer, the caller is able to specify
 * complex gestures like circles, irregular shapes etc, where each pointer may take a
 * different path.
 *
 * To create a single point on a pointer's touch path
 * <code>
 *       PointerCoords p = new PointerCoords();
 *       p.x = stepX;
 *       p.y = stepY;
 *       p.pressure = 1;
 *       p.size = 1;
 * </code>
 * @param touches each array of {@link PointerCoords} constitute a single pointer's touch path.
 *        Multiple {@link PointerCoords} arrays constitute multiple pointers, each with its own
 *        path. Each {@link PointerCoords} in an array constitute a point on a pointer's path.
 * @return <code>true</code> if all points on all paths are injected successfully, <code>false
 *        </code>otherwise
 * @since API Level 18
 */
public boolean performMultiPointerGesture(PointerCoords[] ... touches) {
    boolean ret = true;
    if (touches.length < 2) {
        throw new IllegalArgumentException("Must provide coordinates for at least 2 pointers");
    }

    // Get the pointer with the max steps to inject.
    int maxSteps = 0;
    for (int x = 0; x < touches.length; x++)
        maxSteps = (maxSteps < touches[x].length) ? touches[x].length : maxSteps;

    // specify the properties for each pointer as finger touch
    PointerProperties[] properties = new PointerProperties[touches.length];
    PointerCoords[] pointerCoords = new PointerCoords[touches.length];
    for (int x = 0; x < touches.length; x++) {
        PointerProperties prop = new PointerProperties();
        prop.id = x;
        prop.toolType = MotionEvent.TOOL_TYPE_FINGER;
        properties[x] = prop;

        // for each pointer set the first coordinates for touch down
        pointerCoords[x] = touches[x][0];
    }

    // Touch down all pointers
    long downTime = SystemClock.uptimeMillis();
    MotionEvent event;
    event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 1,
            properties, pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0);
    ret &= injectEventSync(event);

    for (int x = 1; x < touches.length; x++) {
        event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(),
                getPointerAction(MotionEvent.ACTION_POINTER_DOWN, x), x + 1, properties,
                pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0);
        ret &= injectEventSync(event);
    }

    // Move all pointers
    for (int i = 1; i < maxSteps - 1; i++) {
        // for each pointer
        for (int x = 0; x < touches.length; x++) {
            // check if it has coordinates to move
            if (touches[x].length > i)
                pointerCoords[x] = touches[x][i];
            else
                pointerCoords[x] = touches[x][touches[x].length - 1];
        }

        event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(),
                MotionEvent.ACTION_MOVE, touches.length, properties, pointerCoords, 0, 0, 1, 1,
                0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0);

        ret &= injectEventSync(event);
        SystemClock.sleep(MOTION_EVENT_INJECTION_DELAY_MILLIS);
    }

    // For each pointer get the last coordinates
    for (int x = 0; x < touches.length; x++)
        pointerCoords[x] = touches[x][touches[x].length - 1];

    // touch up
    for (int x = 1; x < touches.length; x++) {
        event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(),
                getPointerAction(MotionEvent.ACTION_POINTER_UP, x), x + 1, properties,
                pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0);
        ret &= injectEventSync(event);
    }

    Log.i(LOG_TAG, "x " + pointerCoords[0].x);
    // first to touch down is last up
    event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 1,
            properties, pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0);
    ret &= injectEventSync(event);
    return ret;
}