Fixed bug 4318 - Android move Haptic code to API26 class

Sylvain

- Create SDLHapticHandler_API26
- No need of reflection since SDL2 compile with Android 26 as a min requirement.
- remove spaces
This commit is contained in:
Sam Lantinga 2018-10-16 15:00:43 -07:00
parent 708ad1fd8d
commit 3e3ce6e95c
1 changed files with 49 additions and 53 deletions

View File

@ -1,6 +1,5 @@
package org.libsdl.app;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@ -53,8 +52,13 @@ public class SDLControllerManager
} else {
mJoystickHandler = new SDLJoystickHandler();
}
if (Build.VERSION.SDK_INT >= 26) {
mHapticHandler = new SDLHapticHandler_API26();
} else {
mHapticHandler = new SDLHapticHandler();
}
}
// Joystick glue code, just a series of stubs that redirect to the SDLJoystickHandler instance
public static boolean handleJoystickMotionEvent(MotionEvent event) {
@ -410,6 +414,38 @@ class SDLJoystickHandler_API19 extends SDLJoystickHandler_API16 {
}
}
class SDLHapticHandler_API26 extends SDLHapticHandler {
@Override
public void run(int device_id, float intensity, int length) {
SDLHaptic haptic = getHaptic(device_id);
if (haptic != null) {
Log.d("SDL", "Rtest: Vibe with intensity " + intensity + " for " + length);
if (intensity == 0.0f) {
stop(device_id);
return;
}
int vibeValue = Math.round(intensity * 255);
if (vibeValue > 255) {
vibeValue = 255;
}
if (vibeValue < 1) {
stop(device_id);
return;
}
try {
haptic.vib.vibrate(VibrationEffect.createOneShot(length, vibeValue));
}
catch (Exception e) {
// Fall back to the generic method, which uses DEFAULT_AMPLITUDE, but works even if
// something went horribly wrong with the Android 8.0 APIs.
haptic.vib.vibrate(length);
}
}
}
}
class SDLHapticHandler {
class SDLHaptic {
@ -427,49 +463,9 @@ class SDLHapticHandler {
public void run(int device_id, float intensity, int length) {
SDLHaptic haptic = getHaptic(device_id);
if (haptic != null) {
Log.d("SDL", "Rtest: Vibe with intensity " + intensity + " for " + length);
if (intensity == 0.0f) {
stop(device_id);
return;
}
if (Build.VERSION.SDK_INT >= 26) {
// We have to do this dynamically to avoid issues on earlier SDKs.
// But we want to use the VibrationEffect so we can set amplitude.
try {
int vibeValue = Math.round(intensity * 255);
if (vibeValue > 255) {
vibeValue = 255;
}
if (vibeValue < 1) {
stop(device_id);
return;
}
long longLength = length;
Class vibrationEffectClass = Class.forName("android.os.VibrationEffect");
Method oneShotMethod = vibrationEffectClass.getMethod("createOneShot", long.class, int.class);
Object effect = oneShotMethod.invoke(null, longLength, vibeValue);
Method vibeEffect = android.os.Vibrator.class.getMethod("vibrate", vibrationEffectClass);
vibeEffect.invoke(haptic.vib, vibrationEffectClass.cast(effect));
}
catch (Exception e) {
// Fall back to the generic method, which uses DEFAULT_AMPLITUDE, but works even if
// something went horribly wrong with the Android 8.0 APIs.
haptic.vib.vibrate(length);
}
}
else {
// Fall back to the generic method, which uses DEFAULT_AMPLITUDE, but exists
// on earlier SDKs.
haptic.vib.vibrate (length);
}
}
}
public void stop(int device_id) {
SDLHaptic haptic = getHaptic(device_id);