tilt-shift-camera/app/src/main/java/no/naiv/tiltshift/util/HapticFeedback.kt

71 lines
1.9 KiB
Kotlin
Raw Normal View History

package no.naiv.tiltshift.util
import android.content.Context
import android.os.VibrationEffect
import android.os.Vibrator
import android.os.VibratorManager
import android.util.Log
/**
* Provides haptic feedback for user interactions.
* Gracefully degrades on devices without vibration hardware.
*/
class HapticFeedback(private val context: Context) {
companion object {
private const val TAG = "HapticFeedback"
}
private val vibrator: Vibrator? by lazy {
val vibratorManager =
context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as? VibratorManager
vibratorManager?.defaultVibrator
}
/**
* Light tick for UI feedback (button press, slider change).
*/
fun tick() {
vibrateOrLog(VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK))
}
/**
* Click feedback for confirmations.
*/
fun click() {
vibrateOrLog(VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK))
}
/**
* Heavy click for important actions (photo capture).
*/
fun heavyClick() {
vibrateOrLog(VibrationEffect.createPredefined(VibrationEffect.EFFECT_HEAVY_CLICK))
}
/**
* Success feedback pattern.
*/
fun success() {
val timings = longArrayOf(0, 30, 50, 30)
val amplitudes = intArrayOf(0, 100, 0, 200)
vibrateOrLog(VibrationEffect.createWaveform(timings, amplitudes, -1))
}
/**
* Error feedback pattern.
*/
fun error() {
val timings = longArrayOf(0, 50, 30, 50, 30, 50)
val amplitudes = intArrayOf(0, 150, 0, 150, 0, 150)
vibrateOrLog(VibrationEffect.createWaveform(timings, amplitudes, -1))
}
private fun vibrateOrLog(effect: VibrationEffect) {
try {
vibrator?.vibrate(effect)
} catch (e: Exception) {
Log.w(TAG, "Haptic feedback failed", e)
}
}
}