Merge pull request #107385 from Alex2782/fix_vibrate_android

Fix `Input.vibrate_handheld` on Android.
This commit is contained in:
Rémi Verschelde 2025-06-12 22:48:54 +02:00
commit 6845898461
No known key found for this signature in database
GPG Key ID: C3336907360768E1

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())
} }
} }
} }