Only check for motion sensors on Sony or Nintendo gamepads on Android 12

This works around a bug in Android 12 that leads to random crashes when input devices change.
This commit is contained in:
Cameron Gutman
2023-10-07 19:07:00 -05:00
parent d250f4dc60
commit 30822c1ba5

View File

@@ -681,7 +681,16 @@ public class ControllerHandler implements InputManager.InputDeviceListener, UsbD
// On Android 12, we can try to use the InputDevice's sensors. This may not work if the
// Linux kernel version doesn't have motion sensor support, which is common for third-party
// gamepads.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && prefConfig.gamepadMotionSensors) {
//
// Android 12 has a bug that causes InputDeviceSensorManager to cause a NPE on a background
// thread due to bad error checking in InputListener callbacks. InputDeviceSensorManager is
// created upon the first call to InputDevice.getSensorManager(), so we avoid calling this
// on Android 12 unless we have a gamepad that could plausibly have motion sensors.
// https://cs.android.com/android/_/android/platform/frameworks/base/+/8970010a5e9f3dc5c069f56b4147552accfcbbeb
if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU ||
(Build.VERSION.SDK_INT == Build.VERSION_CODES.S &&
(context.vendorId == 0x054c || context.vendorId == 0x057e))) && // Sony or Nintendo
prefConfig.gamepadMotionSensors) {
if (dev.getSensorManager().getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null || dev.getSensorManager().getDefaultSensor(Sensor.TYPE_GYROSCOPE) != null) {
context.sensorManager = dev.getSensorManager();
}