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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java b/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java
index c6a33e8..782a92e 100644
--- a/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java
+++ b/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java
@@ -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;
@@ -12,7 +11,7 @@ import android.view.*;
import android.util.Log;
-public class SDLControllerManager
+public class SDLControllerManager
{
public static native int nativeSetupJNI();
@@ -53,7 +52,12 @@ public class SDLControllerManager
} else {
mJoystickHandler = new SDLJoystickHandler();
}
- mHapticHandler = new SDLHapticHandler();
+
+ 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
@@ -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 {
@@ -419,7 +455,7 @@ class SDLHapticHandler {
}
private ArrayList<SDLHaptic> mHaptics;
-
+
public SDLHapticHandler() {
mHaptics = new ArrayList<SDLHaptic>();
}
@@ -427,47 +463,7 @@ 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);
- }
+ haptic.vib.vibrate(length);
}
}
@@ -479,7 +475,7 @@ class SDLHapticHandler {
}
public void pollHapticDevices() {
-
+
final int deviceId_VIBRATOR_SERVICE = 999999;
boolean hasVibratorService = false;
@@ -523,7 +519,7 @@ class SDLHapticHandler {
haptic = new SDLHaptic();
haptic.device_id = deviceId_VIBRATOR_SERVICE;
haptic.name = "VIBRATOR_SERVICE";
- haptic.vib = vib;
+ haptic.vib = vib;
mHaptics.add(haptic);
SDLControllerManager.nativeAddHaptic(haptic.device_id, haptic.name);
}
@@ -565,7 +561,7 @@ class SDLHapticHandler {
}
}
return null;
- }
+ }
}
class SDLGenericMotionListener_API12 implements View.OnGenericMotionListener {
@@ -655,7 +651,7 @@ class SDLGenericMotionListener_API24 extends SDLGenericMotionListener_API12 {
case InputDevice.SOURCE_GAMEPAD:
case InputDevice.SOURCE_DPAD:
return SDLControllerManager.handleJoystickMotionEvent(event);
-
+
case InputDevice.SOURCE_MOUSE:
if (!SDLActivity.mSeparateMouseAndTouch) {
break;
@@ -746,7 +742,7 @@ class SDLGenericMotionListener_API26 extends SDLGenericMotionListener_API24 {
case InputDevice.SOURCE_GAMEPAD:
case InputDevice.SOURCE_DPAD:
return SDLControllerManager.handleJoystickMotionEvent(event);
-
+
case InputDevice.SOURCE_MOUSE:
case 12290: // DeX desktop mouse cursor is a separate non-standard input type.
if (!SDLActivity.mSeparateMouseAndTouch) {
@@ -820,12 +816,12 @@ class SDLGenericMotionListener_API26 extends SDLGenericMotionListener_API24 {
SDLActivity.getContentView().requestPointerCapture();
}
else {
- SDLActivity.getContentView().releasePointerCapture();
+ SDLActivity.getContentView().releasePointerCapture();
}
mRelativeModeEnabled = enabled;
return true;
}
- else
+ else
{
return false;
}
@@ -850,4 +846,4 @@ class SDLGenericMotionListener_API26 extends SDLGenericMotionListener_API24 {
// Relative mouse in capture mode will only have relative for X/Y
return event.getY(0);
}
-}
\ No newline at end of file
+}