diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/input/model/ControllerInterface.kt b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/input/model/ControllerInterface.kt index e09e0dec99..9cf9b0e7a7 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/input/model/ControllerInterface.kt +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/input/model/ControllerInterface.kt @@ -6,6 +6,7 @@ import android.content.Context import android.hardware.input.InputManager import android.os.Build import android.os.Handler +import android.os.HandlerThread import android.os.Looper import android.os.VibrationEffect import android.os.Vibrator @@ -17,7 +18,6 @@ import androidx.annotation.Keep import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import org.dolphinemu.dolphinemu.DolphinApplication -import org.dolphinemu.dolphinemu.utils.LooperThread import java.util.concurrent.atomic.AtomicBoolean /** @@ -26,9 +26,9 @@ import java.util.concurrent.atomic.AtomicBoolean */ object ControllerInterface { private var inputDeviceListener: InputDeviceListener? = null - private lateinit var looperThread: LooperThread + private var handlerThread: HandlerThread? = null - private var inputStateUpdatePending = AtomicBoolean(false) + private val inputStateUpdatePending = AtomicBoolean(false) private val inputStateVersion = MutableLiveData(0) private val devicesVersion = MutableLiveData(0) @@ -132,15 +132,18 @@ object ControllerInterface { @Keep @JvmStatic private fun registerInputDeviceListener() { - looperThread = LooperThread("Hotplug thread") - looperThread.start() - if (inputDeviceListener == null) { + handlerThread = HandlerThread("Hotplug thread").apply { start() } + val thread = requireNotNull(handlerThread) { "HandlerThread is not available" } + val im = DolphinApplication.getAppContext() - .getSystemService(Context.INPUT_SERVICE) as InputManager? + .getSystemService(Context.INPUT_SERVICE) as InputManager + val looper = requireNotNull(thread.looper) { + "HandlerThread looper is not available" + } inputDeviceListener = InputDeviceListener() - im!!.registerInputDeviceListener(inputDeviceListener, Handler(looperThread.looper)) + im.registerInputDeviceListener(inputDeviceListener, Handler(looper)) } } @@ -149,10 +152,12 @@ object ControllerInterface { private fun unregisterInputDeviceListener() { if (inputDeviceListener != null) { val im = DolphinApplication.getAppContext() - .getSystemService(Context.INPUT_SERVICE) as InputManager? + .getSystemService(Context.INPUT_SERVICE) as InputManager - im!!.unregisterInputDeviceListener(inputDeviceListener) + im.unregisterInputDeviceListener(inputDeviceListener) inputDeviceListener = null + handlerThread?.quitSafely() + handlerThread = null } } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/LooperThread.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/LooperThread.java deleted file mode 100644 index 8a67ec7e89..0000000000 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/LooperThread.java +++ /dev/null @@ -1,58 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-or-later - -package org.dolphinemu.dolphinemu.utils; - -import android.os.Looper; - -public class LooperThread extends Thread -{ - private Looper mLooper; - - public LooperThread() - { - super(); - } - - public LooperThread(String name) - { - super(name); - } - - @Override - public void run() - { - Looper.prepare(); - - synchronized (this) - { - mLooper = Looper.myLooper(); - notifyAll(); - } - - Looper.loop(); - } - - public Looper getLooper() - { - if (!isAlive()) - { - throw new IllegalStateException(); - } - - synchronized (this) - { - while (mLooper == null) - { - try - { - wait(); - } - catch (InterruptedException ignored) - { - } - } - } - - return mLooper; - } -}