Example usage for android.view MotionEvent getDeviceId

List of usage examples for android.view MotionEvent getDeviceId

Introduction

In this page you can find the example usage for android.view MotionEvent getDeviceId.

Prototype

@Override
public final int getDeviceId() 

Source Link

Usage

From source file:Main.java

private static MotionEvent transformEventOld(MotionEvent e, Matrix m) {
    long downTime = e.getDownTime();
    long eventTime = e.getEventTime();
    int action = e.getAction();
    int pointerCount = e.getPointerCount();
    int[] pointerIds = getPointerIds(e);
    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();

    // Copy the x and y coordinates into an array, map them, and copy back.
    float[] xy = new float[pointerCoords.length * 2];
    for (int i = 0; i < pointerCount; i++) {
        xy[2 * i] = pointerCoords[i].x;//w w  w . j  a v a2s.c om
        xy[2 * i + 1] = pointerCoords[i].y;
    }
    m.mapPoints(xy);
    for (int i = 0; i < pointerCount; i++) {
        pointerCoords[i].x = xy[2 * i];
        pointerCoords[i].y = xy[2 * i + 1];
        pointerCoords[i].orientation = transformAngle(m, pointerCoords[i].orientation);
    }

    MotionEvent n = MotionEvent.obtain(downTime, eventTime, action, pointerCount, pointerIds, pointerCoords,
            metaState, xPrecision, yPrecision, deviceId, edgeFlags, source, flags);

    return n;
}

From source file:Main.java

private static MotionEvent transformEventOld(MotionEvent e, Matrix m) {
    long downTime = e.getDownTime();
    long eventTime = e.getEventTime();
    int action = e.getAction();
    int pointerCount = e.getPointerCount();
    int[] pointerIds = getPointerIds(e);
    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();
    // Copy the x and y coordinates into an array, map them, and copy back.
    float[] xy = new float[pointerCoords.length * 2];
    for (int i = 0; i < pointerCount; i++) {
        xy[2 * i] = pointerCoords[i].x;/*from  www  .  jav a2  s. c o  m*/
        xy[2 * i + 1] = pointerCoords[i].y;
    }
    m.mapPoints(xy);
    for (int i = 0; i < pointerCount; i++) {
        pointerCoords[i].x = xy[2 * i];
        pointerCoords[i].y = xy[2 * i + 1];
        pointerCoords[i].orientation = transformAngle(m, pointerCoords[i].orientation);
    }
    MotionEvent n = MotionEvent.obtain(downTime, eventTime, action, pointerCount, pointerIds, pointerCoords,
            metaState, xPrecision, yPrecision, deviceId, edgeFlags, source, flags);
    return n;
}

From source file:Main.java

public static JSONObject CreateJSonObjectFromMotionEvent(MotionEvent e) throws JSONException {
    JSONObject jObj = new JSONObject();
    jObj.put("downTime", e.getDownTime());
    jObj.put("eventTime", e.getEventTime());
    jObj.put("action", e.getAction());
    jObj.put("pointerCount", e.getPointerCount());
    jObj.put("metaState", e.getMetaState());
    jObj.put("buttonState", e.getButtonState());
    jObj.put("xPrecision", e.getXPrecision());
    jObj.put("yPrecision", e.getYPrecision());
    jObj.put("deviceId", e.getDeviceId());
    jObj.put("edgeFlags", e.getEdgeFlags());
    jObj.put("source", e.getSource());
    jObj.put("flags", e.getFlags());

    for (int i = 0; i < e.getPointerCount(); i++) {
        PointerProperties prop = new PointerProperties();
        e.getPointerProperties(i, prop);

        PointerCoords coords = new PointerCoords();
        e.getPointerCoords(i, coords);/*w  w w . j a va2 s  .  c om*/

        JSONObject pointer = JObjFromPointer(prop, coords);
        jObj.accumulate("pointers", pointer);
    }

    return jObj;
}

From source file:Main.java

