From a93e748e04fa2c6a9dc4f4cf6d93c3fb31b8e31a Mon Sep 17 00:00:00 2001 From: Nishant Mishra Date: Thu, 23 Oct 2025 09:33:00 +0530 Subject: [PATCH] feat(alarm): automatically stop alarm after 1 min Closes: #88 --- .../main/java/org/nsh07/pomodoro/MainActivity.kt | 2 ++ .../org/nsh07/pomodoro/service/TimerService.kt | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/nsh07/pomodoro/MainActivity.kt b/app/src/main/java/org/nsh07/pomodoro/MainActivity.kt index 6310ff5..3b1fc72 100644 --- a/app/src/main/java/org/nsh07/pomodoro/MainActivity.kt +++ b/app/src/main/java/org/nsh07/pomodoro/MainActivity.kt @@ -45,10 +45,12 @@ class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() + appContainer.activityTurnScreenOn = { setShowWhenLocked(it) setTurnScreenOn(it) } + setContent { val preferencesState by settingsViewModel.preferencesState.collectAsStateWithLifecycle() diff --git a/app/src/main/java/org/nsh07/pomodoro/service/TimerService.kt b/app/src/main/java/org/nsh07/pomodoro/service/TimerService.kt index e9158c5..b2423d1 100644 --- a/app/src/main/java/org/nsh07/pomodoro/service/TimerService.kt +++ b/app/src/main/java/org/nsh07/pomodoro/service/TimerService.kt @@ -33,6 +33,7 @@ import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.delay import kotlinx.coroutines.flow.asStateFlow @@ -71,9 +72,11 @@ class TimerService : Service() { private var pauseDuration = 0L 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 var autoAlarmStopScope: Job? = null + private var alarm: MediaPlayer? = null private val vibrator by lazy { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { @@ -154,7 +157,7 @@ class TimerService : Service() { var iterations = -1 - scope.launch { + timerScope.launch { while (true) { if (!timerState.value.timerRunning) break if (startTime == 0L) startTime = SystemClock.elapsedRealtime() @@ -372,6 +375,11 @@ class TimerService : Service() { appContainer.activityTurnScreenOn(true) + autoAlarmStopScope = CoroutineScope(Dispatchers.IO).launch { + delay(1 * 60 * 1000) + stopAlarm() + } + if (timerRepository.vibrateEnabled) { if (!vibrator.hasVibrator()) { return @@ -384,6 +392,8 @@ class TimerService : Service() { } fun stopAlarm() { + autoAlarmStopScope?.cancel() + if (timerRepository.alarmEnabled) { alarm?.pause() alarm?.seekTo(0)