Example usage for android.view MotionEvent getDevice

List of usage examples for android.view MotionEvent getDevice

Introduction

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

Prototype

public final InputDevice getDevice() 

Source Link

Document

Gets the device that this event came from.

Usage

From source file:org.sufficientlysecure.keychain.ui.widget.ListAwareSwipeRefreshLayout.java

/** Called on a touch event, this method exempts a small area in the upper right from pull to
 * refresh handling.//w ww .  ja  v a 2s.com
 *
 * If the touch event happens somewhere in the upper right corner of the screen, we return false
 * to indicate that the event was not handled. This ensures events in that area are always
 * handed through to the list scrollbar handle. For all other cases, we pass the message through
 * to the pull to refresh handler.
 */
@Override
public boolean onTouchEvent(MotionEvent event) {
    // The device may be null. This actually happens
    if (event.getDevice() != null) {
        // MotionEvent.AXIS_X is api level 12, for some reason, so we use a constant 0 here
        float ratioX = event.getX() / event.getDevice().getMotionRange(0).getMax();
        float ratioY = event.getY() / event.getDevice().getMotionRange(1).getMax();
        // if this is the upper right corner, don't handle as pull to refresh event
        if (ratioX > 0.85f && ratioY < 0.15f) {
            return false;
        }
    }
    return super.onTouchEvent(event);
}

From source file:tv.piratemedia.flightcontroller.ControlActivity.java

private void processJoystickInput(MotionEvent event, int historyPos) {

    InputDevice mInputDevice = event.getDevice();

    float x = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_X, historyPos);

    float y = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_Y, historyPos);

    float z = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_Z, historyPos);

    float rz = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_RZ, historyPos);

    float l = getTriggerValue(event, mInputDevice, MotionEvent.AXIS_LTRIGGER, historyPos);

    float r = getTriggerValue(event, mInputDevice, MotionEvent.AXIS_RTRIGGER, historyPos);

    if (lStickCache[0] != x || lStickCache[1] != y) {
        fragment.LeftStick.setPosition(x, y);
        lStickCache[0] = x;/*from   w  ww  .  ja v  a  2  s .  c om*/
        lStickCache[1] = y;
    }

    if (rStickCache[0] != z || rStickCache[1] != rz) {
        fragment.RightStick.setPosition(z, rz);
        rStickCache[0] = z;
        rStickCache[1] = rz;
    }

    if (0f != l || 0f != r) {
        int range = fragment.throttle.getMax();
        float throttle = (float) fragment.throttle.getProgress() / range;
        if (l > 0) {
            if (l > triggerCache[0]) {
                throttle += (l - triggerCache[0]) / 2f;
            }
        } else {
            if (r > triggerCache[1]) {
                throttle -= (r - triggerCache[1]) / 2f;
            }
        }

        fragment.throttle.setProgress((int) (throttle * (float) range));
        fragment.throttle.invalidate();
    }
    triggerCache[0] = l;
    triggerCache[1] = r;
}

From source file:com.example.android.visualgamecontroller.FullscreenActivity.java

@Override
public boolean onGenericMotionEvent(final MotionEvent ev) {
    // Log.d(TAG, "onGenericMotionEvent: " + ev);
    InputDevice device = ev.getDevice();
    // Only care about game controllers.
    if (device != null && device.getId() == mCurrentDeviceId) {
        if (isGamepad(device)) {
            for (AxesMapping axesMapping : AxesMapping.values()) {
                mAxes[axesMapping.ordinal()] = getCenteredAxis(ev, device, axesMapping.getMotionEvent());
            }/* w ww  .  ja  v a  2  s . co m*/
            mControllerView.invalidate();
            return true;
        }
    }
    return super.onGenericMotionEvent(ev);
}

From source file:com.thalmic.android.sample.helloworld.HelloWorldActivity.java

