mirror of
https://github.com/SimpleMobileTools/Simple-Clock.git
synced 2025-02-07 15:38:41 +01:00
stop the timer on expiration if backgrounded
This commit is contained in:
parent
49937ede5c
commit
3f7da61c3f
@ -41,6 +41,7 @@ class TimerFragment : Fragment() {
|
|||||||
private var totalTicks = 0
|
private var totalTicks = 0
|
||||||
private var currentTicks = 0
|
private var currentTicks = 0
|
||||||
private var updateHandler = Handler()
|
private var updateHandler = Handler()
|
||||||
|
private var isForegrounded = true
|
||||||
|
|
||||||
lateinit var view: ViewGroup
|
lateinit var view: ViewGroup
|
||||||
|
|
||||||
@ -88,11 +89,21 @@ class TimerFragment : Fragment() {
|
|||||||
return view
|
return view
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onStart() {
|
||||||
|
super.onStart()
|
||||||
|
isForegrounded = true
|
||||||
|
}
|
||||||
|
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
setupViews()
|
setupViews()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onStop() {
|
||||||
|
super.onStop()
|
||||||
|
isForegrounded = false
|
||||||
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
if (isRunning) {
|
if (isRunning) {
|
||||||
@ -146,7 +157,7 @@ class TimerFragment : Fragment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun resetTimer() {
|
private fun resetTimer() {
|
||||||
updateHandler.removeCallbacksAndMessages(null)
|
updateHandler.removeCallbacks(updateRunnable)
|
||||||
isRunning = false
|
isRunning = false
|
||||||
currentTicks = 0
|
currentTicks = 0
|
||||||
totalTicks = 0
|
totalTicks = 0
|
||||||
@ -156,11 +167,15 @@ class TimerFragment : Fragment() {
|
|||||||
view.timer_reset.beGone()
|
view.timer_reset.beGone()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateDisplayedText() {
|
private fun updateDisplayedText(): Boolean {
|
||||||
val diff = initialSecs - totalTicks
|
val diff = initialSecs - totalTicks
|
||||||
var formattedDuration = Math.abs(diff).getFormattedDuration()
|
var formattedDuration = Math.abs(diff).getFormattedDuration()
|
||||||
if (diff < 0) {
|
if (diff < 0) {
|
||||||
formattedDuration = "-$formattedDuration"
|
formattedDuration = "-$formattedDuration"
|
||||||
|
if (!isForegrounded) {
|
||||||
|
resetTimer()
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
view.timer_time.text = formattedDuration
|
view.timer_time.text = formattedDuration
|
||||||
@ -170,6 +185,7 @@ class TimerFragment : Fragment() {
|
|||||||
val notificationManager = context!!.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
val notificationManager = context!!.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||||
notificationManager.notify(TIMER_NOTIF_ID, notification)
|
notificationManager.notify(TIMER_NOTIF_ID, notification)
|
||||||
}
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("NewApi")
|
@SuppressLint("NewApi")
|
||||||
@ -224,10 +240,11 @@ class TimerFragment : Fragment() {
|
|||||||
private val updateRunnable = object : Runnable {
|
private val updateRunnable = object : Runnable {
|
||||||
override fun run() {
|
override fun run() {
|
||||||
if (isRunning) {
|
if (isRunning) {
|
||||||
updateDisplayedText()
|
if (updateDisplayedText()) {
|
||||||
currentTicks++
|
currentTicks++
|
||||||
totalTicks++
|
totalTicks++
|
||||||
updateHandler.postAtTime(this, uptimeAtStart + currentTicks * UPDATE_INTERVAL)
|
updateHandler.postAtTime(this, uptimeAtStart + currentTicks * UPDATE_INTERVAL)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user