Add ability to add 1 minute to timer
This commit is contained in:
parent
9e790e7e1d
commit
d513b147bc
|
@ -15,16 +15,30 @@ import androidx.core.app.NotificationCompat
|
||||||
import kotlin.math.floor
|
import kotlin.math.floor
|
||||||
|
|
||||||
class TimerService : Service() {
|
class TimerService : Service() {
|
||||||
|
private var notificationManager: NotificationManager? = null
|
||||||
|
private var endMs: Int? = null
|
||||||
private var countdownTimer: CountDownTimer? = null
|
private var countdownTimer: CountDownTimer? = null
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.O)
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
Log.d("TimerService", "Started timer service.")
|
Log.d("TimerService", "Started timer service.")
|
||||||
val endMs = intent!!.extras!!.getInt("milliseconds")
|
if (intent?.action == "add") {
|
||||||
val notificationManager = getManager(applicationContext)
|
endMs = endMs?.plus(60000)
|
||||||
|
applicationContext.stopService(Intent(applicationContext, AlarmService::class.java))
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
endMs = intent!!.extras!!.getInt("milliseconds")
|
||||||
|
}
|
||||||
|
notificationManager = getManager(applicationContext)
|
||||||
val builder = getBuilder(applicationContext)
|
val builder = getBuilder(applicationContext)
|
||||||
countdownTimer?.cancel()
|
countdownTimer?.cancel()
|
||||||
countdownTimer = object : CountDownTimer(endMs.toLong(), 1000) {
|
countdownTimer = getTimer(builder, notificationManager!!)
|
||||||
|
countdownTimer!!.start()
|
||||||
|
return super.onStartCommand(intent, flags, startId)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getTimer(builder: NotificationCompat.Builder, notificationManager: NotificationManager): CountDownTimer {
|
||||||
|
return object : CountDownTimer(endMs!!.toLong(), 1000) {
|
||||||
override fun onTick(currentMs: Long) {
|
override fun onTick(currentMs: Long) {
|
||||||
val seconds = floor((currentMs / 1000).toDouble() % 60)
|
val seconds = floor((currentMs / 1000).toDouble() % 60)
|
||||||
.toInt().toString().padStart(2, '0')
|
.toInt().toString().padStart(2, '0')
|
||||||
|
@ -33,7 +47,7 @@ class TimerService : Service() {
|
||||||
builder.setContentText("$minutes:$seconds")
|
builder.setContentText("$minutes:$seconds")
|
||||||
.setAutoCancel(false)
|
.setAutoCancel(false)
|
||||||
.setDefaults(0)
|
.setDefaults(0)
|
||||||
.setProgress(endMs, currentMs.toInt(), false)
|
.setProgress(endMs!!, currentMs.toInt(), false)
|
||||||
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
|
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
|
||||||
.priority = NotificationCompat.PRIORITY_LOW
|
.priority = NotificationCompat.PRIORITY_LOW
|
||||||
notificationManager.notify(NOTIFICATION_ID, builder.build())
|
notificationManager.notify(NOTIFICATION_ID, builder.build())
|
||||||
|
@ -52,8 +66,6 @@ class TimerService : Service() {
|
||||||
applicationContext.startService(Intent(applicationContext, AlarmService::class.java))
|
applicationContext.startService(Intent(applicationContext, AlarmService::class.java))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
countdownTimer!!.start()
|
|
||||||
return super.onStartCommand(intent, flags, startId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBind(p0: Intent?): IBinder? {
|
override fun onBind(p0: Intent?): IBinder? {
|
||||||
|
@ -63,6 +75,7 @@ class TimerService : Service() {
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
Log.d("TimerService", "Destroying...")
|
Log.d("TimerService", "Destroying...")
|
||||||
countdownTimer?.cancel()
|
countdownTimer?.cancel()
|
||||||
|
notificationManager?.cancel(NOTIFICATION_ID)
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,14 +84,18 @@ class TimerService : Service() {
|
||||||
val contentIntent = Intent(context, MainActivity::class.java)
|
val contentIntent = Intent(context, MainActivity::class.java)
|
||||||
val pendingContent =
|
val pendingContent =
|
||||||
PendingIntent.getActivity(context, 0, contentIntent, PendingIntent.FLAG_IMMUTABLE)
|
PendingIntent.getActivity(context, 0, contentIntent, PendingIntent.FLAG_IMMUTABLE)
|
||||||
val actionIntent = Intent(context, StopTimer::class.java)
|
val stopIntent = Intent(context, StopTimer::class.java)
|
||||||
val pendingAction =
|
val pendingStop =
|
||||||
PendingIntent.getService(context, 0, actionIntent, PendingIntent.FLAG_IMMUTABLE)
|
PendingIntent.getService(context, 0, stopIntent, PendingIntent.FLAG_IMMUTABLE)
|
||||||
|
val addIntent = Intent(context, TimerService::class.java)
|
||||||
|
addIntent.action = "add"
|
||||||
|
val pendingAdd = PendingIntent.getService(context, 0, addIntent, PendingIntent.FLAG_IMMUTABLE)
|
||||||
return NotificationCompat.Builder(context, CHANNEL_ID)
|
return NotificationCompat.Builder(context, CHANNEL_ID)
|
||||||
.setSmallIcon(R.drawable.ic_baseline_hourglass_bottom_24)
|
.setSmallIcon(R.drawable.ic_baseline_hourglass_bottom_24)
|
||||||
.setContentTitle("Resting")
|
.setContentTitle("Resting")
|
||||||
.setContentIntent(pendingContent)
|
.setContentIntent(pendingContent)
|
||||||
.addAction(R.drawable.ic_baseline_stop_24, "STOP", pendingAction)
|
.addAction(R.drawable.ic_baseline_stop_24, "Stop", pendingStop)
|
||||||
|
.addAction(R.drawable.ic_baseline_stop_24, "Add 1 min", pendingAdd)
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.O)
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user