Fix crash when a rumble effect only uses the high-frequency motor

This commit is contained in:
Cameron Gutman 2019-02-17 18:18:00 -08:00
parent bfe6929642
commit ffcb623040

View File

@ -1072,19 +1072,22 @@ public class ControllerHandler implements InputManager.InputDeviceListener, UsbD
} }
private void rumbleVibrator(Vibrator vibrator, short lowFreqMotor, short highFreqMotor) { private void rumbleVibrator(Vibrator vibrator, short lowFreqMotor, short highFreqMotor) {
if (lowFreqMotor == 0 && highFreqMotor == 0) {
// This case is easy - just cancel and get out
vibrator.cancel();
return;
}
// Since we can only use a single amplitude value, compute the desired amplitude // Since we can only use a single amplitude value, compute the desired amplitude
// by taking 75% of the big motor and 25% of the small motor. // by taking 75% of the big motor and 25% of the small motor.
// NB: This value is now 0-255 as required by VibrationEffect. // NB: This value is now 0-255 as required by VibrationEffect.
short lowFreqMotorMSB = (short)((lowFreqMotor >> 8) & 0xFF); short lowFreqMotorMSB = (short)((lowFreqMotor >> 8) & 0xFF);
short highFreqMotorMSB = (short)((lowFreqMotor >> 8) & 0xFF); short highFreqMotorMSB = (short)((highFreqMotor >> 8) & 0xFF);
int simulatedAmplitude = (int)((lowFreqMotorMSB * 0.75) + (highFreqMotorMSB * 0.25)); int simulatedAmplitude = (int)((lowFreqMotorMSB * 0.75) + (highFreqMotorMSB * 0.25));
if (simulatedAmplitude == 0) {
// This case is easy - just cancel the current effect and get out.
// NB: We cannot simply check lowFreqMotor == highFreqMotor == 0
// because our simulatedAmplitude could be 0 even though our inputs
// are not (ex: lowFreqMotor == 0 && highFreqMotor == 1).
vibrator.cancel();
return;
}
// Attempt to use amplitude-based control if we're on Oreo and the device // Attempt to use amplitude-based control if we're on Oreo and the device
// supports amplitude-based vibration control. // supports amplitude-based vibration control.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {