From dc4142e54b340e9e67077be69b4338421ab4090d Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 29 Dec 2020 17:13:38 +0100 Subject: [PATCH] catch some extra exceptions at stroboscope --- .../flashlight/helpers/MyCameraImpl.kt | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyCameraImpl.kt b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyCameraImpl.kt index 027ef95..d58313c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyCameraImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyCameraImpl.kt @@ -12,7 +12,6 @@ import com.simplemobiletools.flashlight.extensions.config import com.simplemobiletools.flashlight.extensions.updateWidgets import com.simplemobiletools.flashlight.models.Events import org.greenrobot.eventbus.EventBus -import java.io.IOException class MyCameraImpl(val context: Context) { var stroboFrequency = 1000L @@ -191,7 +190,11 @@ class MyCameraImpl(val context: Context) { params!!.flashMode = Camera.Parameters.FLASH_MODE_TORCH camera!!.parameters = params - camera!!.startPreview() + try { + camera!!.startPreview() + } catch (e: Exception) { + disableFlashlight() + } } val mainRunnable = Runnable { stateChanged(true) } @@ -270,32 +273,26 @@ class MyCameraImpl(val context: Context) { initCamera() } - val torchOn = camera!!.parameters ?: return@Runnable - val torchOff = camera!!.parameters - torchOn.flashMode = Camera.Parameters.FLASH_MODE_TORCH - torchOff.flashMode = Camera.Parameters.FLASH_MODE_OFF - - val dummy = SurfaceTexture(1) try { + val torchOn = camera!!.parameters ?: return@Runnable + val torchOff = camera!!.parameters + torchOn.flashMode = Camera.Parameters.FLASH_MODE_TORCH + torchOff.flashMode = Camera.Parameters.FLASH_MODE_OFF + + val dummy = SurfaceTexture(1) camera!!.setPreviewTexture(dummy) - } catch (e: IOException) { - } - camera!!.startPreview() + camera!!.startPreview() - while (!shouldStroboscopeStop) { - try { + while (!shouldStroboscopeStop) { camera!!.parameters = torchOn val onDuration = if (isStroboSOS) SOS[sosIndex++ % SOS.size] else stroboFrequency Thread.sleep(onDuration) camera!!.parameters = torchOff val offDuration = if (isStroboSOS) SOS[sosIndex++ % SOS.size] else stroboFrequency Thread.sleep(offDuration) - } catch (e: Exception) { } - } - try { if (camera != null) { camera!!.parameters = torchOff if (!shouldEnableFlashlight || isMarshmallow) { @@ -304,6 +301,7 @@ class MyCameraImpl(val context: Context) { } } } catch (e: RuntimeException) { + shouldStroboscopeStop = true } }