SDL: Added SDLControllerManager::shutdownDeviceListener()

From 36c5809b0927534e290dce1cd1e1f7f5056a6cb2 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 4 Jun 2026 09:19:53 -0700
Subject: [PATCH] Added SDLControllerManager::shutdownDeviceListener()

---
 .../org/libsdl/app/SDLControllerManager.java     | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java b/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java
index d9b41a422230c..64efa4343fa8b 100644
--- a/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java
+++ b/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java
@@ -51,6 +51,7 @@ static native void onNativeHat(int device_id, int hat_id,
 
     protected static SDLJoystickHandler mJoystickHandler;
     protected static SDLHapticHandler mHapticHandler;
+    protected static SDLDeviceListener mDeviceListener;
 
     private static final String TAG = "SDLControllerManager";
 
@@ -71,10 +72,21 @@ static void initialize() {
     }
 
     static void initializeDeviceListener() {
-        InputManager im = (InputManager) SDL.getContext().getSystemService(Context.INPUT_SERVICE);
-        im.registerInputDeviceListener(new SDLDeviceListener(), null);
+        if (mDeviceListener == null) {
+            mDeviceListener = new SDLDeviceListener();
+
+            InputManager im = (InputManager) SDL.getContext().getSystemService(Context.INPUT_SERVICE);
+            im.registerInputDeviceListener(mDeviceListener, null);
+        }
     }
 
+    static void shutdownDeviceListener() {
+        if (mDeviceListener != null) {
+            InputManager im = (InputManager) SDL.getContext().getSystemService(Context.INPUT_SERVICE);
+            im.unregisterInputDeviceListener(mDeviceListener);
+            mDeviceListener = null;
+        }
+    }
 
     // Joystick glue code, just a series of stubs that redirect to the SDLJoystickHandler instance
     static public boolean handleJoystickMotionEvent(MotionEvent event) {