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

The following examples show how to use android.view.InputDevice#getDevice() . 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
protected void addController(int deviceId, boolean sendEvent) {
	try {
		InputDevice device = InputDevice.getDevice(deviceId);
		if (!isController(device)) return;
		String name = device.getName();
		AndroidController controller = new AndroidController(deviceId, name);
		controllerMap.put(deviceId, controller);
		if (sendEvent) {
			synchronized (eventQueue) {
				AndroidControllerEvent event = eventPool.obtain();
				event.type = AndroidControllerEvent.CONNECTED;
				event.controller = controller;
				eventQueue.add(event);
			}
		} else {
			controllers.add(controller);
		}
		Gdx.app.log(TAG, "added controller '" + name + "'");
	} catch (RuntimeException e) {
		// this exception is sometimes thrown by getDevice().
		// we can't use this device anyway, so ignore it and move on
	}
}
 
Example 2
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 3
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 4
Source File: FullscreenActivity.java    From androidtv-VisualGameController with Apache License 2.0 6 votes vote down vote up
@Override
public void onInputDeviceRemoved(int deviceId) {
    Log.d(TAG, "onInputDeviceRemoved: " + deviceId);
    mConnectedDevices.remove(new Integer(deviceId));
    if (mCurrentDeviceId == deviceId) {
        mCurrentDeviceId = -1;
    }
    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();
        }
    }
}
 
Example 5
Source File: FullscreenActivity.java    From androidtv-VisualGameController with Apache License 2.0 6 votes vote down vote up
/**
 * Check for any game controllers that are connected already.
 */
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();
                }
            }
        }
    }
}
 
Example 6
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 7
Source File: InputManagerV9.java    From bluetooth with Apache License 2.0 5 votes vote down vote up
@Override
public void handleMessage(Message msg) {
    super.handleMessage(msg);
    switch (msg.what) {
        case MESSAGE_TEST_FOR_DISCONNECT:
            InputManagerV9 imv = mInputManager.get();
            if (null != imv) {
                long time = SystemClock.elapsedRealtime();
                int size = imv.mDevices.size();
                for (int i = 0; i < size; i++) {
                    long[] lastContact = imv.mDevices.valueAt(i);
                    if (null != lastContact) {
                        if (time - lastContact[0] > CHECK_ELAPSED_TIME) {
                            // check to see if the device has been
                            // disconnected
                            int id = imv.mDevices.keyAt(i);
                            if (null == InputDevice.getDevice(id)) {
                                // disconnected!
                                imv.notifyListeners(ON_DEVICE_REMOVED, id);
                                imv.mDevices.remove(id);
                            } else {
                                lastContact[0] = time;
                            }
                        }
                    }
                }
                sendEmptyMessageDelayed(MESSAGE_TEST_FOR_DISCONNECT,
                        CHECK_ELAPSED_TIME);
            }
            break;
    }
}
 
Example 8
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 9
Source File: FullscreenActivity.java    From androidtv-VisualGameController with Apache License 2.0 5 votes vote down vote up
@Override
public void onInputDeviceAdded(int deviceId) {
    Log.d(TAG, "onInputDeviceAdded: " + deviceId);
    if (!mConnectedDevices.contains(deviceId)) {
        mConnectedDevices.add(new Integer(deviceId));
    }
    if (mCurrentDeviceId == -1) {
        mCurrentDeviceId = deviceId;
        InputDevice dev = InputDevice.getDevice(mCurrentDeviceId);
        if (dev != null) {
            mControllerView.setCurrentControllerNumber(dev.getControllerNumber());
            mControllerView.invalidate();
        }
    }
}
 
Example 10
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 11
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 12
Source File: GamepadList.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private void onInputDeviceAddedImpl(int deviceId) {
    InputDevice inputDevice = InputDevice.getDevice(deviceId);
    if (!isGamepadDevice(inputDevice)) return;
    synchronized (mLock) {
        registerGamepad(inputDevice);
    }
}
 
Example 13
Source File: GamepadList.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private void initializeDevices() {
    // Get list of all the attached input devices.
    int[] deviceIds = mInputManager.getInputDeviceIds();
    for (int i = 0; i < deviceIds.length; i++) {
        InputDevice inputDevice = InputDevice.getDevice(deviceIds[i]);
        // Check for gamepad device
        if (isGamepadDevice(inputDevice)) {
            // Register a new gamepad device.
            registerGamepad(inputDevice);
        }
    }
}
 
Example 14
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;
}
 
Example 15
Source File: InputManagerV9.java    From bluetooth with Apache License 2.0 4 votes vote down vote up
@Override
public InputDevice getInputDevice(int id) {
    return InputDevice.getDevice(id);
}
 
Example 16
Source File: GameView.java    From bluetooth with Apache License 2.0 4 votes vote down vote up
/**
 * Uses the device descriptor to try to assign the same color to the same
 * joystick. If there are two joysticks of the same type connected over USB,
 * or the API is < API level 16, it will be unable to distinguish the two
 * devices.
 *
 * @param shipID
 * @return
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private Ship getShipForId(int shipID) {
    Ship currentShip = mShips.get(shipID);
    if (null == currentShip) {

        // do we know something about this ship already?
        InputDevice dev = InputDevice.getDevice(shipID);
        String deviceString = null;
        Integer shipColor = null;
        if (null != dev) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                deviceString = dev.getDescriptor();
            } else {
                deviceString = dev.getName();
            }
            shipColor = mDescriptorMap.get(deviceString);
        }

        if (null != shipColor) {
            int color = shipColor;
            int numShips = mShips.size();
            // do we already have a ship with this color?
            for (int i = 0; i < numShips; i++) {
                if (mShips.valueAt(i).getColor() == color) {
                    shipColor = null;
                    // we won't store this value either --- if the first
                    // controller gets disconnected/connected, it will get
                    // the same color.
                    deviceString = null;
                }
            }
        }
        if (null != shipColor) {
            currentShip = new Ship(shipColor);
            if (null != deviceString) {
                mDescriptorMap.remove(deviceString);
            }
        } else {
            currentShip = new Ship(getNextShipColor());
        }
        mShips.append(shipID, currentShip);
        currentShip.setInputDevice(dev);

        if (null != deviceString) {
            mDescriptorMap.put(deviceString, currentShip.getColor());
        }
    }
    return currentShip;
}
 
Example 17
Source File: InputModeManager.java    From talkback with Apache License 2.0 4 votes vote down vote up
private static boolean isEventFromNonAlphabeticKeyboard(@NonNull KeyEvent event) {
  InputDevice inputDevice = InputDevice.getDevice(event.getDeviceId());
  return (inputDevice != null)
      && (InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC == inputDevice.getKeyboardType());
}