public static MotionEvent hoverMotionEventAtPosition(View view, int action, int xPercent, int yPercent) {
    MotionEvent ev = motionEventAtPosition(view, action, xPercent, yPercent);

    MotionEvent.PointerProperties[] pointerProperties = new MotionEvent.PointerProperties[1];
    pointerProperties[0] = new MotionEvent.PointerProperties();

    MotionEvent.PointerCoords[] pointerCoords = new MotionEvent.PointerCoords[1];
    pointerCoords[0] = new MotionEvent.PointerCoords();
    pointerCoords[0].x = ev.getX();/*from  ww  w . j  av  a2 s  .c om*/
    pointerCoords[0].y = ev.getY();

    return MotionEvent.obtain(ev.getDownTime(), ev.getEventTime(), ev.getAction(), 1, pointerProperties,
            pointerCoords, ev.getMetaState(), 0, ev.getXPrecision(), ev.getYPrecision(), ev.getDeviceId(),
            ev.getEdgeFlags(), InputDevice.SOURCE_CLASS_POINTER, ev.getFlags());
}

From source file:foam.jellyfish.StarwispActivity.java

@Override
public boolean onGenericMotionEvent(final MotionEvent event) {
    //Get the player #
    int player = OuyaController.getPlayerNumByDeviceId(event.getDeviceId());

    Log.i("starwisp", "ogme");

    // Joystick/*from   w  ww  . j  a  v  a 2  s .  c  o  m*/
    if ((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
        //Get all the axis for the event
        float LS_X = event.getAxisValue(OuyaController.AXIS_LS_X);
        float LS_Y = event.getAxisValue(OuyaController.AXIS_LS_Y);

        float RS_X = event.getAxisValue(OuyaController.AXIS_RS_X);
        float RS_Y = event.getAxisValue(OuyaController.AXIS_RS_Y);
        float L2 = event.getAxisValue(OuyaController.AXIS_L2);
        float R2 = event.getAxisValue(OuyaController.AXIS_R2);

        Log.i("starwisp", "controller " + LS_X + " " + LS_Y + RS_X + " " + RS_Y + L2 + " " + R2);

        Scheme.eval("(on-fling " + LS_X * -500 + " " + LS_Y * -500 + ")");
    }

    //Touchpad
    if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
        //Print the pixel coordinates of the cursor
        Log.i("starwisp", "Cursor X: " + event.getX() + "Cursor Y: " + event.getY());
    }

    return true;
}

From source file:com.goodhustle.ouyaunitybridge.OuyaUnityActivity.java

@Override
public boolean onGenericMotionEvent(MotionEvent event) {
    // Pass to OuyaController first, then process.
    boolean handled = false;
    int playerNum = OuyaController.getPlayerNumByDeviceId(event.getDeviceId());

    // Add the additional conditional that this must be a joystick event (not a pointer event).
    if (playerNum != DEVICE_NOT_OUYACONTROLLER_COMPATIBLE
            && ((event.getSource() & InputDevice.SOURCE_JOYSTICK) != 0)) {
        handled = OuyaController.onGenericMotionEvent(event);
        if (mPaused)
            return handled || super.onGenericMotionEvent(event);

        // Check if this was a joystick or touch hover event
        try {//w  w  w . j  a v a  2s  .  c om
            ControllerState data = playerStates[playerNum];
            OuyaController c = OuyaController.getControllerByPlayer(playerNum);
            if (data != null) {
                data.AxisLSX = c.getAxisValue(OuyaController.AXIS_LS_X);
                data.AxisLSY = c.getAxisValue(OuyaController.AXIS_LS_Y);
                data.AxisRSX = c.getAxisValue(OuyaController.AXIS_RS_X);
                data.AxisRSY = c.getAxisValue(OuyaController.AXIS_RS_Y);
                data.AxisLT = c.getAxisValue(OuyaController.AXIS_L2);
                data.AxisRT = c.getAxisValue(OuyaController.AXIS_R2);
            }
        } catch (Exception e) {
            Log.i(LOG_TAG, "Exception occurred getting controller state for player " + playerNum + ": "
                    + e.toString());
        }
    }
    return handled || super.onGenericMotionEvent(event);
}

From source file:com.amazon.appstream.fireclient.FireClientActivity.java

