Handle cases when camerFlash may be null in MyCameraImpl

This commit is contained in:
Ensar Sarajčić
2023-09-20 11:26:03 +02:00
parent 2a8d7c2a8c
commit 0b64092651

View File

@@ -38,6 +38,14 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
fun newInstance(context: Context, cameraTorchListener: CameraTorchListener? = null) = MyCameraImpl(context, cameraTorchListener) fun newInstance(context: Context, cameraTorchListener: CameraTorchListener? = null) = MyCameraImpl(context, cameraTorchListener)
} }
private val cameraFlash: CameraFlash?
get() {
if (MyCameraImpl.cameraFlash == null) {
handleCameraSetup()
}
return MyCameraImpl.cameraFlash
}
init { init {
handleCameraSetup() handleCameraSetup()
stroboFrequency = context.config.stroboscopeFrequency stroboFrequency = context.config.stroboscopeFrequency
@@ -62,7 +70,7 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
disableFlashlight() disableFlashlight()
} }
cameraFlash!!.unregisterListeners() cameraFlash?.unregisterListeners()
if (!tryInitCamera()) { if (!tryInitCamera()) {
return false return false
@@ -104,7 +112,7 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
disableFlashlight() disableFlashlight()
} }
cameraFlash!!.unregisterListeners() cameraFlash?.unregisterListeners()
return if (isSOSRunning) { return if (isSOSRunning) {
stopSOS() stopSOS()
@@ -131,8 +139,8 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
fun handleCameraSetup() { fun handleCameraSetup() {
try { try {
if (cameraFlash == null) { if (MyCameraImpl.cameraFlash == null) {
cameraFlash = CameraFlash(context, cameraTorchListener) MyCameraImpl.cameraFlash = CameraFlash(context, cameraTorchListener)
} }
} catch (e: Exception) { } catch (e: Exception) {
EventBus.getDefault().post(Events.CameraUnavailable()) EventBus.getDefault().post(Events.CameraUnavailable())
@@ -157,8 +165,8 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
} }
try { try {
cameraFlash!!.initialize() cameraFlash?.initialize()
cameraFlash!!.toggleFlashlight(true) cameraFlash?.toggleFlashlight(true)
} catch (e: Exception) { } catch (e: Exception) {
context.showErrorToast(e) context.showErrorToast(e)
disableFlashlight() disableFlashlight()
@@ -174,7 +182,7 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
} }
try { try {
cameraFlash!!.toggleFlashlight(false) cameraFlash?.toggleFlashlight(false)
} catch (e: Exception) { } catch (e: Exception) {
context.showErrorToast(e) context.showErrorToast(e)
disableFlashlight() disableFlashlight()
@@ -205,7 +213,7 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
} }
cameraFlash?.release() cameraFlash?.release()
cameraFlash = null MyCameraImpl.cameraFlash = null
cameraTorchListener = null cameraTorchListener = null
isFlashlightOn = false isFlashlightOn = false
@@ -228,10 +236,10 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
handleCameraSetup() handleCameraSetup()
while (!shouldStroboscopeStop) { while (!shouldStroboscopeStop) {
try { try {
cameraFlash!!.toggleFlashlight(true) cameraFlash?.toggleFlashlight(true)
val onDuration = if (isStroboSOS) SOS[sosIndex++ % SOS.size] else stroboFrequency val onDuration = if (isStroboSOS) SOS[sosIndex++ % SOS.size] else stroboFrequency
Thread.sleep(onDuration) Thread.sleep(onDuration)
cameraFlash!!.toggleFlashlight(false) cameraFlash?.toggleFlashlight(false)
val offDuration = if (isStroboSOS) SOS[sosIndex++ % SOS.size] else stroboFrequency val offDuration = if (isStroboSOS) SOS[sosIndex++ % SOS.size] else stroboFrequency
Thread.sleep(offDuration) Thread.sleep(offDuration)
} catch (e: Exception) { } catch (e: Exception) {
@@ -242,9 +250,9 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
// disable flash immediately if stroboscope is stopped and normal flash mode is disabled // disable flash immediately if stroboscope is stopped and normal flash mode is disabled
if (shouldStroboscopeStop && !shouldEnableFlashlight) { if (shouldStroboscopeStop && !shouldEnableFlashlight) {
handleCameraSetup() handleCameraSetup()
cameraFlash!!.toggleFlashlight(false) cameraFlash?.toggleFlashlight(false)
cameraFlash!!.release() cameraFlash?.release()
cameraFlash = null MyCameraImpl.cameraFlash = null
} }
shouldStroboscopeStop = false shouldStroboscopeStop = false
@@ -271,19 +279,19 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
} }
fun getMaximumBrightnessLevel(): Int { fun getMaximumBrightnessLevel(): Int {
return cameraFlash!!.getMaximumBrightnessLevel() return cameraFlash?.getMaximumBrightnessLevel() ?: MIN_BRIGHTNESS_LEVEL
} }
fun getCurrentBrightnessLevel(): Int { fun getCurrentBrightnessLevel(): Int {
return cameraFlash!!.getCurrentBrightnessLevel() return cameraFlash?.getCurrentBrightnessLevel() ?: DEFAULT_BRIGHTNESS_LEVEL
} }
fun supportsBrightnessControl(): Boolean { fun supportsBrightnessControl(): Boolean {
return cameraFlash!!.supportsBrightnessControl() return cameraFlash?.supportsBrightnessControl() ?: false
} }
fun updateBrightnessLevel(level: Int) { fun updateBrightnessLevel(level: Int) {
cameraFlash!!.changeTorchBrightness(level) cameraFlash?.changeTorchBrightness(level)
} }
fun onCameraNotAvailable() { fun onCameraNotAvailable() {