From ffcb623040c12623d527cc39707d5fec39561e92 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sun, 17 Feb 2019 18:18:00 -0800 Subject: [PATCH] Fix crash when a rumble effect only uses the high-frequency motor --- .../binding/input/ControllerHandler.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/limelight/binding/input/ControllerHandler.java b/app/src/main/java/com/limelight/binding/input/ControllerHandler.java index 188554c0..beb23891 100644 --- a/app/src/main/java/com/limelight/binding/input/ControllerHandler.java +++ b/app/src/main/java/com/limelight/binding/input/ControllerHandler.java @@ -1072,19 +1072,22 @@ public class ControllerHandler implements InputManager.InputDeviceListener, UsbD } 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 // by taking 75% of the big motor and 25% of the small motor. // NB: This value is now 0-255 as required by VibrationEffect. 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)); + 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 // supports amplitude-based vibration control. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {