fix: Show only the progress for the current timer in notification when running on Android 15 or lower

This commit is contained in:
Nishant Mishra
2025-09-14 17:07:32 +05:30
parent c1c2f57ddc
commit fdca3cfa2a

View File

@@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import android.app.Service import android.app.Service
import android.content.Intent import android.content.Intent
import android.media.MediaPlayer import android.media.MediaPlayer
import android.os.Build
import android.os.IBinder import android.os.IBinder
import android.os.SystemClock import android.os.SystemClock
import android.provider.Settings import android.provider.Settings
@@ -107,20 +108,27 @@ class TimerService : Service() {
} }
private fun toggleTimer() { private fun toggleTimer() {
if (timerState.value.timerRunning) {
notificationBuilder notificationBuilder
.clearActions() .clearActions()
.addTimerActions( .addTimerActions(
this, this,
if (timerState.value.timerRunning) R.drawable.pause else R.drawable.play, R.drawable.play,
if (timerState.value.timerRunning) "Stop" else "Start" "Start"
) )
if (timerState.value.timerRunning) {
showTimerNotification(time.toInt(), paused = true) showTimerNotification(time.toInt(), paused = true)
_timerState.update { currentState -> _timerState.update { currentState ->
currentState.copy(timerRunning = false) currentState.copy(timerRunning = false)
} }
pauseTime = SystemClock.elapsedRealtime() pauseTime = SystemClock.elapsedRealtime()
} else { } else {
notificationBuilder
.clearActions()
.addTimerActions(
this,
R.drawable.pause,
"Stop"
)
_timerState.update { it.copy(timerRunning = true) } _timerState.update { it.copy(timerRunning = true) }
if (pauseTime != 0L) pauseDuration += SystemClock.elapsedRealtime() - pauseTime if (pauseTime != 0L) pauseDuration += SystemClock.elapsedRealtime() - pauseTime
@@ -202,6 +210,9 @@ class TimerService : Service() {
.setStyle( .setStyle(
NotificationCompat.ProgressStyle().also { NotificationCompat.ProgressStyle().also {
// Add all the Focus, Short break and long break intervals in order // Add all the Focus, Short break and long break intervals in order
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BAKLAVA) {
// Android 16 and later supports live updates
// Set progress bar sections if on Baklava or later
for (i in 0..<timerRepository.sessionLength * 2) { for (i in 0..<timerRepository.sessionLength * 2) {
if (i % 2 == 0) it.addProgressSegment( if (i % 2 == 0) it.addProgressSegment(
NotificationCompat.ProgressStyle.Segment( NotificationCompat.ProgressStyle.Segment(
@@ -219,9 +230,22 @@ class TimerService : Service() {
).setColor(cs.tertiary.toArgb()) ).setColor(cs.tertiary.toArgb())
) )
} }
} else {
it.addProgressSegment(
NotificationCompat.ProgressStyle.Segment(
when (timerState.value.timerMode) {
TimerMode.FOCUS -> timerRepository.focusTime.toInt()
TimerMode.SHORT_BREAK -> timerRepository.shortBreakTime.toInt()
else -> timerRepository.longBreakTime.toInt()
}
)
)
}
} }
.setProgress( // Set the current progress by filling the previous intervals and part of the current interval .setProgress( // Set the current progress by filling the previous intervals and part of the current interval
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BAKLAVA) {
(totalTime - remainingTime) + ((cycles + 1) / 2) * timerRepository.focusTime.toInt() + (cycles / 2) * timerRepository.shortBreakTime.toInt() (totalTime - remainingTime) + ((cycles + 1) / 2) * timerRepository.focusTime.toInt() + (cycles / 2) * timerRepository.shortBreakTime.toInt()
} else (totalTime - remainingTime)
) )
) )
.setWhen(System.currentTimeMillis() + remainingTime) // Sets the Live Activity/Now Bar chip time .setWhen(System.currentTimeMillis() + remainingTime) // Sets the Live Activity/Now Bar chip time
@@ -310,7 +334,7 @@ class TimerService : Service() {
_timerState.update { currentState -> _timerState.update { currentState ->
currentState.copy(alarmRinging = false) currentState.copy(alarmRinging = false)
} }
notificationBuilder.clearActions().addTimerActions(this, R.drawable.play, "Start") notificationBuilder.clearActions().addTimerActions(this, R.drawable.play, "Start next")
showTimerNotification( showTimerNotification(
when (timerState.value.timerMode) { when (timerState.value.timerMode) {
TimerMode.FOCUS -> timerRepository.focusTime.toInt() TimerMode.FOCUS -> timerRepository.focusTime.toInt()