mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-06-05 21:59:17 +02:00
avoid updating the CalDAV calendar too frequently
This commit is contained in:
@@ -13,8 +13,7 @@ import com.simplemobiletools.calendar.pro.extensions.*
|
|||||||
import com.simplemobiletools.calendar.pro.models.*
|
import com.simplemobiletools.calendar.pro.models.*
|
||||||
import com.simplemobiletools.calendar.pro.objects.States.isUpdatingCalDAV
|
import com.simplemobiletools.calendar.pro.objects.States.isUpdatingCalDAV
|
||||||
import com.simplemobiletools.commons.extensions.*
|
import com.simplemobiletools.commons.extensions.*
|
||||||
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CALENDAR
|
import com.simplemobiletools.commons.helpers.*
|
||||||
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CALENDAR
|
|
||||||
import org.joda.time.DateTimeZone
|
import org.joda.time.DateTimeZone
|
||||||
import org.joda.time.format.DateTimeFormat
|
import org.joda.time.format.DateTimeFormat
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@@ -43,6 +42,7 @@ class CalDAVHelper(val context: Context) {
|
|||||||
|
|
||||||
fetchCalDAVCalendarEvents(calendar.id, localEventType.id!!, showToasts)
|
fetchCalDAVCalendarEvents(calendar.id, localEventType.id!!, showToasts)
|
||||||
}
|
}
|
||||||
|
|
||||||
context.scheduleCalDAVSync(true)
|
context.scheduleCalDAVSync(true)
|
||||||
callback()
|
callback()
|
||||||
} finally {
|
} finally {
|
||||||
@@ -59,13 +59,13 @@ class CalDAVHelper(val context: Context) {
|
|||||||
|
|
||||||
val uri = Calendars.CONTENT_URI
|
val uri = Calendars.CONTENT_URI
|
||||||
val projection = arrayOf(
|
val projection = arrayOf(
|
||||||
Calendars._ID,
|
Calendars._ID,
|
||||||
Calendars.CALENDAR_DISPLAY_NAME,
|
Calendars.CALENDAR_DISPLAY_NAME,
|
||||||
Calendars.ACCOUNT_NAME,
|
Calendars.ACCOUNT_NAME,
|
||||||
Calendars.ACCOUNT_TYPE,
|
Calendars.ACCOUNT_TYPE,
|
||||||
Calendars.OWNER_ACCOUNT,
|
Calendars.OWNER_ACCOUNT,
|
||||||
Calendars.CALENDAR_COLOR,
|
Calendars.CALENDAR_COLOR,
|
||||||
Calendars.CALENDAR_ACCESS_LEVEL)
|
Calendars.CALENDAR_ACCESS_LEVEL)
|
||||||
|
|
||||||
val selection = if (ids.trim().isNotEmpty()) "${Calendars._ID} IN ($ids)" else null
|
val selection = if (ids.trim().isNotEmpty()) "${Calendars._ID} IN ($ids)" else null
|
||||||
context.queryCursor(uri, projection, selection, showErrors = showToasts) { cursor ->
|
context.queryCursor(uri, projection, selection, showErrors = showToasts) { cursor ->
|
||||||
@@ -83,39 +83,38 @@ class CalDAVHelper(val context: Context) {
|
|||||||
return calendars
|
return calendars
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if the calendars color or title has changed
|
||||||
fun updateCalDAVCalendar(eventType: EventType) {
|
fun updateCalDAVCalendar(eventType: EventType) {
|
||||||
val uri = Calendars.CONTENT_URI
|
val uri = Calendars.CONTENT_URI
|
||||||
val values = fillCalendarContentValues(eventType)
|
|
||||||
val newUri = ContentUris.withAppendedId(uri, eventType.caldavCalendarId.toLong())
|
val newUri = ContentUris.withAppendedId(uri, eventType.caldavCalendarId.toLong())
|
||||||
try {
|
val projection = arrayOf(
|
||||||
context.contentResolver.update(newUri, values, null, null)
|
Calendars.CALENDAR_COLOR_KEY,
|
||||||
} catch (e: IllegalArgumentException) {
|
Calendars.CALENDAR_COLOR,
|
||||||
}
|
Calendars.CALENDAR_DISPLAY_NAME
|
||||||
}
|
)
|
||||||
|
|
||||||
private fun fillCalendarContentValues(eventType: EventType): ContentValues {
|
context.queryCursor(newUri, projection) { cursor ->
|
||||||
val colorKey = getEventTypeColorKey(eventType)
|
val properColorKey = cursor.getIntValue(Calendars.CALENDAR_COLOR_KEY)
|
||||||
return ContentValues().apply {
|
val properColor = cursor.getIntValue(Calendars.CALENDAR_COLOR)
|
||||||
put(Calendars.CALENDAR_COLOR_KEY, colorKey)
|
val displayName = cursor.getStringValue(Calendars.CALENDAR_DISPLAY_NAME)
|
||||||
put(Calendars.CALENDAR_DISPLAY_NAME, eventType.title)
|
if (eventType.color != properColor || displayName != eventType.title) {
|
||||||
}
|
val values = fillCalendarContentValues(properColorKey, displayName)
|
||||||
}
|
try {
|
||||||
|
context.contentResolver.update(newUri, values, null, null)
|
||||||
@SuppressLint("MissingPermission")
|
eventType.color = properColor
|
||||||
private fun getEventTypeColorKey(eventType: EventType): Int {
|
eventType.title = displayName
|
||||||
val uri = Colors.CONTENT_URI
|
context.eventTypesDB.insertOrUpdate(eventType)
|
||||||
val projection = arrayOf(Colors.COLOR_KEY)
|
} catch (e: IllegalArgumentException) {
|
||||||
val selection = "${Colors.COLOR_TYPE} = ? AND ${Colors.COLOR} = ? AND ${Colors.ACCOUNT_NAME} = ?"
|
}
|
||||||
val selectionArgs = arrayOf(Colors.TYPE_CALENDAR.toString(), eventType.color.toString(), eventType.caldavEmail)
|
|
||||||
|
|
||||||
val cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
|
|
||||||
cursor?.use {
|
|
||||||
if (cursor.moveToFirst()) {
|
|
||||||
return cursor.getStringValue(Colors.COLOR_KEY).toInt()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return -1
|
private fun fillCalendarContentValues(colorKey: Int, title: String): ContentValues {
|
||||||
|
return ContentValues().apply {
|
||||||
|
put(Calendars.CALENDAR_COLOR_KEY, colorKey)
|
||||||
|
put(Calendars.CALENDAR_DISPLAY_NAME, title)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("MissingPermission")
|
@SuppressLint("MissingPermission")
|
||||||
@@ -157,21 +156,21 @@ class CalDAVHelper(val context: Context) {
|
|||||||
|
|
||||||
val uri = Events.CONTENT_URI
|
val uri = Events.CONTENT_URI
|
||||||
val projection = arrayOf(
|
val projection = arrayOf(
|
||||||
Events._ID,
|
Events._ID,
|
||||||
Events.TITLE,
|
Events.TITLE,
|
||||||
Events.DESCRIPTION,
|
Events.DESCRIPTION,
|
||||||
Events.DTSTART,
|
Events.DTSTART,
|
||||||
Events.DTEND,
|
Events.DTEND,
|
||||||
Events.DURATION,
|
Events.DURATION,
|
||||||
Events.EXDATE,
|
Events.EXDATE,
|
||||||
Events.ALL_DAY,
|
Events.ALL_DAY,
|
||||||
Events.RRULE,
|
Events.RRULE,
|
||||||
Events.ORIGINAL_ID,
|
Events.ORIGINAL_ID,
|
||||||
Events.ORIGINAL_INSTANCE_TIME,
|
Events.ORIGINAL_INSTANCE_TIME,
|
||||||
Events.EVENT_LOCATION,
|
Events.EVENT_LOCATION,
|
||||||
Events.EVENT_TIMEZONE,
|
Events.EVENT_TIMEZONE,
|
||||||
Events.CALENDAR_TIME_ZONE,
|
Events.CALENDAR_TIME_ZONE,
|
||||||
Events.DELETED)
|
Events.DELETED)
|
||||||
|
|
||||||
val selection = "${Events.CALENDAR_ID} = $calendarId"
|
val selection = "${Events.CALENDAR_ID} = $calendarId"
|
||||||
context.queryCursor(uri, projection, selection, showErrors = showToasts) { cursor ->
|
context.queryCursor(uri, projection, selection, showErrors = showToasts) { cursor ->
|
||||||
@@ -203,15 +202,15 @@ class CalDAVHelper(val context: Context) {
|
|||||||
val reminder3 = reminders.getOrNull(2)
|
val reminder3 = reminders.getOrNull(2)
|
||||||
val importId = getCalDAVEventImportId(calendarId, id)
|
val importId = getCalDAVEventImportId(calendarId, id)
|
||||||
val eventTimeZone = cursor.getStringValue(Events.EVENT_TIMEZONE)
|
val eventTimeZone = cursor.getStringValue(Events.EVENT_TIMEZONE)
|
||||||
?: cursor.getStringValue(Events.CALENDAR_TIME_ZONE) ?: DateTimeZone.getDefault().id
|
?: cursor.getStringValue(Events.CALENDAR_TIME_ZONE) ?: DateTimeZone.getDefault().id
|
||||||
|
|
||||||
val source = "$CALDAV-$calendarId"
|
val source = "$CALDAV-$calendarId"
|
||||||
val repeatRule = Parser().parseRepeatInterval(rrule, startTS)
|
val repeatRule = Parser().parseRepeatInterval(rrule, startTS)
|
||||||
val event = Event(null, startTS, endTS, title, location, description, reminder1?.minutes ?: REMINDER_OFF,
|
val event = Event(null, startTS, endTS, title, location, description, reminder1?.minutes ?: REMINDER_OFF,
|
||||||
reminder2?.minutes ?: REMINDER_OFF, reminder3?.minutes ?: REMINDER_OFF, reminder1?.type
|
reminder2?.minutes ?: REMINDER_OFF, reminder3?.minutes ?: REMINDER_OFF, reminder1?.type
|
||||||
?: REMINDER_NOTIFICATION, reminder2?.type ?: REMINDER_NOTIFICATION, reminder3?.type
|
?: REMINDER_NOTIFICATION, reminder2?.type ?: REMINDER_NOTIFICATION, reminder3?.type
|
||||||
?: REMINDER_NOTIFICATION, repeatRule.repeatInterval, repeatRule.repeatRule,
|
?: REMINDER_NOTIFICATION, repeatRule.repeatInterval, repeatRule.repeatRule,
|
||||||
repeatRule.repeatLimit, ArrayList(), attendees, importId, eventTimeZone, allDay, eventTypeId, source = source)
|
repeatRule.repeatLimit, ArrayList(), attendees, importId, eventTimeZone, allDay, eventTypeId, source = source)
|
||||||
|
|
||||||
if (event.getIsAllDay()) {
|
if (event.getIsAllDay()) {
|
||||||
event.startTS = Formatter.getShiftedImportTimestamp(event.startTS)
|
event.startTS = Formatter.getShiftedImportTimestamp(event.startTS)
|
||||||
@@ -469,8 +468,8 @@ class CalDAVHelper(val context: Context) {
|
|||||||
val reminders = ArrayList<Reminder>()
|
val reminders = ArrayList<Reminder>()
|
||||||
val uri = Reminders.CONTENT_URI
|
val uri = Reminders.CONTENT_URI
|
||||||
val projection = arrayOf(
|
val projection = arrayOf(
|
||||||
Reminders.MINUTES,
|
Reminders.MINUTES,
|
||||||
Reminders.METHOD)
|
Reminders.METHOD)
|
||||||
val selection = "${Reminders.EVENT_ID} = $eventId"
|
val selection = "${Reminders.EVENT_ID} = $eventId"
|
||||||
|
|
||||||
context.queryCursor(uri, projection, selection) { cursor ->
|
context.queryCursor(uri, projection, selection) { cursor ->
|
||||||
@@ -490,10 +489,10 @@ class CalDAVHelper(val context: Context) {
|
|||||||
val attendees = ArrayList<Attendee>()
|
val attendees = ArrayList<Attendee>()
|
||||||
val uri = Attendees.CONTENT_URI
|
val uri = Attendees.CONTENT_URI
|
||||||
val projection = arrayOf(
|
val projection = arrayOf(
|
||||||
Attendees.ATTENDEE_NAME,
|
Attendees.ATTENDEE_NAME,
|
||||||
Attendees.ATTENDEE_EMAIL,
|
Attendees.ATTENDEE_EMAIL,
|
||||||
Attendees.ATTENDEE_STATUS,
|
Attendees.ATTENDEE_STATUS,
|
||||||
Attendees.ATTENDEE_RELATIONSHIP)
|
Attendees.ATTENDEE_RELATIONSHIP)
|
||||||
val selection = "${Attendees.EVENT_ID} = $eventId"
|
val selection = "${Attendees.EVENT_ID} = $eventId"
|
||||||
context.queryCursor(uri, projection, selection) { cursor ->
|
context.queryCursor(uri, projection, selection) { cursor ->
|
||||||
val name = cursor.getStringValue(Attendees.ATTENDEE_NAME) ?: ""
|
val name = cursor.getStringValue(Attendees.ATTENDEE_NAME) ?: ""
|
||||||
|
@@ -10,9 +10,7 @@ import android.content.Context
|
|||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.provider.CalendarContract
|
import android.provider.CalendarContract
|
||||||
import com.simplemobiletools.calendar.pro.extensions.config
|
|
||||||
import com.simplemobiletools.calendar.pro.extensions.recheckCalDAVCalendars
|
import com.simplemobiletools.calendar.pro.extensions.recheckCalDAVCalendars
|
||||||
import com.simplemobiletools.calendar.pro.extensions.refreshCalDAVCalendars
|
|
||||||
|
|
||||||
// based on https://developer.android.com/reference/android/app/job/JobInfo.Builder.html#addTriggerContentUri(android.app.job.JobInfo.TriggerContentUri)
|
// based on https://developer.android.com/reference/android/app/job/JobInfo.Builder.html#addTriggerContentUri(android.app.job.JobInfo.TriggerContentUri)
|
||||||
@TargetApi(Build.VERSION_CODES.N)
|
@TargetApi(Build.VERSION_CODES.N)
|
||||||
@@ -34,13 +32,13 @@ class CalDAVUpdateListener : JobService() {
|
|||||||
val uri = CalendarContract.Calendars.CONTENT_URI
|
val uri = CalendarContract.Calendars.CONTENT_URI
|
||||||
JobInfo.Builder(CALDAV_EVENT_CONTENT_JOB, componentName).apply {
|
JobInfo.Builder(CALDAV_EVENT_CONTENT_JOB, componentName).apply {
|
||||||
addTriggerContentUri(JobInfo.TriggerContentUri(uri, JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS))
|
addTriggerContentUri(JobInfo.TriggerContentUri(uri, JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS))
|
||||||
context.getSystemService(JobScheduler::class.java).schedule(build())
|
(context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler).schedule(build())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isScheduled(context: Context): Boolean {
|
fun isScheduled(context: Context): Boolean {
|
||||||
val jobScheduler = context.getSystemService(JobScheduler::class.java)
|
val jobScheduler = context.getSystemService(JobScheduler::class.java)
|
||||||
val jobs = jobScheduler.allPendingJobs ?: return false
|
val jobs = jobScheduler.allPendingJobs
|
||||||
return jobs.any { it.id == CALDAV_EVENT_CONTENT_JOB }
|
return jobs.any { it.id == CALDAV_EVENT_CONTENT_JOB }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user