Show notification on screen when timer is complete

This commit is contained in:
Brandon Presley 2022-08-22 18:31:47 +12:00
parent a7dede6abc
commit 44a94d029a

View File

@ -1,9 +1,8 @@
package com.massive package com.massive
import android.app.NotificationChannel import android.app.*
import android.app.NotificationManager import android.app.NotificationManager.IMPORTANCE_HIGH
import android.app.PendingIntent import android.app.NotificationManager.IMPORTANCE_LOW
import android.app.Service
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Build import android.os.Build
@ -15,7 +14,7 @@ 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 manager: NotificationManager? = null
private var endMs: Int? = null private var endMs: Int? = null
private var currentMs: Long? = null private var currentMs: Long? = null
private var countdownTimer: CountDownTimer? = null private var countdownTimer: CountDownTimer? = null
@ -27,22 +26,22 @@ class TimerService : Service() {
Log.d("TimerService", "endMs=$endMs,currentMs=$currentMs") Log.d("TimerService", "endMs=$endMs,currentMs=$currentMs")
vibrate = intent!!.extras!!.getBoolean("vibrate") vibrate = intent!!.extras!!.getBoolean("vibrate")
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 {
endMs = intent.extras!!.getInt("milliseconds") endMs = intent.extras!!.getInt("milliseconds")
} }
Log.d("TimerService", "endMs=$endMs,currentMs=$currentMs") Log.d("TimerService", "endMs=$endMs,currentMs=$currentMs")
notificationManager = getManager(applicationContext) manager = getManager(applicationContext)
val builder = getBuilder(applicationContext) val builder = getBuilder(applicationContext)
countdownTimer?.cancel() countdownTimer?.cancel()
countdownTimer = getTimer(builder, notificationManager!!) countdownTimer = getTimer(builder)
countdownTimer!!.start() countdownTimer!!.start()
return super.onStartCommand(intent, flags, startId) return super.onStartCommand(intent, flags, startId)
} }
private fun getTimer(builder: NotificationCompat.Builder, notificationManager: NotificationManager): CountDownTimer { private fun getTimer(builder: NotificationCompat.Builder): CountDownTimer {
return object : CountDownTimer(endMs!!.toLong(), 1000) { return object : CountDownTimer(endMs!!.toLong(), 1000) {
override fun onTick(current: Long) { override fun onTick(current: Long) {
currentMs = current currentMs = current
@ -56,20 +55,29 @@ 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
notificationManager.notify(NOTIFICATION_ID, builder.build()) manager?.notify(NOTIFICATION_ID_PENDING, builder.build())
} }
@RequiresApi(Build.VERSION_CODES.M) @RequiresApi(Build.VERSION_CODES.M)
override fun onFinish() { override fun onFinish() {
val finishIntent = Intent(applicationContext, StopAlarm::class.java) val finishIntent = Intent(applicationContext, StopAlarm::class.java)
val finishPending = val finishPending =
PendingIntent.getActivity(applicationContext, 0, finishIntent, PendingIntent.FLAG_IMMUTABLE) PendingIntent.getActivity(
applicationContext,
0,
finishIntent,
PendingIntent.FLAG_IMMUTABLE
)
builder.setContentText("Timer finished.") builder.setContentText("Timer finished.")
.setAutoCancel(true) .setAutoCancel(true)
.setProgress(0, 0, false)
.setOngoing(false) .setOngoing(false)
.setContentIntent(finishPending) .setContentIntent(finishPending)
.setChannelId(CHANNEL_ID_DONE)
.setCategory(NotificationCompat.CATEGORY_ALARM) .setCategory(NotificationCompat.CATEGORY_ALARM)
.priority = NotificationCompat.PRIORITY_HIGH .priority = NotificationCompat.PRIORITY_HIGH
notificationManager.notify(NOTIFICATION_ID, builder.build()) manager?.notify(NOTIFICATION_ID_DONE, builder.build())
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)
applicationContext.startService(alarmIntent) applicationContext.startService(alarmIntent)
@ -84,7 +92,8 @@ 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) manager?.cancel(NOTIFICATION_ID_PENDING)
manager?.cancel(NOTIFICATION_ID_DONE)
super.onDestroy() super.onDestroy()
} }
@ -104,7 +113,7 @@ class TimerService : Service() {
} else { } else {
PendingIntent.getService(context, 0, addIntent, PendingIntent.FLAG_UPDATE_CURRENT) PendingIntent.getService(context, 0, addIntent, PendingIntent.FLAG_UPDATE_CURRENT)
} }
return NotificationCompat.Builder(context, CHANNEL_ID) return NotificationCompat.Builder(context, CHANNEL_ID_PENDING)
.setSmallIcon(R.drawable.ic_baseline_hourglass_bottom_24) .setSmallIcon(R.drawable.ic_baseline_hourglass_bottom_24)
.setContentTitle("Resting") .setContentTitle("Resting")
.setContentIntent(pendingContent) .setContentIntent(pendingContent)
@ -114,21 +123,28 @@ class TimerService : Service() {
@RequiresApi(Build.VERSION_CODES.O) @RequiresApi(Build.VERSION_CODES.O)
private fun getManager(context: Context): NotificationManager { private fun getManager(context: Context): NotificationManager {
val importance = NotificationManager.IMPORTANCE_LOW val alarmsChannel = NotificationChannel(
val channel = NotificationChannel( CHANNEL_ID_DONE,
CHANNEL_ID, CHANNEL_ID_DONE,
CHANNEL_ID, importance IMPORTANCE_HIGH
) )
channel.description = "Alarms for rest timings." alarmsChannel.description = "Alarms for rest timers."
alarmsChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
val notificationManager = context.getSystemService( val notificationManager = context.getSystemService(
NotificationManager::class.java NotificationManager::class.java
) )
notificationManager.createNotificationChannel(channel) notificationManager.createNotificationChannel(alarmsChannel)
val timersChannel =
NotificationChannel(CHANNEL_ID_PENDING, CHANNEL_ID_PENDING, IMPORTANCE_LOW)
timersChannel.description = "Progress on rest timers."
notificationManager.createNotificationChannel(timersChannel)
return notificationManager return notificationManager
} }
companion object { companion object {
private const val CHANNEL_ID = "MassiveTimer" private const val CHANNEL_ID_PENDING = "MassiveTimer"
private const val NOTIFICATION_ID = 1 private const val CHANNEL_ID_DONE = "MassiveDone"
private const val NOTIFICATION_ID_PENDING = 1
private const val NOTIFICATION_ID_DONE = 2
} }
} }