99 lines
3.4 KiB
Kotlin
99 lines
3.4 KiB
Kotlin
package com.adlerosn.brasilfurfest
|
|
|
|
import android.content.Context
|
|
import android.os.Bundle
|
|
import android.os.PowerManager
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
import android.view.View
|
|
import com.adlerosn.brasilfurfest.helper.getString
|
|
import com.adlerosn.brasilfurfest.helper.resourcesAliased
|
|
import com.adlerosn.brasilfurfest.helper.uiThreadWithLogger
|
|
import kotlinx.android.synthetic.main.activity_countdown.*
|
|
import org.jetbrains.anko.doAsync
|
|
import java.util.*
|
|
|
|
|
|
class CountdownActivity : AppCompatActivity() {
|
|
|
|
private var screenOn = true
|
|
private lateinit var pm: PowerManager
|
|
private val now get() = System.currentTimeMillis()
|
|
private var objective = now
|
|
private var finishesAt : Long? = null
|
|
private lateinit var timer: Timer
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
setContentView(R.layout.activity_countdown)
|
|
resourcesAliased = this.resources
|
|
batteryWarning.visibility = View.GONE
|
|
pm = getSystemService(Context.POWER_SERVICE) as PowerManager
|
|
countdownTitle.text = intent.getStringExtra("label") ?: ""
|
|
objective = intent.getLongExtra("counter", objective)
|
|
finishesAt = intent.getLongExtra("finishesAt", -1).let { if (it<0) null else it }
|
|
screenOn = pm.isInteractive
|
|
timer = Timer()
|
|
updateTime()
|
|
timerScreen()
|
|
}
|
|
|
|
private fun timerScreen(){
|
|
timer.cancel()
|
|
timer.purge()
|
|
timer = Timer()
|
|
doAsync {
|
|
timer.scheduleAtFixedRate(
|
|
object : TimerTask() {
|
|
override fun run() {
|
|
uiThreadWithLogger {
|
|
updateTime()
|
|
}
|
|
}
|
|
},
|
|
1,
|
|
1000.toLong()
|
|
)
|
|
}
|
|
}
|
|
|
|
private fun updateTime(){
|
|
val remaining = objective-now
|
|
if ( remaining >= 0 ) {
|
|
countdownData.text = countdownString(remaining)
|
|
countdownLabel.text = R.string.until_it_starts.getString()
|
|
countdownData.visibility = View.VISIBLE
|
|
countdownLabel.visibility = View.VISIBLE
|
|
} else {
|
|
val finishes = finishesAt?.let { it-now }
|
|
if ( (finishes != null) && (finishes >= 0)) {
|
|
countdownData.text = countdownString(finishes)
|
|
countdownLabel.text = R.string.until_it_ends.getString()
|
|
countdownData.visibility = View.VISIBLE
|
|
countdownLabel.visibility = View.VISIBLE
|
|
} else {
|
|
countdownData.text = R.string.already_finished.getString()
|
|
countdownData.visibility = View.VISIBLE
|
|
countdownLabel.visibility = View.INVISIBLE
|
|
}
|
|
}
|
|
if (pm.isInteractive != screenOn) {
|
|
screenOn = pm.isInteractive
|
|
timerScreen()
|
|
}
|
|
}
|
|
|
|
private fun countdownString(remaining: Long): String {
|
|
val (_, s, m, h, d) = countdownSplitter(remaining)
|
|
return "%dd %02dh %02dm %02ds"// %03dms"
|
|
.format(d, h, m, s)//, ms)
|
|
}
|
|
|
|
private fun countdownSplitter(remaining: Long) = listOf(
|
|
remaining%1000,
|
|
(remaining/(1000))%60,
|
|
(remaining/(60*1000))%60,
|
|
(remaining/(3600*1000))%24,
|
|
remaining/(24*3600*1000)
|
|
)
|
|
}
|