Always cancel old timer when new one starts
Previously we didn't cancel the alarm or notification. Closes #45
This commit is contained in:
parent
686666b953
commit
118b87bef1
|
@ -16,7 +16,7 @@ import androidx.core.app.NotificationCompat
|
||||||
import kotlin.math.floor
|
import kotlin.math.floor
|
||||||
|
|
||||||
class TimerService() : Service() {
|
class TimerService() : Service() {
|
||||||
private lateinit var manager: NotificationManager
|
private var manager: NotificationManager? = null
|
||||||
private var countdownTimer: CountDownTimer? = null
|
private var countdownTimer: CountDownTimer? = null
|
||||||
private var endMs: Int = 0
|
private var endMs: Int = 0
|
||||||
private var currentMs: Long = 0
|
private var currentMs: Long = 0
|
||||||
|
@ -27,8 +27,9 @@ class TimerService() : Service() {
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
vibrate = intent!!.extras!!.getBoolean("vibrate")
|
vibrate = intent!!.extras!!.getBoolean("vibrate")
|
||||||
sound = intent.extras?.getString("sound")
|
sound = intent.extras?.getString("sound")
|
||||||
|
manager?.cancel(NOTIFICATION_ID_DONE)
|
||||||
|
applicationContext.stopService(Intent(applicationContext, AlarmService::class.java))
|
||||||
if (intent.action == "add") {
|
if (intent.action == "add") {
|
||||||
manager.cancel(NOTIFICATION_ID_DONE)
|
|
||||||
endMs = currentMs.toInt().plus(60000)
|
endMs = currentMs.toInt().plus(60000)
|
||||||
applicationContext.stopService(Intent(applicationContext, AlarmService::class.java))
|
applicationContext.stopService(Intent(applicationContext, AlarmService::class.java))
|
||||||
} else {
|
} else {
|
||||||
|
@ -57,7 +58,7 @@ class TimerService() : Service() {
|
||||||
.setProgress(endMs, current.toInt(), false)
|
.setProgress(endMs, current.toInt(), false)
|
||||||
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
|
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
|
||||||
.priority = NotificationCompat.PRIORITY_LOW
|
.priority = NotificationCompat.PRIORITY_LOW
|
||||||
manager.notify(NOTIFICATION_ID_PENDING, builder.build())
|
manager!!.notify(NOTIFICATION_ID_PENDING, builder.build())
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.M)
|
@RequiresApi(Build.VERSION_CODES.M)
|
||||||
|
@ -87,8 +88,8 @@ class TimerService() : Service() {
|
||||||
.setCategory(NotificationCompat.CATEGORY_ALARM)
|
.setCategory(NotificationCompat.CATEGORY_ALARM)
|
||||||
.setDeleteIntent(pendingStop)
|
.setDeleteIntent(pendingStop)
|
||||||
.priority = NotificationCompat.PRIORITY_HIGH
|
.priority = NotificationCompat.PRIORITY_HIGH
|
||||||
manager.notify(NOTIFICATION_ID_DONE, builder.build())
|
manager!!.notify(NOTIFICATION_ID_DONE, builder.build())
|
||||||
manager.cancel(NOTIFICATION_ID_PENDING)
|
manager!!.cancel(NOTIFICATION_ID_PENDING)
|
||||||
val alarmIntent = Intent(applicationContext, AlarmService::class.java)
|
val alarmIntent = Intent(applicationContext, AlarmService::class.java)
|
||||||
alarmIntent.putExtra("vibrate", vibrate)
|
alarmIntent.putExtra("vibrate", vibrate)
|
||||||
alarmIntent.putExtra("sound", sound)
|
alarmIntent.putExtra("sound", sound)
|
||||||
|
@ -104,8 +105,8 @@ class TimerService() : Service() {
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
Log.d("TimerService", "Destroying...")
|
Log.d("TimerService", "Destroying...")
|
||||||
countdownTimer?.cancel()
|
countdownTimer?.cancel()
|
||||||
manager.cancel(NOTIFICATION_ID_PENDING)
|
manager?.cancel(NOTIFICATION_ID_PENDING)
|
||||||
manager.cancel(NOTIFICATION_ID_DONE)
|
manager?.cancel(NOTIFICATION_ID_DONE)
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user