87 lines
2.6 KiB
Kotlin
87 lines
2.6 KiB
Kotlin
|
|
package no.naiv.tiltshift.util
|
||
|
|
|
||
|
|
import android.content.Context
|
||
|
|
import android.os.Build
|
||
|
|
import android.os.VibrationEffect
|
||
|
|
import android.os.Vibrator
|
||
|
|
import android.os.VibratorManager
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Provides haptic feedback for user interactions.
|
||
|
|
*/
|
||
|
|
class HapticFeedback(private val context: Context) {
|
||
|
|
|
||
|
|
private val vibrator: Vibrator by lazy {
|
||
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||
|
|
val vibratorManager = context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
|
||
|
|
vibratorManager.defaultVibrator
|
||
|
|
} else {
|
||
|
|
@Suppress("DEPRECATION")
|
||
|
|
context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Light tick for UI feedback (button press, slider change).
|
||
|
|
*/
|
||
|
|
fun tick() {
|
||
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||
|
|
vibrator.vibrate(VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK))
|
||
|
|
} else {
|
||
|
|
@Suppress("DEPRECATION")
|
||
|
|
vibrator.vibrate(10L)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Click feedback for confirmations.
|
||
|
|
*/
|
||
|
|
fun click() {
|
||
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||
|
|
vibrator.vibrate(VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK))
|
||
|
|
} else {
|
||
|
|
@Suppress("DEPRECATION")
|
||
|
|
vibrator.vibrate(20L)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Heavy click for important actions (photo capture).
|
||
|
|
*/
|
||
|
|
fun heavyClick() {
|
||
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||
|
|
vibrator.vibrate(VibrationEffect.createPredefined(VibrationEffect.EFFECT_HEAVY_CLICK))
|
||
|
|
} else {
|
||
|
|
@Suppress("DEPRECATION")
|
||
|
|
vibrator.vibrate(40L)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Success feedback pattern.
|
||
|
|
*/
|
||
|
|
fun success() {
|
||
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||
|
|
val timings = longArrayOf(0, 30, 50, 30)
|
||
|
|
val amplitudes = intArrayOf(0, 100, 0, 200)
|
||
|
|
vibrator.vibrate(VibrationEffect.createWaveform(timings, amplitudes, -1))
|
||
|
|
} else {
|
||
|
|
@Suppress("DEPRECATION")
|
||
|
|
vibrator.vibrate(longArrayOf(0, 30, 50, 30), -1)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Error feedback pattern.
|
||
|
|
*/
|
||
|
|
fun error() {
|
||
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||
|
|
val timings = longArrayOf(0, 50, 30, 50, 30, 50)
|
||
|
|
val amplitudes = intArrayOf(0, 150, 0, 150, 0, 150)
|
||
|
|
vibrator.vibrate(VibrationEffect.createWaveform(timings, amplitudes, -1))
|
||
|
|
} else {
|
||
|
|
@Suppress("DEPRECATION")
|
||
|
|
vibrator.vibrate(longArrayOf(0, 50, 30, 50, 30, 50), -1)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|