Commit 3d9fca61 authored by Christian Sadilek's avatar Christian Sadilek
Browse files

Closes #3292: Handle IllegalStateException for CameraCaptureSession

parent b556c7ea
......@@ -416,7 +416,7 @@ class QrFragment : Fragment() {
val surface = Surface(texture)
val mImageSurface = imageReader?.surface
try {
handleCaptureException("Failed to create camera preview session") {
cameraDevice?.let {
previewRequestBuilder = it.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW).apply {
addTarget(mImageSurface as Surface)
......@@ -434,14 +434,12 @@ class QrFragment : Fragment() {
previewRequest = previewRequestBuilder?.build()
captureSession = cameraCaptureSession
try {
handleCaptureException("Failed to request capture") {
cameraCaptureSession.setRepeatingRequest(
previewRequest as CaptureRequest,
captureCallback,
backgroundHandler
previewRequest as CaptureRequest,
captureCallback,
backgroundHandler
)
} catch (e: CameraAccessException) {
logger.error("Failed to request capture", e)
}
}
......@@ -452,8 +450,20 @@ class QrFragment : Fragment() {
it.createCaptureSession(Arrays.asList(mImageSurface, surface), stateCallback, null)
}
} catch (e: CameraAccessException) {
logger.error("Failed to create camera preview session", e)
}
}
@Suppress("TooGenericExceptionCaught")
private fun handleCaptureException(msg: String, block: () -> Unit) {
try {
block()
} catch (e: Exception) {
when (e) {
is CameraAccessException, is IllegalStateException -> {
logger.error(msg, e)
}
else -> throw e
}
}
}
......@@ -545,7 +555,6 @@ class QrFragment : Fragment() {
return processImage(bitmaps[0])
}
@Suppress("MagicNumber")
fun processImage(bitmap: BinaryBitmap): Void? {
if (qrState != STATE_DECODE_PROGRESS) {
return null
......
......@@ -178,6 +178,27 @@ class QrFragmentTest {
}
}
@Test
fun `catches and handles IllegalStateException when creating preview session`() {
val qrFragment = spy(QrFragment.newInstance(mock(QrFragment.OnScanCompleteListener::class.java)))
var camera: CameraDevice = mock()
`when`(camera.createCaptureRequest(anyInt())).thenThrow(IllegalStateException("CameraDevice was already closed"))
qrFragment.cameraDevice = camera
val textureView: AutoFitTextureView = mock()
`when`(textureView.surfaceTexture).thenReturn(mock())
qrFragment.textureView = textureView
qrFragment.previewSize = mock()
try {
qrFragment.createCameraPreviewSession()
} catch (e: IllegalStateException) {
fail("IllegalStateException should have been caught and logged, not re-thrown.")
}
}
@Test
fun `catches and handles CameraAccessException when opening camera`() {
val qrFragment = spy(QrFragment.newInstance(mock(QrFragment.OnScanCompleteListener::class.java)))
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment