Updated on 2026-08-14

This commit is contained in:
Tangem 2024-08-07 15:42:58 +03:00
parent ac8436460c
commit 459db3d0df
17 changed files with 263 additions and 102 deletions

View file

@ -1,36 +0,0 @@
package com.tangem.tap.common.haptic
import android.os.Build
import android.os.VibrationEffect
import android.os.Vibrator
import com.tangem.core.ui.haptic.HapticManager
class DefaultHapticManager(private val vibrator: Vibrator) : HapticManager {
override fun vibrateShort() {
vibrate(VIBRATION_SHORT_DURATION)
}
override fun vibrateMeduim() {
vibrate(VIBRATION_MEDIUM_LOW_DURATION)
}
override fun vibrateLong() {
vibrate(VIBRATION_MEDIUM_DURATION)
}
private fun vibrate(durationMs: Long) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneShot(durationMs, VibrationEffect.DEFAULT_AMPLITUDE))
} else {
vibrator.vibrate(durationMs)
}
}
companion object {
const val VIBRATION_SHORT_DURATION = 50L
const val VIBRATION_MEDIUM_LOW_DURATION = 75L
const val VIBRATION_MEDIUM_DURATION = 100L
const val VIBRATION_LONG_DURATION = 200L
}
}

View file

@ -0,0 +1,38 @@
package com.tangem.tap.common.haptic
import android.os.Build
import android.os.VibrationEffect
import android.os.Vibrator
import androidx.annotation.ChecksSdkIntAtLeast
import com.tangem.core.ui.haptic.TangemHapticEffect
import com.tangem.core.ui.haptic.VibratorHapticManager
internal class DefaultVibratorHapticManager(
private val vibrator: Vibrator,
) : VibratorHapticManager {
private val isHapticEnabled = deviceSupportsVibrationEffects()
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.Q)
private fun deviceSupportsVibrationEffects(): Boolean = when {
!vibrator.hasVibrator() -> false
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R ->
vibrator.areAllEffectsSupported(
TangemHapticEffect.OneTime.DoubleClick.code,
TangemHapticEffect.OneTime.HeavyClick.code,
TangemHapticEffect.OneTime.Tick.code,
TangemHapticEffect.OneTime.Click.code,
) == Vibrator.VIBRATION_EFFECT_SUPPORT_YES
Build.VERSION.SDK_INT == Build.VERSION_CODES.Q -> true
else -> false
}
override fun performOneTime(effect: TangemHapticEffect.OneTime) {
if (!isHapticEnabled) return
vibrator.vibrate(VibrationEffect.createPredefined(effect.code))
}
}