Commit d03409e118785c0a7135173ff4492a0c9d45799d

Sam Lantinga 2017-08-14T06:18:08

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 ...

diff --git a/android-project/src/org/libsdl/app/SDLActivity.java b/android-project/src/org/libsdl/app/SDLActivity.java
index d500b6d..711b7f3 100644
--- a/android-project/src/org/libsdl/app/SDLActivity.java
+++ b/android-project/src/org/libsdl/app/SDLActivity.java
@@ -593,23 +593,6 @@ public class SDLActivity extends Activity {
        return false;
     }
 
-
-
-    /**
-     * This method is called by SDL using JNI.
-     */
-    public static int openURL(String url)
-    {
-       try {
-          Intent i = new Intent(Intent.ACTION_VIEW);
-          i.setData(Uri.parse(url));
-          mSingleton.startActivity(i);
-       } catch (Exception ex) {
-          return -1;
-       }
-       return 0;
-    }
-
     /**
      * This method is called by SDL using JNI.
      */
@@ -892,6 +875,24 @@ public class SDLActivity extends Activity {
         }
     }
 
+    /**
+     * This method is called by SDL using JNI.
+     */
+    public static void pollHapticDevices() {
+        if (SDLActivity.mSDLThread != null) {
+            mHapticHandler.pollHapticDevices();
+        }
+    }
+
+    /**
+     * This method is called by SDL using JNI.
+     */
+    public static void hapticRun(int device_id, int length) {
+        if (SDLActivity.mSDLThread != null) {
+            mHapticHandler.run(device_id, length);
+        }
+    }
+
     // Check if a given device is considered a possible SDL joystick
     public static boolean isDeviceSDLJoystick(int deviceId) {
         InputDevice device = InputDevice.getDevice(deviceId);
@@ -1808,18 +1809,6 @@ class SDLJoystickHandler_API12 extends SDLJoystickHandler {
         return null;
     }
 
-    public static void pollHapticDevices() {
-        if (SDLActivity.mSDLThread != null) {
-            mHapticHandler.pollHapticDevices();
-        }
-    }
-
-    public static void hapticRun(int device_id, int length) {
-        if (SDLActivity.mSDLThread != null) {
-            mHapticHandler.run(device_id, length);
-        }
-    }
-
     @Override
     public boolean handleMotionEvent(MotionEvent event) {
         if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) != 0) {
@@ -1918,6 +1907,10 @@ class SDLHapticHandler {
     }
 
     public void pollHapticDevices() {
+        
+        final int deviceId_VIBRATOR_SERVICE = 999999;
+        boolean hasVibrator = false;
+
         int[] deviceIds = InputDevice.getDeviceIds();
         // It helps processing the device ids in reverse order
         // For example, in the case of the XBox 360 wireless dongle,
@@ -1940,6 +1933,23 @@ class SDLHapticHandler {
             }
         }
 
+        /* Check VIBRATOR_SERVICE */
+        {
+           Vibrator vib = (Vibrator) SDLActivity.mSingleton.getContext().getSystemService(Context.VIBRATOR_SERVICE);
+           if (vib != null && vib.hasVibrator()) {
+              hasVibrator = true;
+              SDLHaptic haptic = getHaptic(deviceId_VIBRATOR_SERVICE);
+              if (haptic == null) {
+                 haptic = new SDLHaptic();
+                 haptic.device_id = deviceId_VIBRATOR_SERVICE;
+                 haptic.name = "VIBRATOR_SERVICE";
+                 haptic.vib = vib; 
+                 mHaptics.add(haptic);
+                 SDLActivity.nativeAddHaptic(haptic.device_id, haptic.name);
+              }
+           }
+        }
+
         /* Check removed devices */
         ArrayList<Integer> removedDevices = new ArrayList<Integer>();
         for(int i=0; i < mHaptics.size(); i++) {
@@ -1948,7 +1958,10 @@ class SDLHapticHandler {
             for (j=0; j < deviceIds.length; j++) {
                 if (device_id == deviceIds[j]) break;
             }
-            if (j == deviceIds.length) {
+
+            if (device_id == deviceId_VIBRATOR_SERVICE && hasVibrator) {
+               // don't remove the vibrator if it is still present
+            } else if (j == deviceIds.length) {
                 removedDevices.add(device_id);
             }
         }