Skip to content

Commit 0b64092

Browse files
committed
Handle cases when camerFlash may be null in MyCameraImpl
1 parent 2a8d7c2 commit 0b64092

File tree

1 file changed

+25
-17
lines changed
  • app/src/main/kotlin/com/simplemobiletools/flashlight/helpers

1 file changed

+25
-17
lines changed

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

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
3838
fun newInstance(context: Context, cameraTorchListener: CameraTorchListener? = null) = MyCameraImpl(context, cameraTorchListener)
3939
}
4040

41+
private val cameraFlash: CameraFlash?
42+
get() {
43+
if (MyCameraImpl.cameraFlash == null) {
44+
handleCameraSetup()
45+
}
46+
return MyCameraImpl.cameraFlash
47+
}
48+
4149
init {
4250
handleCameraSetup()
4351
stroboFrequency = context.config.stroboscopeFrequency
@@ -62,7 +70,7 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
6270
disableFlashlight()
6371
}
6472

65-
cameraFlash!!.unregisterListeners()
73+
cameraFlash?.unregisterListeners()
6674

6775
if (!tryInitCamera()) {
6876
return false
@@ -104,7 +112,7 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
104112
disableFlashlight()
105113
}
106114

107-
cameraFlash!!.unregisterListeners()
115+
cameraFlash?.unregisterListeners()
108116

109117
return if (isSOSRunning) {
110118
stopSOS()
@@ -131,8 +139,8 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
131139

132140
fun handleCameraSetup() {
133141
try {
134-
if (cameraFlash == null) {
135-
cameraFlash = CameraFlash(context, cameraTorchListener)
142+
if (MyCameraImpl.cameraFlash == null) {
143+
MyCameraImpl.cameraFlash = CameraFlash(context, cameraTorchListener)
136144
}
137145
} catch (e: Exception) {
138146
EventBus.getDefault().post(Events.CameraUnavailable())
@@ -157,8 +165,8 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
157165
}
158166

159167
try {
160-
cameraFlash!!.initialize()
161-
cameraFlash!!.toggleFlashlight(true)
168+
cameraFlash?.initialize()
169+
cameraFlash?.toggleFlashlight(true)
162170
} catch (e: Exception) {
163171
context.showErrorToast(e)
164172
disableFlashlight()
@@ -174,7 +182,7 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
174182
}
175183

176184
try {
177-
cameraFlash!!.toggleFlashlight(false)
185+
cameraFlash?.toggleFlashlight(false)
178186
} catch (e: Exception) {
179187
context.showErrorToast(e)
180188
disableFlashlight()
@@ -205,7 +213,7 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
205213
}
206214

207215
cameraFlash?.release()
208-
cameraFlash = null
216+
MyCameraImpl.cameraFlash = null
209217
cameraTorchListener = null
210218

211219
isFlashlightOn = false
@@ -228,10 +236,10 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
228236
handleCameraSetup()
229237
while (!shouldStroboscopeStop) {
230238
try {
231-
cameraFlash!!.toggleFlashlight(true)
239+
cameraFlash?.toggleFlashlight(true)
232240
val onDuration = if (isStroboSOS) SOS[sosIndex++ % SOS.size] else stroboFrequency
233241
Thread.sleep(onDuration)
234-
cameraFlash!!.toggleFlashlight(false)
242+
cameraFlash?.toggleFlashlight(false)
235243
val offDuration = if (isStroboSOS) SOS[sosIndex++ % SOS.size] else stroboFrequency
236244
Thread.sleep(offDuration)
237245
} catch (e: Exception) {
@@ -242,9 +250,9 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
242250
// disable flash immediately if stroboscope is stopped and normal flash mode is disabled
243251
if (shouldStroboscopeStop && !shouldEnableFlashlight) {
244252
handleCameraSetup()
245-
cameraFlash!!.toggleFlashlight(false)
246-
cameraFlash!!.release()
247-
cameraFlash = null
253+
cameraFlash?.toggleFlashlight(false)
254+
cameraFlash?.release()
255+
MyCameraImpl.cameraFlash = null
248256
}
249257

250258
shouldStroboscopeStop = false
@@ -271,19 +279,19 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
271279
}
272280

273281
fun getMaximumBrightnessLevel(): Int {
274-
return cameraFlash!!.getMaximumBrightnessLevel()
282+
return cameraFlash?.getMaximumBrightnessLevel() ?: MIN_BRIGHTNESS_LEVEL
275283
}
276284

277285
fun getCurrentBrightnessLevel(): Int {
278-
return cameraFlash!!.getCurrentBrightnessLevel()
286+
return cameraFlash?.getCurrentBrightnessLevel() ?: DEFAULT_BRIGHTNESS_LEVEL
279287
}
280288

281289
fun supportsBrightnessControl(): Boolean {
282-
return cameraFlash!!.supportsBrightnessControl()
290+
return cameraFlash?.supportsBrightnessControl() ?: false
283291
}
284292

285293
fun updateBrightnessLevel(level: Int) {
286-
cameraFlash!!.changeTorchBrightness(level)
294+
cameraFlash?.changeTorchBrightness(level)
287295
}
288296

289297
fun onCameraNotAvailable() {

0 commit comments

Comments
 (0)