Example usage for android.view InputDevice getDevice

List of usage examples for android.view InputDevice getDevice

Introduction

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

Prototype

public static InputDevice getDevice(int id) 

Source Link

Document

Gets information about the input device with the specified id.

Usage

From source file:Main.java

public static int getConnectedInputDevices() {
    int[] deviceIDs = InputDevice.getDeviceIds();
    int result = 0;
    for (int i = 0; i < deviceIDs.length; i++) {
        InputDevice dev = InputDevice.getDevice(deviceIDs[i]);
        /*if (dev.isVirtual())
           continue;*/// ww w . j a  va  2  s .co m

        int sources = dev.getSources();
        if ((sources & InputDevice.SOURCE_CLASS_BUTTON) != 0)
            result++;
    }
    return result;
}

From source file:org.uoyabause.android.tv.GameSelectActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {

    InputDevice dev = InputDevice.getDevice(event.getDeviceId());
    if (dev != null && dev.getName().contains("HuiJia")) {
        if (event.getKeyCode() > 200) {
            return true;
        }//w w  w.  j  a  v a  2s .c  o  m

    }
    return super.dispatchKeyEvent(event);
}

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

/**
 * Check for any game controllers that are connected already.
 *///from   ww  w.  ja  v a 2 s.c om
private void checkGameControllers() {
    Log.d(TAG, "checkGameControllers");
    int[] deviceIds = mInputManager.getInputDeviceIds();
    for (int deviceId : deviceIds) {
        InputDevice dev = InputDevice.getDevice(deviceId);
        int sources = dev.getSources();

        // Verify that the device has gamepad buttons, control sticks, or
        // both.
        if (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
                || ((sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK)) {
            // This device is a game controller. Store its device ID.
            if (!mConnectedDevices.contains(deviceId)) {
                mConnectedDevices.add(deviceId);
                if (mCurrentDeviceId == -1) {
                    mCurrentDeviceId = deviceId;
                    mControllerView.setCurrentControllerNumber(dev.getControllerNumber());
                    mControllerView.invalidate();
                }
            }
        }
    }
}

From source file:info.bartowski.easteregg.MLand.java

public ArrayList getGameControllers() {
    mGameControllers.clear();/* w w w .  j a  v a 2s  .c o m*/
    int[] deviceIds = InputDevice.getDeviceIds();
    for (int deviceId : deviceIds) {
        InputDevice dev = InputDevice.getDevice(deviceId);
        if (isGamePad(dev)) {
            if (!mGameControllers.contains(deviceId)) {
                mGameControllers.add(deviceId);
            }
        }
    }
    return mGameControllers;
}

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.//from w ww  .j a  v a 2s. c  o 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:org.ulteo.ovd.AndRdpActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    AudioManager audioManager;// ww  w.  jav  a 2 s  . c  o  m

    if (Config.DEBUG)
        Log.i(Config.TAG, "AndRdpActivity.dispatchKeyEvent " + event);

    // Disable keypress if it is from a mouse or touchpad.
    if (event.getDeviceId() >= 0) {
        InputDevice dev = InputDevice.getDevice(event.getDeviceId());
        if (dev.getSources() == InputDevice.SOURCE_MOUSE || dev.getSources() == InputDevice.SOURCE_TOUCHPAD)
            return true;
    }

    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_MENU:
            OnThreeFingers(null);
            return true;
        case KeyEvent.KEYCODE_BACK:
            Log.i(Config.TAG, "Back key pressed");
            askLogoutDialog();
            return true;
        case KeyEvent.KEYCODE_VOLUME_UP:
            audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE,
                    AudioManager.FLAG_SHOW_UI);
            return true;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER,
                    AudioManager.FLAG_SHOW_UI);
            return true;
        }
    } else if (event.getAction() == KeyEvent.ACTION_UP) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_MENU:
        case KeyEvent.KEYCODE_BACK:
        case KeyEvent.KEYCODE_VOLUME_UP:
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            return true;
        }
    }

    if (isLoggedIn() && rdp.dispatchKeyEvent(event))
        return true;

    return super.dispatchKeyEvent(event);
}

From source file:info.bartowski.easteregg.MLand.java

private void thump(int playerIndex, long ms) {
    if (mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
        // No interruptions. Not even game haptics.
        return;//from   w  w  w.ja  v  a 2  s  . c  o  m
    }
    if (playerIndex < mGameControllers.size()) {
        int controllerId = mGameControllers.get(playerIndex);
        InputDevice dev = InputDevice.getDevice(controllerId);
        if (dev != null && dev.getVibrator().hasVibrator()) {
            Utility.compatVibrate(dev.getVibrator(), (long) (ms * CONTROLLER_VIBRATION_MULTIPLIER));
            return;
        }
    }
    Utility.compatVibrate(mVibrator, (long) (ms * CONTROLLER_VIBRATION_MULTIPLIER));
}

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

@Override
public void onInputDeviceAdded(int deviceId) {
    Log.d(TAG, "onInputDeviceAdded: " + deviceId);
    if (!mConnectedDevices.contains(deviceId)) {
        mConnectedDevices.add(new Integer(deviceId));
    }//  w  w  w  .  java 2  s .  co m
    if (mCurrentDeviceId == -1) {
        mCurrentDeviceId = deviceId;
        InputDevice dev = InputDevice.getDevice(mCurrentDeviceId);
        if (dev != null) {
            mControllerView.setCurrentControllerNumber(dev.getControllerNumber());
            mControllerView.invalidate();
        }
    }
}

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

@Override
public void onInputDeviceRemoved(int deviceId) {
    Log.d(TAG, "onInputDeviceRemoved: " + deviceId);
    mConnectedDevices.remove(new Integer(deviceId));
    if (mCurrentDeviceId == deviceId) {
        mCurrentDeviceId = -1;//from   www .  j  a v  a2  s. c o m
    }
    if (mConnectedDevices.size() == 0) {
        mControllerView.setCurrentControllerNumber(-1);
        mControllerView.invalidate();
    } else {
        mCurrentDeviceId = mConnectedDevices.get(0);
        InputDevice dev = InputDevice.getDevice(mCurrentDeviceId);
        if (dev != null) {
            mControllerView.setCurrentControllerNumber(dev.getControllerNumber());
            mControllerView.invalidate();
        }
    }
}

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

private ArrayList<Device> checkDevices() {
    //Get a list of all device id's and assign them to players.
    ArrayList<Device> devices = new ArrayList<Device>();
    int[] deviceIds = InputDevice.getDeviceIds();

    for (int count = 0; count < deviceIds.length; count++) {
        InputDevice d = InputDevice.getDevice(deviceIds[count]);
        if (!d.isVirtual()) {
            Device device = new Device();
            device.id = d.getId();//from   w ww .ja  va  2  s.c  o m
            device.player = OuyaController.getPlayerNumByDeviceId(device.id);
            if (device.player != DEVICE_NOT_OUYACONTROLLER_COMPATIBLE) {
                device.name = d.getName();
                devices.add(device);
            }
        }
    }
    return devices;
}