@@ -337,25 +337,34 @@ def on_stop(self):
337
337
338
338
def test_pyjnius (self , * args ):
339
339
try :
340
- from jnius import autoclass
340
+ from jnius import autoclass , cast
341
341
except ImportError :
342
- raise_error (" Could not import pyjnius" )
342
+ raise_error (' Could not import pyjnius' )
343
343
return
344
-
345
- print ("Attempting to vibrate with pyjnius" )
346
- # Todo: fix vibrate with Api level >= 26
347
- # vibrate was deprecated in API level 26:
348
- # https://developer.android.com/reference/android/os/Vibrator
349
- try :
350
- PythonActivity = autoclass ("org.kivy.android.PythonActivity" )
351
- activity = PythonActivity .mActivity
352
- Intent = autoclass ("android.content.Intent" )
353
- Context = autoclass ("android.content.Context" )
354
- vibrator = activity .getSystemService (Context .VIBRATOR_SERVICE )
355
-
344
+ print ('Attempting to vibrate with pyjnius' )
345
+ ANDROID_VERSION = autoclass ('android.os.Build$VERSION' )
346
+ SDK_INT = ANDROID_VERSION .SDK_INT
347
+
348
+ Context = autoclass ("android.content.Context" )
349
+ PythonActivity = autoclass ('org.kivy.android.PythonActivity' )
350
+ activity = PythonActivity .mActivity
351
+
352
+ vibrator_service = activity .getSystemService (Context .VIBRATOR_SERVICE )
353
+ vibrator = cast ("android.os.Vibrator" , vibrator_service )
354
+
355
+ if vibrator and SDK_INT >= 26 :
356
+ print ("Using android's `VibrationEffect` (SDK >= 26)" )
357
+ VibrationEffect = autoclass ("android.os.VibrationEffect" )
358
+ vibrator .vibrate (
359
+ VibrationEffect .createOneShot (
360
+ 1000 , VibrationEffect .DEFAULT_AMPLITUDE ,
361
+ ),
362
+ )
363
+ elif vibrator :
364
+ print ("Using deprecated android's vibrate (SDK < 26)" )
356
365
vibrator .vibrate (1000 )
357
- except Exception as e :
358
- raise_error ( "Error when trying to vibrate: {}" . format ( e ) )
366
+ else :
367
+ print ( 'Something happened...vibrator service disabled?' )
359
368
360
369
def test_ctypes (self , * args ):
361
370
try :
0 commit comments