Android: better readibility for pollInputDevices() and move isDeviceSDLJoystick() test at first since there are always non-joystick devices present.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java b/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java
index d6db065..12eebc6 100644
--- a/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java
+++ b/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java
@@ -177,13 +177,14 @@ class SDLJoystickHandler_API16 extends SDLJoystickHandler {
@Override
public void pollInputDevices() {
int[] deviceIds = InputDevice.getDeviceIds();
- for(int i=0; i < deviceIds.length; ++i) {
- SDLJoystick joystick = getJoystick(deviceIds[i]);
- if (joystick == null) {
- InputDevice joystickDevice = InputDevice.getDevice(deviceIds[i]);
- if (SDLControllerManager.isDeviceSDLJoystick(deviceIds[i])) {
+
+ for (int device_id : deviceIds) {
+ if (SDLControllerManager.isDeviceSDLJoystick(device_id)) {
+ SDLJoystick joystick = getJoystick(device_id);
+ if (joystick == null) {
+ InputDevice joystickDevice = InputDevice.getDevice(device_id);
joystick = new SDLJoystick();
- joystick.device_id = deviceIds[i];
+ joystick.device_id = device_id;
joystick.name = joystickDevice.getName();
joystick.desc = getJoystickDescriptor(joystickDevice);
joystick.axes = new ArrayList<InputDevice.MotionRange>();
@@ -191,53 +192,57 @@ class SDLJoystickHandler_API16 extends SDLJoystickHandler {
List<InputDevice.MotionRange> ranges = joystickDevice.getMotionRanges();
Collections.sort(ranges, new RangeComparator());
- for (InputDevice.MotionRange range : ranges ) {
+ for (InputDevice.MotionRange range : ranges) {
if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
- if (range.getAxis() == MotionEvent.AXIS_HAT_X ||
- range.getAxis() == MotionEvent.AXIS_HAT_Y) {
+ if (range.getAxis() == MotionEvent.AXIS_HAT_X || range.getAxis() == MotionEvent.AXIS_HAT_Y) {
joystick.hats.add(range);
- }
- else {
+ } else {
joystick.axes.add(range);
}
}
}
mJoysticks.add(joystick);
- SDLControllerManager.nativeAddJoystick(joystick.device_id, joystick.name, joystick.desc, getVendorId(joystickDevice), getProductId(joystickDevice), false, getButtonMask(joystickDevice), joystick.axes.size(), joystick.hats.size()/2, 0);
+ SDLControllerManager.nativeAddJoystick(joystick.device_id, joystick.name, joystick.desc,
+ getVendorId(joystickDevice), getProductId(joystickDevice), false,
+ getButtonMask(joystickDevice), joystick.axes.size(), joystick.hats.size()/2, 0);
}
}
}
/* Check removed devices */
- ArrayList<Integer> removedDevices = new ArrayList<Integer>();
- for(int i=0; i < mJoysticks.size(); i++) {
- int device_id = mJoysticks.get(i).device_id;
- int j;
- for (j=0; j < deviceIds.length; j++) {
- if (device_id == deviceIds[j]) break;
+ ArrayList<Integer> removedDevices = null;
+ for (SDLJoystick joystick : mJoysticks) {
+ int device_id = joystick.device_id;
+ int i;
+ for (i = 0; i < deviceIds.length; i++) {
+ if (device_id == deviceIds[i]) break;
}
- if (j == deviceIds.length) {
- removedDevices.add(Integer.valueOf(device_id));
+ if (i == deviceIds.length) {
+ if (removedDevices == null) {
+ removedDevices = new ArrayList<Integer>();
+ }
+ removedDevices.add(device_id);
}
}
- for(int i=0; i < removedDevices.size(); i++) {
- int device_id = removedDevices.get(i).intValue();
- SDLControllerManager.nativeRemoveJoystick(device_id);
- for (int j=0; j < mJoysticks.size(); j++) {
- if (mJoysticks.get(j).device_id == device_id) {
- mJoysticks.remove(j);
- break;
+ if (removedDevices != null) {
+ for (int device_id : removedDevices) {
+ SDLControllerManager.nativeRemoveJoystick(device_id);
+ for (int i = 0; i < mJoysticks.size(); i++) {
+ if (mJoysticks.get(i).device_id == device_id) {
+ mJoysticks.remove(i);
+ break;
+ }
}
}
}
}
protected SDLJoystick getJoystick(int device_id) {
- for(int i=0; i < mJoysticks.size(); i++) {
- if (mJoysticks.get(i).device_id == device_id) {
- return mJoysticks.get(i);
+ for (SDLJoystick joystick : mJoysticks) {
+ if (joystick.device_id == device_id) {
+ return joystick;
}
}
return null;