tweak the way caldav sync is handled

This commit is contained in:
tibbi
2021-12-29 15:58:18 +01:00
parent 4052d8de7e
commit 95ce2f9858
8 changed files with 15 additions and 12 deletions

View File

@ -493,7 +493,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
} }
syncCalDAVCalendars { syncCalDAVCalendars {
calDAVHelper.refreshCalendars(true) { calDAVHelper.refreshCalendars(true, true) {
calDAVChanged() calDAVChanged()
} }
} }

View File

@ -318,7 +318,7 @@ class SettingsActivity : SimpleActivity() {
} }
syncCalDAVCalendars { syncCalDAVCalendars {
calDAVHelper.refreshCalendars(true) { calDAVHelper.refreshCalendars(true, true) {
if (settings_caldav_sync.isChecked) { if (settings_caldav_sync.isChecked) {
toast(R.string.synchronization_completed) toast(R.string.synchronization_completed)
} }

View File

@ -395,10 +395,10 @@ fun Context.getNewEventTimestampFromCode(dayCode: String, allowChangingDay: Bool
fun Context.getSyncedCalDAVCalendars() = calDAVHelper.getCalDAVCalendars(config.caldavSyncedCalendarIds, false) fun Context.getSyncedCalDAVCalendars() = calDAVHelper.getCalDAVCalendars(config.caldavSyncedCalendarIds, false)
fun Context.recheckCalDAVCalendars(callback: () -> Unit) { fun Context.recheckCalDAVCalendars(scheduleNextCalDAVSync: Boolean, callback: () -> Unit) {
if (config.caldavSync) { if (config.caldavSync) {
ensureBackgroundThread { ensureBackgroundThread {
calDAVHelper.refreshCalendars(false, callback) calDAVHelper.refreshCalendars(false, scheduleNextCalDAVSync, callback)
updateWidgets() updateWidgets()
} }
} }
@ -406,8 +406,9 @@ fun Context.recheckCalDAVCalendars(callback: () -> Unit) {
fun Context.scheduleCalDAVSync(activate: Boolean) { fun Context.scheduleCalDAVSync(activate: Boolean) {
val syncIntent = Intent(applicationContext, CalDAVSyncReceiver::class.java) 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 val alarm = getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarm.cancel(pendingIntent)
if (activate) { if (activate) {
val syncCheckInterval = 2 * AlarmManager.INTERVAL_HOUR 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) alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + syncCheckInterval, syncCheckInterval, pendingIntent)
} catch (ignored: Exception) { } catch (ignored: Exception) {
} }
} else {
alarm.cancel(pendingIntent)
} }
} }

View File

@ -23,7 +23,7 @@ import org.joda.time.format.DateTimeFormat
class CalDAVHelper(val context: Context) { class CalDAVHelper(val context: Context) {
private val eventsHelper = context.eventsHelper private val eventsHelper = context.eventsHelper
fun refreshCalendars(showToasts: Boolean, callback: () -> Unit) { fun refreshCalendars(showToasts: Boolean, scheduleNextSync: Boolean, callback: () -> Unit) {
if (isUpdatingCalDAV) { if (isUpdatingCalDAV) {
return return
} }
@ -44,7 +44,10 @@ class CalDAVHelper(val context: Context) {
fetchCalDAVCalendarEvents(calendar.id, localEventType.id!!, showToasts) fetchCalDAVCalendarEvents(calendar.id, localEventType.id!!, showToasts)
} }
if (scheduleNextSync) {
context.scheduleCalDAVSync(true) context.scheduleCalDAVSync(true)
}
callback() callback()
} finally { } finally {
isUpdatingCalDAV = false isUpdatingCalDAV = false

View File

@ -3,6 +3,7 @@ package com.simplemobiletools.calendar.pro.helpers
const val STORED_LOCALLY_ONLY = 0 const val STORED_LOCALLY_ONLY = 0
const val ROW_COUNT = 6 const val ROW_COUNT = 6
const val COLUMN_COUNT = 7 const val COLUMN_COUNT = 7
const val SCHEDULE_CALDAV_REQUEST_CODE = 10000
const val DAY_CODE = "day_code" const val DAY_CODE = "day_code"
const val YEAR_LABEL = "year" const val YEAR_LABEL = "year"

View File

@ -51,7 +51,7 @@ class CalDAVUpdateListener : JobService() {
mRunningParams = params mRunningParams = params
if (params.triggeredContentAuthorities != null && params.triggeredContentUris != null) { if (params.triggeredContentAuthorities != null && params.triggeredContentUris != null) {
recheckCalDAVCalendars {} recheckCalDAVCalendars(false) {}
} }
mHandler.post(mWorker) mHandler.post(mWorker)

View File

@ -15,7 +15,7 @@ class BootCompletedReceiver : BroadcastReceiver() {
context.apply { context.apply {
scheduleAllEvents() scheduleAllEvents()
notifyRunningEvents() notifyRunningEvents()
recheckCalDAVCalendars {} recheckCalDAVCalendars(true) {}
} }
} }
} }

View File

@ -14,7 +14,7 @@ class CalDAVSyncReceiver : BroadcastReceiver() {
context.refreshCalDAVCalendars(context.config.caldavSyncedCalendarIds, false) context.refreshCalDAVCalendars(context.config.caldavSyncedCalendarIds, false)
} }
context.recheckCalDAVCalendars { context.recheckCalDAVCalendars(true) {
context.updateWidgets() context.updateWidgets()
} }
} }