-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathHybridHaptics.kt
More file actions
125 lines (112 loc) · 3.68 KB
/
HybridHaptics.kt
File metadata and controls
125 lines (112 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.margelo.nitro.haptics
import android.content.Context
import android.os.Build
import android.os.VibrationEffect
import android.os.Vibrator
import android.os.VibratorManager
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.annotation.Keep
import com.facebook.proguard.annotations.DoNotStrip
import com.margelo.nitro.NitroModules
import com.margelo.nitro.haptics.HapticsVibrationType
import com.margelo.nitro.haptics.ImpactFeedbackStyle
import com.margelo.nitro.haptics.NotificationFeedbackType
class HybridHaptics: HybridHapticsSpec() {
private val TAG = "Haptics"
private val context: Context
get() = NitroModules.applicationContext ?: throw Error("Lost applicationContext")
private val vibrator: Vibrator
get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
(context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager).defaultVibrator
} else {
@Suppress("DEPRECATION")
context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
}
private val impactTypes = mapOf(
ImpactFeedbackStyle.LIGHT to HapticsVibrationType(
longArrayOf(0, 50),
intArrayOf(0, 30),
longArrayOf(0, 20)
),
ImpactFeedbackStyle.SOFT to HapticsVibrationType(
longArrayOf(0, 50),
intArrayOf(0, 30),
longArrayOf(0, 20)
),
ImpactFeedbackStyle.MEDIUM to HapticsVibrationType(
longArrayOf(0, 43),
intArrayOf(0, 50),
longArrayOf(0, 43)
),
ImpactFeedbackStyle.RIGID to HapticsVibrationType(
longArrayOf(0, 43),
intArrayOf(0, 50),
longArrayOf(0, 43)
),
ImpactFeedbackStyle.HEAVY to HapticsVibrationType(
longArrayOf(0, 60),
intArrayOf(0, 70),
longArrayOf(0, 61)
)
)
private val notificationTypes = mapOf(
NotificationFeedbackType.SUCCESS to HapticsVibrationType(
longArrayOf(0, 40, 100, 40),
intArrayOf(0, 50, 0, 60),
longArrayOf(0, 40, 100, 40)
),
NotificationFeedbackType.WARNING to HapticsVibrationType(
longArrayOf(0, 40, 120, 60),
intArrayOf(0, 40, 0, 60),
longArrayOf(0, 40, 120, 60)
),
NotificationFeedbackType.ERROR to HapticsVibrationType(
longArrayOf(0, 60, 100, 40, 80, 50),
intArrayOf(0, 50, 0, 40, 0, 50),
longArrayOf(0, 60, 100, 40, 80, 50)
)
)
private val selectionType = HapticsVibrationType(
timings = longArrayOf(0, 50),
amplitudes = intArrayOf(0, 30),
oldSDKPattern = longArrayOf(0, 70)
)
@DoNotStrip
@Keep
override fun impact(style: ImpactFeedbackStyle): Unit {
val vibrationType: HapticsVibrationType = impactTypes.getOrElse(style) {
throw Error("'style' must be one of ['light', 'medium', 'heavy', 'rigid', 'soft']. Obtained $style'.")
}
vibrate(vibrationType)
}
@DoNotStrip
@Keep
override fun notification(type: NotificationFeedbackType): Unit {
val vibrationType: HapticsVibrationType = notificationTypes.getOrElse(type) {
throw Error("'type' must be one of ['success', 'warning', 'error']. Obtained $type'.")
}
vibrate(vibrationType)
}
@DoNotStrip
@Keep
override fun selection(): Unit {
vibrate(selectionType)
}
@DoNotStrip
@Keep
override fun impactWithIntensity(style: ImpactFeedbackStyle, intensity: Double): Unit {
Log.e("Haptics", "impactWithIntensity is unsupported on Android")
}
override val memorySize: Long
get() = 0L
private fun vibrate(type: HapticsVibrationType) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createWaveform(type.timings, type.amplitudes, -1))
} else {
@Suppress("DEPRECATION")
vibrator.vibrate(type.oldSDKPattern, -1)
}
}
}