avoid updating the CalDAV calendar too frequently
This commit is contained in:
parent
7a335e5e1e
commit
ac8fc24906
2 changed files with 62 additions and 65 deletions
|
@ -13,8 +13,7 @@ import com.simplemobiletools.calendar.pro.extensions.*
|
|||
import com.simplemobiletools.calendar.pro.models.*
|
||||
import com.simplemobiletools.calendar.pro.objects.States.isUpdatingCalDAV
|
||||
import com.simplemobiletools.commons.extensions.*
|
||||
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CALENDAR
|
||||
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CALENDAR
|
||||
import com.simplemobiletools.commons.helpers.*
|
||||
import org.joda.time.DateTimeZone
|
||||
import org.joda.time.format.DateTimeFormat
|
||||
import java.util.*
|
||||
|
@ -43,6 +42,7 @@ class CalDAVHelper(val context: Context) {
|
|||
|
||||
fetchCalDAVCalendarEvents(calendar.id, localEventType.id!!, showToasts)
|
||||
}
|
||||
|
||||
context.scheduleCalDAVSync(true)
|
||||
callback()
|
||||
} finally {
|
||||
|
@ -83,41 +83,40 @@ class CalDAVHelper(val context: Context) {
|
|||
return calendars
|
||||
}
|
||||
|
||||
// check if the calendars color or title has changed
|
||||
fun updateCalDAVCalendar(eventType: EventType) {
|
||||
val uri = Calendars.CONTENT_URI
|
||||
val values = fillCalendarContentValues(eventType)
|
||||
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 {
|
||||
context.contentResolver.update(newUri, values, null, null)
|
||||
eventType.color = properColor
|
||||
eventType.title = displayName
|
||||
context.eventTypesDB.insertOrUpdate(eventType)
|
||||
} catch (e: IllegalArgumentException) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun fillCalendarContentValues(eventType: EventType): ContentValues {
|
||||
val colorKey = getEventTypeColorKey(eventType)
|
||||
private fun fillCalendarContentValues(colorKey: Int, title: String): ContentValues {
|
||||
return ContentValues().apply {
|
||||
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")
|
||||
fun getAvailableCalDAVCalendarColors(eventType: EventType): ArrayList<Int> {
|
||||
val colors = SparseIntArray()
|
||||
|
|
|
@ -10,9 +10,7 @@ import android.content.Context
|
|||
import android.os.Build
|
||||
import android.os.Handler
|
||||
import android.provider.CalendarContract
|
||||
import com.simplemobiletools.calendar.pro.extensions.config
|
||||
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)
|
||||
@TargetApi(Build.VERSION_CODES.N)
|
||||
|
@ -34,13 +32,13 @@ class CalDAVUpdateListener : JobService() {
|
|||
val uri = CalendarContract.Calendars.CONTENT_URI
|
||||
JobInfo.Builder(CALDAV_EVENT_CONTENT_JOB, componentName).apply {
|
||||
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 {
|
||||
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 }
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue