下面列出了android.view.InputDevice#getSources ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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();
}
}
}
}
}
/** 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;
}
}
}
/**
* @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;
}
/**
* Utility method to determine if input device is a gamepad.
*
* @param device
* @return
*/
private boolean isGamepad(InputDevice device) {
if ((device.getSources() &
InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD
|| (device.getSources() &
InputDevice.SOURCE_CLASS_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) {
return true;
}
return false;
}
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;
}
void findControllersAndAttachShips() {
int[] deviceIds = mInputManager.getInputDeviceIds();
for (int deviceId : deviceIds) {
InputDevice dev = mInputManager.getInputDevice(deviceId);
int sources = dev.getSources();
// if the device is a gamepad/joystick, create a ship to represent it
if (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) ||
((sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK)) {
// if the device has a gamepad or joystick
getShipForId(deviceId);
}
}
}
/** 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;
}
}
}
private boolean isController(InputDevice device) {
return ((device.getSources() & InputDevice.SOURCE_CLASS_JOYSTICK) == InputDevice.SOURCE_CLASS_JOYSTICK)
&& (((device.getSources() & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
|| (device.getKeyboardType() != InputDevice.KEYBOARD_TYPE_ALPHABETIC));
}
private static boolean isGamepadDevice(InputDevice inputDevice) {
if (inputDevice == null) return false;
return ((inputDevice.getSources() & InputDevice.SOURCE_JOYSTICK)
== InputDevice.SOURCE_JOYSTICK);
}
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;
}