Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for exact alarms not allowed #568

Merged
merged 4 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion serviceLibrary/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
<!-- <uses-permission android:name="android.permission.USE_EXACT_ALARM" /> this is only required for calendar apps -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,27 @@ internal class AlarmPingSender(val service: MqttService) : MqttPingSender {
Timber.d("Schedule next alarm at $nextAlarmInMilliseconds ms")
val alarmManager = service.getSystemService(Service.ALARM_SERVICE) as AlarmManager
pendingIntent?.let {
if (Build.VERSION.SDK_INT >= 23) {
//add check for android 13 and up
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
//check if we can scheduele exact alarms
if (alarmManager.canScheduleExactAlarms()) {
Timber.d("Alarm schedule using setExactAndAllowWhileIdle, next: $delayInMilliseconds")
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextAlarmInMilliseconds, it)
} else // it's up to the app to ask for permission so we schedule with a no exact but we do warn in the logs about it
{
Timber.w("Not allowed to schedule exact alarms! using non exact alarm")
Timber.w("Alarm schedule using setAndAllowWhileIdle, next: $delayInMilliseconds")
alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextAlarmInMilliseconds, it)
}
} else 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
} else {
Timber.d("Alarm schedule using setExact, delay: $delayInMilliseconds")
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextAlarmInMilliseconds, it)
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextAlarmInMilliseconds, it)
}
}
}

Expand Down
Loading