diff --git a/app/src/main/java/emu/skyline/EmulationActivity.kt b/app/src/main/java/emu/skyline/EmulationActivity.kt index 481fd541..c2d3626e 100644 --- a/app/src/main/java/emu/skyline/EmulationActivity.kt +++ b/app/src/main/java/emu/skyline/EmulationActivity.kt @@ -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) diff --git a/app/src/main/java/emu/skyline/input/onscreen/OnScreenControllerView.kt b/app/src/main/java/emu/skyline/input/onscreen/OnScreenControllerView.kt index cce9e7f3..c7d2b617 100644 --- a/app/src/main/java/emu/skyline/input/onscreen/OnScreenControllerView.kt +++ b/app/src/main/java/emu/skyline/input/onscreen/OnScreenControllerView.kt @@ -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) { diff --git a/app/src/main/java/emu/skyline/input/onscreen/OnScreenEditActivity.kt b/app/src/main/java/emu/skyline/input/onscreen/OnScreenEditActivity.kt index 53585289..d1dfecfe 100644 --- a/app/src/main/java/emu/skyline/input/onscreen/OnScreenEditActivity.kt +++ b/app/src/main/java/emu/skyline/input/onscreen/OnScreenEditActivity.kt @@ -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 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 ->