Massive/android/app/src/main/java/com/massive/TimerService.kt

125 lines
5.3 KiB
Kotlin
Raw Normal View History

2022-07-05 03:33:42 +00:00
package com.massive
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
2022-07-05 03:33:42 +00:00
import android.app.Service
import android.content.Context
2022-07-05 03:33:42 +00:00
import android.content.Intent
import android.os.Build
import android.os.CountDownTimer
import android.os.IBinder
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import kotlin.math.floor
2022-07-05 03:33:42 +00:00
class TimerService : Service() {
2022-07-19 05:03:43 +00:00
private var notificationManager: NotificationManager? = null
private var endMs: Int? = null
2022-07-25 23:37:57 +00:00
private var currentMs: Long? = null
private var countdownTimer: CountDownTimer? = null
2022-07-05 03:33:42 +00:00
@RequiresApi(Build.VERSION_CODES.O)
2022-07-05 03:33:42 +00:00
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d("TimerService", "Started timer service.")
2022-07-25 23:37:57 +00:00
Log.d("TimerService", "endMs=$endMs,currentMs=$currentMs")
2022-07-19 05:03:43 +00:00
if (intent?.action == "add") {
2022-07-25 23:37:57 +00:00
endMs = currentMs!!.toInt().plus(60000)
2022-07-19 05:03:43 +00:00
applicationContext.stopService(Intent(applicationContext, AlarmService::class.java))
}
else {
endMs = intent!!.extras!!.getInt("milliseconds")
}
2022-07-25 23:37:57 +00:00
Log.d("TimerService", "endMs=$endMs,currentMs=$currentMs")
2022-07-19 05:03:43 +00:00
notificationManager = getManager(applicationContext)
val builder = getBuilder(applicationContext)
countdownTimer?.cancel()
2022-07-19 05:03:43 +00:00
countdownTimer = getTimer(builder, notificationManager!!)
countdownTimer!!.start()
2022-07-05 03:33:42 +00:00
return super.onStartCommand(intent, flags, startId)
}
2022-07-19 05:03:43 +00:00
private fun getTimer(builder: NotificationCompat.Builder, notificationManager: NotificationManager): CountDownTimer {
return object : CountDownTimer(endMs!!.toLong(), 1000) {
2022-07-25 23:37:57 +00:00
override fun onTick(current: Long) {
currentMs = current
val seconds = floor((current / 1000).toDouble() % 60)
2022-07-19 05:03:43 +00:00
.toInt().toString().padStart(2, '0')
2022-07-25 23:37:57 +00:00
val minutes = floor((current / 1000).toDouble() / 60)
2022-07-19 05:03:43 +00:00
.toInt().toString().padStart(2, '0')
builder.setContentText("$minutes:$seconds")
.setAutoCancel(false)
.setDefaults(0)
2022-07-25 23:37:57 +00:00
.setProgress(endMs!!, current.toInt(), false)
2022-07-19 05:03:43 +00:00
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
.priority = NotificationCompat.PRIORITY_LOW
notificationManager.notify(NOTIFICATION_ID, builder.build())
}
2022-07-25 23:37:57 +00:00
@RequiresApi(Build.VERSION_CODES.M)
2022-07-19 05:03:43 +00:00
override fun onFinish() {
val finishIntent = Intent(applicationContext, StopAlarm::class.java)
val finishPending =
PendingIntent.getActivity(applicationContext, 0, finishIntent, PendingIntent.FLAG_IMMUTABLE)
builder.setContentText("Timer finished.")
.setAutoCancel(true)
.setOngoing(false)
.setContentIntent(finishPending)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.priority = NotificationCompat.PRIORITY_HIGH
notificationManager.notify(NOTIFICATION_ID, builder.build())
applicationContext.startService(Intent(applicationContext, AlarmService::class.java))
}
}
}
2022-07-05 03:33:42 +00:00
override fun onBind(p0: Intent?): IBinder? {
return null
}
override fun onDestroy() {
Log.d("TimerService", "Destroying...")
countdownTimer?.cancel()
2022-07-19 05:03:43 +00:00
notificationManager?.cancel(NOTIFICATION_ID)
2022-07-05 03:33:42 +00:00
super.onDestroy()
}
@RequiresApi(Build.VERSION_CODES.M)
private fun getBuilder(context: Context): NotificationCompat.Builder {
val contentIntent = Intent(context, MainActivity::class.java)
val pendingContent =
PendingIntent.getActivity(context, 0, contentIntent, PendingIntent.FLAG_IMMUTABLE)
2022-07-19 05:03:43 +00:00
val stopIntent = Intent(context, StopTimer::class.java)
val pendingStop =
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)
.setSmallIcon(R.drawable.ic_baseline_hourglass_bottom_24)
.setContentTitle("Resting")
.setContentIntent(pendingContent)
2022-07-19 05:03:43 +00:00
.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)
private fun getManager(context: Context): NotificationManager {
val importance = NotificationManager.IMPORTANCE_LOW
val channel = NotificationChannel(
CHANNEL_ID,
CHANNEL_ID, importance
)
channel.description = "Alarms for rest timings."
val notificationManager = context.getSystemService(
NotificationManager::class.java
)
notificationManager.createNotificationChannel(channel)
return notificationManager
}
companion object {
private const val CHANNEL_ID = "MassiveTimer"
private const val NOTIFICATION_ID = 1
}
2022-07-05 03:33:42 +00:00
}