diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/input/ui/MotionAlertDialog.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/input/ui/MotionAlertDialog.java deleted file mode 100644 index 9872bdd01e..0000000000 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/input/ui/MotionAlertDialog.java +++ /dev/null @@ -1,99 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-or-later - -package org.dolphinemu.dolphinemu.features.input.ui; - -import android.app.Activity; -import android.view.InputDevice; -import android.view.KeyEvent; -import android.view.MotionEvent; - -import androidx.annotation.NonNull; -import androidx.appcompat.app.AlertDialog; - -import org.dolphinemu.dolphinemu.features.input.model.ControllerInterface; -import org.dolphinemu.dolphinemu.features.input.model.MappingCommon; -import org.dolphinemu.dolphinemu.features.input.model.view.InputMappingControlSetting; - -/** - * {@link AlertDialog} derivative that listens for - * motion events from controllers and joysticks. - */ -public final class MotionAlertDialog extends AlertDialog -{ - private final Activity mActivity; - private final InputMappingControlSetting mSetting; - private final boolean mAllDevices; - private boolean mRunning = false; - - /** - * Constructor - * - * @param activity The current {@link Activity}. - * @param setting The setting to show this dialog for. - * @param allDevices Whether to detect inputs from devices other than the configured one. - */ - public MotionAlertDialog(Activity activity, InputMappingControlSetting setting, - boolean allDevices) - { - super(activity); - - mActivity = activity; - mSetting = setting; - mAllDevices = allDevices; - } - - @Override - protected void onStart() - { - super.onStart(); - - mRunning = true; - new Thread(() -> - { - String result = MappingCommon.detectInput(mSetting.getController(), mAllDevices); - mActivity.runOnUiThread(() -> - { - if (mRunning) - { - mSetting.setValue(result); - dismiss(); - } - }); - }).start(); - } - - @Override - protected void onStop() - { - super.onStop(); - mRunning = false; - } - - @Override - public boolean dispatchKeyEvent(KeyEvent event) - { - ControllerInterface.dispatchKeyEvent(event); - - if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.isLongPress()) - { - // Special case: Let the user cancel by long-pressing Back (intended for non-touch devices) - mSetting.clearValue(); - dismiss(); - } - - return true; - } - - @Override - public boolean dispatchGenericMotionEvent(@NonNull MotionEvent event) - { - if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) - { - // Special case: Let the user cancel by touching an on-screen button - return super.dispatchGenericMotionEvent(event); - } - - ControllerInterface.dispatchGenericMotionEvent(event); - return true; - } -} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/input/ui/MotionAlertDialog.kt b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/input/ui/MotionAlertDialog.kt new file mode 100644 index 0000000000..ff4a278f4c --- /dev/null +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/input/ui/MotionAlertDialog.kt @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +package org.dolphinemu.dolphinemu.features.input.ui + +import android.app.Activity +import android.view.InputDevice +import android.view.KeyEvent +import android.view.MotionEvent +import androidx.appcompat.app.AlertDialog +import org.dolphinemu.dolphinemu.features.input.model.ControllerInterface +import org.dolphinemu.dolphinemu.features.input.model.MappingCommon +import org.dolphinemu.dolphinemu.features.input.model.view.InputMappingControlSetting + +/** + * [AlertDialog] derivative that listens for + * motion events from controllers and joysticks. + * + * @param activity The current [Activity]. + * @param setting The setting to show this dialog for. + * @param allDevices Whether to detect inputs from devices other than the configured one. + */ +class MotionAlertDialog( + private val activity: Activity, + private val setting: InputMappingControlSetting, + private val allDevices: Boolean +) : AlertDialog(activity) { + private var running = false + + override fun onStart() { + super.onStart() + + running = true + Thread { + val result = MappingCommon.detectInput(setting.controller, allDevices) + activity.runOnUiThread { + if (running) { + setting.value = result + dismiss() + } + } + }.start() + } + + override fun onStop() { + super.onStop() + running = false + } + + override fun dispatchKeyEvent(event: KeyEvent): Boolean { + ControllerInterface.dispatchKeyEvent(event) + if (event.keyCode == KeyEvent.KEYCODE_BACK && event.isLongPress) { + // Special case: Let the user cancel by long-pressing Back (intended for non-touch devices) + setting.clearValue() + dismiss() + } + return true + } + + override fun dispatchGenericMotionEvent(event: MotionEvent): Boolean { + if (event.source and InputDevice.SOURCE_CLASS_POINTER != 0) { + // Special case: Let the user cancel by touching an on-screen button + return super.dispatchGenericMotionEvent(event) + } + + ControllerInterface.dispatchGenericMotionEvent(event) + return true + } +}