2022-07-05 03:33:42 +00:00
|
|
|
package com.massive
|
|
|
|
|
2022-07-16 04:12:28 +00:00
|
|
|
import android.app.NotificationChannel
|
|
|
|
import android.app.NotificationManager
|
|
|
|
import android.app.PendingIntent
|
2022-07-05 03:33:42 +00:00
|
|
|
import android.app.Service
|
2022-07-16 04:12:28 +00:00
|
|
|
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
|
2022-07-16 04:12:28 +00:00
|
|
|
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
|
2022-07-06 04:27:36 +00:00
|
|
|
private var countdownTimer: CountDownTimer? = null
|
2022-08-20 04:37:59 +00:00
|
|
|
private var vibrate: Boolean = true
|
2022-07-05 03:33:42 +00:00
|
|
|
|
2022-07-16 04:12:28 +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-08-20 04:37:59 +00:00
|
|
|
vibrate = intent!!.extras!!.getBoolean("vibrate")
|
|
|
|
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 {
|
2022-08-20 04:37:59 +00:00
|
|
|
endMs = intent.extras!!.getInt("milliseconds")
|
2022-07-19 05:03:43 +00:00
|
|
|
}
|
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)
|
2022-07-16 04:12:28 +00:00
|
|
|
val builder = getBuilder(applicationContext)
|
2022-07-06 04:27:36 +00:00
|
|
|
countdownTimer?.cancel()
|
2022-07-19 05:03:43 +00:00
|
|
|
countdownTimer = getTimer(builder, notificationManager!!)
|
2022-07-06 04:27:36 +00:00
|
|
|
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())
|
2022-08-20 04:37:59 +00:00
|
|
|
val alarmIntent = Intent(applicationContext, AlarmService::class.java)
|
|
|
|
alarmIntent.putExtra("vibrate", vibrate)
|
|
|
|
applicationContext.startService(alarmIntent)
|
2022-07-19 05:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 03:33:42 +00:00
|
|
|
override fun onBind(p0: Intent?): IBinder? {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroy() {
|
2022-07-06 04:27:36 +00:00
|
|
|
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()
|
|
|
|
}
|
2022-07-16 04:12:28 +00:00
|
|
|
|
2022-08-22 03:51:03 +00:00
|
|
|
@RequiresApi(Build.VERSION_CODES.M)
|
2022-07-16 04:12:28 +00:00
|
|
|
private fun getBuilder(context: Context): NotificationCompat.Builder {
|
|
|
|
val contentIntent = Intent(context, MainActivity::class.java)
|
|
|
|
val pendingContent =
|
2022-08-22 03:51:03 +00:00
|
|
|
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 =
|
2022-08-22 03:51:03 +00:00
|
|
|
PendingIntent.getService(context, 0, stopIntent, PendingIntent.FLAG_IMMUTABLE)
|
2022-07-19 05:03:43 +00:00
|
|
|
val addIntent = Intent(context, TimerService::class.java)
|
|
|
|
addIntent.action = "add"
|
2022-08-22 03:48:53 +00:00
|
|
|
addIntent.putExtra("vibrate", vibrate)
|
|
|
|
val pendingAdd = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
|
|
|
PendingIntent.getService(context, 0, addIntent, PendingIntent.FLAG_MUTABLE)
|
|
|
|
} else {
|
|
|
|
PendingIntent.getService(context, 0, addIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
|
|
|
}
|
2022-07-16 04:12:28 +00:00
|
|
|
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)
|
2022-07-16 04:12:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
}
|