Fixed bug 4318 - Android move Haptic code to API26 class
authorSam Lantinga <slouken@libsdl.org>
Tue, 16 Oct 2018 15:00:43 -0700
changeset 123340789a425e8d7
parent 12333 898f03b00670
child 12336 2f4ebd8f5693
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
android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java
     1.1 --- a/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java	Tue Oct 16 14:58:07 2018 -0700
     1.2 +++ b/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java	Tue Oct 16 15:00:43 2018 -0700
     1.3 @@ -1,6 +1,5 @@
     1.4  package org.libsdl.app;
     1.5  
     1.6 -import java.lang.reflect.Method;
     1.7  import java.util.ArrayList;
     1.8  import java.util.Collections;
     1.9  import java.util.Comparator;
    1.10 @@ -12,7 +11,7 @@
    1.11  import android.util.Log;
    1.12  
    1.13  
    1.14 -public class SDLControllerManager 
    1.15 +public class SDLControllerManager
    1.16  {
    1.17  
    1.18      public static native int nativeSetupJNI();
    1.19 @@ -53,7 +52,12 @@
    1.20          } else {
    1.21              mJoystickHandler = new SDLJoystickHandler();
    1.22          }
    1.23 -        mHapticHandler = new SDLHapticHandler();
    1.24 +
    1.25 +        if (Build.VERSION.SDK_INT >= 26) {
    1.26 +            mHapticHandler = new SDLHapticHandler_API26();
    1.27 +        } else {
    1.28 +            mHapticHandler = new SDLHapticHandler();
    1.29 +        }
    1.30      }
    1.31  
    1.32      // Joystick glue code, just a series of stubs that redirect to the SDLJoystickHandler instance
    1.33 @@ -410,6 +414,38 @@
    1.34      }
    1.35  }
    1.36  
    1.37 +class SDLHapticHandler_API26 extends SDLHapticHandler {
    1.38 +    @Override
    1.39 +    public void run(int device_id, float intensity, int length) {
    1.40 +        SDLHaptic haptic = getHaptic(device_id);
    1.41 +        if (haptic != null) {
    1.42 +            Log.d("SDL", "Rtest: Vibe with intensity " + intensity + " for " + length);
    1.43 +            if (intensity == 0.0f) {
    1.44 +                stop(device_id);
    1.45 +                return;
    1.46 +            }
    1.47 +
    1.48 +            int vibeValue = Math.round(intensity * 255);
    1.49 +
    1.50 +            if (vibeValue > 255) {
    1.51 +                vibeValue = 255;
    1.52 +            }
    1.53 +            if (vibeValue < 1) {
    1.54 +                stop(device_id);
    1.55 +                return;
    1.56 +            }
    1.57 +            try {
    1.58 +                haptic.vib.vibrate(VibrationEffect.createOneShot(length, vibeValue));
    1.59 +            }
    1.60 +            catch (Exception e) {
    1.61 +                // Fall back to the generic method, which uses DEFAULT_AMPLITUDE, but works even if
    1.62 +                // something went horribly wrong with the Android 8.0 APIs.
    1.63 +                haptic.vib.vibrate(length);
    1.64 +            }
    1.65 +        }
    1.66 +    }
    1.67 +}
    1.68 +
    1.69  class SDLHapticHandler {
    1.70  
    1.71      class SDLHaptic {
    1.72 @@ -419,7 +455,7 @@
    1.73      }
    1.74  
    1.75      private ArrayList<SDLHaptic> mHaptics;
    1.76 -    
    1.77 +
    1.78      public SDLHapticHandler() {
    1.79          mHaptics = new ArrayList<SDLHaptic>();
    1.80      }
    1.81 @@ -427,47 +463,7 @@
    1.82      public void run(int device_id, float intensity, int length) {
    1.83          SDLHaptic haptic = getHaptic(device_id);
    1.84          if (haptic != null) {
    1.85 -
    1.86 -            Log.d("SDL", "Rtest: Vibe with intensity " + intensity + " for " + length);
    1.87 -            if (intensity == 0.0f) {
    1.88 -                stop(device_id);
    1.89 -                return;
    1.90 -            }
    1.91 -
    1.92 -            if (Build.VERSION.SDK_INT >= 26) {
    1.93 -                // We have to do this dynamically to avoid issues on earlier SDKs.
    1.94 -                // But we want to use the VibrationEffect so we can set amplitude.
    1.95 -
    1.96 -                try {
    1.97 -                    int vibeValue = Math.round(intensity * 255);
    1.98 -
    1.99 -                    if (vibeValue > 255) {
   1.100 -                        vibeValue = 255;
   1.101 -                    }
   1.102 -                    if (vibeValue < 1) {
   1.103 -                        stop(device_id);
   1.104 -                        return;
   1.105 -                    }
   1.106 -
   1.107 -                    long longLength = length;
   1.108 -                    Class vibrationEffectClass = Class.forName("android.os.VibrationEffect");
   1.109 -                    Method oneShotMethod = vibrationEffectClass.getMethod("createOneShot", long.class, int.class);
   1.110 -                    Object effect = oneShotMethod.invoke(null, longLength, vibeValue);
   1.111 -                    Method vibeEffect = android.os.Vibrator.class.getMethod("vibrate", vibrationEffectClass);
   1.112 -                    vibeEffect.invoke(haptic.vib, vibrationEffectClass.cast(effect));
   1.113 -                }
   1.114 -                catch (Exception e) {
   1.115 -                    // Fall back to the generic method, which uses DEFAULT_AMPLITUDE, but works even if
   1.116 -                    // something went horribly wrong with the Android 8.0 APIs.
   1.117 -                    haptic.vib.vibrate(length);
   1.118 -                }
   1.119 -            }
   1.120 -            else {
   1.121 -                // Fall back to the generic method, which uses DEFAULT_AMPLITUDE, but exists
   1.122 -                // on earlier SDKs.
   1.123 -
   1.124 -                haptic.vib.vibrate (length);
   1.125 -            }
   1.126 +            haptic.vib.vibrate(length);
   1.127          }
   1.128      }
   1.129  
   1.130 @@ -479,7 +475,7 @@
   1.131      }
   1.132  
   1.133      public void pollHapticDevices() {
   1.134 -        
   1.135 +
   1.136          final int deviceId_VIBRATOR_SERVICE = 999999;
   1.137          boolean hasVibratorService = false;
   1.138  
   1.139 @@ -523,7 +519,7 @@
   1.140                      haptic = new SDLHaptic();
   1.141                      haptic.device_id = deviceId_VIBRATOR_SERVICE;
   1.142                      haptic.name = "VIBRATOR_SERVICE";
   1.143 -                    haptic.vib = vib; 
   1.144 +                    haptic.vib = vib;
   1.145                      mHaptics.add(haptic);
   1.146                      SDLControllerManager.nativeAddHaptic(haptic.device_id, haptic.name);
   1.147                  }
   1.148 @@ -565,7 +561,7 @@
   1.149              }
   1.150          }
   1.151          return null;
   1.152 -    }   
   1.153 +    }
   1.154  }
   1.155  
   1.156  class SDLGenericMotionListener_API12 implements View.OnGenericMotionListener {
   1.157 @@ -655,7 +651,7 @@
   1.158              case InputDevice.SOURCE_GAMEPAD:
   1.159              case InputDevice.SOURCE_DPAD:
   1.160                  return SDLControllerManager.handleJoystickMotionEvent(event);
   1.161 -                
   1.162 +
   1.163              case InputDevice.SOURCE_MOUSE:
   1.164                  if (!SDLActivity.mSeparateMouseAndTouch) {
   1.165                      break;
   1.166 @@ -746,7 +742,7 @@
   1.167              case InputDevice.SOURCE_GAMEPAD:
   1.168              case InputDevice.SOURCE_DPAD:
   1.169                  return SDLControllerManager.handleJoystickMotionEvent(event);
   1.170 -                
   1.171 +
   1.172              case InputDevice.SOURCE_MOUSE:
   1.173              case 12290: // DeX desktop mouse cursor is a separate non-standard input type.
   1.174                  if (!SDLActivity.mSeparateMouseAndTouch) {
   1.175 @@ -820,12 +816,12 @@
   1.176                  SDLActivity.getContentView().requestPointerCapture();
   1.177              }
   1.178              else {
   1.179 -                SDLActivity.getContentView().releasePointerCapture();            
   1.180 +                SDLActivity.getContentView().releasePointerCapture();
   1.181              }
   1.182              mRelativeModeEnabled = enabled;
   1.183              return true;
   1.184          }
   1.185 -        else 
   1.186 +        else
   1.187          {
   1.188              return false;
   1.189          }
   1.190 @@ -850,4 +846,4 @@
   1.191          // Relative mouse in capture mode will only have relative for X/Y
   1.192          return event.getY(0);
   1.193      }
   1.194 -}
   1.195 \ No newline at end of file
   1.196 +}