1package com.peachpasswords.haptics23import android.content.Context4import android.os.Build5import android.os.VibrationEffect6import android.os.Vibrator7import android.os.VibratorManager8import android.view.HapticFeedbackConstants9import android.view.View1011object PeachHaptics {12 fun onUnlock(view: View) = view.performHapticFeedback(HapticFeedbackConstants.CONFIRM)13 fun onEntryTap(view: View) = view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY)14 fun onCopy(view: View) = view.performHapticFeedback(HapticFeedbackConstants.CLOCK_TICK)15 fun onSheetOpen(view: View) = view.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK)16 fun onSheetClose(view: View) = view.performHapticFeedback(HapticFeedbackConstants.GESTURE_END)17 fun onError(view: View) = view.performHapticFeedback(HapticFeedbackConstants.REJECT)18 fun onButton(view: View) = view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY)19 fun onCardFocus(view: View) = view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY)2021 fun onLongPress(context: Context) = vibrate(context, 20L, 180)22 fun onSwipeCopy(context: Context) = vibrate(context, 15L, 150)2324 fun onPairingComplete(context: Context) {25 val vibrator = getVibrator(context) ?: return26 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {27 vibrator.vibrate(28 VibrationEffect.createWaveform(29 longArrayOf(0, 30, 60, 50),30 intArrayOf(0, 180, 0, 120),31 -1,32 )33 )34 }35 }3637 private fun vibrate(context: Context, duration: Long, amplitude: Int) {38 val vibrator = getVibrator(context) ?: return39 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {40 vibrator.vibrate(VibrationEffect.createOneShot(duration, amplitude))41 } else {42 @Suppress("DEPRECATION")43 vibrator.vibrate(duration)44 }45 }4647 private fun getVibrator(context: Context): Vibrator? {48 return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {49 val manager = context.getSystemService(VibratorManager::class.java)50 manager?.defaultVibrator51 } else {52 @Suppress("DEPRECATION")53 context.getSystemService(Context.VIBRATOR_SERVICE) as? Vibrator54 }55 }56}