diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bf3c1014c..7801a2420 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -127,6 +127,8 @@ + + diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt index a98f0abb6..708c0e650 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt @@ -87,6 +87,8 @@ class MainActivity : SimpleActivity(), NavigationListener { if (!hasCalendarPermission()) { config.caldavSync = false } + + recheckCalDAVCalendars() } override fun onResume() { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt index c9841a712..9dbd4ac9c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt @@ -151,11 +151,7 @@ class SettingsActivity : SimpleActivity() { dbHelper.insertEventType(eventType) } } - - calendars.forEach { - val eventTypeId = dbHelper.getEventTypeIdWithTitle(it.displayName) - CalDAVEventsHandler(applicationContext).fetchCalDAVCalendarEvents(it.id, eventTypeId) - } + CalDAVEventsHandler(applicationContext).refreshCalendars() } oldCalendarIds.filter { !newCalendarIds.contains(it) }.forEach { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt index fdf1ff621..b26626d35 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt @@ -21,6 +21,7 @@ import com.simplemobiletools.calendar.activities.EventActivity import com.simplemobiletools.calendar.helpers.* import com.simplemobiletools.calendar.helpers.Formatter import com.simplemobiletools.calendar.models.Event +import com.simplemobiletools.calendar.receivers.CalDAVSyncReceiver import com.simplemobiletools.calendar.receivers.NotificationReceiver import com.simplemobiletools.calendar.services.SnoozeService import com.simplemobiletools.commons.extensions.getContrastColor @@ -234,6 +235,25 @@ fun Context.getNewEventTimestampFromCode(dayCode: String) = Formatter.getLocalDa fun Context.getCurrentOffset() = SimpleDateFormat("Z", Locale.getDefault()).format(Date()) +fun Context.recheckCalDAVCalendars() { + if (config.caldavSync) { + CalDAVEventsHandler(this).refreshCalendars() + } +} + +fun Context.scheduleCalDAVSync(activate: Boolean) { + val syncIntent = Intent(this, CalDAVSyncReceiver::class.java) + val pendingIntent = PendingIntent.getBroadcast(this, 0, syncIntent, PendingIntent.FLAG_CANCEL_CURRENT) + val alarm = getSystemService(Context.ALARM_SERVICE) as AlarmManager + + if (activate) { + val syncCheckInterval = 4 * AlarmManager.INTERVAL_HOUR + alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + syncCheckInterval, syncCheckInterval, pendingIntent) + } else { + alarm.cancel(pendingIntent) + } +} + val Context.config: Config get() = Config.newInstance(this) val Context.dbHelper: DBHelper get() = DBHelper.newInstance(this) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVEventsHandler.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVEventsHandler.kt index 55287c3a2..8cd65a66d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVEventsHandler.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVEventsHandler.kt @@ -6,8 +6,10 @@ import android.content.Context import android.database.Cursor import android.provider.CalendarContract import android.provider.CalendarContract.Reminders +import com.simplemobiletools.calendar.extensions.config import com.simplemobiletools.calendar.extensions.dbHelper import com.simplemobiletools.calendar.extensions.hasCalendarPermission +import com.simplemobiletools.calendar.extensions.scheduleCalDAVSync import com.simplemobiletools.calendar.models.CalDAVCalendar import com.simplemobiletools.calendar.models.Event import com.simplemobiletools.commons.extensions.getIntValue @@ -16,6 +18,14 @@ import com.simplemobiletools.commons.extensions.getStringValue import java.util.* class CalDAVEventsHandler(val context: Context) { + fun refreshCalendars() { + getCalDAVCalendars(context.config.caldavSyncedCalendarIDs).forEach { + val eventTypeId = context.dbHelper.getEventTypeIdWithTitle(it.displayName) + CalDAVEventsHandler(context).fetchCalDAVCalendarEvents(it.id, eventTypeId) + } + context.scheduleCalDAVSync(true) + } + fun getCalDAVCalendars(ids: String = ""): List { val calendars = ArrayList() if (!context.hasCalendarPermission()) { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt index ed1d1647a..0418c8f2f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt @@ -4,6 +4,7 @@ import android.content.Context import android.media.RingtoneManager import android.text.format.DateFormat import com.simplemobiletools.calendar.R +import com.simplemobiletools.calendar.extensions.scheduleCalDAVSync import com.simplemobiletools.commons.helpers.BaseConfig import java.util.* @@ -72,7 +73,10 @@ class Config(context: Context) : BaseConfig(context) { var caldavSync: Boolean get() = prefs.getBoolean(CALDAV_SYNC, false) - set(caldavSync) = prefs.edit().putBoolean(CALDAV_SYNC, caldavSync).apply() + set(caldavSync) { + context.scheduleCalDAVSync(caldavSync) + prefs.edit().putBoolean(CALDAV_SYNC, caldavSync).apply() + } var caldavSyncedCalendarIDs: String get() = prefs.getString(CALDAV_SYNCED_CALENDAR_IDS, "") diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/BootCompletedReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/BootCompletedReceiver.kt index fd48ea442..8bdeeebce 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/BootCompletedReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/BootCompletedReceiver.kt @@ -4,6 +4,7 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import com.simplemobiletools.calendar.extensions.notifyRunningEvents +import com.simplemobiletools.calendar.extensions.recheckCalDAVCalendars import com.simplemobiletools.calendar.extensions.scheduleAllEvents class BootCompletedReceiver : BroadcastReceiver() { @@ -12,6 +13,7 @@ class BootCompletedReceiver : BroadcastReceiver() { context.apply { scheduleAllEvents() notifyRunningEvents() + recheckCalDAVCalendars() } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/CalDAVSyncReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/CalDAVSyncReceiver.kt new file mode 100644 index 000000000..0b3322fed --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/CalDAVSyncReceiver.kt @@ -0,0 +1,12 @@ +package com.simplemobiletools.calendar.receivers + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import com.simplemobiletools.calendar.extensions.recheckCalDAVCalendars + +class CalDAVSyncReceiver : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + context.recheckCalDAVCalendars() + } +}