Retrieve the vibrator service outside of OSC View

Retrieving the vibrator service inside the view crashes the layout editor. Fix by retrieving it in the activity and passing it to the view.
This commit is contained in:
lynxnb 2023-03-05 18:40:19 +01:00 committed by Niccolò Betto
parent 0eed72664d
commit 17f45c0366
3 changed files with 27 additions and 21 deletions

View File

@ -75,6 +75,11 @@ class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback, View.OnTo
*/ */
lateinit var item : AppItem lateinit var item : AppItem
/**
* The built-in [Vibrator] of the device
*/
lateinit var builtinVibrator : Vibrator
/** /**
* A map of [Vibrator]s that correspond to [InputManager.controllers] * A map of [Vibrator]s that correspond to [InputManager.controllers]
*/ */
@ -263,6 +268,14 @@ class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback, View.OnTo
inputHandler = InputHandler(inputManager, emulationSettings) inputHandler = InputHandler(inputManager, emulationSettings)
setContentView(binding.root) setContentView(binding.root)
builtinVibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val vibratorManager = getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
vibratorManager.defaultVibrator
} else {
@Suppress("DEPRECATION")
getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// Android might not allow child views to overlap the system bars // Android might not allow child views to overlap the system bars
// Override this behavior and force content to extend into the cutout area // Override this behavior and force content to extend into the cutout area
@ -317,6 +330,7 @@ class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback, View.OnTo
// Hide on screen controls when first controller is not set // Hide on screen controls when first controller is not set
binding.onScreenControllerView.apply { binding.onScreenControllerView.apply {
vibrator = builtinVibrator
controllerType = inputHandler.getFirstControllerType() controllerType = inputHandler.getFirstControllerType()
isGone = controllerType == ControllerType.None || !appSettings.onScreenControl isGone = controllerType == ControllerType.None || !appSettings.onScreenControl
setOnButtonStateChangedListener(::onButtonStateChanged) setOnButtonStateChangedListener(::onButtonStateChanged)
@ -558,15 +572,8 @@ class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback, View.OnTo
} else { } else {
inputManager.controllers[index]!!.rumbleDeviceDescriptor?.let { inputManager.controllers[index]!!.rumbleDeviceDescriptor?.let {
if (it == Controller.BuiltinRumbleDeviceDescriptor) { if (it == Controller.BuiltinRumbleDeviceDescriptor) {
val vibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { vibrators[index] = builtinVibrator
val vibratorManager = getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager builtinVibrator
vibratorManager.defaultVibrator
} else {
@Suppress("DEPRECATION")
getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
}
vibrators[index] = vibrator
vibrator
} else { } else {
for (id in InputDevice.getDeviceIds()) { for (id in InputDevice.getDeviceIds()) {
val device = InputDevice.getDevice(id) val device = InputDevice.getDevice(id)

View File

@ -9,14 +9,10 @@ import android.animation.Animator
import android.animation.AnimatorListenerAdapter import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator import android.animation.ValueAnimator
import android.content.Context import android.content.Context
import android.content.Context.VIBRATOR_MANAGER_SERVICE
import android.content.Context.VIBRATOR_SERVICE
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.PointF import android.graphics.PointF
import android.os.Build
import android.os.VibrationEffect import android.os.VibrationEffect
import android.os.Vibrator import android.os.Vibrator
import android.os.VibratorManager
import android.util.AttributeSet import android.util.AttributeSet
import android.view.MotionEvent import android.view.MotionEvent
import android.view.View import android.view.View
@ -66,12 +62,9 @@ class OnScreenControllerView @JvmOverloads constructor(context : Context, attrs
field = value field = value
(controls.circularButtons + controls.rectangularButtons + controls.triggerButtons).forEach { it.hapticFeedback = hapticFeedback } (controls.circularButtons + controls.rectangularButtons + controls.triggerButtons).forEach { it.hapticFeedback = hapticFeedback }
} }
private val vibrator: Vibrator =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { // Populated externally by the activity, as retrieving the vibrator service inside the view crashes the layout editor
(context.getSystemService(VIBRATOR_MANAGER_SERVICE) as VibratorManager).defaultVibrator lateinit var vibrator : Vibrator
} else {
@Suppress("DEPRECATION") (context.getSystemService(VIBRATOR_SERVICE) as Vibrator)
}
private val effectClick = VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK) private val effectClick = VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK)
override fun onDraw(canvas : Canvas) { override fun onDraw(canvas : Canvas) {

View File

@ -7,6 +7,8 @@ package emu.skyline.input.onscreen
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.Vibrator
import android.os.VibratorManager
import android.view.* import android.view.*
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
@ -97,8 +99,6 @@ class OnScreenEditActivity : AppCompatActivity() {
}) })
show() show()
} }
} }
private val actions : List<Pair<Int, () -> Unit>> = listOf( private val actions : List<Pair<Int, () -> Unit>> = listOf(
@ -131,6 +131,12 @@ class OnScreenEditActivity : AppCompatActivity() {
} }
} }
binding.onScreenControllerView.vibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
(getSystemService(VIBRATOR_MANAGER_SERVICE) as VibratorManager).defaultVibrator
else
@Suppress("DEPRECATION")
getSystemService(VIBRATOR_SERVICE) as Vibrator
binding.onScreenControllerView.recenterSticks = appSettings.onScreenControlRecenterSticks binding.onScreenControllerView.recenterSticks = appSettings.onScreenControlRecenterSticks
actions.forEach { pair -> actions.forEach { pair ->