private void processJoystickInput(MotionEvent event, int historyPos) {

    InputDevice mInputDevice = event.getDevice();

    // Calculate the horizontal distance to move by
    // using the input value from one of these physical controls:
    // the left control stick, hat axis, or the right control stick.
    float x = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_X, historyPos);
    if (x == 0) {
        x = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_HAT_X, historyPos);
    }//  www .  ja  v a 2 s.c  o  m
    if (x == 0) {
        x = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_Z, historyPos);
    }

    // Calculate the vertical distance to move by
    // using the input value from one of these physical controls:
    // the left control stick, hat switch, or the right control stick.
    float y = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_Y, historyPos);
    if (y == 0) {
        y = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_HAT_Y, historyPos);
    }
    if (y == 0) {
        y = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_RZ, historyPos);
    }

    deltaJoystick = ((Math.abs(oldJoystickX - x) >= 0.1) || (Math.abs(oldJoystickY - y) >= 0.1));

    JoystickData joystickDataX = new JoystickData("x", x);
    JoystickData joystickDataY = new JoystickData("y", y);

    if (deltaJoystick) {

        mPitch.setText("y is :" + y);
        oldJoystickX = x;
        oldJoystickY = y;

        if (client != null) {
            try {

                //client.send("servo01", "moveTo",(roll+90.0));
                client.send("joystick", "publishJoystickInput", joystickDataX);
                client.send("joystick", "publishJoystickInput", joystickDataY);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

From source file:com.fishstix.dosboxfree.DBGLSurfaceView.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
private void processJoystickInput(MotionEvent event, int historyPos) {
    float hatX = 0.0f;
    InputDevice.MotionRange range = event.getDevice().getMotionRange(MotionEvent.AXIS_HAT_X, event.getSource());
    if (range != null) {
        if (historyPos >= 0) {
            hatX = InputDeviceState.ProcessAxis(range,
                    event.getHistoricalAxisValue(MotionEvent.AXIS_HAT_X, historyPos));
        } else {/*from  w w w  .j a v  a2s. co  m*/
            hatX = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_HAT_X));
        }
    }

    float hatY = 0.0f;
    range = event.getDevice().getMotionRange(MotionEvent.AXIS_HAT_Y, event.getSource());
    if (range != null) {
        if (historyPos >= 0) {
            hatY = InputDeviceState.ProcessAxis(range,
                    event.getHistoricalAxisValue(MotionEvent.AXIS_HAT_Y, historyPos));
        } else {
            hatY = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_HAT_Y));
        }
    }

    float joyX = 0.0f;
    range = event.getDevice().getMotionRange(MotionEvent.AXIS_X, event.getSource());
    if (range != null) {
        if (historyPos >= 0) {
            joyX = InputDeviceState.ProcessAxis(range,
                    event.getHistoricalAxisValue(MotionEvent.AXIS_X, historyPos));
        } else {
            joyX = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_X));
        }
    }

    float joyY = 0.0f;
    range = event.getDevice().getMotionRange(MotionEvent.AXIS_Y, event.getSource());
    if (range != null) {
        if (historyPos >= 0) {
            joyY = InputDeviceState.ProcessAxis(range,
                    event.getHistoricalAxisValue(MotionEvent.AXIS_Y, historyPos));
        } else {
            joyY = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_Y));
        }
    }

    float joy2X = 0.0f;
    range = event.getDevice().getMotionRange(MotionEvent.AXIS_Z, event.getSource());
    if (range != null) {
        if (historyPos >= 0) {
            joy2X = InputDeviceState.ProcessAxis(range,
                    event.getHistoricalAxisValue(MotionEvent.AXIS_Z, historyPos));
        } else {
            joy2X = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_Z));
        }
    }

    float joy2Y = 0.0f;
    range = event.getDevice().getMotionRange(MotionEvent.AXIS_RZ, event.getSource());
    if (range != null) {
        if (historyPos >= 0) {
            joy2Y = InputDeviceState.ProcessAxis(range,
                    event.getHistoricalAxisValue(MotionEvent.AXIS_RZ, historyPos));
        } else {
            joy2Y = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_RZ));
        }
    }

    if (mAnalogStickPref == 0) {
        mMouseThread.setCoord(
                (int) ((Math.abs(joyX * 32.0f) > DEADZONE) ? (-joyX * 32.0f * mMouseSensitivityX) : 0),
                (int) ((Math.abs(joyY * 32.0f) > DEADZONE) ? (-joyY * 32.0f * mMouseSensitivityY) : 0));
        DosBoxControl.nativeJoystick((int) ((joy2X * 256.0f) + mJoyCenterX),
                (int) ((joy2Y * 256.0f) + mJoyCenterY), ACTION_MOVE, -1);
    } else {
        mMouseThread.setCoord(
                (int) ((Math.abs(joy2X * 32.0f) > DEADZONE) ? (-joy2X * 32.0f * mMouseSensitivityX) : 0),
                (int) ((Math.abs(joy2Y * 32.0f) > DEADZONE) ? (-joy2Y * 32.0f * mMouseSensitivityY) : 0));
        DosBoxControl.nativeJoystick((int) ((joyX * 256.0f) + mJoyCenterX),
                (int) ((joyY * 256.0f) + mJoyCenterY), ACTION_MOVE, -1);
    }

    // Handle all other keyevents
    int value = 0;
    int tKeyCode = MAP_NONE;

    if (hatX < 0) {
        value = customMap.get(KeyEvent.KEYCODE_DPAD_LEFT);
        if (value > 0) {
            // found a valid mapping
            tKeyCode = getMappedKeyCode(value, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_LEFT));
            if (tKeyCode > MAP_NONE) {
                DosBoxControl.sendNativeKey(tKeyCode, true, mModifierCtrl, mModifierAlt, mModifierShift);
            }
        }
        hatXlast = hatX;
    } else if (hatX > 0) {
        value = customMap.get(KeyEvent.KEYCODE_DPAD_RIGHT);
        if (value > 0) {
            // found a valid mapping
            tKeyCode = getMappedKeyCode(value, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT));
            if (tKeyCode > MAP_NONE) {
                DosBoxControl.sendNativeKey(tKeyCode, true, mModifierCtrl, mModifierAlt, mModifierShift);
            }
        }
        hatXlast = hatX;
    } else {
        // released
        if (hatX != hatXlast) {
            if (hatXlast < 0) {
                value = customMap.get(KeyEvent.KEYCODE_DPAD_LEFT);
                if (value > 0) {
                    // found a valid mapping
                    tKeyCode = getMappedKeyCode(value,
                            new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_LEFT));
                    if (tKeyCode > MAP_NONE) {
                        DosBoxControl.sendNativeKey(tKeyCode, false, mModifierCtrl, mModifierAlt,
                                mModifierShift);
                    }
                }
            } else if (hatXlast > 0) {
                value = customMap.get(KeyEvent.KEYCODE_DPAD_RIGHT);
                if (value > 0) {
                    // found a valid mapping
                    tKeyCode = getMappedKeyCode(value,
                            new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_RIGHT));
                    if (tKeyCode > MAP_NONE) {
                        DosBoxControl.sendNativeKey(tKeyCode, false, mModifierCtrl, mModifierAlt,
                                mModifierShift);
                    }
                }
            }
        }
        hatXlast = hatX;
    }
    if (hatY < 0) {
        value = customMap.get(KeyEvent.KEYCODE_DPAD_UP);
        if (value > 0) {
            // found a valid mapping
            tKeyCode = getMappedKeyCode(value, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_UP));
            if (tKeyCode > MAP_NONE) {
                DosBoxControl.sendNativeKey(tKeyCode, true, mModifierCtrl, mModifierAlt, mModifierShift);
            }
        }
        hatYlast = hatY;
    } else if (hatY > 0) {
        value = customMap.get(KeyEvent.KEYCODE_DPAD_DOWN);
        if (value > 0) {
            // found a valid mapping
            tKeyCode = getMappedKeyCode(value, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_DOWN));
            if (tKeyCode > MAP_NONE) {
                DosBoxControl.sendNativeKey(tKeyCode, true, mModifierCtrl, mModifierAlt, mModifierShift);
            }
        }
        hatYlast = hatY;
    } else {
        // released
        if (hatY != hatYlast) {
            if (hatYlast < 0) {
                value = customMap.get(KeyEvent.KEYCODE_DPAD_UP);
                if (value > 0) {
                    // found a valid mapping
                    tKeyCode = getMappedKeyCode(value,
                            new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_UP));
                    if (tKeyCode > MAP_NONE) {
                        DosBoxControl.sendNativeKey(tKeyCode, false, mModifierCtrl, mModifierAlt,
                                mModifierShift);
                    }
                }
            } else if (hatYlast > 0) {
                value = customMap.get(KeyEvent.KEYCODE_DPAD_DOWN);
                if (value > 0) {
                    // found a valid mapping
                    tKeyCode = getMappedKeyCode(value,
                            new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_DOWN));
                    if (tKeyCode > MAP_NONE) {
                        DosBoxControl.sendNativeKey(tKeyCode, false, mModifierCtrl, mModifierAlt,
                                mModifierShift);
                    }
                }
            }
        }
        hatYlast = hatY;
    }
}