Java Code Examples for android.view.InputDevice#getDeviceIds()

The following examples show how to use android.view.InputDevice#getDeviceIds() . 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: AndroidControllers.java    From gdx-controllerutils with Apache License 2.0 6 votes vote down vote up
private void gatherControllers(boolean sendEvent) {
	// gather all joysticks and gamepads, remove any disconnected ones
	IntMap<AndroidController> removedControllers = new IntMap<AndroidController>();
	removedControllers.putAll(controllerMap);
	
	for(int deviceId: InputDevice.getDeviceIds()) {
		InputDevice device = InputDevice.getDevice(deviceId);
		AndroidController controller = controllerMap.get(deviceId);
		if(controller != null) {
			removedControllers.remove(deviceId);
		} else {
			addController(deviceId, sendEvent);
		}
	}
	
	for(Entry<AndroidController> entry: removedControllers.entries()) {
		removeController(entry.key);
	}
}
 
Example 2
Source File: TouchpadView.java    From gdk-apidemo-sample with Apache License 2.0 6 votes vote down vote up
/** Looks up the hardware resolution of the Glass touchpad. */
private void lookupTouchpadHardwareResolution() {
    int[] deviceIds = InputDevice.getDeviceIds();
    for (int deviceId : deviceIds) {
        InputDevice device = InputDevice.getDevice(deviceId);
        if ((device.getSources() & InputDevice.SOURCE_TOUCHPAD) != 0) {
            logVerbose("Touchpad motion range: x-axis [%d, %d] y-axis [%d, %d]",
                    device.getMotionRange(MotionEvent.AXIS_X).getMin(),
                    device.getMotionRange(MotionEvent.AXIS_X).getMax(),
                    device.getMotionRange(MotionEvent.AXIS_Y).getMin(),
                    device.getMotionRange(MotionEvent.AXIS_Y).getMax());

            mTouchpadHardwareWidth = device.getMotionRange(MotionEvent.AXIS_X).getRange();
            mTouchpadHardwareHeight = device.getMotionRange(MotionEvent.AXIS_Y).getRange();
            // Stop after we've seen the first touchpad device, because there might be multiple
            // devices in this list if the user is currently screencasting with MyGlass. The
            // first one will always be the hardware touchpad.
            break;
        }
    }
}
 
Example 3
Source File: ScanGunKeyEventHelper.java    From scangon with Apache License 2.0 5 votes vote down vote up
/**
 * 输入设备是否存在
 * @param deviceName
 * @return
 */
private boolean isInputDeviceExist(String deviceName) {
    int[] deviceIds = InputDevice.getDeviceIds();

    for (int id : deviceIds) {
        if (InputDevice.getDevice(id).getName().equals(deviceName)) {
            return true;
        }
    }
    return false;
}
 
Example 4
Source File: TouchDevice.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * @return an array of two ints: result[0] represents the pointer-types and result[1] represents
 *         the hover-types supported by the device, where each int is the union (bitwise OR) of
 *         corresponding type (PointerType/HoverType) bits.
 */
@CalledByNative
private static int[] availablePointerAndHoverTypes() {
    int[] result = new int[2];
    result[0] = result[1] = 0;

    for (int deviceId : InputDevice.getDeviceIds()) {
        InputDevice inputDevice = InputDevice.getDevice(deviceId);
        if (inputDevice == null) continue;

        int sources = inputDevice.getSources();

        if (hasSource(sources, InputDevice.SOURCE_MOUSE)
                || hasSource(sources, InputDevice.SOURCE_STYLUS)
                || hasSource(sources, InputDevice.SOURCE_TOUCHPAD)
                || hasSource(sources, InputDevice.SOURCE_TRACKBALL)) {
            result[0] |= PointerType.FINE;
        } else if (hasSource(sources, InputDevice.SOURCE_TOUCHSCREEN)) {
            result[0] |= PointerType.COARSE;
        }

        if (hasSource(sources, InputDevice.SOURCE_MOUSE)
                || hasSource(sources, InputDevice.SOURCE_TOUCHPAD)
                || hasSource(sources, InputDevice.SOURCE_TRACKBALL)) {
            result[1] |= HoverType.HOVER;
        } else if (hasSource(sources, InputDevice.SOURCE_STYLUS)
                || hasSource(sources, InputDevice.SOURCE_TOUCHSCREEN)) {
            result[1] |= HoverType.NONE;
        }

        // Remaining InputDevice sources: SOURCE_DPAD, SOURCE_GAMEPAD, SOURCE_JOYSTICK,
        // SOURCE_KEYBOARD, SOURCE_TOUCH_NAVIGATION, SOURCE_UNKNOWN
    }

    if (result[0] == 0) result[0] = PointerType.NONE;
    if (result[1] == 0) result[1] = HoverType.NONE;

    return result;
}
 
Example 5
Source File: MainActivity.java    From ForgePE with GNU Affero General Public License v3.0 5 votes vote down vote up
public int getKeyFromKeyCode(int keyCode, int metaState, int deviceId) {
    if (deviceId < 0) {
        int[] ids = InputDevice.getDeviceIds();
        if (ids.length != 0) {
            deviceId = ids[deviceId];

            InputDevice device = InputDevice.getDevice(deviceId);
            if (device != null) {
                return device.getKeyCharacterMap().get(keyCode, metaState);
            }
        }
    }
    return 0;
}
 
