catch some extra exceptions at stroboscope

This commit is contained in:
tibbi 2020-12-29 17:13:38 +01:00
parent fbf6e0e111
commit dc4142e54b
1 changed files with 14 additions and 16 deletions

View File

@ -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
try {
camera!!.startPreview()
} catch (e: Exception) {
disableFlashlight()
}
}
val mainRunnable = Runnable { stateChanged(true) }
@ -270,32 +273,26 @@ class MyCameraImpl(val context: Context) {
initCamera()
}
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)
try {
camera!!.setPreviewTexture(dummy)
} catch (e: IOException) {
}
camera!!.startPreview()
while (!shouldStroboscopeStop) {
try {
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
}
}