avoid updating the CalDAV calendar too frequently

This commit is contained in:
tibbi
2021-02-23 14:23:40 +01:00
parent 7a335e5e1e
commit ac8fc24906
2 changed files with 62 additions and 65 deletions

View File

@@ -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 {
@@ -83,41 +83,40 @@ 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())
val projection = arrayOf(
Calendars.CALENDAR_COLOR_KEY,
Calendars.CALENDAR_COLOR,
Calendars.CALENDAR_DISPLAY_NAME
)
context.queryCursor(newUri, projection) { cursor ->
val properColorKey = cursor.getIntValue(Calendars.CALENDAR_COLOR_KEY)
val properColor = cursor.getIntValue(Calendars.CALENDAR_COLOR)
val displayName = cursor.getStringValue(Calendars.CALENDAR_DISPLAY_NAME)
if (eventType.color != properColor || displayName != eventType.title) {
val values = fillCalendarContentValues(properColorKey, displayName)
try { try {
context.contentResolver.update(newUri, values, null, null) context.contentResolver.update(newUri, values, null, null)
eventType.color = properColor
eventType.title = displayName
context.eventTypesDB.insertOrUpdate(eventType)
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
} }
} }
}
}
private fun fillCalendarContentValues(eventType: EventType): ContentValues { private fun fillCalendarContentValues(colorKey: Int, title: String): ContentValues {
val colorKey = getEventTypeColorKey(eventType)
return ContentValues().apply { return ContentValues().apply {
put(Calendars.CALENDAR_COLOR_KEY, colorKey) put(Calendars.CALENDAR_COLOR_KEY, colorKey)
put(Calendars.CALENDAR_DISPLAY_NAME, eventType.title) put(Calendars.CALENDAR_DISPLAY_NAME, title)
} }
} }
@SuppressLint("MissingPermission")
private fun getEventTypeColorKey(eventType: EventType): Int {
val uri = Colors.CONTENT_URI
val projection = arrayOf(Colors.COLOR_KEY)
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
}
@SuppressLint("MissingPermission") @SuppressLint("MissingPermission")
fun getAvailableCalDAVCalendarColors(eventType: EventType): ArrayList<Int> { fun getAvailableCalDAVCalendarColors(eventType: EventType): ArrayList<Int> {
val colors = SparseIntArray() val colors = SparseIntArray()

View File

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