Remove non null assertions in TimerService
This might help fix the error we were seeing on the play store.
This commit is contained in:
parent
f41e7b3ffe
commit
e6228b3990
|
@ -15,8 +15,7 @@ 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 var countdownTimer: CountDownTimer? = null
|
private var countdownTimer: CountDownTimer? = null
|
||||||
private var endMs: Int = 0
|
private var endMs: Int = 0
|
||||||
private var currentMs: Long = 0
|
private var currentMs: Long = 0
|
||||||
|
@ -27,16 +26,17 @@ class TimerService() : Service() {
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
vibrate = intent?.extras?.getBoolean("vibrate") == true
|
vibrate = intent?.extras?.getBoolean("vibrate") == true
|
||||||
sound = intent?.extras?.getString("sound")
|
sound = intent?.extras?.getString("sound")
|
||||||
manager?.cancel(NOTIFICATION_ID_DONE)
|
val manager = getManager()
|
||||||
|
manager.cancel(NOTIFICATION_ID_DONE)
|
||||||
applicationContext.stopService(Intent(applicationContext, AlarmService::class.java))
|
applicationContext.stopService(Intent(applicationContext, AlarmService::class.java))
|
||||||
if (intent?.action == "add") {
|
if (intent?.action == "add") {
|
||||||
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")
|
val ms = intent?.extras?.getInt("milliseconds")
|
||||||
|
if (ms != null) endMs = ms;
|
||||||
}
|
}
|
||||||
Log.d("TimerService", "endMs=$endMs,currentMs=$currentMs,vibrate=$vibrate,sound=$sound")
|
Log.d("TimerService", "endMs=$endMs,currentMs=$currentMs,vibrate=$vibrate,sound=$sound")
|
||||||
manager = getManager(applicationContext)
|
|
||||||
val builder = getBuilder(applicationContext)
|
val builder = getBuilder(applicationContext)
|
||||||
countdownTimer?.cancel()
|
countdownTimer?.cancel()
|
||||||
countdownTimer = getTimer(builder)
|
countdownTimer = getTimer(builder)
|
||||||
|
@ -46,6 +46,7 @@ class TimerService() : Service() {
|
||||||
|
|
||||||
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) {
|
||||||
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
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)
|
||||||
|
@ -58,10 +59,11 @@ 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
|
||||||
manager!!.notify(NOTIFICATION_ID_PENDING, builder.build())
|
val manager = getManager()
|
||||||
|
manager.notify(NOTIFICATION_ID_PENDING, builder.build())
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.M)
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
override fun onFinish() {
|
override fun onFinish() {
|
||||||
val finishIntent = Intent(applicationContext, StopAlarm::class.java)
|
val finishIntent = Intent(applicationContext, StopAlarm::class.java)
|
||||||
val finishPending =
|
val finishPending =
|
||||||
|
@ -89,8 +91,9 @@ 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())
|
val manager = getManager()
|
||||||
manager!!.cancel(NOTIFICATION_ID_PENDING)
|
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)
|
||||||
alarmIntent.putExtra("sound", sound)
|
alarmIntent.putExtra("sound", sound)
|
||||||
|
@ -103,11 +106,13 @@ class TimerService() : Service() {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
Log.d("TimerService", "Destroying...")
|
Log.d("TimerService", "Destroying...")
|
||||||
countdownTimer?.cancel()
|
countdownTimer?.cancel()
|
||||||
manager?.cancel(NOTIFICATION_ID_PENDING)
|
val manager = getManager()
|
||||||
manager?.cancel(NOTIFICATION_ID_DONE)
|
manager.cancel(NOTIFICATION_ID_PENDING)
|
||||||
|
manager.cancel(NOTIFICATION_ID_DONE)
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,7 +144,7 @@ class TimerService() : Service() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.O)
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
private fun getManager(context: Context): NotificationManager {
|
private fun getManager(): NotificationManager {
|
||||||
val alarmsChannel = NotificationChannel(
|
val alarmsChannel = NotificationChannel(
|
||||||
CHANNEL_ID_DONE,
|
CHANNEL_ID_DONE,
|
||||||
CHANNEL_ID_DONE,
|
CHANNEL_ID_DONE,
|
||||||
|
@ -147,7 +152,7 @@ class TimerService() : Service() {
|
||||||
)
|
)
|
||||||
alarmsChannel.description = "Alarms for rest timers."
|
alarmsChannel.description = "Alarms for rest timers."
|
||||||
alarmsChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
|
alarmsChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
|
||||||
val notificationManager = context.getSystemService(
|
val notificationManager = applicationContext.getSystemService(
|
||||||
NotificationManager::class.java
|
NotificationManager::class.java
|
||||||
)
|
)
|
||||||
notificationManager.createNotificationChannel(alarmsChannel)
|
notificationManager.createNotificationChannel(alarmsChannel)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user