/**
 * A "generic motion event" includes joystick and mouse motion
 * when the mouse button isn't down. In our simple sample, we're
 * not handling the joystick, but this is where any such code
 * would live.//w w w .j  av  a  2 s  .co m
 *
 * This will only ever be called in HONEYCOMB_MR1 (12) or later, so I'm marking the
 * function using \@TargetApi to allow it to call the super.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@Override
public boolean dispatchGenericMotionEvent(MotionEvent event) {
    if (event.getSource() == InputDevice.SOURCE_MOUSE) {
        event.getPointerCoords(0, mCoordHolder);
        switch (event.getAction()) {
        case MotionEvent.ACTION_HOVER_MOVE:
            AppStreamInterface.mouseEvent((int) mCoordHolder.x, (int) mCoordHolder.y, 0);
            break;

        default:
            return super.dispatchGenericMotionEvent(event);
        }
        return true;
    } else if (event.getSource() == InputDevice.SOURCE_JOYSTICK) {
        Log.v(TAG, "Joystick event:" + event.toString());

        if (!mInitializedJoystick) {
            mInitializedJoystick = true;

            InputDevice joystick = InputDevice.getDevice(event.getDeviceId());
            InputDevice.MotionRange lThumbX = joystick.getMotionRange(MotionEvent.AXIS_X,
                    InputDevice.SOURCE_JOYSTICK);
            InputDevice.MotionRange lThumbY = joystick.getMotionRange(MotionEvent.AXIS_Y,
                    InputDevice.SOURCE_JOYSTICK);
            InputDevice.MotionRange rThumbX = joystick.getMotionRange(MotionEvent.AXIS_Z);
            InputDevice.MotionRange rThumbY = joystick.getMotionRange(MotionEvent.AXIS_RZ);
            InputDevice.MotionRange rTrigger = joystick.getMotionRange(MotionEvent.AXIS_GAS);
            if (rTrigger == null) {
                rTrigger = joystick.getMotionRange(MotionEvent.AXIS_RTRIGGER);
                mRTrigger = MotionEvent.AXIS_RTRIGGER;
            }

            InputDevice.MotionRange lTrigger = joystick.getMotionRange(MotionEvent.AXIS_BRAKE);
            if (lTrigger == null) {
                lTrigger = joystick.getMotionRange(MotionEvent.AXIS_LTRIGGER);
                mLTrigger = MotionEvent.AXIS_LTRIGGER;
            }

            List<InputDevice.MotionRange> ranges = joystick.getMotionRanges();

            InputDevice.MotionRange dPad = null;
            String name = joystick.getName();

            /*
            The Amazon Fire Game Controller follows the NVidia standard of sending
            AXIS_HAT_X/AXIS_HAT_Y results when the user hits the D-Pad. Only if we
            return false from dispatchGenericMotionEvent() will it then send
            DPAD keycodes.
                    
            But the most popular Android joystick on the market at this time, the
            Nyko Playpad Pro, returns AXIS_HAT_X/AXIS_HAT_Y results when the left
            analog stick hits its extremes, meaning that the analog stick will
            generate DPAD keys if we return false. The Nyko generates DPAD keys
            directly for the DPAD controller.
                    
            So we have two incompatible standards fighting with each other. Probably
            the safest thing to do would be to ask the user to press on their DPAD
            and see what messages we get, but that is beyond the scope of this
            example code.
            */
            if (name.equals("Amazon Fire Game Controler")) {
                for (int i = 0; i < ranges.size(); ++i) {
                    InputDevice.MotionRange range = ranges.get(i);
                    int axis = range.getAxis();

                    if (axis == MotionEvent.AXIS_HAT_X || axis == MotionEvent.AXIS_HAT_Y) {
                        dPad = ranges.get(i);
                        break;
                    }
                }
            }

            JoystickHelper.joystickDeadZones(lTrigger, rTrigger, lThumbX, lThumbY, rThumbX, rThumbY, dPad);
        }

        float lThumbX = event.getAxisValue(MotionEvent.AXIS_X);
        float lThumbY = -event.getAxisValue(MotionEvent.AXIS_Y);
        float rThumbX = event.getAxisValue(MotionEvent.AXIS_Z);
        float rThumbY = -event.getAxisValue(MotionEvent.AXIS_RZ);
        float lTrigger = event.getAxisValue(mLTrigger);
        float rTrigger = event.getAxisValue(mRTrigger);
        float hatX = event.getAxisValue(MotionEvent.AXIS_HAT_X);
        float hatY = event.getAxisValue(MotionEvent.AXIS_HAT_Y);

        JoystickHelper.setJoystickState(lTrigger, rTrigger, lThumbX, lThumbY, rThumbX, rThumbY, hatX, hatY);
        return true;
    }

    return super.dispatchGenericMotionEvent(event);
}

