mirror of
https://github.com/SimpleMobileTools/Simple-Flashlight.git
synced 2025-02-14 02:40:51 +01:00
Wrap null safe calls to cameraFlash
with toasting
This commit is contained in:
parent
0b64092651
commit
97b5389bc6
@ -70,7 +70,9 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
|
||||
disableFlashlight()
|
||||
}
|
||||
|
||||
cameraFlash?.unregisterListeners()
|
||||
cameraFlash.runOrToast {
|
||||
unregisterListeners()
|
||||
}
|
||||
|
||||
if (!tryInitCamera()) {
|
||||
return false
|
||||
@ -112,7 +114,9 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
|
||||
disableFlashlight()
|
||||
}
|
||||
|
||||
cameraFlash?.unregisterListeners()
|
||||
cameraFlash.runOrToast {
|
||||
unregisterListeners()
|
||||
}
|
||||
|
||||
return if (isSOSRunning) {
|
||||
stopSOS()
|
||||
@ -165,8 +169,10 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
|
||||
}
|
||||
|
||||
try {
|
||||
cameraFlash?.initialize()
|
||||
cameraFlash?.toggleFlashlight(true)
|
||||
cameraFlash.runOrToast {
|
||||
initialize()
|
||||
toggleFlashlight(true)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
context.showErrorToast(e)
|
||||
disableFlashlight()
|
||||
@ -182,7 +188,9 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
|
||||
}
|
||||
|
||||
try {
|
||||
cameraFlash?.toggleFlashlight(false)
|
||||
cameraFlash.runOrToast {
|
||||
toggleFlashlight(false)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
context.showErrorToast(e)
|
||||
disableFlashlight()
|
||||
@ -206,13 +214,17 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
|
||||
}
|
||||
|
||||
fun releaseCamera() {
|
||||
cameraFlash?.unregisterListeners()
|
||||
cameraFlash.runOrToast {
|
||||
unregisterListeners()
|
||||
}
|
||||
|
||||
if (isFlashlightOn) {
|
||||
disableFlashlight()
|
||||
}
|
||||
|
||||
cameraFlash?.release()
|
||||
cameraFlash.runOrToast {
|
||||
release()
|
||||
}
|
||||
MyCameraImpl.cameraFlash = null
|
||||
cameraTorchListener = null
|
||||
|
||||
@ -236,10 +248,14 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
|
||||
handleCameraSetup()
|
||||
while (!shouldStroboscopeStop) {
|
||||
try {
|
||||
cameraFlash?.toggleFlashlight(true)
|
||||
cameraFlash.runOrToast {
|
||||
toggleFlashlight(true)
|
||||
}
|
||||
val onDuration = if (isStroboSOS) SOS[sosIndex++ % SOS.size] else stroboFrequency
|
||||
Thread.sleep(onDuration)
|
||||
cameraFlash?.toggleFlashlight(false)
|
||||
cameraFlash.runOrToast {
|
||||
toggleFlashlight(false)
|
||||
}
|
||||
val offDuration = if (isStroboSOS) SOS[sosIndex++ % SOS.size] else stroboFrequency
|
||||
Thread.sleep(offDuration)
|
||||
} catch (e: Exception) {
|
||||
@ -250,8 +266,10 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
|
||||
// disable flash immediately if stroboscope is stopped and normal flash mode is disabled
|
||||
if (shouldStroboscopeStop && !shouldEnableFlashlight) {
|
||||
handleCameraSetup()
|
||||
cameraFlash?.toggleFlashlight(false)
|
||||
cameraFlash?.release()
|
||||
cameraFlash.runOrToast {
|
||||
toggleFlashlight(false)
|
||||
release()
|
||||
}
|
||||
MyCameraImpl.cameraFlash = null
|
||||
}
|
||||
|
||||
@ -279,22 +297,41 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
|
||||
}
|
||||
|
||||
fun getMaximumBrightnessLevel(): Int {
|
||||
return cameraFlash?.getMaximumBrightnessLevel() ?: MIN_BRIGHTNESS_LEVEL
|
||||
return cameraFlash.runOrToastWithDefault(MIN_BRIGHTNESS_LEVEL) {
|
||||
getMaximumBrightnessLevel()
|
||||
}
|
||||
}
|
||||
|
||||
fun getCurrentBrightnessLevel(): Int {
|
||||
return cameraFlash?.getCurrentBrightnessLevel() ?: DEFAULT_BRIGHTNESS_LEVEL
|
||||
return cameraFlash.runOrToastWithDefault(DEFAULT_BRIGHTNESS_LEVEL) {
|
||||
getCurrentBrightnessLevel()
|
||||
}
|
||||
}
|
||||
|
||||
fun supportsBrightnessControl(): Boolean {
|
||||
return cameraFlash?.supportsBrightnessControl() ?: false
|
||||
return cameraFlash.runOrToastWithDefault(false) {
|
||||
supportsBrightnessControl()
|
||||
}
|
||||
}
|
||||
|
||||
fun updateBrightnessLevel(level: Int) {
|
||||
cameraFlash?.changeTorchBrightness(level)
|
||||
cameraFlash.runOrToast {
|
||||
changeTorchBrightness(level)
|
||||
}
|
||||
}
|
||||
|
||||
fun onCameraNotAvailable() {
|
||||
disableFlashlight()
|
||||
}
|
||||
|
||||
private fun <T> CameraFlash?.runOrToastWithDefault(defaultValue: T, block: CameraFlash.() -> T): T {
|
||||
return try {
|
||||
this!!.block()
|
||||
} catch (e: Exception) {
|
||||
context.showErrorToast(e)
|
||||
defaultValue
|
||||
}
|
||||
}
|
||||
|
||||
private fun CameraFlash?.runOrToast(block: CameraFlash.() -> Unit) = runOrToastWithDefault(Unit, block)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user