下面列出了android.view.InputDevice#getDevice ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
}
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
}
}
/**
* 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();
}
}
}
}
}
@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();
}
}
}
/** 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;
}
}
}