Fixed bug 3191 - haptic system on android?
authorSam Lantinga <slouken@libsdl.org>
Mon, 14 Aug 2017 06:18:08 -0700
changeset 112834276937a9792
parent 11282 180e8906dc3c
child 11284 3db78361e751
Fixed bug 3191 - haptic system on android?

Sylvain

- add vibrator service in the list of haptic devices. I use an hard-coded device_id for it ...
android-project/src/org/libsdl/app/SDLActivity.java
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Mon Aug 14 05:51:44 2017 -0700
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Mon Aug 14 06:18:08 2017 -0700
     1.3 @@ -593,23 +593,6 @@
     1.4         return false;
     1.5      }
     1.6  
     1.7 -
     1.8 -
     1.9 -    /**
    1.10 -     * This method is called by SDL using JNI.
    1.11 -     */
    1.12 -    public static int openURL(String url)
    1.13 -    {
    1.14 -       try {
    1.15 -          Intent i = new Intent(Intent.ACTION_VIEW);
    1.16 -          i.setData(Uri.parse(url));
    1.17 -          mSingleton.startActivity(i);
    1.18 -       } catch (Exception ex) {
    1.19 -          return -1;
    1.20 -       }
    1.21 -       return 0;
    1.22 -    }
    1.23 -
    1.24      /**
    1.25       * This method is called by SDL using JNI.
    1.26       */
    1.27 @@ -892,6 +875,24 @@
    1.28          }
    1.29      }
    1.30  
    1.31 +    /**
    1.32 +     * This method is called by SDL using JNI.
    1.33 +     */
    1.34 +    public static void pollHapticDevices() {
    1.35 +        if (SDLActivity.mSDLThread != null) {
    1.36 +            mHapticHandler.pollHapticDevices();
    1.37 +        }
    1.38 +    }
    1.39 +
    1.40 +    /**
    1.41 +     * This method is called by SDL using JNI.
    1.42 +     */
    1.43 +    public static void hapticRun(int device_id, int length) {
    1.44 +        if (SDLActivity.mSDLThread != null) {
    1.45 +            mHapticHandler.run(device_id, length);
    1.46 +        }
    1.47 +    }
    1.48 +
    1.49      // Check if a given device is considered a possible SDL joystick
    1.50      public static boolean isDeviceSDLJoystick(int deviceId) {
    1.51          InputDevice device = InputDevice.getDevice(deviceId);
    1.52 @@ -1808,18 +1809,6 @@
    1.53          return null;
    1.54      }
    1.55  
    1.56 -    public static void pollHapticDevices() {
    1.57 -        if (SDLActivity.mSDLThread != null) {
    1.58 -            mHapticHandler.pollHapticDevices();
    1.59 -        }
    1.60 -    }
    1.61 -
    1.62 -    public static void hapticRun(int device_id, int length) {
    1.63 -        if (SDLActivity.mSDLThread != null) {
    1.64 -            mHapticHandler.run(device_id, length);
    1.65 -        }
    1.66 -    }
    1.67 -
    1.68      @Override
    1.69      public boolean handleMotionEvent(MotionEvent event) {
    1.70          if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) != 0) {
    1.71 @@ -1918,6 +1907,10 @@
    1.72      }
    1.73  
    1.74      public void pollHapticDevices() {
    1.75 +        
    1.76 +        final int deviceId_VIBRATOR_SERVICE = 999999;
    1.77 +        boolean hasVibrator = false;
    1.78 +
    1.79          int[] deviceIds = InputDevice.getDeviceIds();
    1.80          // It helps processing the device ids in reverse order
    1.81          // For example, in the case of the XBox 360 wireless dongle,
    1.82 @@ -1940,6 +1933,23 @@
    1.83              }
    1.84          }
    1.85  
    1.86 +        /* Check VIBRATOR_SERVICE */
    1.87 +        {
    1.88 +           Vibrator vib = (Vibrator) SDLActivity.mSingleton.getContext().getSystemService(Context.VIBRATOR_SERVICE);
    1.89 +           if (vib != null && vib.hasVibrator()) {
    1.90 +              hasVibrator = true;
    1.91 +              SDLHaptic haptic = getHaptic(deviceId_VIBRATOR_SERVICE);
    1.92 +              if (haptic == null) {
    1.93 +                 haptic = new SDLHaptic();
    1.94 +                 haptic.device_id = deviceId_VIBRATOR_SERVICE;
    1.95 +                 haptic.name = "VIBRATOR_SERVICE";
    1.96 +                 haptic.vib = vib; 
    1.97 +                 mHaptics.add(haptic);
    1.98 +                 SDLActivity.nativeAddHaptic(haptic.device_id, haptic.name);
    1.99 +              }
   1.100 +           }
   1.101 +        }
   1.102 +
   1.103          /* Check removed devices */
   1.104          ArrayList<Integer> removedDevices = new ArrayList<Integer>();
   1.105          for(int i=0; i < mHaptics.size(); i++) {
   1.106 @@ -1948,7 +1958,10 @@
   1.107              for (j=0; j < deviceIds.length; j++) {
   1.108                  if (device_id == deviceIds[j]) break;
   1.109              }
   1.110 -            if (j == deviceIds.length) {
   1.111 +
   1.112 +            if (device_id == deviceId_VIBRATOR_SERVICE && hasVibrator) {
   1.113 +               // don't remove the vibrator if it is still present
   1.114 +            } else if (j == deviceIds.length) {
   1.115                  removedDevices.add(device_id);
   1.116              }
   1.117          }