implement the manual caldav calendar refreshing

This commit is contained in:
tibbi 2017-08-20 21:55:34 +02:00
parent d766aaed70
commit 3505567083
22 changed files with 74 additions and 10 deletions

View File

@ -1,11 +1,15 @@
package com.simplemobiletools.calendar.activities package com.simplemobiletools.calendar.activities
import android.Manifest import android.Manifest
import android.content.ContentResolver
import android.content.Intent import android.content.Intent
import android.content.pm.ActivityInfo import android.content.pm.ActivityInfo
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.database.ContentObserver
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.os.Handler
import android.provider.CalendarContract
import android.support.v4.app.ActivityCompat import android.support.v4.app.ActivityCompat
import android.support.v4.view.ViewPager import android.support.v4.view.ViewPager
import android.util.SparseIntArray import android.util.SparseIntArray
@ -45,6 +49,7 @@ import java.util.*
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
class MainActivity : SimpleActivity(), NavigationListener { class MainActivity : SimpleActivity(), NavigationListener {
private val CALDAV_SYNC_DELAY = 2000L
private val PREFILLED_MONTHS = 97 private val PREFILLED_MONTHS = 97
private val PREFILLED_YEARS = 31 private val PREFILLED_YEARS = 31
private val PREFILLED_WEEKS = 61 private val PREFILLED_WEEKS = 61
@ -59,6 +64,7 @@ class MainActivity : SimpleActivity(), NavigationListener {
private var mStoredIsSundayFirst = false private var mStoredIsSundayFirst = false
private var mStoredUse24HourFormat = false private var mStoredUse24HourFormat = false
private var mShouldFilterBeVisible = false private var mShouldFilterBeVisible = false
private var mCalDAVSyncHandler = Handler()
private var mDefaultWeeklyPage = 0 private var mDefaultWeeklyPage = 0
private var mDefaultMonthlyPage = 0 private var mDefaultMonthlyPage = 0
@ -88,7 +94,7 @@ class MainActivity : SimpleActivity(), NavigationListener {
config.caldavSync = false config.caldavSync = false
} }
recheckCalDAVCalendars() recheckCalDAVCalendars {}
} }
override fun onResume() { override fun onResume() {
@ -126,6 +132,12 @@ class MainActivity : SimpleActivity(), NavigationListener {
mStoredUse24HourFormat = config.use24hourFormat mStoredUse24HourFormat = config.use24hourFormat
} }
override fun onStop() {
super.onStop()
mCalDAVSyncHandler.removeCallbacksAndMessages(null)
contentResolver.unregisterContentObserver(calDAVSyncObserver)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_main, menu) menuInflater.inflate(R.menu.menu_main, menu)
menu.findItem(R.id.filter).isVisible = mShouldFilterBeVisible menu.findItem(R.id.filter).isVisible = mShouldFilterBeVisible
@ -214,7 +226,25 @@ class MainActivity : SimpleActivity(), NavigationListener {
} }
private fun refreshCalDAVAccounts() { private fun refreshCalDAVAccounts() {
toast(R.string.refreshing)
val uri = CalendarContract.Calendars.CONTENT_URI
contentResolver.registerContentObserver(uri, false, calDAVSyncObserver)
ContentResolver.requestSync(null, uri.authority, Bundle())
}
private val calDAVSyncObserver = object : ContentObserver(Handler()) {
override fun onChange(selfChange: Boolean) {
super.onChange(selfChange)
if (!selfChange) {
mCalDAVSyncHandler.removeCallbacksAndMessages(null)
mCalDAVSyncHandler.postDelayed({
recheckCalDAVCalendars {
refreshViewPager()
toast(R.string.refreshing_complete)
}
}, CALDAV_SYNC_DELAY)
}
}
} }
private fun addHolidays() { private fun addHolidays() {

View File

@ -143,15 +143,14 @@ class SettingsActivity : SimpleActivity() {
Thread({ Thread({
if (newCalendarIds.isNotEmpty()) { if (newCalendarIds.isNotEmpty()) {
val eventTypeNames = dbHelper.fetchEventTypes().map { it.title.toLowerCase() } as ArrayList<String> val eventTypeNames = dbHelper.fetchEventTypes().map { it.title.toLowerCase() } as ArrayList<String>
val calendars = CalDAVEventsHandler(applicationContext).getCalDAVCalendars(config.caldavSyncedCalendarIDs) getSyncedCalDAVCalendars().forEach {
calendars.forEach {
if (!eventTypeNames.contains(it.displayName.toLowerCase())) { if (!eventTypeNames.contains(it.displayName.toLowerCase())) {
val eventType = EventType(0, it.displayName, it.color, it.id) val eventType = EventType(0, it.displayName, it.color, it.id)
eventTypeNames.add(it.displayName.toLowerCase()) eventTypeNames.add(it.displayName.toLowerCase())
dbHelper.insertEventType(eventType) dbHelper.insertEventType(eventType)
} }
} }
CalDAVEventsHandler(applicationContext).refreshCalendars() CalDAVEventsHandler(applicationContext).refreshCalendars {}
} }
oldCalendarIds.filter { !newCalendarIds.contains(it) }.forEach { oldCalendarIds.filter { !newCalendarIds.contains(it) }.forEach {

View File

@ -235,9 +235,11 @@ 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() { fun Context.getSyncedCalDAVCalendars() = CalDAVEventsHandler(this).getCalDAVCalendars(config.caldavSyncedCalendarIDs)
fun Context.recheckCalDAVCalendars(callback: () -> Unit) {
if (config.caldavSync) { if (config.caldavSync) {
CalDAVEventsHandler(this).refreshCalendars() CalDAVEventsHandler(this).refreshCalendars(callback)
} }
} }

View File

@ -18,12 +18,13 @@ import com.simplemobiletools.commons.extensions.getStringValue
import java.util.* import java.util.*
class CalDAVEventsHandler(val context: Context) { class CalDAVEventsHandler(val context: Context) {
fun refreshCalendars() { fun refreshCalendars(callback: () -> Unit) {
getCalDAVCalendars(context.config.caldavSyncedCalendarIDs).forEach { getCalDAVCalendars(context.config.caldavSyncedCalendarIDs).forEach {
val eventTypeId = context.dbHelper.getEventTypeIdWithTitle(it.displayName) val eventTypeId = context.dbHelper.getEventTypeIdWithTitle(it.displayName)
CalDAVEventsHandler(context).fetchCalDAVCalendarEvents(it.id, eventTypeId) CalDAVEventsHandler(context).fetchCalDAVCalendarEvents(it.id, eventTypeId)
} }
context.scheduleCalDAVSync(true) context.scheduleCalDAVSync(true)
callback()
} }
fun getCalDAVCalendars(ids: String = ""): List<CalDAVCalendar> { fun getCalDAVCalendars(ids: String = ""): List<CalDAVCalendar> {

View File

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

View File

@ -7,6 +7,6 @@ import com.simplemobiletools.calendar.extensions.recheckCalDAVCalendars
class CalDAVSyncReceiver : BroadcastReceiver() { class CalDAVSyncReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
context.recheckCalDAVCalendars() context.recheckCalDAVCalendars {}
} }
} }

View File

@ -197,6 +197,8 @@
<string name="manage_synced_calendars">Manage synced calendars</string> <string name="manage_synced_calendars">Manage synced calendars</string>
<string name="store_locally_only">Store locally only</string> <string name="store_locally_only">Store locally only</string>
<string name="refresh_caldav_accounts">Refresh CalDAV accounts</string> <string name="refresh_caldav_accounts">Refresh CalDAV accounts</string>
<string name="refreshing">Refreshing…</string>
<string name="refreshing_complete">Refreshing complete</string>
<plurals name="by_minutes"> <plurals name="by_minutes">
<item quantity="one">%1$d minute</item> <item quantity="one">%1$d minute</item>

View File

@ -197,6 +197,8 @@
<string name="manage_synced_calendars">Manage synced calendars</string> <string name="manage_synced_calendars">Manage synced calendars</string>
<string name="store_locally_only">Store locally only</string> <string name="store_locally_only">Store locally only</string>
<string name="refresh_caldav_accounts">Refresh CalDAV accounts</string> <string name="refresh_caldav_accounts">Refresh CalDAV accounts</string>
<string name="refreshing">Refreshing…</string>
<string name="refreshing_complete">Refreshing complete</string>
<plurals name="by_minutes"> <plurals name="by_minutes">
<item quantity="one">%1$d minute</item> <item quantity="one">%1$d minute</item>

View File

@ -197,6 +197,8 @@
<string name="manage_synced_calendars">Manage synced calendars</string> <string name="manage_synced_calendars">Manage synced calendars</string>
<string name="store_locally_only">Store locally only</string> <string name="store_locally_only">Store locally only</string>
<string name="refresh_caldav_accounts">Refresh CalDAV accounts</string> <string name="refresh_caldav_accounts">Refresh CalDAV accounts</string>
<string name="refreshing">Refreshing…</string>
<string name="refreshing_complete">Refreshing complete</string>
<plurals name="by_minutes"> <plurals name="by_minutes">
<item quantity="one">%1$d minute</item> <item quantity="one">%1$d minute</item>

View File

@ -197,6 +197,8 @@
<string name="manage_synced_calendars">Manage synced calendars</string> <string name="manage_synced_calendars">Manage synced calendars</string>
<string name="store_locally_only">Store locally only</string> <string name="store_locally_only">Store locally only</string>
<string name="refresh_caldav_accounts">Refresh CalDAV accounts</string> <string name="refresh_caldav_accounts">Refresh CalDAV accounts</string>
<string name="refreshing">Refreshing…</string>
<string name="refreshing_complete">Refreshing complete</string>
<plurals name="by_minutes"> <plurals name="by_minutes">
<item quantity="one">%1$d minute</item> <item quantity="one">%1$d minute</item>

View File

@ -197,6 +197,8 @@
<string name="manage_synced_calendars">Manage synced calendars</string> <string name="manage_synced_calendars">Manage synced calendars</string>
<string name="store_locally_only">Store locally only</string> <string name="store_locally_only">Store locally only</string>
<string name="refresh_caldav_accounts">Refresh CalDAV accounts</string> <string name="refresh_caldav_accounts">Refresh CalDAV accounts</string>
<string name="refreshing">Refreshing…</string>
<string name="refreshing_complete">Refreshing complete</string>
<plurals name="by_minutes"> <plurals name="by_minutes">
<item quantity="one">%1$d minute</item> <item quantity="one">%1$d minute</item>

View File

@ -197,6 +197,8 @@
<string name="manage_synced_calendars">Manage synced calendars</string> <string name="manage_synced_calendars">Manage synced calendars</string>
<string name="store_locally_only">Store locally only</string> <string name="store_locally_only">Store locally only</string>
<string name="refresh_caldav_accounts">Refresh CalDAV accounts</string> <string name="refresh_caldav_accounts">Refresh CalDAV accounts</string>
<string name="refreshing">Refreshing…</string>
<string name="refreshing_complete">Refreshing complete</string>
<plurals name="by_minutes"> <plurals name="by_minutes">
<item quantity="one">%1$d minute</item> <item quantity="one">%1$d minute</item>

View File

@ -197,6 +197,8 @@
<string name="manage_synced_calendars">Manage synced calendars</string> <string name="manage_synced_calendars">Manage synced calendars</string>
<string name="store_locally_only">Store locally only</string> <string name="store_locally_only">Store locally only</string>
<string name="refresh_caldav_accounts">Refresh CalDAV accounts</string> <string name="refresh_caldav_accounts">Refresh CalDAV accounts</string>
<string name="refreshing">Refreshing…</string>
<string name="refreshing_complete">Refreshing complete</string>
<plurals name="by_minutes"> <plurals name="by_minutes">
<item quantity="one">%1$d minute</item> <item quantity="one">%1$d minute</item>

View File

@ -197,6 +197,8 @@
<string name="manage_synced_calendars">Manage synced calendars</string> <string name="manage_synced_calendars">Manage synced calendars</string>
<string name="store_locally_only">Store locally only</string> <string name="store_locally_only">Store locally only</string>
<string name="refresh_caldav_accounts">Refresh CalDAV accounts</string> <string name="refresh_caldav_accounts">Refresh CalDAV accounts</string>
<string name="refreshing">Refreshing…</string>
<string name="refreshing_complete">Refreshing complete</string>
<plurals name="by_minutes"> <plurals name="by_minutes">
<item quantity="one">%1$d minute</item> <item quantity="one">%1$d minute</item>

View File

@ -202,6 +202,8 @@
<string name="manage_synced_calendars">Manage synced calendars</string> <string name="manage_synced_calendars">Manage synced calendars</string>
<string name="store_locally_only">Store locally only</string> <string name="store_locally_only">Store locally only</string>
<string name="refresh_caldav_accounts">Refresh CalDAV accounts</string> <string name="refresh_caldav_accounts">Refresh CalDAV accounts</string>
<string name="refreshing">Refreshing…</string>
<string name="refreshing_complete">Refreshing complete</string>
<plurals name="by_minutes"> <plurals name="by_minutes">
<item quantity="one">%1$d minutę</item> <item quantity="one">%1$d minutę</item>

View File

@ -197,6 +197,8 @@
<string name="manage_synced_calendars">Manage synced calendars</string> <string name="manage_synced_calendars">Manage synced calendars</string>
<string name="store_locally_only">Store locally only</string> <string name="store_locally_only">Store locally only</string>
<string name="refresh_caldav_accounts">Refresh CalDAV accounts</string> <string name="refresh_caldav_accounts">Refresh CalDAV accounts</string>
<string name="refreshing">Refreshing…</string>
<string name="refreshing_complete">Refreshing complete</string>
<plurals name="by_minutes"> <plurals name="by_minutes">
<item quantity="one">%1$d minute</item> <item quantity="one">%1$d minute</item>

View File

@ -197,6 +197,8 @@
<string name="manage_synced_calendars">Gerir calendários sincronizados</string> <string name="manage_synced_calendars">Gerir calendários sincronizados</string>
<string name="store_locally_only">Store locally only</string> <string name="store_locally_only">Store locally only</string>
<string name="refresh_caldav_accounts">Refresh CalDAV accounts</string> <string name="refresh_caldav_accounts">Refresh CalDAV accounts</string>
<string name="refreshing">Refreshing…</string>
<string name="refreshing_complete">Refreshing complete</string>
<plurals name="by_minutes"> <plurals name="by_minutes">
<item quantity="one">%1$d minuto</item> <item quantity="one">%1$d minuto</item>

View File

@ -211,6 +211,8 @@
<string name="manage_synced_calendars">Manage synced calendars</string> <string name="manage_synced_calendars">Manage synced calendars</string>
<string name="store_locally_only">Store locally only</string> <string name="store_locally_only">Store locally only</string>
<string name="refresh_caldav_accounts">Refresh CalDAV accounts</string> <string name="refresh_caldav_accounts">Refresh CalDAV accounts</string>
<string name="refreshing">Refreshing…</string>
<string name="refreshing_complete">Refreshing complete</string>
<plurals name="by_minutes"> <plurals name="by_minutes">
<item quantity="one">%1$d минута</item> <item quantity="one">%1$d минута</item>

View File

@ -203,7 +203,9 @@
<string name="select_caldav_calendars">Zvoľte kalendáre pre synchronizáciu</string> <string name="select_caldav_calendars">Zvoľte kalendáre pre synchronizáciu</string>
<string name="manage_synced_calendars">Spravovať synchronizované kalendáre</string> <string name="manage_synced_calendars">Spravovať synchronizované kalendáre</string>
<string name="store_locally_only">Uložiť iba lokálne</string> <string name="store_locally_only">Uložiť iba lokálne</string>
<string name="refresh_caldav_accounts">Obnoviť CalDAV účty Refresh CalDAV accounts</string> <string name="refresh_caldav_accounts">Obnoviť CalDAV účty</string>
<string name="refreshing">Obnovuje sa…</string>
<string name="refreshing_complete">Obnovovanie ukončené</string>
<plurals name="by_minutes"> <plurals name="by_minutes">
<item quantity="one">%1$d minútu</item> <item quantity="one">%1$d minútu</item>

View File

@ -197,6 +197,8 @@
<string name="manage_synced_calendars">Manage synced calendars</string> <string name="manage_synced_calendars">Manage synced calendars</string>
<string name="store_locally_only">Store locally only</string> <string name="store_locally_only">Store locally only</string>
<string name="refresh_caldav_accounts">Refresh CalDAV accounts</string> <string name="refresh_caldav_accounts">Refresh CalDAV accounts</string>
<string name="refreshing">Refreshing…</string>
<string name="refreshing_complete">Refreshing complete</string>
<plurals name="by_minutes"> <plurals name="by_minutes">
<item quantity="one">%1$d minut</item> <item quantity="one">%1$d minut</item>

View File

@ -197,6 +197,8 @@
<string name="manage_synced_calendars">Manage synced calendars</string> <string name="manage_synced_calendars">Manage synced calendars</string>
<string name="store_locally_only">Store locally only</string> <string name="store_locally_only">Store locally only</string>
<string name="refresh_caldav_accounts">Refresh CalDAV accounts</string> <string name="refresh_caldav_accounts">Refresh CalDAV accounts</string>
<string name="refreshing">Refreshing…</string>
<string name="refreshing_complete">Refreshing complete</string>
<plurals name="by_minutes"> <plurals name="by_minutes">
<item quantity="one">%1$d minute</item> <item quantity="one">%1$d minute</item>

View File

@ -197,6 +197,8 @@
<string name="manage_synced_calendars">Manage synced calendars</string> <string name="manage_synced_calendars">Manage synced calendars</string>
<string name="store_locally_only">Store locally only</string> <string name="store_locally_only">Store locally only</string>
<string name="refresh_caldav_accounts">Refresh CalDAV accounts</string> <string name="refresh_caldav_accounts">Refresh CalDAV accounts</string>
<string name="refreshing">Refreshing…</string>
<string name="refreshing_complete">Refreshing complete</string>
<plurals name="by_minutes"> <plurals name="by_minutes">
<item quantity="one">%1$d minute</item> <item quantity="one">%1$d minute</item>