Fix EmulationActivity.vibrateDevice assert due to null Vibrator

`EmulationActivity.vibrateDevice` would assert when a `null` Vibrator is provided due to one not being set in the controller configuration. This has now been fixed by the code not playing a vibration when a vibration device isn't selected.
This commit is contained in:
PixelyIon 2021-10-31 23:21:50 +05:30
parent 414c0104c3
commit bed9fbf5e7
3 changed files with 15 additions and 6 deletions

View File

@ -469,10 +469,10 @@ class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback, View.OnTo
@Suppress("unused")
fun vibrateDevice(index : Int, timing : LongArray, amplitude : IntArray) {
val vibrator = if (vibrators[index] != null) {
vibrators[index]!!
vibrators[index]
} else {
inputManager.controllers[index]!!.rumbleDeviceDescriptor?.let {
if (it == "builtin") {
if (it == Controller.BuiltinRumbleDeviceDescriptor) {
val vibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val vibratorManager = getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
vibratorManager.defaultVibrator
@ -496,12 +496,15 @@ class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback, View.OnTo
return@let vibrator
}
}
return@let null
}
} as Vibrator
}
}
val effect = VibrationEffect.createWaveform(timing, amplitude, 0)
vibrator.vibrate(effect)
vibrator?.let {
val effect = VibrationEffect.createWaveform(timing, amplitude, 0)
it.vibrate(effect)
}
}
@Suppress("unused")

View File

@ -48,6 +48,11 @@ open class Controller(val id : Int, var type : ControllerType, var rumbleDeviceD
companion object {
@JvmStatic
private val serialVersionUID = 6529685098267757690L
/**
* The value of [rumbleDeviceDescriptor] when the built-in Vibrator should be utilized
*/
const val BuiltinRumbleDeviceDescriptor = "builtin"
}
}

View File

@ -16,6 +16,7 @@ import emu.skyline.R
import emu.skyline.adapter.controller.ControllerGeneralViewItem
import emu.skyline.databinding.RumbleDialogBinding
import emu.skyline.di.getInputManager
import emu.skyline.input.Controller
import emu.skyline.input.ControllerActivity
/**
@ -73,7 +74,7 @@ class RumbleDialog @JvmOverloads constructor(val item : ControllerGeneralViewIte
if (!vibrator.hasVibrator())
binding.rumbleBuiltin.isEnabled = false
binding.rumbleBuiltin.setOnClickListener {
controller.rumbleDeviceDescriptor = "builtin"
controller.rumbleDeviceDescriptor = Controller.BuiltinRumbleDeviceDescriptor
controller.rumbleDeviceName = getString(R.string.builtin_vibrator)
item.update()