@@ -10,13 +10,30 @@ import com.wstxda.switchai.utils.Constants
1010
1111object VibrationService {
1212
13- fun performVibration ( context : Context ) {
14- if ( ! isVibrationEnabled(context)) return
13+ private const val EFFECT_CLICK = 1
14+ private const val EFFECT_TICK = 2
1515
16- val vibrator = context.getSystemService(Vibrator ::class .java) ? : return
16+ fun Context.openAssistantVibration () {
17+ if (! isVibrationEnabled()) return
18+ performVibration(
19+ effectId = getEffectClick(), fallbackDuration = 40
20+ )
21+ }
22+
23+ fun Context.buttonVibration () {
24+ performVibration(
25+ effectId = getEffectTick(), fallbackDuration = 20
26+ )
27+ }
28+
29+ private fun Context.performVibration (
30+ effectId : Int ,
31+ fallbackDuration : Long ,
32+ ) {
33+ val vibrator = getSystemService(Vibrator ::class .java) ? : return
1734
1835 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
19- val effect = VibrationEffect .createPredefined(VibrationEffect . EFFECT_CLICK )
36+ val effect = VibrationEffect .createPredefined(effectId )
2037
2138 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
2239 val attrs =
@@ -25,21 +42,32 @@ object VibrationService {
2542 } else {
2643 vibrator.vibrate(effect)
2744 }
28-
2945 } else {
3046 val effect = VibrationEffect .createOneShot(
31- 40 , VibrationEffect .DEFAULT_AMPLITUDE
47+ fallbackDuration , VibrationEffect .DEFAULT_AMPLITUDE
3248 )
3349 vibrator.vibrate(effect)
3450 }
3551 }
3652
37- private fun isVibrationEnabled (context : Context ): Boolean {
38- val prefs = PreferenceManager .getDefaultSharedPreferences(context)
39- return prefs.getBoolean(Constants .ASSISTANT_VIBRATION_PREF_KEY , true )
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+ }
4059 }
4160
42- fun Context.openAssistantVibration () {
43- performVibration(this )
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+ }
68+
69+ private fun Context.isVibrationEnabled (): Boolean {
70+ val prefs = PreferenceManager .getDefaultSharedPreferences(this )
71+ return prefs.getBoolean(Constants .ASSISTANT_VIBRATION_PREF_KEY , true )
4472 }
4573}
0 commit comments