Skip to content

Commit 6c2591f

Browse files
committed
Disable sos tile if flash is current used for another service
1 parent e26ec31 commit 6c2591f

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

app/src/main/java/com/wstxda/toolkit/manager/tools/SosFlasher.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class SosFlasher(context: Context) {
3333
private val _isTorchOn = MutableStateFlow(false)
3434
private val _torchAvailable = MutableStateFlow(true)
3535

36+
val isTorchOn = _isTorchOn.asStateFlow()
3637
val isTorchAvailable = _torchAvailable.asStateFlow()
3738

3839
private var sosJob: Job? = null

app/src/main/java/com/wstxda/toolkit/manager/tools/SosManager.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ import kotlinx.coroutines.Dispatchers
66
import kotlinx.coroutines.SupervisorJob
77
import kotlinx.coroutines.cancel
88
import kotlinx.coroutines.flow.MutableStateFlow
9+
import kotlinx.coroutines.flow.SharingStarted
10+
import kotlinx.coroutines.flow.StateFlow
911
import kotlinx.coroutines.flow.asStateFlow
1012
import kotlinx.coroutines.flow.collectLatest
13+
import kotlinx.coroutines.flow.combine
14+
import kotlinx.coroutines.flow.stateIn
1115
import kotlinx.coroutines.launch
1216

1317
class SosManager(context: Context) {
@@ -18,7 +22,19 @@ class SosManager(context: Context) {
1822
private val _isActive = MutableStateFlow(false)
1923
val isActive = _isActive.asStateFlow()
2024

21-
val isFlashAvailable = flasher.isTorchAvailable
25+
val isFlashAvailable: StateFlow<Boolean> = combine(
26+
flasher.isTorchAvailable, flasher.isTorchOn, _isActive
27+
) { available, isOn, active ->
28+
if (active) {
29+
available
30+
} else {
31+
available && !isOn
32+
}
33+
}.stateIn(
34+
scope = managerScope,
35+
started = SharingStarted.WhileSubscribed(5000),
36+
initialValue = flasher.isTorchAvailable.value
37+
)
2238

2339
init {
2440
managerScope.launch {
@@ -35,6 +51,8 @@ class SosManager(context: Context) {
3551
fun toggle() {
3652
if (!hasFlashHardware()) return
3753

54+
if (!_isActive.value && !isFlashAvailable.value) return
55+
3856
if (_isActive.value) {
3957
stopInternal()
4058
} else {

app/src/main/java/com/wstxda/toolkit/tiles/sensors/CompassTileService.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ private val CAN_ONLY_START_FOREGROUND_ON_CLICK =
2424
class CompassTileService : BaseTileService() {
2525

2626
private val compassModule by lazy { CompassModule.getInstance(applicationContext) }
27-
2827
private val compassLabelProvider by lazy { CompassLabelProvider(applicationContext) }
2928
private val compassIconProvider by lazy { CompassIconProvider(applicationContext) }
3029

app/src/main/java/com/wstxda/toolkit/tiles/tools/SosTileService.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class SosTileService : BaseTileService() {
2525
Toast.makeText(this, getString(R.string.not_supported), Toast.LENGTH_SHORT).show()
2626
return
2727
}
28+
29+
if (qsTile?.state == Tile.STATE_UNAVAILABLE) return
30+
2831
sosManager.toggle()
2932
}
3033

0 commit comments

Comments
 (0)