1
- ''' Implementation Vibrator for Android.'''
1
+ """ Implementation Vibrator for Android."""
2
2
3
- from jnius import autoclass
3
+ from jnius import autoclass , cast
4
4
from plyer .facades import Vibrator
5
5
from plyer .platforms .android import activity
6
6
from plyer .platforms .android import SDK_INT
7
7
8
- Context = autoclass ('android.content.Context' )
9
- vibrator = activity .getSystemService (Context .VIBRATOR_SERVICE )
8
+ Context = autoclass ("android.content.Context" )
9
+ vibrator_service = activity .getSystemService (Context .VIBRATOR_SERVICE )
10
+ vibrator = cast ("android.os.Vibrator" , vibrator_service )
11
+ if SDK_INT >= 26 :
12
+ VibrationEffect = autoclass ("android.os.VibrationEffect" )
10
13
11
14
12
15
class AndroidVibrator (Vibrator ):
13
- ''' Android Vibrator class.
16
+ """ Android Vibrator class.
14
17
15
18
Supported features:
16
19
* vibrate for some period of time.
17
20
* vibrate from given pattern.
18
21
* cancel vibration.
19
22
* check whether Vibrator exists.
20
- '''
23
+ """
21
24
22
25
def _vibrate (self , time = None , ** kwargs ):
23
26
if vibrator :
24
- vibrator .vibrate (int (1000 * time ))
27
+ if SDK_INT >= 26 :
28
+ vibrator .vibrate (
29
+ VibrationEffect .createOneShot (
30
+ int (1000 * time ), VibrationEffect .DEFAULT_AMPLITUDE
31
+ )
32
+ )
33
+ else :
34
+ vibrator .vibrate (int (1000 * time ))
25
35
26
36
def _pattern (self , pattern = None , repeat = None , ** kwargs ):
27
37
pattern = [int (1000 * time ) for time in pattern ]
28
38
29
39
if vibrator :
30
- vibrator .vibrate (pattern , repeat )
40
+ if SDK_INT >= 26 :
41
+ vibrator .vibrate (
42
+ VibrationEffect .createWaveform (pattern , repeat )
43
+ )
44
+ else :
45
+ vibrator .vibrate (pattern , repeat )
31
46
32
47
def _exists (self , ** kwargs ):
33
48
if SDK_INT >= 11 :
34
49
return vibrator .hasVibrator ()
35
- elif activity . getSystemService ( Context . VIBRATOR_SERVICE ) is None :
50
+ elif vibrator_service is None :
36
51
raise NotImplementedError ()
37
52
return True
38
53
@@ -41,8 +56,8 @@ def _cancel(self, **kwargs):
41
56
42
57
43
58
def instance ():
44
- ''' Returns Vibrator with android features.
59
+ """ Returns Vibrator with android features.
45
60
46
61
:return: instance of class AndroidVibrator
47
- '''
62
+ """
48
63
return AndroidVibrator ()
0 commit comments