Example 6
Source File: MainActivity.java    From bluetooth with Apache License 2.0 5 votes vote down vote up
public ArrayList getGameControllerIds() {
    ArrayList gameControllerDeviceIds = new ArrayList();
    int[] deviceIds = InputDevice.getDeviceIds();
    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.
            name.setText(dev.getName());
            if (!gameControllerDeviceIds.contains(deviceId)) {
                gameControllerDeviceIds.add(deviceId);
            }
            //possible both maybe true.
            if ((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
                isGamePad = true;
            if ((sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK)
                isJoyStick = true;
            logger.append("GamePad: " + isGamePad + "\n");
            logger.append("JoyStick: " + isJoyStick + "\n");
        }

    }
    return gameControllerDeviceIds;
}
 
Example 7
Source File: InputManagerV9.java    From bluetooth with Apache License 2.0 5 votes vote down vote up
@Override
public int[] getInputDeviceIds() {
    // add any hitherto unknown devices to our
    // collection of watched input devices
    int[] activeDevices = InputDevice.getDeviceIds();
    long time = SystemClock.elapsedRealtime();
    for ( int id : activeDevices ) {
        long[] lastContact = mDevices.get(id);
        if ( null == lastContact ) {
            // we have a new device
            mDevices.put(id, new long[] { time });
        }
    }
    return activeDevices;
}
 
Example 8
Source File: TouchpadView.java    From PTVGlass with MIT License 5 votes vote down vote up
/** Looks up the hardware resolution of the Glass touchpad. */
private void lookupTouchpadHardwareResolution() {
    int[] deviceIds = InputDevice.getDeviceIds();
    for (int deviceId : deviceIds) {
        InputDevice device = InputDevice.getDevice(deviceId);
        if ((device.getSources() & InputDevice.SOURCE_TOUCHPAD) != 0) {
            mTouchpadHardwareWidth = device.getMotionRange(MotionEvent.AXIS_X).getRange();
            mTouchpadHardwareHeight = device.getMotionRange(MotionEvent.AXIS_Y).getRange();
            // Stop after we've seen the first touchpad device, because there might be multiple
            // devices in this list if the user is currently screencasting with MyGlass. The
            // first one will always be the hardware touchpad.
            break;
        }
    }
}
 
Example 9
Source File: AndroidJoystickJoyInput14.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public List<Joystick> loadJoysticks(int joyId, InputManager inputManager) {
    logger.log(Level.INFO, "loading Joystick devices");
    ArrayList<Joystick> joysticks = new ArrayList<Joystick>();
    joysticks.clear();
    joystickIndex.clear();

    ArrayList<Integer> gameControllerDeviceIds = new ArrayList<>();
    int[] deviceIds = InputDevice.getDeviceIds();
    for (int deviceId : deviceIds) {
        InputDevice dev = InputDevice.getDevice(deviceId);
        int sources = dev.getSources();
        logger.log(Level.FINE, "deviceId[{0}] sources: {1}", new Object[]{deviceId, sources});

        // 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 (!gameControllerDeviceIds.contains(deviceId)) {
                gameControllerDeviceIds.add(deviceId);
                logger.log(Level.FINE, "Attempting to create joystick for device: {0}", dev);
                // Create an AndroidJoystick and store the InputDevice so we
                // can later correspond the input from the InputDevice to the
                // appropriate jME Joystick event
                AndroidJoystick joystick = new AndroidJoystick(inputManager,
                                                            joyInput,
                                                            dev,
                                                            joyId+joysticks.size(),
                                                            dev.getName());
                joystickIndex.put(deviceId, joystick);
                joysticks.add(joystick);

                // Each analog input is reported as a MotionRange
                // The axis number corresponds to the type of axis
                // The AndroidJoystick.addAxis(MotionRange) converts the axis
                // type reported by Android into the jME Joystick axis
                List<MotionRange> motionRanges = dev.getMotionRanges();
                for (MotionRange motionRange: motionRanges) {
                    logger.log(Level.INFO, "motion range: {0}", motionRange.toString());
                    logger.log(Level.INFO, "axis: {0}", motionRange.getAxis());
                    JoystickAxis axis = joystick.addAxis(motionRange);
                    logger.log(Level.INFO, "added axis: {0}", axis);
                }

                // InputDevice has a method for determining if a keyCode is
                // supported (InputDevice  public boolean[] hasKeys (int... keys)).
                // But this method wasn't added until rev 19 (Android 4.4)
                // Therefore, we only can query the entire device and see if
                // any InputDevice supports the keyCode.  This may result in
                // buttons being configured that don't exist on the specific
                // device, but I haven't found a better way yet.
                for (int keyCode: AndroidGamepadButtons) {
                    logger.log(Level.INFO, "button[{0}]: {1}",
                            new Object[]{keyCode, KeyCharacterMap.deviceHasKey(keyCode)});
                    if (KeyCharacterMap.deviceHasKey(keyCode)) {
                        // add button even though we aren't sure if the button
                        // actually exists on this InputDevice
                        logger.log(Level.INFO, "button[{0}] exists somewhere", keyCode);
                        JoystickButton button = joystick.addButton(keyCode);
                        logger.log(Level.INFO, "added button: {0}", button);
                    }
                }

            }
        }
    }


    loaded = true;
    return joysticks;
}