Skip to content

Commit

Permalink
Fix api34 issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesa2 committed Jul 20, 2023
1 parent 40cd155 commit dbe6678
Showing 1 changed file with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ internal class AlarmPingSender(val service: MqttService) : MqttPingSender {
override fun stop() {
Timber.d("Unregister AlarmReceiver to MqttService ${clientComms!!.client.clientId}")
if (hasStarted) {
if (pendingIntent != null) {
pendingIntent?.let {
// Cancel Alarm.
val alarmManager = service.getSystemService(Service.ALARM_SERVICE) as AlarmManager
alarmManager.cancel(pendingIntent)
alarmManager.cancel(it)
}
hasStarted = false
try {
Expand All @@ -79,14 +79,16 @@ internal class AlarmPingSender(val service: MqttService) : MqttPingSender {
val nextAlarmInMilliseconds = SystemClock.elapsedRealtime() + delayInMilliseconds
Timber.d("Schedule next alarm at $nextAlarmInMilliseconds ms")
val alarmManager = service.getSystemService(Service.ALARM_SERVICE) as AlarmManager
if (Build.VERSION.SDK_INT >= 23) {
// In SDK 23 and above, dosing will prevent setExact, setExactAndAllowWhileIdle will force
// the device to run this task whilst dosing.
Timber.d("Alarm schedule using setExactAndAllowWhileIdle, next: $delayInMilliseconds")
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextAlarmInMilliseconds, pendingIntent)
} else
Timber.d("Alarm schedule using setExact, delay: $delayInMilliseconds")
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextAlarmInMilliseconds, pendingIntent)
pendingIntent?.let {
if (Build.VERSION.SDK_INT >= 23) {
// In SDK 23 and above, dosing will prevent setExact, setExactAndAllowWhileIdle will force
// the device to run this task whilst dosing.
Timber.d("Alarm schedule using setExactAndAllowWhileIdle, next: $delayInMilliseconds")
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextAlarmInMilliseconds, it)
} else
Timber.d("Alarm schedule using setExact, delay: $delayInMilliseconds")
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextAlarmInMilliseconds, it)
}
}

fun backgroundExecute(comms: ClientComms?): Boolean {
Expand Down

0 comments on commit dbe6678

Please sign in to comment.