SDL: camera: Fixed Android hotplug.

From 6296677bc9355f178cef152ab4d542ebd81eb0f0 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Mon, 19 Feb 2024 23:52:13 -0500
Subject: [PATCH] camera: Fixed Android hotplug.

---
 src/camera/android/SDL_camera_android.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/camera/android/SDL_camera_android.c b/src/camera/android/SDL_camera_android.c
index 621f7c7dcaf8..f96990eef590 100644
--- a/src/camera/android/SDL_camera_android.c
+++ b/src/camera/android/SDL_camera_android.c
@@ -376,18 +376,18 @@ static void onImageAvailable(void *context, AImageReader *reader)
 
 static void onDisconnected(void *context, ACameraDevice *device)
 {
-    // SDL_CameraDevice *_this = (SDL_CameraDevice *) context;
     #if DEBUG_CAMERA
     SDL_Log("CAMERA: CB onDisconnected");
     #endif
+    SDL_CameraDeviceDisconnected((SDL_CameraDevice *) context);
 }
 
 static void onError(void *context, ACameraDevice *device, int error)
 {
-    // SDL_CameraDevice *_this = (SDL_CameraDevice *) context;
     #if DEBUG_CAMERA
     SDL_Log("CAMERA: CB onError");
     #endif
+    SDL_CameraDeviceDisconnected((SDL_CameraDevice *) context);
 }
 
 static void onClosed(void* context, ACameraCaptureSession *session)
@@ -717,8 +717,15 @@ static void onCameraUnavailable(void *context, const char *cameraId)
     #if DEBUG_CAMERA
     SDL_Log("CAMERA: CB onCameraUnvailable('%s')", cameraId);
     #endif
+
     SDL_assert(cameraId != NULL);
-    SDL_CameraDeviceDisconnected(SDL_FindPhysicalCameraDeviceByCallback(FindAndroidCameraDeviceByID, (void *) cameraId));
+
+    // THIS CALLBACK FIRES WHEN YOU OPEN THE DEVICE YOURSELF.  :(
+    // Make sure we don't have the device opened, in which case onDisconnected will fire instead if actually lost.
+    SDL_CameraDevice *device = SDL_FindPhysicalCameraDeviceByCallback(FindAndroidCameraDeviceByID, (void *) cameraId);
+    if (device && !device->hidden) {
+        SDL_CameraDeviceDisconnected(device);
+    }
 }
 
 static const ACameraManager_AvailabilityCallbacks camera_availability_listener = {