From source file:jmri.enginedriver.throttle.java

@Override
public boolean dispatchGenericMotionEvent(android.view.MotionEvent event) {
    //Log.d("Engine_Driver", "dgme " + event.getAction());
    //        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
    if (!whichGamePadMode.equals(WHICH_GAMEPAD_MODE_NONE)) { // respond to the gamepad and keyboard inputs only if the preference is set

        boolean acceptEvent = true; // default to assuming that we will respond to the event

        int action;
        int whichThrottle;
        int repeatCnt = 0;
        int whichGamePadIsEventFrom = findWhichGamePadEventIsFrom(event.getDeviceId(), 0); // dummy eventKeyCode

        if ((whichGamePadIsEventFrom > -1) && (whichGamePadIsEventFrom < gamePadDeviceIdsTested.length)) { // the event came from a gamepad
            if (gamePadDeviceIdsTested[getGamePadIndexFromThrottleNo(
                    whichGamePadIsEventFrom)] != GAMEPAD_GOOD) { //if not, testing for this gamepad is not complete or has failed
                acceptEvent = false;//from  w ww .  j  a va  2 s .  c om
            }
        } else {
            acceptEvent = false;
        }

        if (acceptEvent) {
            float xAxis;
            xAxis = event.getAxisValue(MotionEvent.AXIS_X);
            float yAxis = event.getAxisValue(MotionEvent.AXIS_Y);
            float xAxis2 = event.getAxisValue(MotionEvent.AXIS_Z);
            float yAxis2 = event.getAxisValue(MotionEvent.AXIS_RZ);

            if ((xAxis != 0) || (yAxis != 0)) {
                action = ACTION_DOWN;
            } else {
                action = ACTION_UP;
            }
            if ((usingMultiplePads) && (whichGamePadIsEventFrom >= -1)) { // we have multiple gamepads AND the preference is set to make use of them AND the event came for a gamepad
                if (whichGamePadIsEventFrom >= 0) {
                    whichThrottle = whichGamePadIsEventFrom;
                } else {
                    GamepadFeedbackSound(true);
                    return (true);
                }
            } else {
                whichThrottle = whichVolume; // work out which throttle the volume keys are currently set to contol... and use that one
            }

            boolean isActive = getConsist(whichThrottle).isActive();

            if (action == ACTION_UP) {
                mGamepadAutoIncrement = false;
                mGamepadAutoDecrement = false;
                GamepadFeedbackSoundStop();
            }

            if (yAxis == -1) { // DPAD Up Button
                performButtonAction(5, action, isActive, whichThrottle, whichGamePadIsEventFrom, repeatCnt);
                return (true); // stop processing this key

            } else if (yAxis == 1) { // DPAD Down Button
                performButtonAction(7, action, isActive, whichThrottle, whichGamePadIsEventFrom, repeatCnt);
                return (true); // stop processing this key

            } else if (xAxis == -1) { // DPAD Left Button
                performButtonAction(8, action, isActive, whichThrottle, whichGamePadIsEventFrom, repeatCnt);
                return (true); // stop processing this key

            } else if (xAxis == 1) { // DPAD Right Button
                performButtonAction(6, action, isActive, whichThrottle, whichGamePadIsEventFrom, repeatCnt);
                return (true); // stop processing this key
            }

            if (yAxis2 == -1) { // DPAD2 Up Button
                performButtonAction(5, action, isActive, whichThrottle, whichGamePadIsEventFrom, repeatCnt);
                return (true); // stop processing this key

            } else if (yAxis2 == 1) { // DPAD2 Down Button
                performButtonAction(7, action, isActive, whichThrottle, whichGamePadIsEventFrom, repeatCnt);
                return (true); // stop processing this key

            } else if (xAxis2 == -1) { // DPAD2 Left Button
                performButtonAction(8, action, isActive, whichThrottle, whichGamePadIsEventFrom, repeatCnt);
                return (true); // stop processing this key

            } else if (xAxis2 == 1) { // DPAD2 Right Button
                performButtonAction(6, action, isActive, whichThrottle, whichGamePadIsEventFrom, repeatCnt);
                return (true); // stop processing this key
            }
        } else { // event is from a gamepad that has not finished testing. Ignore it
            return (true); // stop processing this key
        }
        //            }
    }
    return super.dispatchGenericMotionEvent(event);
}