Fix some old bugs where we overrode the device ID to 0 wrongly for back button

The checks for pad were wrong, and also the old path simply passed 0.
This commit is contained in:
Henrik Rydgård 2021-08-09 13:03:26 +02:00
parent f9c381dfae
commit b898e4587f
2 changed files with 19 additions and 14 deletions

View file

@ -12,7 +12,7 @@ import android.view.MotionEvent;
public class InputDeviceState {
private static final String TAG = "InputDeviceState";
private static final int deviceId = NativeApp.DEVICE_ID_PAD_0;
public static final int deviceId = NativeApp.DEVICE_ID_PAD_0;
private InputDevice mDevice;
private int[] mAxes;

View file

@ -912,7 +912,6 @@ public abstract class NativeActivity extends Activity {
}
}
// We simply grab the first input device to produce an event and ignore all others that are connected.
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
private InputDeviceState getInputDeviceState(InputEvent event) {
InputDevice device = event.getDevice();
@ -967,24 +966,29 @@ public abstract class NativeActivity extends Activity {
// Let's let back and menu through to dispatchKeyEvent.
boolean passThrough = false;
int sources = event.getSource();
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_BACK:
case KeyEvent.KEYCODE_MENU:
passThrough = true;
Log.i(TAG, "Passing through key, source = " + sources);
break;
default:
break;
}
// Don't passthrough back button if gamepad.
int sources = event.getSource();
switch (sources) {
case InputDevice.SOURCE_GAMEPAD:
case InputDevice.SOURCE_JOYSTICK:
case InputDevice.SOURCE_DPAD:
// Don't passthrough back or menu button if from gamepad.
// XInput device on Android returns source 1281 or 0x501, which equals GAMEPAD | KEYBOARD.
// Shield Remote returns 769 or 0x301 which equals DPAD | KEYBOARD.
if ((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD ||
(sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK ||
(sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD)
{
passThrough = false;
break;
}
Log.i(TAG, "Input event device sources: " + sources);
if (!passThrough) {
switch (event.getAction()) {
@ -1058,18 +1062,18 @@ public abstract class NativeActivity extends Activity {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (event.isAltPressed()) {
NativeApp.keyDown(0, 1004, repeat); // special custom keycode for the O button on Xperia Play
NativeApp.keyDown(InputDeviceState.deviceId, 1004, repeat); // special custom keycode for the O button on Xperia Play
} else if (NativeApp.isAtTopLevel()) {
Log.i(TAG, "IsAtTopLevel returned true.");
Log.i(TAG, "IsAtTopLevel returned true.");
// Pass through the back event.
return super.onKeyDown(keyCode, event);
} else {
NativeApp.keyDown(0, keyCode, repeat);
NativeApp.keyDown(InputDeviceState.deviceId, keyCode, repeat);
}
return true;
case KeyEvent.KEYCODE_MENU:
case KeyEvent.KEYCODE_SEARCH:
NativeApp.keyDown(0, keyCode, repeat);
NativeApp.keyDown(InputDeviceState.deviceId, keyCode, repeat);
return true;
case KeyEvent.KEYCODE_DPAD_UP:
@ -1078,6 +1082,7 @@ public abstract class NativeActivity extends Activity {
case KeyEvent.KEYCODE_DPAD_RIGHT:
// Joysticks are supported in Honeycomb MR1 and later via the onGenericMotionEvent method.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1 && event.getSource() == InputDevice.SOURCE_JOYSTICK) {
// Pass through / ignore
return super.onKeyDown(keyCode, event);
}
// Fall through
@ -1085,7 +1090,7 @@ public abstract class NativeActivity extends Activity {
// send the rest of the keys through.
// TODO: get rid of the three special cases above by adjusting the native side of the code.
// Log.d(TAG, "Key down: " + keyCode + ", KeyEvent: " + event);
return NativeApp.keyDown(0, keyCode, repeat);
return NativeApp.keyDown(InputDeviceState.deviceId, keyCode, repeat);
}
}