@@ -4,7 +4,8 @@ import android.content.Context
44import android.os.Build
55import android.os.Vibrator
66import android.os.VibrationEffect
7- import com.haptics.HapticsVibrationType
7+ import android.media.AudioAttributes
8+ import android.os.VibrationAttributes
89import android.view.HapticFeedbackConstants
910import com.facebook.react.bridge.Promise
1011import com.facebook.react.bridge.ReactApplicationContext
@@ -24,6 +25,21 @@ class HapticsModule(reactContext: ReactApplicationContext) :
2425 }
2526 }
2627
28+ private val audioAttributes: AudioAttributes by lazy {
29+ AudioAttributes .Builder ()
30+ .setContentType(AudioAttributes .CONTENT_TYPE_SONIFICATION )
31+ .setUsage(AudioAttributes .USAGE_ASSISTANCE_SONIFICATION )
32+ .build()
33+ }
34+
35+ private val vibrationAttributes: Any? by lazy {
36+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
37+ VibrationAttributes .createForUsage(VibrationAttributes .USAGE_TOUCH )
38+ } else {
39+ null
40+ }
41+ }
42+
2743 override fun getName (): String {
2844 return NAME
2945 }
@@ -95,10 +111,19 @@ class HapticsModule(reactContext: ReactApplicationContext) :
95111 } else {
96112 VibrationEffect .createWaveform(type.timings, - 1 )
97113 }
98- vibrator?.vibrate(effect)
114+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU && vibrationAttributes != null ) {
115+ vibrator?.vibrate(effect, vibrationAttributes as VibrationAttributes )
116+ } else {
117+ vibrator?.vibrate(effect, audioAttributes)
118+ }
99119 } else {
100- @Suppress(" DEPRECATION" )
101- vibrator?.vibrate(type.oldFallback, - 1 )
120+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
121+ @Suppress(" DEPRECATION" )
122+ vibrator?.vibrate(type.oldFallback, - 1 , audioAttributes)
123+ } else {
124+ @Suppress(" DEPRECATION" )
125+ vibrator?.vibrate(type.oldFallback, - 1 )
126+ }
102127 }
103128 }
104129
0 commit comments