From 3558a5c7240f985def985eaa0cef078f34c8dec9 Mon Sep 17 00:00:00 2001 From: alv-cor Date: Tue, 27 Jan 2026 15:37:23 +0100 Subject: [PATCH 1/2] fix: simple timer restart after 15 minutes Android checks every 15 minutes if service is alive with an intent whose action is null. MainActivity now starts FocusModeService with the ACTION_START intent action. FocusModeService handles ACTION_START explicitly in onStartCommand, improving intent handling and service control. --- Reef/src/main/java/dev/pranav/reef/MainActivity.kt | 4 +++- .../pranav/reef/accessibility/FocusModeService.kt | 14 +++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Reef/src/main/java/dev/pranav/reef/MainActivity.kt b/Reef/src/main/java/dev/pranav/reef/MainActivity.kt index fdc8fcd..c10c957 100644 --- a/Reef/src/main/java/dev/pranav/reef/MainActivity.kt +++ b/Reef/src/main/java/dev/pranav/reef/MainActivity.kt @@ -670,7 +670,9 @@ class MainActivity: ComponentActivity() { } } } - startForegroundService(Intent(this, FocusModeService::class.java)) + startForegroundService(Intent(this, FocusModeService::class.java).apply { + action = FocusModeService.ACTION_START + }) } private fun pauseFocusMode() { diff --git a/Reef/src/main/java/dev/pranav/reef/accessibility/FocusModeService.kt b/Reef/src/main/java/dev/pranav/reef/accessibility/FocusModeService.kt index a9904c7..7c1c9c2 100644 --- a/Reef/src/main/java/dev/pranav/reef/accessibility/FocusModeService.kt +++ b/Reef/src/main/java/dev/pranav/reef/accessibility/FocusModeService.kt @@ -11,6 +11,7 @@ import android.content.pm.ServiceInfo import android.os.Build import android.os.CountDownTimer import android.os.IBinder +import android.util.Log import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import androidx.core.app.ServiceCompat @@ -34,6 +35,7 @@ class FocusModeService: Service() { private const val BREAK_ALERT_NOTIFICATION_ID = 2 private const val COMPLETE_NOTIFICATION_ID = 3 const val ACTION_TIMER_UPDATED = "dev.pranav.reef.TIMER_UPDATED" + const val ACTION_START = "dev.pranav.reef.START_TIMER" const val ACTION_PAUSE = "dev.pranav.reef.PAUSE_TIMER" const val ACTION_RESUME = "dev.pranav.reef.RESUME_TIMER" const val ACTION_RESTART = "dev.pranav.reef.RESTART_TIMER" @@ -43,7 +45,6 @@ class FocusModeService: Service() { private val notificationManager by lazy { NotificationManagerCompat.from(this) } private val systemNotificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } - private var countDownTimer: CountDownTimer? = null private var notificationBuilder: NotificationCompat.Builder? = null private var previousInterruptionFilter: Int? = null @@ -79,21 +80,20 @@ class FocusModeService: Service() { } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { - val isFocusActive = prefs.getBoolean("focus_mode", false) - - if (!isFocusActive && intent?.action == null) { - stopSelf() + if (intent?.action == null) { + stopSelfResult(startId) return START_NOT_STICKY } promoteToForeground() - when (intent?.action) { + when (intent.action) { ACTION_PAUSE -> pauseTimer() ACTION_RESUME -> resumeTimer() ACTION_RESTART -> restartCurrentPhase() - else -> startTimer() + ACTION_START -> startTimer() } + return START_STICKY } From d2cb4ec3648a6ea95e626fca97a4a0091495a5e1 Mon Sep 17 00:00:00 2001 From: alv-cor Date: Tue, 27 Jan 2026 18:34:09 +0100 Subject: [PATCH 2/2] Only promote to foreground if not pausing timer Updated onStartCommand to call promoteToForeground only when the intent action is not ACTION_PAUSE, preventing unnecessary foreground promotion when pausing the timer. Removes unnecessary stopSelf that killed the timer on null actions. --- .../java/dev/pranav/reef/accessibility/FocusModeService.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Reef/src/main/java/dev/pranav/reef/accessibility/FocusModeService.kt b/Reef/src/main/java/dev/pranav/reef/accessibility/FocusModeService.kt index 7c1c9c2..1b69ed8 100644 --- a/Reef/src/main/java/dev/pranav/reef/accessibility/FocusModeService.kt +++ b/Reef/src/main/java/dev/pranav/reef/accessibility/FocusModeService.kt @@ -81,11 +81,12 @@ class FocusModeService: Service() { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { if (intent?.action == null) { - stopSelfResult(startId) return START_NOT_STICKY } - promoteToForeground() + if (intent.action != ACTION_PAUSE) { + promoteToForeground() + } when (intent.action) { ACTION_PAUSE -> pauseTimer()