SDL: Claim and release USB interfaces on the input thread on Android

From 8e6f5c42efcc3525d9763e3bf8b488ede25eaecc Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 26 Oct 2026 09:40:18 -0700
Subject: [PATCH] Claim and release USB interfaces on the input thread on
 Android

This fixes an ANR where the UI thread is trying to release a USB interface while long blocking I/O is happening
---
 .../java/org/libsdl/app/HIDDeviceUSB.java     | 57 +++++++++++++------
 1 file changed, 41 insertions(+), 16 deletions(-)

diff --git a/android-project/app/src/main/java/org/libsdl/app/HIDDeviceUSB.java b/android-project/app/src/main/java/org/libsdl/app/HIDDeviceUSB.java
index 08875d2bb7cc0..4445e07a4f01f 100644
--- a/android-project/app/src/main/java/org/libsdl/app/HIDDeviceUSB.java
+++ b/android-project/app/src/main/java/org/libsdl/app/HIDDeviceUSB.java
@@ -19,9 +19,10 @@ class HIDDeviceUSB implements HIDDevice {
     protected UsbEndpoint mInputEndpoint;
     protected UsbEndpoint mOutputEndpoint;
     protected InputThread mInputThread;
-    protected boolean mRunning;
-    protected boolean mFrozen;
-    protected boolean mClaimed;
+    protected volatile boolean mRunning;
+    protected volatile boolean mFrozen;
+    protected final Object mFrozenLock = new Object();
+    protected volatile boolean mClaimed;
 
     public HIDDeviceUSB(HIDDeviceManager manager, UsbDevice usbDevice, int interface_index) {
         mManager = manager;
@@ -298,23 +299,42 @@ public void shutdown() {
 
     @Override
     public void setFrozen(boolean frozen) {
-        mFrozen = frozen;
-
-        /* If we have a valid device connection and the claim state doesn't match what we want, try to correct that. */
-        if (mConnection != null && mClaimed == mFrozen) {
-            UsbInterface iface = mDevice.getInterface(mInterfaceIndex);
-            if (frozen) {
-                mClaimed = !mConnection.releaseInterface(iface);
-                if (mClaimed) {
-                    Log.e(TAG, "Tried to release claim on USB device, but failed!");
-                }
+        synchronized (mFrozenLock) {
+            mFrozen = frozen;
+            mFrozenLock.notifyAll();
+        }
+    }
+
+    // Release our claim on the interface while we're frozen so the system can use the device, and
+    // reclaim it when we're unfrozen. This is called on the input thread so the UI thread never blocks
+    // on USB calls.
+    protected void waitWhileFrozen() {
+        UsbInterface iface = mDevice.getInterface(mInterfaceIndex);
+        if (mClaimed) {
+            if (mConnection.releaseInterface(iface)) {
+                mClaimed = false;
             } else {
-                mClaimed = mConnection.claimInterface(iface, true);
-                if (!mClaimed) {
-                    Log.e(TAG, "Tried to regain claim on USB device, but failed!");
+                Log.e(TAG, "Tried to release claim on USB device, but failed!");
+            }
+        }
+
+        synchronized (mFrozenLock) {
+            while (mRunning && mFrozen) {
+                try {
+                    mFrozenLock.wait();
+                } catch (InterruptedException e) {
+                    // close() interrupts us when it's time to stop
                 }
             }
         }
+
+        if (mRunning && !mClaimed) {
+            if (mConnection.claimInterface(iface, true)) {
+                mClaimed = true;
+            } else {
+                Log.e(TAG, "Tried to regain claim on USB device, but failed!");
+            }
+        }
     }
 
     protected class InputThread extends Thread {
@@ -323,6 +343,11 @@ public void run() {
             int packetSize = mInputEndpoint.getMaxPacketSize();
             byte[] packet = new byte[packetSize];
             while (mRunning) {
+                if (mFrozen) {
+                    waitWhileFrozen();
+                    continue;
+                }
+
                 int r;
                 try
                 {