Fix settings for timer after adding one minute

Both vibration and sound settings weren't applying
after adding one minute to the timer. It would instead
play the default alarm and always vibrate.
This commit is contained in:
Brandon Presley 2022-09-08 22:11:48 +12:00
parent 63049eed52
commit c44c57e1c4

View File

@ -1,10 +1,12 @@
package com.massive package com.massive
import android.annotation.SuppressLint
import android.app.* import android.app.*
import android.app.NotificationManager.IMPORTANCE_HIGH import android.app.NotificationManager.IMPORTANCE_HIGH
import android.app.NotificationManager.IMPORTANCE_LOW import android.app.NotificationManager.IMPORTANCE_LOW
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri
import android.os.Build import android.os.Build
import android.os.CountDownTimer import android.os.CountDownTimer
import android.os.IBinder import android.os.IBinder
@ -13,38 +15,36 @@ import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import kotlin.math.floor import kotlin.math.floor
class TimerService : Service() { class TimerService() : Service() {
private var manager: NotificationManager? = null private lateinit var manager: NotificationManager
private var endMs: Int? = null private lateinit var countdownTimer: CountDownTimer
private var currentMs: Long? = null private var endMs: Int = 0
private var countdownTimer: CountDownTimer? = null private var currentMs: Long = 0
private var vibrate: Boolean = true private var vibrate: Boolean = true
private var sound: String? = null private var sound: String? = 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", "endMs=$endMs,currentMs=$currentMs")
vibrate = intent!!.extras!!.getBoolean("vibrate") vibrate = intent!!.extras!!.getBoolean("vibrate")
sound = intent.extras?.getString("sound") sound = intent.extras?.getString("sound")
if (intent.action == "add") { if (intent.action == "add") {
manager?.cancel(NOTIFICATION_ID_DONE) 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,vibrate=$vibrate,sound=$sound")
manager = getManager(applicationContext) manager = getManager(applicationContext)
val builder = getBuilder(applicationContext) val builder = getBuilder(applicationContext)
countdownTimer?.cancel() countdownTimer.cancel()
countdownTimer = getTimer(builder) 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): 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
val seconds = floor((current / 1000).toDouble() % 60) val seconds = floor((current / 1000).toDouble() % 60)
@ -54,10 +54,10 @@ class TimerService : Service() {
builder.setContentText("$minutes:$seconds") builder.setContentText("$minutes:$seconds")
.setAutoCancel(false) .setAutoCancel(false)
.setDefaults(0) .setDefaults(0)
.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
manager?.notify(NOTIFICATION_ID_PENDING, builder.build()) manager.notify(NOTIFICATION_ID_PENDING, builder.build())
} }
@RequiresApi(Build.VERSION_CODES.M) @RequiresApi(Build.VERSION_CODES.M)
@ -87,8 +87,8 @@ class TimerService : Service() {
.setCategory(NotificationCompat.CATEGORY_ALARM) .setCategory(NotificationCompat.CATEGORY_ALARM)
.setDeleteIntent(pendingStop) .setDeleteIntent(pendingStop)
.priority = NotificationCompat.PRIORITY_HIGH .priority = NotificationCompat.PRIORITY_HIGH
manager?.notify(NOTIFICATION_ID_DONE, builder.build()) manager.notify(NOTIFICATION_ID_DONE, builder.build())
manager?.cancel(NOTIFICATION_ID_PENDING) 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)
alarmIntent.putExtra("sound", sound) alarmIntent.putExtra("sound", sound)
@ -103,12 +103,13 @@ class TimerService : Service() {
override fun onDestroy() { override fun onDestroy() {
Log.d("TimerService", "Destroying...") Log.d("TimerService", "Destroying...")
countdownTimer?.cancel() countdownTimer.cancel()
manager?.cancel(NOTIFICATION_ID_PENDING) manager.cancel(NOTIFICATION_ID_PENDING)
manager?.cancel(NOTIFICATION_ID_DONE) manager.cancel(NOTIFICATION_ID_DONE)
super.onDestroy() super.onDestroy()
} }
@SuppressLint("UnspecifiedImmutableFlag")
@RequiresApi(Build.VERSION_CODES.M) @RequiresApi(Build.VERSION_CODES.M)
private fun getBuilder(context: Context): NotificationCompat.Builder { private fun getBuilder(context: Context): NotificationCompat.Builder {
val contentIntent = Intent(context, MainActivity::class.java) val contentIntent = Intent(context, MainActivity::class.java)
@ -120,6 +121,8 @@ class TimerService : Service() {
val addIntent = Intent(context, TimerService::class.java) val addIntent = Intent(context, TimerService::class.java)
addIntent.action = "add" addIntent.action = "add"
addIntent.putExtra("vibrate", vibrate) addIntent.putExtra("vibrate", vibrate)
addIntent.putExtra("sound", sound)
addIntent.data = Uri.parse("$currentMs")
val pendingAdd = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { val pendingAdd = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
PendingIntent.getService(context, 0, addIntent, PendingIntent.FLAG_MUTABLE) PendingIntent.getService(context, 0, addIntent, PendingIntent.FLAG_MUTABLE)
} else { } else {