package com.craigvg.lichun_android.utils import android.content.Context import android.os.Build import android.os.VibrationEffect import android.os.Vibrator import android.os.VibratorManager import android.view.HapticFeedbackConstants import android.view.View /** * Haptic feedback utility * Ported from iOS HapticFeedback.swift */ object HapticFeedback { /** * Light haptic feedback (keyboard tap) */ fun light(view: View) { view.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP) } /** * Medium haptic feedback (context click) */ fun medium(view: View) { view.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK) } /** * Heavy haptic feedback (long press) */ fun heavy(view: View) { view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS) } /** * Success haptic feedback */ fun success(view: View) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { view.performHapticFeedback(HapticFeedbackConstants.CONFIRM) } else { view.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK) } } /** * Warning haptic feedback */ fun warning(context: Context) { vibrate(context, 100L) } /** * Error haptic feedback */ fun error(context: Context) { vibrate(context, 200L) } /** * Custom vibration */ private fun vibrate(context: Context, duration: Long) { val vibrator = 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 } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { vibrator.vibrate(VibrationEffect.createOneShot(duration, VibrationEffect.DEFAULT_AMPLITUDE)) } else { @Suppress("DEPRECATION") vibrator.vibrate(duration) } } }