@@ -23,47 +23,45 @@ class Haptics(private val context: Context) {
2323 perform(getEffectTick())
2424 }
2525
26- fun long (duration : Long ) {
27- performOneShot(duration)
26+ fun long (duration : Long , force : Boolean = false ) {
27+ performOneShot(duration, force )
2828 }
2929
3030 fun cancel () {
3131 vibrator.cancel()
3232 }
3333
3434 private fun perform (effectId : Int ) {
35- if (! isAllowed()) return
35+ if (! isAllowed(force = false )) return
3636
3737 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
3838 val effect = VibrationEffect .createPredefined(effectId)
3939
4040 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
4141 val attrs =
42- VibrationAttributes .Builder ().setUsage(VibrationAttributes .USAGE_ALARM ).build()
43-
42+ VibrationAttributes .Builder ().setUsage(VibrationAttributes .USAGE_TOUCH ).build()
4443 vibrator.vibrate(effect, attrs)
4544 } else {
4645 @Suppress(" DEPRECATION" ) vibrator.vibrate(effect)
4746 }
48-
4947 } else {
50- val effect = VibrationEffect .createOneShot(
51- 12 , VibrationEffect .DEFAULT_AMPLITUDE
52- )
48+ val effect = VibrationEffect .createOneShot(12 , VibrationEffect .DEFAULT_AMPLITUDE )
5349 @Suppress(" DEPRECATION" ) vibrator.vibrate(effect)
5450 }
5551 }
5652
57- private fun performOneShot (duration : Long ) {
58- if (! isAllowed()) return
53+ private fun performOneShot (duration : Long , force : Boolean ) {
54+ if (! isAllowed(force )) return
5955
6056 val effect = VibrationEffect .createOneShot(
6157 duration, VibrationEffect .DEFAULT_AMPLITUDE
6258 )
6359
6460 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
65- val attrs =
66- VibrationAttributes .Builder ().setUsage(VibrationAttributes .USAGE_ALARM ).build()
61+ val usage =
62+ if (force) VibrationAttributes .USAGE_ALARM else VibrationAttributes .USAGE_TOUCH
63+
64+ val attrs = VibrationAttributes .Builder ().setUsage(usage).build()
6765 vibrator.vibrate(effect, attrs)
6866 } else {
6967 @Suppress(" DEPRECATION" ) vibrator.vibrate(effect)
@@ -74,9 +72,9 @@ class Haptics(private val context: Context) {
7472 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) VibrationEffect .EFFECT_TICK
7573 else 2
7674
77- private fun isAllowed (): Boolean {
75+ private fun isAllowed (force : Boolean ): Boolean {
7876 if (! vibrator.hasVibrator()) return false
79-
77+ if (force) return true
8078
8179 val am = context.getSystemService(AudioManager ::class .java)
8280 if (am.ringerMode == AudioManager .RINGER_MODE_SILENT ) return false
0 commit comments