Skip to content

Commit 3558a5c

Browse files
committed
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.
1 parent a3fe77c commit 3558a5c

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

Reef/src/main/java/dev/pranav/reef/MainActivity.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,9 @@ class MainActivity: ComponentActivity() {
670670
}
671671
}
672672
}
673-
startForegroundService(Intent(this, FocusModeService::class.java))
673+
startForegroundService(Intent(this, FocusModeService::class.java).apply {
674+
action = FocusModeService.ACTION_START
675+
})
674676
}
675677

676678
private fun pauseFocusMode() {

Reef/src/main/java/dev/pranav/reef/accessibility/FocusModeService.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import android.content.pm.ServiceInfo
1111
import android.os.Build
1212
import android.os.CountDownTimer
1313
import android.os.IBinder
14+
import android.util.Log
1415
import androidx.core.app.NotificationCompat
1516
import androidx.core.app.NotificationManagerCompat
1617
import androidx.core.app.ServiceCompat
@@ -34,6 +35,7 @@ class FocusModeService: Service() {
3435
private const val BREAK_ALERT_NOTIFICATION_ID = 2
3536
private const val COMPLETE_NOTIFICATION_ID = 3
3637
const val ACTION_TIMER_UPDATED = "dev.pranav.reef.TIMER_UPDATED"
38+
const val ACTION_START = "dev.pranav.reef.START_TIMER"
3739
const val ACTION_PAUSE = "dev.pranav.reef.PAUSE_TIMER"
3840
const val ACTION_RESUME = "dev.pranav.reef.RESUME_TIMER"
3941
const val ACTION_RESTART = "dev.pranav.reef.RESTART_TIMER"
@@ -43,7 +45,6 @@ class FocusModeService: Service() {
4345

4446
private val notificationManager by lazy { NotificationManagerCompat.from(this) }
4547
private val systemNotificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager }
46-
4748
private var countDownTimer: CountDownTimer? = null
4849
private var notificationBuilder: NotificationCompat.Builder? = null
4950
private var previousInterruptionFilter: Int? = null
@@ -79,21 +80,20 @@ class FocusModeService: Service() {
7980
}
8081

8182
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
82-
val isFocusActive = prefs.getBoolean("focus_mode", false)
83-
84-
if (!isFocusActive && intent?.action == null) {
85-
stopSelf()
83+
if (intent?.action == null) {
84+
stopSelfResult(startId)
8685
return START_NOT_STICKY
8786
}
8887

8988
promoteToForeground()
9089

91-
when (intent?.action) {
90+
when (intent.action) {
9291
ACTION_PAUSE -> pauseTimer()
9392
ACTION_RESUME -> resumeTimer()
9493
ACTION_RESTART -> restartCurrentPhase()
95-
else -> startTimer()
94+
ACTION_START -> startTimer()
9695
}
96+
9797
return START_STICKY
9898
}
9999

0 commit comments

Comments
 (0)