therealbluepandabear
04/08/2021, 4:45 AMclass VibratorHelper {
companion object {
private val vibrationNotSupportedMessage: String = "Your device (${Build.MANUFACTURER + Build.MODEL} API ${Build.VERSION.SDK_INT}) does not support vibration."
fun vibrateFor(milliseconds: Long, context: Context) {
val service = context.getSystemService(VIBRATOR_SERVICE) as Vibrator
@Suppress("DEPRECATION")
if (!service.hasVibrator()) Toast.makeText(context, vibrationNotSupportedMessage, Toast.LENGTH_SHORT) else service.vibrate(milliseconds)
}
fun vibrateIndefinitely(context: Context) = vibrateFor(Long.MAX_VALUE, context)
}
}
ephemient
04/08/2021, 4:54 AMtherealbluepandabear
04/08/2021, 4:56 AMephemient
04/08/2021, 5:30 AMtherealbluepandabear
04/08/2021, 5:35 AMephemient
04/08/2021, 5:36 AMobject VibrationHelper {
....
}
therealbluepandabear
04/08/2021, 5:37 AMephemient
04/08/2021, 5:38 AMimport androidx.core.content.getSystemService
fun Context.vibrate(millis: Long, amplitude: Int = VibrationEffect.DEFAULT_AMPLITUDE) {
val vibrator = getSystemService<Vibrator>()
if (vibrator?.hasVibrator() != true) {
Toast.makeText(this, R.string.vibration_not_supported, Toast.LENGTH_SHORT).show()
return
}
vibrator.vibrate(VibrationEffect.createOneShot(millis, amplitude))
}
define your string in a translatable strings.xmltherealbluepandabear
04/08/2021, 5:42 AMephemient
04/08/2021, 5:46 AM