Skip to content

Commit 353fb14

Browse files
committed
adding some stroboscope improvements
1 parent 9ac8d07 commit 353fb14

File tree

1 file changed

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

1 file changed

+35
-17
lines changed

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

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ class MyCameraImpl(val context: Context) {
2525
private var bus: Bus? = null
2626
private var isMarshmallow = false
2727
private var shouldEnableFlashlight = false
28+
private var isStroboSOS = false // are we sending SOS, or casual stroboscope?
2829

2930
private var marshmallowCamera: MarshmallowCamera? = null
3031
@Volatile
3132
private var shouldStroboscopeStop = false
3233
@Volatile
3334
private var isStroboscopeRunning = false
3435
@Volatile
36+
private var shouldSOSStop = false
37+
@Volatile
3538
private var isSOSRunning = false
3639

3740
fun newInstance(context: Context) = MyCameraImpl(context)
@@ -55,6 +58,7 @@ class MyCameraImpl(val context: Context) {
5558
}
5659

5760
fun toggleStroboscope(): Boolean {
61+
isStroboSOS = false
5862
if (!isStroboscopeRunning) {
5963
disableFlashlight()
6064
}
@@ -63,12 +67,13 @@ class MyCameraImpl(val context: Context) {
6367
return false
6468
}
6569

66-
if (isStroboscopeRunning) {
70+
return if (isStroboscopeRunning) {
6771
stopStroboscope()
72+
false
6873
} else {
6974
Thread(stroboscope).start()
75+
true
7076
}
71-
return true
7277
}
7378

7479
fun stopStroboscope() {
@@ -77,6 +82,7 @@ class MyCameraImpl(val context: Context) {
7782
}
7883

7984
fun toggleSOS(): Boolean {
85+
isStroboSOS = true
8086
if (isStroboscopeRunning) {
8187
stopStroboscope()
8288
}
@@ -89,12 +95,17 @@ class MyCameraImpl(val context: Context) {
8995
disableFlashlight()
9096
}
9197

92-
isSOSRunning = !isSOSRunning
98+
if (isSOSRunning) {
99+
stopSOS()
100+
} else {
101+
isSOSRunning = true
102+
Thread(stroboscope).start()
103+
}
93104
return isSOSRunning
94105
}
95106

96107
fun stopSOS() {
97-
isSOSRunning = false
108+
shouldSOSStop = true
98109
bus!!.post(Events.StopSOS())
99110
}
100111

@@ -160,14 +171,9 @@ class MyCameraImpl(val context: Context) {
160171
}
161172

162173
fun enableFlashlight() {
163-
if (isSOSRunning) {
164-
shouldEnableFlashlight = true
165-
stopSOS()
166-
return
167-
}
168-
169174
shouldStroboscopeStop = true
170-
if (isStroboscopeRunning) {
175+
shouldSOSStop = true
176+
if (isStroboscopeRunning || isSOSRunning) {
171177
shouldEnableFlashlight = true
172178
return
173179
}
@@ -189,7 +195,7 @@ class MyCameraImpl(val context: Context) {
189195
}
190196

191197
private fun disableFlashlight() {
192-
if (isStroboscopeRunning) {
198+
if (isStroboscopeRunning || isSOSRunning) {
193199
return
194200
}
195201

@@ -228,15 +234,21 @@ class MyCameraImpl(val context: Context) {
228234
bus?.unregister(this)
229235
isFlashlightOn = false
230236
shouldStroboscopeStop = true
237+
shouldSOSStop = true
231238
}
232239

233240
private val stroboscope = Runnable {
234-
if (isStroboscopeRunning) {
241+
if (isStroboscopeRunning || isSOSRunning) {
235242
return@Runnable
236243
}
237244

238-
shouldStroboscopeStop = false
239-
isStroboscopeRunning = true
245+
if (isStroboSOS) {
246+
shouldSOSStop = false
247+
isSOSRunning = true
248+
} else {
249+
shouldStroboscopeStop = false
250+
isStroboscopeRunning = true
251+
}
240252

241253
if (isNougatPlus()) {
242254
while (!shouldStroboscopeStop) {
@@ -247,6 +259,7 @@ class MyCameraImpl(val context: Context) {
247259
Thread.sleep(stroboFrequency)
248260
} catch (e: Exception) {
249261
shouldStroboscopeStop = true
262+
shouldSOSStop = true
250263
}
251264
}
252265
} else {
@@ -289,8 +302,13 @@ class MyCameraImpl(val context: Context) {
289302
}
290303
}
291304

292-
isStroboscopeRunning = false
293-
shouldStroboscopeStop = false
305+
if (isStroboSOS) {
306+
shouldSOSStop = false
307+
isSOSRunning = false
308+
} else {
309+
shouldStroboscopeStop = false
310+
isStroboscopeRunning = false
311+
}
294312

295313
if (shouldEnableFlashlight) {
296314
enableFlashlight()

0 commit comments

Comments
 (0)