Created
August 11, 2022 13:38
-
-
Save yasincidem/9a7dc3e7918fd361f25eff96ab4c8c98 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun Context.vibrate() { | |
val vibrator: Vibrator = try { | |
getSystemService(Context.VIBRATOR_SERVICE) as Vibrator | |
} catch (exception: ClassNotFoundException) { | |
exception.recordException() | |
null | |
} ?: return | |
when { | |
VERSION.SDK_INT >= VERSION_CODES.Q -> { | |
VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK).also(vibrator::vibrate) | |
} | |
else -> { | |
VibrationEffect.createOneShot(VIBRATION_DURATION, VibrationEffect.DEFAULT_AMPLITUDE).also(vibrator::vibrate) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment