Skip to content

Commit 97b5389

Browse files
committed
Wrap null safe calls to cameraFlash with toasting
1 parent 0b64092 commit 97b5389

File tree

1 file changed

+52
-15
lines changed
  • app/src/main/kotlin/com/simplemobiletools/flashlight/helpers

1 file changed

+52
-15
lines changed

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

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
7070
disableFlashlight()
7171
}
7272

73-
cameraFlash?.unregisterListeners()
73+
cameraFlash.runOrToast {
74+
unregisterListeners()
75+
}
7476

7577
if (!tryInitCamera()) {
7678
return false
@@ -112,7 +114,9 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
112114
disableFlashlight()
113115
}
114116

115-
cameraFlash?.unregisterListeners()
117+
cameraFlash.runOrToast {
118+
unregisterListeners()
119+
}
116120

117121
return if (isSOSRunning) {
118122
stopSOS()
@@ -165,8 +169,10 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
165169
}
166170

167171
try {
168-
cameraFlash?.initialize()
169-
cameraFlash?.toggleFlashlight(true)
172+
cameraFlash.runOrToast {
173+
initialize()
174+
toggleFlashlight(true)
175+
}
170176
} catch (e: Exception) {
171177
context.showErrorToast(e)
172178
disableFlashlight()
@@ -182,7 +188,9 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
182188
}
183189

184190
try {
185-
cameraFlash?.toggleFlashlight(false)
191+
cameraFlash.runOrToast {
192+
toggleFlashlight(false)
193+
}
186194
} catch (e: Exception) {
187195
context.showErrorToast(e)
188196
disableFlashlight()
@@ -206,13 +214,17 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
206214
}
207215

208216
fun releaseCamera() {
209-
cameraFlash?.unregisterListeners()
217+
cameraFlash.runOrToast {
218+
unregisterListeners()
219+
}
210220

211221
if (isFlashlightOn) {
212222
disableFlashlight()
213223
}
214224

215-
cameraFlash?.release()
225+
cameraFlash.runOrToast {
226+
release()
227+
}
216228
MyCameraImpl.cameraFlash = null
217229
cameraTorchListener = null
218230

@@ -236,10 +248,14 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
236248
handleCameraSetup()
237249
while (!shouldStroboscopeStop) {
238250
try {
239-
cameraFlash?.toggleFlashlight(true)
251+
cameraFlash.runOrToast {
252+
toggleFlashlight(true)
253+
}
240254
val onDuration = if (isStroboSOS) SOS[sosIndex++ % SOS.size] else stroboFrequency
241255
Thread.sleep(onDuration)
242-
cameraFlash?.toggleFlashlight(false)
256+
cameraFlash.runOrToast {
257+
toggleFlashlight(false)
258+
}
243259
val offDuration = if (isStroboSOS) SOS[sosIndex++ % SOS.size] else stroboFrequency
244260
Thread.sleep(offDuration)
245261
} catch (e: Exception) {
@@ -250,8 +266,10 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
250266
// disable flash immediately if stroboscope is stopped and normal flash mode is disabled
251267
if (shouldStroboscopeStop && !shouldEnableFlashlight) {
252268
handleCameraSetup()
253-
cameraFlash?.toggleFlashlight(false)
254-
cameraFlash?.release()
269+
cameraFlash.runOrToast {
270+
toggleFlashlight(false)
271+
release()
272+
}
255273
MyCameraImpl.cameraFlash = null
256274
}
257275

@@ -279,22 +297,41 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
279297
}
280298

281299
fun getMaximumBrightnessLevel(): Int {
282-
return cameraFlash?.getMaximumBrightnessLevel() ?: MIN_BRIGHTNESS_LEVEL
300+
return cameraFlash.runOrToastWithDefault(MIN_BRIGHTNESS_LEVEL) {
301+
getMaximumBrightnessLevel()
302+
}
283303
}
284304

285305
fun getCurrentBrightnessLevel(): Int {
286-
return cameraFlash?.getCurrentBrightnessLevel() ?: DEFAULT_BRIGHTNESS_LEVEL
306+
return cameraFlash.runOrToastWithDefault(DEFAULT_BRIGHTNESS_LEVEL) {
307+
getCurrentBrightnessLevel()
308+
}
287309
}
288310

289311
fun supportsBrightnessControl(): Boolean {
290-
return cameraFlash?.supportsBrightnessControl() ?: false
312+
return cameraFlash.runOrToastWithDefault(false) {
313+
supportsBrightnessControl()
314+
}
291315
}
292316

293317
fun updateBrightnessLevel(level: Int) {
294-
cameraFlash?.changeTorchBrightness(level)
318+
cameraFlash.runOrToast {
319+
changeTorchBrightness(level)
320+
}
295321
}
296322

297323
fun onCameraNotAvailable() {
298324
disableFlashlight()
299325
}
326+
327+
private fun <T> CameraFlash?.runOrToastWithDefault(defaultValue: T, block: CameraFlash.() -> T): T {
328+
return try {
329+
this!!.block()
330+
} catch (e: Exception) {
331+
context.showErrorToast(e)
332+
defaultValue
333+
}
334+
}
335+
336+
private fun CameraFlash?.runOrToast(block: CameraFlash.() -> Unit) = runOrToastWithDefault(Unit, block)
300337
}

0 commit comments

Comments
 (0)