|
| 1 | +package com.haptics |
| 2 | + |
| 3 | +import android.os.Build |
| 4 | +import com.haptics.HapticsVibrationType |
| 5 | +import android.view.HapticFeedbackConstants |
| 6 | + |
| 7 | +object HapticsUtils { |
| 8 | + private data class HapticInfo(val constant: Int, val requiredApi: Int) |
| 9 | + |
| 10 | + private val notificationTypes = mapOf( |
| 11 | + "success" to HapticsVibrationType( |
| 12 | + timings = longArrayOf(0, 25, 80, 35), |
| 13 | + amplitudes = intArrayOf(0, 120, 0, 180), |
| 14 | + oldFallback = longArrayOf(0, 25, 80, 35) |
| 15 | + ), |
| 16 | + "warning" to HapticsVibrationType( |
| 17 | + timings = longArrayOf(0, 20, 100, 50), |
| 18 | + amplitudes = intArrayOf(0, 200, 0, 255), |
| 19 | + oldFallback = longArrayOf(0, 20, 100, 50) |
| 20 | + ), |
| 21 | + "error" to HapticsVibrationType( |
| 22 | + timings = longArrayOf(0, 20, 50, 20, 50, 20), |
| 23 | + amplitudes = intArrayOf(0, 150, 0, 200, 0, 255), |
| 24 | + oldFallback = longArrayOf(0, 20, 50, 20, 50, 20) |
| 25 | + ) |
| 26 | + ) |
| 27 | + |
| 28 | + private val impactTypes = mapOf( |
| 29 | + "light" to HapticsVibrationType( |
| 30 | + timings = longArrayOf(0, 20), |
| 31 | + amplitudes = intArrayOf(0, 110), |
| 32 | + oldFallback = longArrayOf(0, 20) |
| 33 | + ), |
| 34 | + "soft" to HapticsVibrationType( |
| 35 | + timings = longArrayOf(0, 50), |
| 36 | + amplitudes = intArrayOf(0, 100), |
| 37 | + oldFallback = longArrayOf(0, 50) |
| 38 | + ), |
| 39 | + "medium" to HapticsVibrationType( |
| 40 | + timings = longArrayOf(0, 40), |
| 41 | + amplitudes = intArrayOf(0, 180), |
| 42 | + oldFallback = longArrayOf(0, 40) |
| 43 | + ), |
| 44 | + "rigid" to HapticsVibrationType( |
| 45 | + timings = longArrayOf(0, 30), |
| 46 | + amplitudes = intArrayOf(0, 220), |
| 47 | + oldFallback = longArrayOf(0, 30) |
| 48 | + ), |
| 49 | + "heavy" to HapticsVibrationType( |
| 50 | + timings = longArrayOf(0, 60), |
| 51 | + amplitudes = intArrayOf(0, 255), |
| 52 | + oldFallback = longArrayOf(0, 60) |
| 53 | + ) |
| 54 | + ) |
| 55 | + |
| 56 | + private val ALL_HAPTIC_TYPES = mapOf( |
| 57 | + "long-press" to HapticInfo(HapticFeedbackConstants.LONG_PRESS, 1), |
| 58 | + "clock-tick" to HapticInfo(HapticFeedbackConstants.CLOCK_TICK, 1), |
| 59 | + "virtual-key" to HapticInfo(HapticFeedbackConstants.VIRTUAL_KEY, 1), |
| 60 | + "keyboard-tap" to HapticInfo(HapticFeedbackConstants.KEYBOARD_TAP, 1), |
| 61 | + "reject" to HapticInfo(HapticFeedbackConstants.REJECT, Build.VERSION_CODES.R), |
| 62 | + "confirm" to HapticInfo(HapticFeedbackConstants.CONFIRM, Build.VERSION_CODES.R), |
| 63 | + "gesture-end" to HapticInfo(HapticFeedbackConstants.GESTURE_END, Build.VERSION_CODES.R), |
| 64 | + "gesture-start" to HapticInfo(HapticFeedbackConstants.GESTURE_START, Build.VERSION_CODES.R), |
| 65 | + "context-click" to HapticInfo(HapticFeedbackConstants.CONTEXT_CLICK, Build.VERSION_CODES.M), |
| 66 | + "keyboard-press" to HapticInfo(HapticFeedbackConstants.KEYBOARD_PRESS, Build.VERSION_CODES.O_MR1), |
| 67 | + "toggle-on" to HapticInfo(HapticFeedbackConstants.TOGGLE_ON, Build.VERSION_CODES.UPSIDE_DOWN_CAKE), |
| 68 | + "toggle-off" to HapticInfo(HapticFeedbackConstants.TOGGLE_OFF, Build.VERSION_CODES.UPSIDE_DOWN_CAKE), |
| 69 | + "drag-start" to HapticInfo(HapticFeedbackConstants.DRAG_START, Build.VERSION_CODES.UPSIDE_DOWN_CAKE), |
| 70 | + "keyboard-release" to HapticInfo(HapticFeedbackConstants.KEYBOARD_RELEASE, Build.VERSION_CODES.O_MR1), |
| 71 | + "text-handle-move" to HapticInfo(HapticFeedbackConstants.TEXT_HANDLE_MOVE, Build.VERSION_CODES.O_MR1), |
| 72 | + "no-haptics" to HapticInfo(HapticFeedbackConstants.NO_HAPTICS, Build.VERSION_CODES.UPSIDE_DOWN_CAKE), |
| 73 | + "segment-tick" to HapticInfo(HapticFeedbackConstants.SEGMENT_TICK, Build.VERSION_CODES.UPSIDE_DOWN_CAKE), |
| 74 | + "virtual-key-release" to HapticInfo(HapticFeedbackConstants.VIRTUAL_KEY_RELEASE, Build.VERSION_CODES.O_MR1), |
| 75 | + "segment-frequent-tick" to HapticInfo(HapticFeedbackConstants.SEGMENT_FREQUENT_TICK, Build.VERSION_CODES.UPSIDE_DOWN_CAKE), |
| 76 | + "gesture-threshold-activate" to HapticInfo(HapticFeedbackConstants.GESTURE_THRESHOLD_ACTIVATE, Build.VERSION_CODES.UPSIDE_DOWN_CAKE), |
| 77 | + "gesture-threshold-deactivate" to HapticInfo(HapticFeedbackConstants.GESTURE_THRESHOLD_DEACTIVATE, Build.VERSION_CODES.UPSIDE_DOWN_CAKE) |
| 78 | + ) |
| 79 | + |
| 80 | + fun getNotificationType(type: String): HapticsVibrationType = |
| 81 | + notificationTypes[type] ?: throw IllegalArgumentException("'type' must be one of ${notificationTypes.keys}. Obtained '$type'.") |
| 82 | + |
| 83 | + fun getImpactType(style: String): HapticsVibrationType = |
| 84 | + impactTypes[style] ?: throw IllegalArgumentException("'style' must be one of ${impactTypes.keys}. Obtained '$style'.") |
| 85 | + |
| 86 | + fun getSelectionType(): HapticsVibrationType = |
| 87 | + HapticsVibrationType( |
| 88 | + timings = longArrayOf(0, 10), |
| 89 | + amplitudes = intArrayOf(0, 90), |
| 90 | + oldFallback = longArrayOf(0, 10) |
| 91 | + ) |
| 92 | + |
| 93 | + fun getAndroidHapticsType(type: String): Int { |
| 94 | + val hapticInfo = ALL_HAPTIC_TYPES[type] |
| 95 | + ?: throw IllegalArgumentException( |
| 96 | + "'type' must be one of ${ALL_HAPTIC_TYPES.keys.joinToString()}. Obtained '$type'." |
| 97 | + ) |
| 98 | + return if (Build.VERSION.SDK_INT >= hapticInfo.requiredApi) { |
| 99 | + hapticInfo.constant |
| 100 | + } else { |
| 101 | + HapticFeedbackConstants.VIRTUAL_KEY |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
0 commit comments