11package com.wstxda.switchai.ui.utils
22
3- import android.app.NotificationManager
43import android.content.Context
54import android.media.AudioManager
65import android.os.Build
7- import android.os.PowerManager
86import android.os.VibrationAttributes
97import android.os.VibrationEffect
108import android.os.Vibrator
@@ -13,9 +11,6 @@ import com.wstxda.switchai.utils.Constants
1311
1412object VibrationService {
1513
16- private const val EFFECT_CLICK = 1
17- private const val EFFECT_TICK = 2
18-
1914 fun Context.openAssistantVibration () {
2015 if (! isVibrationAllowed()) return
2116 performVibration(effectId = getEffectClick(), fallbackDuration = 12 )
@@ -26,15 +21,11 @@ object VibrationService {
2621 performVibration(effectId = getEffectTick(), fallbackDuration = 8 )
2722 }
2823
29- private fun Context.performVibration (
30- effectId : Int ,
31- fallbackDuration : Long ,
32- ) {
24+ private fun Context.performVibration (effectId : Int , fallbackDuration : Long ) {
3325 val vibrator = getSystemService(Vibrator ::class .java) ? : return
3426
3527 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
3628 val effect = VibrationEffect .createPredefined(effectId)
37-
3829 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
3930 val attrs =
4031 VibrationAttributes .Builder ().setUsage(VibrationAttributes .USAGE_TOUCH ).build()
@@ -43,32 +34,20 @@ object VibrationService {
4334 vibrator.vibrate(effect)
4435 }
4536 } else {
46- val effect = VibrationEffect .createOneShot (
47- fallbackDuration, VibrationEffect .DEFAULT_AMPLITUDE
37+ vibrator.vibrate (
38+ VibrationEffect .createOneShot( fallbackDuration, VibrationEffect .DEFAULT_AMPLITUDE )
4839 )
49- vibrator.vibrate(effect)
5040 }
5141 }
5242
53- private fun getEffectClick (): Int {
54- return if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
55- VibrationEffect .EFFECT_CLICK
56- } else {
57- EFFECT_CLICK
58- }
59- }
43+ private fun getEffectClick (): Int =
44+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) VibrationEffect .EFFECT_CLICK else 1
6045
61- private fun getEffectTick (): Int {
62- return if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
63- VibrationEffect .EFFECT_TICK
64- } else {
65- EFFECT_TICK
66- }
67- }
46+ private fun getEffectTick (): Int =
47+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) VibrationEffect .EFFECT_TICK else 2
6848
6949 private fun Context.isVibrationAllowed (): Boolean {
70- val preferenceHelper = PreferenceHelper (this )
71- if (! preferenceHelper.getBoolean(Constants .ASSISTANT_VIBRATION_PREF_KEY , true )) {
50+ if (! PreferenceHelper (this ).getBoolean(Constants .ASSISTANT_VIBRATION_PREF_KEY , true )) {
7251 return false
7352 }
7453
@@ -77,19 +56,7 @@ object VibrationService {
7756 return false
7857 }
7958
80- val powerManager = getSystemService(Context .POWER_SERVICE ) as PowerManager
81- if (powerManager.isPowerSaveMode) {
82- return false
83- }
84-
8559 val audioManager = getSystemService(Context .AUDIO_SERVICE ) as AudioManager
86- if (audioManager.ringerMode == AudioManager .RINGER_MODE_SILENT ) {
87- return false
88- }
89-
90- val notificationManager =
91- getSystemService(Context .NOTIFICATION_SERVICE ) as NotificationManager
92- val currentFilter = notificationManager.currentInterruptionFilter
93- return currentFilter == NotificationManager .INTERRUPTION_FILTER_ALL
60+ return audioManager.ringerMode != AudioManager .RINGER_MODE_SILENT
9461 }
9562}
0 commit comments