Fix Input.vibrate_handheld on Android.

This commit is contained in:
Alexander Hartmann 2025-06-11 02:54:53 +02:00
parent 1bbfe637c6
commit a1e12dca3f

View File

@ -1011,25 +1011,29 @@ class Godot(private val context: Context) {
@Keep @Keep
private fun vibrate(durationMs: Int, amplitude: Int) { private fun vibrate(durationMs: Int, amplitude: Int) {
if (durationMs > 0 && requestPermission("VIBRATE")) { if (durationMs > 0 && requestPermission("VIBRATE")) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { try {
if (amplitude <= -1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibratorService.vibrate( if (amplitude <= -1) {
VibrationEffect.createOneShot( vibratorService.vibrate(
durationMs.toLong(), VibrationEffect.createOneShot(
VibrationEffect.DEFAULT_AMPLITUDE durationMs.toLong(),
VibrationEffect.DEFAULT_AMPLITUDE
)
) )
) } else {
vibratorService.vibrate(
VibrationEffect.createOneShot(
durationMs.toLong(),
amplitude
)
)
}
} else { } else {
vibratorService.vibrate( // deprecated in API 26
VibrationEffect.createOneShot( vibratorService.vibrate(durationMs.toLong())
durationMs.toLong(),
amplitude
)
)
} }
} else { } catch (e: SecurityException) {
// deprecated in API 26 Log.w(TAG, "SecurityException: VIBRATE permission not found. Make sure it is declared in the manifest or enabled in the export preset.")
vibratorService.vibrate(durationMs.toLong())
} }
} }
} }