diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt index 8af000359..551f9394c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt @@ -493,7 +493,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { } syncCalDAVCalendars { - calDAVHelper.refreshCalendars(true) { + calDAVHelper.refreshCalendars(true, true) { calDAVChanged() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SettingsActivity.kt index bdc977f03..49ecd69f6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SettingsActivity.kt @@ -318,7 +318,7 @@ class SettingsActivity : SimpleActivity() { } syncCalDAVCalendars { - calDAVHelper.refreshCalendars(true) { + calDAVHelper.refreshCalendars(true, true) { if (settings_caldav_sync.isChecked) { toast(R.string.synchronization_completed) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt index 181a66993..24e1e9ece 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt @@ -395,10 +395,10 @@ fun Context.getNewEventTimestampFromCode(dayCode: String, allowChangingDay: Bool fun Context.getSyncedCalDAVCalendars() = calDAVHelper.getCalDAVCalendars(config.caldavSyncedCalendarIds, false) -fun Context.recheckCalDAVCalendars(callback: () -> Unit) { +fun Context.recheckCalDAVCalendars(scheduleNextCalDAVSync: Boolean, callback: () -> Unit) { if (config.caldavSync) { ensureBackgroundThread { - calDAVHelper.refreshCalendars(false, callback) + calDAVHelper.refreshCalendars(false, scheduleNextCalDAVSync, callback) updateWidgets() } } @@ -406,8 +406,9 @@ fun Context.recheckCalDAVCalendars(callback: () -> Unit) { fun Context.scheduleCalDAVSync(activate: Boolean) { val syncIntent = Intent(applicationContext, CalDAVSyncReceiver::class.java) - val pendingIntent = PendingIntent.getBroadcast(applicationContext, 0, syncIntent, PendingIntent.FLAG_UPDATE_CURRENT) + val pendingIntent = PendingIntent.getBroadcast(applicationContext, SCHEDULE_CALDAV_REQUEST_CODE, syncIntent, PendingIntent.FLAG_UPDATE_CURRENT) val alarm = getSystemService(Context.ALARM_SERVICE) as AlarmManager + alarm.cancel(pendingIntent) if (activate) { val syncCheckInterval = 2 * AlarmManager.INTERVAL_HOUR @@ -415,8 +416,6 @@ fun Context.scheduleCalDAVSync(activate: Boolean) { alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + syncCheckInterval, syncCheckInterval, pendingIntent) } catch (ignored: Exception) { } - } else { - alarm.cancel(pendingIntent) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt index 04c67db8f..0e8c98ca2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt @@ -23,7 +23,7 @@ import org.joda.time.format.DateTimeFormat class CalDAVHelper(val context: Context) { private val eventsHelper = context.eventsHelper - fun refreshCalendars(showToasts: Boolean, callback: () -> Unit) { + fun refreshCalendars(showToasts: Boolean, scheduleNextSync: Boolean, callback: () -> Unit) { if (isUpdatingCalDAV) { return } @@ -44,7 +44,10 @@ class CalDAVHelper(val context: Context) { fetchCalDAVCalendarEvents(calendar.id, localEventType.id!!, showToasts) } - context.scheduleCalDAVSync(true) + if (scheduleNextSync) { + context.scheduleCalDAVSync(true) + } + callback() } finally { isUpdatingCalDAV = false diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt index 296c2cb5b..40bcd9f9e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt @@ -3,6 +3,7 @@ package com.simplemobiletools.calendar.pro.helpers const val STORED_LOCALLY_ONLY = 0 const val ROW_COUNT = 6 const val COLUMN_COUNT = 7 +const val SCHEDULE_CALDAV_REQUEST_CODE = 10000 const val DAY_CODE = "day_code" const val YEAR_LABEL = "year" diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/jobs/CalDAVUpdateListener.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/jobs/CalDAVUpdateListener.kt index 123a197e6..612db100b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/jobs/CalDAVUpdateListener.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/jobs/CalDAVUpdateListener.kt @@ -51,7 +51,7 @@ class CalDAVUpdateListener : JobService() { mRunningParams = params if (params.triggeredContentAuthorities != null && params.triggeredContentUris != null) { - recheckCalDAVCalendars {} + recheckCalDAVCalendars(false) {} } mHandler.post(mWorker) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/BootCompletedReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/BootCompletedReceiver.kt index 876df8bd6..e6cd7e523 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/BootCompletedReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/BootCompletedReceiver.kt @@ -15,7 +15,7 @@ class BootCompletedReceiver : BroadcastReceiver() { context.apply { scheduleAllEvents() notifyRunningEvents() - recheckCalDAVCalendars {} + recheckCalDAVCalendars(true) {} } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/CalDAVSyncReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/CalDAVSyncReceiver.kt index b5260463a..9ad1eb9c9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/CalDAVSyncReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/CalDAVSyncReceiver.kt @@ -14,7 +14,7 @@ class CalDAVSyncReceiver : BroadcastReceiver() { context.refreshCalDAVCalendars(context.config.caldavSyncedCalendarIds, false) } - context.recheckCalDAVCalendars { + context.recheckCalDAVCalendars(true) { context.updateWidgets() } }