Android: Use Android's HandlerThread in ControllerInterface instead of our own implementation
This commit is contained in:
+15
-10
@@ -6,6 +6,7 @@ import android.content.Context
|
|||||||
import android.hardware.input.InputManager
|
import android.hardware.input.InputManager
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
|
import android.os.HandlerThread
|
||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
import android.os.VibrationEffect
|
import android.os.VibrationEffect
|
||||||
import android.os.Vibrator
|
import android.os.Vibrator
|
||||||
@@ -17,7 +18,6 @@ import androidx.annotation.Keep
|
|||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import org.dolphinemu.dolphinemu.DolphinApplication
|
import org.dolphinemu.dolphinemu.DolphinApplication
|
||||||
import org.dolphinemu.dolphinemu.utils.LooperThread
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean
|
import java.util.concurrent.atomic.AtomicBoolean
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -26,9 +26,9 @@ import java.util.concurrent.atomic.AtomicBoolean
|
|||||||
*/
|
*/
|
||||||
object ControllerInterface {
|
object ControllerInterface {
|
||||||
private var inputDeviceListener: InputDeviceListener? = null
|
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 inputStateVersion = MutableLiveData(0)
|
||||||
private val devicesVersion = MutableLiveData(0)
|
private val devicesVersion = MutableLiveData(0)
|
||||||
|
|
||||||
@@ -132,15 +132,18 @@ object ControllerInterface {
|
|||||||
@Keep
|
@Keep
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
private fun registerInputDeviceListener() {
|
private fun registerInputDeviceListener() {
|
||||||
looperThread = LooperThread("Hotplug thread")
|
|
||||||
looperThread.start()
|
|
||||||
|
|
||||||
if (inputDeviceListener == null) {
|
if (inputDeviceListener == null) {
|
||||||
|
handlerThread = HandlerThread("Hotplug thread").apply { start() }
|
||||||
|
val thread = requireNotNull(handlerThread) { "HandlerThread is not available" }
|
||||||
|
|
||||||
val im = DolphinApplication.getAppContext()
|
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()
|
inputDeviceListener = InputDeviceListener()
|
||||||
im!!.registerInputDeviceListener(inputDeviceListener, Handler(looperThread.looper))
|
im.registerInputDeviceListener(inputDeviceListener, Handler(looper))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,10 +152,12 @@ object ControllerInterface {
|
|||||||
private fun unregisterInputDeviceListener() {
|
private fun unregisterInputDeviceListener() {
|
||||||
if (inputDeviceListener != null) {
|
if (inputDeviceListener != null) {
|
||||||
val im = DolphinApplication.getAppContext()
|
val im = DolphinApplication.getAppContext()
|
||||||
.getSystemService(Context.INPUT_SERVICE) as InputManager?
|
.getSystemService(Context.INPUT_SERVICE) as InputManager
|
||||||
|
|
||||||
im!!.unregisterInputDeviceListener(inputDeviceListener)
|
im.unregisterInputDeviceListener(inputDeviceListener)
|
||||||
inputDeviceListener = null
|
inputDeviceListener = null
|
||||||
|
handlerThread?.quitSafely()
|
||||||
|
handlerThread = 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user