schedule a caldav sync for every 4 hours
This commit is contained in:
parent
e718c02275
commit
c5a45a96b9
|
@ -127,6 +127,8 @@
|
||||||
|
|
||||||
<receiver android:name=".receivers.NotificationReceiver"/>
|
<receiver android:name=".receivers.NotificationReceiver"/>
|
||||||
|
|
||||||
|
<receiver android:name=".receivers.CalDAVSyncReceiver"/>
|
||||||
|
|
||||||
<receiver android:name=".receivers.BootCompletedReceiver">
|
<receiver android:name=".receivers.BootCompletedReceiver">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.BOOT_COMPLETED"/>
|
<action android:name="android.intent.action.BOOT_COMPLETED"/>
|
||||||
|
|
|
@ -87,6 +87,8 @@ class MainActivity : SimpleActivity(), NavigationListener {
|
||||||
if (!hasCalendarPermission()) {
|
if (!hasCalendarPermission()) {
|
||||||
config.caldavSync = false
|
config.caldavSync = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
recheckCalDAVCalendars()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
|
|
|
@ -151,11 +151,7 @@ class SettingsActivity : SimpleActivity() {
|
||||||
dbHelper.insertEventType(eventType)
|
dbHelper.insertEventType(eventType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
CalDAVEventsHandler(applicationContext).refreshCalendars()
|
||||||
calendars.forEach {
|
|
||||||
val eventTypeId = dbHelper.getEventTypeIdWithTitle(it.displayName)
|
|
||||||
CalDAVEventsHandler(applicationContext).fetchCalDAVCalendarEvents(it.id, eventTypeId)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
oldCalendarIds.filter { !newCalendarIds.contains(it) }.forEach {
|
oldCalendarIds.filter { !newCalendarIds.contains(it) }.forEach {
|
||||||
|
|
|
@ -21,6 +21,7 @@ import com.simplemobiletools.calendar.activities.EventActivity
|
||||||
import com.simplemobiletools.calendar.helpers.*
|
import com.simplemobiletools.calendar.helpers.*
|
||||||
import com.simplemobiletools.calendar.helpers.Formatter
|
import com.simplemobiletools.calendar.helpers.Formatter
|
||||||
import com.simplemobiletools.calendar.models.Event
|
import com.simplemobiletools.calendar.models.Event
|
||||||
|
import com.simplemobiletools.calendar.receivers.CalDAVSyncReceiver
|
||||||
import com.simplemobiletools.calendar.receivers.NotificationReceiver
|
import com.simplemobiletools.calendar.receivers.NotificationReceiver
|
||||||
import com.simplemobiletools.calendar.services.SnoozeService
|
import com.simplemobiletools.calendar.services.SnoozeService
|
||||||
import com.simplemobiletools.commons.extensions.getContrastColor
|
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.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.config: Config get() = Config.newInstance(this)
|
||||||
|
|
||||||
val Context.dbHelper: DBHelper get() = DBHelper.newInstance(this)
|
val Context.dbHelper: DBHelper get() = DBHelper.newInstance(this)
|
||||||
|
|
|
@ -6,8 +6,10 @@ import android.content.Context
|
||||||
import android.database.Cursor
|
import android.database.Cursor
|
||||||
import android.provider.CalendarContract
|
import android.provider.CalendarContract
|
||||||
import android.provider.CalendarContract.Reminders
|
import android.provider.CalendarContract.Reminders
|
||||||
|
import com.simplemobiletools.calendar.extensions.config
|
||||||
import com.simplemobiletools.calendar.extensions.dbHelper
|
import com.simplemobiletools.calendar.extensions.dbHelper
|
||||||
import com.simplemobiletools.calendar.extensions.hasCalendarPermission
|
import com.simplemobiletools.calendar.extensions.hasCalendarPermission
|
||||||
|
import com.simplemobiletools.calendar.extensions.scheduleCalDAVSync
|
||||||
import com.simplemobiletools.calendar.models.CalDAVCalendar
|
import com.simplemobiletools.calendar.models.CalDAVCalendar
|
||||||
import com.simplemobiletools.calendar.models.Event
|
import com.simplemobiletools.calendar.models.Event
|
||||||
import com.simplemobiletools.commons.extensions.getIntValue
|
import com.simplemobiletools.commons.extensions.getIntValue
|
||||||
|
@ -16,6 +18,14 @@ import com.simplemobiletools.commons.extensions.getStringValue
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class CalDAVEventsHandler(val context: Context) {
|
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> {
|
fun getCalDAVCalendars(ids: String = ""): List<CalDAVCalendar> {
|
||||||
val calendars = ArrayList<CalDAVCalendar>()
|
val calendars = ArrayList<CalDAVCalendar>()
|
||||||
if (!context.hasCalendarPermission()) {
|
if (!context.hasCalendarPermission()) {
|
||||||
|
|
|
@ -4,6 +4,7 @@ import android.content.Context
|
||||||
import android.media.RingtoneManager
|
import android.media.RingtoneManager
|
||||||
import android.text.format.DateFormat
|
import android.text.format.DateFormat
|
||||||
import com.simplemobiletools.calendar.R
|
import com.simplemobiletools.calendar.R
|
||||||
|
import com.simplemobiletools.calendar.extensions.scheduleCalDAVSync
|
||||||
import com.simplemobiletools.commons.helpers.BaseConfig
|
import com.simplemobiletools.commons.helpers.BaseConfig
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
|
@ -72,7 +73,10 @@ class Config(context: Context) : BaseConfig(context) {
|
||||||
|
|
||||||
var caldavSync: Boolean
|
var caldavSync: Boolean
|
||||||
get() = prefs.getBoolean(CALDAV_SYNC, false)
|
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
|
var caldavSyncedCalendarIDs: String
|
||||||
get() = prefs.getString(CALDAV_SYNCED_CALENDAR_IDS, "")
|
get() = prefs.getString(CALDAV_SYNCED_CALENDAR_IDS, "")
|
||||||
|
|
|
@ -4,6 +4,7 @@ import android.content.BroadcastReceiver
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import com.simplemobiletools.calendar.extensions.notifyRunningEvents
|
import com.simplemobiletools.calendar.extensions.notifyRunningEvents
|
||||||
|
import com.simplemobiletools.calendar.extensions.recheckCalDAVCalendars
|
||||||
import com.simplemobiletools.calendar.extensions.scheduleAllEvents
|
import com.simplemobiletools.calendar.extensions.scheduleAllEvents
|
||||||
|
|
||||||
class BootCompletedReceiver : BroadcastReceiver() {
|
class BootCompletedReceiver : BroadcastReceiver() {
|
||||||
|
@ -12,6 +13,7 @@ class BootCompletedReceiver : BroadcastReceiver() {
|
||||||
context.apply {
|
context.apply {
|
||||||
scheduleAllEvents()
|
scheduleAllEvents()
|
||||||
notifyRunningEvents()
|
notifyRunningEvents()
|
||||||
|
recheckCalDAVCalendars()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue