Skip to content

Commit 551b335

Browse files
committed
add some handling for flashlight, sos and stroboscope cancelling each other
1 parent af58c36 commit 551b335

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class MainActivity : SimpleActivity() {
3939
appLaunched(BuildConfig.APPLICATION_ID)
4040

4141
mBus = BusProvider.instance
42-
changeIconColor(config.backgroundColor.getContrastColor(), stroboscope_btn)
42+
changeIconColor(getContrastColor(), stroboscope_btn)
4343

4444
bright_display_btn.setOnClickListener {
4545
startActivity(Intent(applicationContext, BrightDisplayActivity::class.java))
@@ -66,7 +66,7 @@ class MainActivity : SimpleActivity() {
6666
mCameraImpl!!.handleCameraSetup()
6767
checkState(MyCameraImpl.isFlashlightOn)
6868

69-
val contrastColor = config.backgroundColor.getContrastColor()
69+
val contrastColor = getContrastColor()
7070
changeIconColor(contrastColor, bright_display_btn)
7171
bright_display_btn.beVisibleIf(config.brightDisplay)
7272
sos_btn.beVisibleIf(config.sos)
@@ -202,14 +202,18 @@ class MainActivity : SimpleActivity() {
202202

203203
private fun cameraPermissionGranted(isSOS: Boolean) {
204204
if (isSOS) {
205+
val isSOSRunning = mCameraImpl!!.toggleSOS()
206+
sos_btn.setTextColor(if (isSOSRunning) getAdjustedPrimaryColor() else getContrastColor())
205207
} else {
206208
if (mCameraImpl!!.toggleStroboscope()) {
207209
stroboscope_bar.beInvisibleIf(stroboscope_bar.isVisible())
208-
changeIconColor(if (stroboscope_bar.isVisible()) getAdjustedPrimaryColor() else config.backgroundColor.getContrastColor(), stroboscope_btn)
210+
changeIconColor(if (stroboscope_bar.isVisible()) getAdjustedPrimaryColor() else getContrastColor(), stroboscope_btn)
209211
}
210212
}
211213
}
212214

215+
private fun getContrastColor() = config.backgroundColor.getContrastColor()
216+
213217
private fun releaseCamera() {
214218
mCameraImpl?.releaseCamera()
215219
mCameraImpl = null
@@ -220,6 +224,17 @@ class MainActivity : SimpleActivity() {
220224
checkState(event.isEnabled)
221225
}
222226

227+
@Subscribe
228+
fun stopStroboscope(event: Events.StopStroboscope) {
229+
stroboscope_bar.beInvisible()
230+
changeIconColor(getContrastColor(), stroboscope_btn)
231+
}
232+
233+
@Subscribe
234+
fun stopSOS(event: Events.StopSOS) {
235+
sos_btn.setTextColor(getContrastColor())
236+
}
237+
223238
private fun checkState(isEnabled: Boolean) {
224239
if (isEnabled) {
225240
enableFlashlight()
@@ -233,12 +248,12 @@ class MainActivity : SimpleActivity() {
233248
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
234249
mIsFlashlightOn = true
235250

236-
changeIconColor(config.backgroundColor.getContrastColor(), stroboscope_btn)
251+
changeIconColor(getContrastColor(), stroboscope_btn)
237252
stroboscope_bar.beInvisible()
238253
}
239254

240255
private fun disableFlashlight() {
241-
changeIconColor(config.backgroundColor.getContrastColor(), flashlight_btn)
256+
changeIconColor(getContrastColor(), flashlight_btn)
242257
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
243258
mIsFlashlightOn = false
244259
}

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class MyCameraImpl(val context: Context) {
3131
private var shouldStroboscopeStop = false
3232
@Volatile
3333
private var isStroboscopeRunning = false
34+
@Volatile
35+
private var isSOSRunning = false
3436

3537
fun newInstance(context: Context) = MyCameraImpl(context)
3638
}
@@ -78,6 +80,25 @@ class MyCameraImpl(val context: Context) {
7880

7981
fun stopStroboscope() {
8082
shouldStroboscopeStop = true
83+
bus!!.post(Events.StopStroboscope())
84+
}
85+
86+
fun toggleSOS(): Boolean {
87+
if (isStroboscopeRunning) {
88+
stopStroboscope()
89+
}
90+
91+
if (isFlashlightOn) {
92+
disableFlashlight()
93+
}
94+
95+
isSOSRunning = !isSOSRunning
96+
return isSOSRunning
97+
}
98+
99+
fun stopSOS() {
100+
isSOSRunning = false
101+
bus!!.post(Events.StopSOS())
81102
}
82103

83104
fun handleCameraSetup() {
@@ -95,8 +116,9 @@ class MyCameraImpl(val context: Context) {
95116
}
96117

97118
private fun setupCamera() {
98-
if (isMarshmallow)
119+
if (isMarshmallow) {
99120
return
121+
}
100122

101123
if (camera == null) {
102124
initCamera()
@@ -112,7 +134,6 @@ class MyCameraImpl(val context: Context) {
112134
} catch (e: Exception) {
113135
bus!!.post(Events.CameraUnavailable())
114136
}
115-
116137
}
117138

118139
private fun checkFlashlight() {
@@ -128,6 +149,12 @@ class MyCameraImpl(val context: Context) {
128149
}
129150

130151
fun enableFlashlight() {
152+
if (isSOSRunning) {
153+
shouldEnableFlashlight = true
154+
stopSOS()
155+
return
156+
}
157+
131158
shouldStroboscopeStop = true
132159
if (isStroboscopeRunning) {
133160
shouldEnableFlashlight = true

app/src/main/kotlin/com/simplemobiletools/flashlight/models/Events.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ class Events {
44
class StateChanged(val isEnabled: Boolean)
55

66
class CameraUnavailable
7+
8+
class StopStroboscope
9+
10+
class StopSOS
711
}

0 commit comments

Comments
 (0)