Harden CameraManager error handling

- Catch SecurityException separately with permission-specific message
- Replace raw e.message with generic user-friendly error strings
- Wrap cameraProviderFuture.get() in try-catch to handle CameraX
  initialization failures instead of crashing

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Ole-Morten Duesund 2026-03-05 11:56:03 +01:00
commit 18a4289b96

View file

@ -73,9 +73,14 @@ class CameraManager(private val context: Context) {
val cameraProviderFuture = ProcessCameraProvider.getInstance(context)
cameraProviderFuture.addListener({
try {
cameraProvider = cameraProviderFuture.get()
lensController.initialize(cameraProvider?.availableCameraInfos ?: emptyList())
bindCameraUseCases(lifecycleOwner)
} catch (e: Exception) {
Log.e(TAG, "CameraX initialization failed", e)
_error.value = "Camera could not initialize. Please restart the app."
}
}, ContextCompat.getMainExecutor(context))
}
@ -129,9 +134,12 @@ class CameraManager(private val context: Context) {
provideSurface(request)
}
} catch (e: SecurityException) {
Log.e(TAG, "Camera permission denied at runtime", e)
_error.value = "Camera permission was revoked. Please grant it in Settings."
} catch (e: Exception) {
Log.e(TAG, "Camera binding failed", e)
_error.value = "Camera failed: ${e.message}"
_error.value = "Camera could not start. Please try again or restart the app."
}
}