schedule a caldav sync for every 4 hours

This commit is contained in:
tibbi 2017-08-20 19:05:29 +02:00
parent e718c02275
commit c5a45a96b9
8 changed files with 54 additions and 6 deletions

View file

@ -127,6 +127,8 @@
<receiver android:name=".receivers.NotificationReceiver"/>
<receiver android:name=".receivers.CalDAVSyncReceiver"/>
<receiver android:name=".receivers.BootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>

View file

@ -87,6 +87,8 @@ class MainActivity : SimpleActivity(), NavigationListener {
if (!hasCalendarPermission()) {
config.caldavSync = false
}
recheckCalDAVCalendars()
}
override fun onResume() {

View file

@ -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 {

View file

@ -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)

View file

@ -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<CalDAVCalendar> {
val calendars = ArrayList<CalDAVCalendar>()
if (!context.hasCalendarPermission()) {

View file

@ -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, "")

View file

@ -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()
}
}
}

View file

@ -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()
}
}