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
/**
* The built-in [Vibrator] of the device
*/
lateinit var builtinVibrator : Vibrator
/**
* 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)
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) {
// Android might not allow child views to overlap the system bars
// 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
binding.onScreenControllerView.apply {
vibrator = builtinVibrator
controllerType = inputHandler.getFirstControllerType()
isGone = controllerType == ControllerType.None || !appSettings.onScreenControl
setOnButtonStateChangedListener(::onButtonStateChanged)
@ -558,15 +572,8 @@ class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback, View.OnTo
} else {
inputManager.controllers[index]!!.rumbleDeviceDescriptor?.let {
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
} else {
@Suppress("DEPRECATION")
getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
}
vibrators[index] = vibrator
vibrator
vibrators[index] = builtinVibrator
builtinVibrator
} else {
for (id in InputDevice.getDeviceIds()) {
val device = InputDevice.getDevice(id)

View File

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

View File

@ -7,6 +7,8 @@ package emu.skyline.input.onscreen
import android.os.Build
import android.os.Bundle
import android.os.Vibrator
import android.os.VibratorManager
import android.view.*
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
@ -97,8 +99,6 @@ class OnScreenEditActivity : AppCompatActivity() {
})
show()
}
}
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
actions.forEach { pair ->