feat(alarm): automatically stop alarm after 1 min

Closes: #88
This commit is contained in:
Nishant Mishra
2025-10-23 09:33:00 +05:30
parent aa4889a1fb
commit a93e748e04
2 changed files with 14 additions and 2 deletions

View File

@@ -45,10 +45,12 @@ class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
enableEdgeToEdge() enableEdgeToEdge()
appContainer.activityTurnScreenOn = { appContainer.activityTurnScreenOn = {
setShowWhenLocked(it) setShowWhenLocked(it)
setTurnScreenOn(it) setTurnScreenOn(it)
} }
setContent { setContent {
val preferencesState by settingsViewModel.preferencesState.collectAsStateWithLifecycle() val preferencesState by settingsViewModel.preferencesState.collectAsStateWithLifecycle()

View File

@@ -33,6 +33,7 @@ import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat import androidx.core.app.NotificationManagerCompat
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
@@ -71,9 +72,11 @@ class TimerService : Service() {
private var pauseDuration = 0L private var pauseDuration = 0L
private var job = SupervisorJob() private var job = SupervisorJob()
private val scope = CoroutineScope(Dispatchers.IO + job) private val timerScope = CoroutineScope(Dispatchers.IO + job)
private val skipScope = CoroutineScope(Dispatchers.IO + job) private val skipScope = CoroutineScope(Dispatchers.IO + job)
private var autoAlarmStopScope: Job? = null
private var alarm: MediaPlayer? = null private var alarm: MediaPlayer? = null
private val vibrator by lazy { private val vibrator by lazy {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
@@ -154,7 +157,7 @@ class TimerService : Service() {
var iterations = -1 var iterations = -1
scope.launch { timerScope.launch {
while (true) { while (true) {
if (!timerState.value.timerRunning) break if (!timerState.value.timerRunning) break
if (startTime == 0L) startTime = SystemClock.elapsedRealtime() if (startTime == 0L) startTime = SystemClock.elapsedRealtime()
@@ -372,6 +375,11 @@ class TimerService : Service() {
appContainer.activityTurnScreenOn(true) appContainer.activityTurnScreenOn(true)
autoAlarmStopScope = CoroutineScope(Dispatchers.IO).launch {
delay(1 * 60 * 1000)
stopAlarm()
}
if (timerRepository.vibrateEnabled) { if (timerRepository.vibrateEnabled) {
if (!vibrator.hasVibrator()) { if (!vibrator.hasVibrator()) {
return return
@@ -384,6 +392,8 @@ class TimerService : Service() {
} }
fun stopAlarm() { fun stopAlarm() {
autoAlarmStopScope?.cancel()
if (timerRepository.alarmEnabled) { if (timerRepository.alarmEnabled) {
alarm?.pause() alarm?.pause()
alarm?.seekTo(0) alarm?.seekTo(0)