Skip to content

Commit dc4142e

Browse files
committed
catch some extra exceptions at stroboscope
1 parent fbf6e0e commit dc4142e

File tree

1 file changed

+14
-16
lines changed
  • app/src/main/kotlin/com/simplemobiletools/flashlight/helpers

1 file changed

+14
-16
lines changed

app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyCameraImpl.kt

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import com.simplemobiletools.flashlight.extensions.config
1212
import com.simplemobiletools.flashlight.extensions.updateWidgets
1313
import com.simplemobiletools.flashlight.models.Events
1414
import org.greenrobot.eventbus.EventBus
15-
import java.io.IOException
1615

1716
class MyCameraImpl(val context: Context) {
1817
var stroboFrequency = 1000L
@@ -191,7 +190,11 @@ class MyCameraImpl(val context: Context) {
191190

192191
params!!.flashMode = Camera.Parameters.FLASH_MODE_TORCH
193192
camera!!.parameters = params
194-
camera!!.startPreview()
193+
try {
194+
camera!!.startPreview()
195+
} catch (e: Exception) {
196+
disableFlashlight()
197+
}
195198
}
196199

197200
val mainRunnable = Runnable { stateChanged(true) }
@@ -270,32 +273,26 @@ class MyCameraImpl(val context: Context) {
270273
initCamera()
271274
}
272275

273-
val torchOn = camera!!.parameters ?: return@Runnable
274-
val torchOff = camera!!.parameters
275-
torchOn.flashMode = Camera.Parameters.FLASH_MODE_TORCH
276-
torchOff.flashMode = Camera.Parameters.FLASH_MODE_OFF
277-
278-
val dummy = SurfaceTexture(1)
279276
try {
277+
val torchOn = camera!!.parameters ?: return@Runnable
278+
val torchOff = camera!!.parameters
279+
torchOn.flashMode = Camera.Parameters.FLASH_MODE_TORCH
280+
torchOff.flashMode = Camera.Parameters.FLASH_MODE_OFF
281+
282+
val dummy = SurfaceTexture(1)
280283
camera!!.setPreviewTexture(dummy)
281-
} catch (e: IOException) {
282-
}
283284

284-
camera!!.startPreview()
285+
camera!!.startPreview()
285286

286-
while (!shouldStroboscopeStop) {
287-
try {
287+
while (!shouldStroboscopeStop) {
288288
camera!!.parameters = torchOn
289289
val onDuration = if (isStroboSOS) SOS[sosIndex++ % SOS.size] else stroboFrequency
290290
Thread.sleep(onDuration)
291291
camera!!.parameters = torchOff
292292
val offDuration = if (isStroboSOS) SOS[sosIndex++ % SOS.size] else stroboFrequency
293293
Thread.sleep(offDuration)
294-
} catch (e: Exception) {
295294
}
296-
}
297295

298-
try {
299296
if (camera != null) {
300297
camera!!.parameters = torchOff
301298
if (!shouldEnableFlashlight || isMarshmallow) {
@@ -304,6 +301,7 @@ class MyCameraImpl(val context: Context) {
304301
}
305302
}
306303
} catch (e: RuntimeException) {
304+
shouldStroboscopeStop = true
307305
}
308306
}
309307

0 commit comments

Comments
 (0)