mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-06-05 21:59:17 +02:00
shortening some code, no real functionality change
This commit is contained in:
@@ -5,8 +5,7 @@ import android.content.ContentUris
|
|||||||
import android.content.ContentValues
|
import android.content.ContentValues
|
||||||
import android.content.Context
|
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.util.SparseIntArray
|
import android.util.SparseIntArray
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import com.google.gson.reflect.TypeToken
|
import com.google.gson.reflect.TypeToken
|
||||||
@@ -22,6 +21,7 @@ import org.joda.time.format.DateTimeFormat
|
|||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.collections.ArrayList
|
import kotlin.collections.ArrayList
|
||||||
|
|
||||||
|
@SuppressLint("MissingPermission")
|
||||||
class CalDAVHelper(val context: Context) {
|
class CalDAVHelper(val context: Context) {
|
||||||
private val eventsHelper = context.eventsHelper
|
private val eventsHelper = context.eventsHelper
|
||||||
|
|
||||||
@@ -58,45 +58,34 @@ class CalDAVHelper(val context: Context) {
|
|||||||
return calendars
|
return calendars
|
||||||
}
|
}
|
||||||
|
|
||||||
val uri = CalendarContract.Calendars.CONTENT_URI
|
val uri = Calendars.CONTENT_URI
|
||||||
val projection = arrayOf(
|
val projection = arrayOf(
|
||||||
CalendarContract.Calendars._ID,
|
Calendars._ID,
|
||||||
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
|
Calendars.CALENDAR_DISPLAY_NAME,
|
||||||
CalendarContract.Calendars.ACCOUNT_NAME,
|
Calendars.ACCOUNT_NAME,
|
||||||
CalendarContract.Calendars.ACCOUNT_TYPE,
|
Calendars.ACCOUNT_TYPE,
|
||||||
CalendarContract.Calendars.OWNER_ACCOUNT,
|
Calendars.OWNER_ACCOUNT,
|
||||||
CalendarContract.Calendars.CALENDAR_COLOR,
|
Calendars.CALENDAR_COLOR,
|
||||||
CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL)
|
Calendars.CALENDAR_ACCESS_LEVEL)
|
||||||
|
|
||||||
val selection = if (ids.trim().isNotEmpty()) "${CalendarContract.Calendars._ID} IN ($ids)" else null
|
val selection = if (ids.trim().isNotEmpty()) "${Calendars._ID} IN ($ids)" else null
|
||||||
var cursor: Cursor? = null
|
context.queryCursor(uri, projection, selection, showErrors = showToasts) { cursor ->
|
||||||
try {
|
val id = cursor.getIntValue(Calendars._ID)
|
||||||
cursor = context.contentResolver.query(uri, projection, selection, null, null)
|
val displayName = cursor.getStringValue(Calendars.CALENDAR_DISPLAY_NAME)
|
||||||
if (cursor?.moveToFirst() == true) {
|
val accountName = cursor.getStringValue(Calendars.ACCOUNT_NAME)
|
||||||
do {
|
val accountType = cursor.getStringValue(Calendars.ACCOUNT_TYPE)
|
||||||
val id = cursor.getIntValue(CalendarContract.Calendars._ID)
|
val ownerName = cursor.getStringValue(Calendars.OWNER_ACCOUNT) ?: ""
|
||||||
val displayName = cursor.getStringValue(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME)
|
val color = cursor.getIntValue(Calendars.CALENDAR_COLOR)
|
||||||
val accountName = cursor.getStringValue(CalendarContract.Calendars.ACCOUNT_NAME)
|
val accessLevel = cursor.getIntValue(Calendars.CALENDAR_ACCESS_LEVEL)
|
||||||
val accountType = cursor.getStringValue(CalendarContract.Calendars.ACCOUNT_TYPE)
|
val calendar = CalDAVCalendar(id, displayName, accountName, accountType, ownerName, color, accessLevel)
|
||||||
val ownerName = cursor.getStringValue(CalendarContract.Calendars.OWNER_ACCOUNT) ?: ""
|
calendars.add(calendar)
|
||||||
val color = cursor.getIntValue(CalendarContract.Calendars.CALENDAR_COLOR)
|
|
||||||
val accessLevel = cursor.getIntValue(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL)
|
|
||||||
val calendar = CalDAVCalendar(id, displayName, accountName, accountType, ownerName, color, accessLevel)
|
|
||||||
calendars.add(calendar)
|
|
||||||
} while (cursor.moveToNext())
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
|
||||||
if (showToasts) {
|
|
||||||
context.showErrorToast(e)
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
cursor?.close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return calendars
|
return calendars
|
||||||
}
|
}
|
||||||
|
|
||||||
fun updateCalDAVCalendar(eventType: EventType) {
|
fun updateCalDAVCalendar(eventType: EventType) {
|
||||||
val uri = CalendarContract.Calendars.CONTENT_URI
|
val uri = Calendars.CONTENT_URI
|
||||||
val values = fillCalendarContentValues(eventType)
|
val values = fillCalendarContentValues(eventType)
|
||||||
val newUri = ContentUris.withAppendedId(uri, eventType.caldavCalendarId.toLong())
|
val newUri = ContentUris.withAppendedId(uri, eventType.caldavCalendarId.toLong())
|
||||||
try {
|
try {
|
||||||
@@ -108,23 +97,23 @@ class CalDAVHelper(val context: Context) {
|
|||||||
private fun fillCalendarContentValues(eventType: EventType): ContentValues {
|
private fun fillCalendarContentValues(eventType: EventType): ContentValues {
|
||||||
val colorKey = getEventTypeColorKey(eventType)
|
val colorKey = getEventTypeColorKey(eventType)
|
||||||
return ContentValues().apply {
|
return ContentValues().apply {
|
||||||
put(CalendarContract.Calendars.CALENDAR_COLOR_KEY, colorKey)
|
put(Calendars.CALENDAR_COLOR_KEY, colorKey)
|
||||||
put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, eventType.title)
|
put(Calendars.CALENDAR_DISPLAY_NAME, eventType.title)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("MissingPermission")
|
@SuppressLint("MissingPermission")
|
||||||
private fun getEventTypeColorKey(eventType: EventType): Int {
|
private fun getEventTypeColorKey(eventType: EventType): Int {
|
||||||
val uri = CalendarContract.Colors.CONTENT_URI
|
val uri = Colors.CONTENT_URI
|
||||||
val projection = arrayOf(CalendarContract.Colors.COLOR_KEY)
|
val projection = arrayOf(Colors.COLOR_KEY)
|
||||||
val selection = "${CalendarContract.Colors.COLOR_TYPE} = ? AND ${CalendarContract.Colors.COLOR} = ? AND ${CalendarContract.Colors.ACCOUNT_NAME} = ?"
|
val selection = "${Colors.COLOR_TYPE} = ? AND ${Colors.COLOR} = ? AND ${Colors.ACCOUNT_NAME} = ?"
|
||||||
val selectionArgs = arrayOf(CalendarContract.Colors.TYPE_CALENDAR.toString(), eventType.color.toString(), eventType.caldavEmail)
|
val selectionArgs = arrayOf(Colors.TYPE_CALENDAR.toString(), eventType.color.toString(), eventType.caldavEmail)
|
||||||
|
|
||||||
var cursor: Cursor? = null
|
var cursor: Cursor? = null
|
||||||
try {
|
try {
|
||||||
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
|
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
|
||||||
if (cursor?.moveToFirst() == true) {
|
if (cursor?.moveToFirst() == true) {
|
||||||
return cursor.getStringValue(CalendarContract.Colors.COLOR_KEY).toInt()
|
return cursor.getStringValue(Colors.COLOR_KEY).toInt()
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
cursor?.close()
|
cursor?.close()
|
||||||
@@ -136,23 +125,15 @@ class CalDAVHelper(val context: Context) {
|
|||||||
@SuppressLint("MissingPermission")
|
@SuppressLint("MissingPermission")
|
||||||
fun getAvailableCalDAVCalendarColors(eventType: EventType): ArrayList<Int> {
|
fun getAvailableCalDAVCalendarColors(eventType: EventType): ArrayList<Int> {
|
||||||
val colors = SparseIntArray()
|
val colors = SparseIntArray()
|
||||||
val uri = CalendarContract.Colors.CONTENT_URI
|
val uri = Colors.CONTENT_URI
|
||||||
val projection = arrayOf(CalendarContract.Colors.COLOR, CalendarContract.Colors.COLOR_KEY)
|
val projection = arrayOf(Colors.COLOR, Colors.COLOR_KEY)
|
||||||
val selection = "${CalendarContract.Colors.COLOR_TYPE} = ? AND ${CalendarContract.Colors.ACCOUNT_NAME} = ?"
|
val selection = "${Colors.COLOR_TYPE} = ? AND ${Colors.ACCOUNT_NAME} = ?"
|
||||||
val selectionArgs = arrayOf(CalendarContract.Colors.TYPE_CALENDAR.toString(), eventType.caldavEmail)
|
val selectionArgs = arrayOf(Colors.TYPE_CALENDAR.toString(), eventType.caldavEmail)
|
||||||
|
|
||||||
var cursor: Cursor? = null
|
context.queryCursor(uri, projection, selection, selectionArgs) { cursor ->
|
||||||
try {
|
val colorKey = cursor.getIntValue(Colors.COLOR_KEY)
|
||||||
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
|
val color = cursor.getIntValue(Colors.COLOR)
|
||||||
if (cursor?.moveToFirst() == true) {
|
colors.put(colorKey, color)
|
||||||
do {
|
|
||||||
val colorKey = cursor.getIntValue(CalendarContract.Colors.COLOR_KEY)
|
|
||||||
val color = cursor.getIntValue(CalendarContract.Colors.COLOR)
|
|
||||||
colors.put(colorKey, color)
|
|
||||||
} while (cursor.moveToNext())
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
cursor?.close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var sortedColors = ArrayList<Int>(colors.size())
|
var sortedColors = ArrayList<Int>(colors.size())
|
||||||
@@ -173,151 +154,139 @@ class CalDAVHelper(val context: Context) {
|
|||||||
importIdsMap[it.importId] = it
|
importIdsMap[it.importId] = it
|
||||||
}
|
}
|
||||||
|
|
||||||
val uri = CalendarContract.Events.CONTENT_URI
|
val uri = Events.CONTENT_URI
|
||||||
val projection = arrayOf(
|
val projection = arrayOf(
|
||||||
CalendarContract.Events._ID,
|
Events._ID,
|
||||||
CalendarContract.Events.TITLE,
|
Events.TITLE,
|
||||||
CalendarContract.Events.DESCRIPTION,
|
Events.DESCRIPTION,
|
||||||
CalendarContract.Events.DTSTART,
|
Events.DTSTART,
|
||||||
CalendarContract.Events.DTEND,
|
Events.DTEND,
|
||||||
CalendarContract.Events.DURATION,
|
Events.DURATION,
|
||||||
CalendarContract.Events.EXDATE,
|
Events.EXDATE,
|
||||||
CalendarContract.Events.ALL_DAY,
|
Events.ALL_DAY,
|
||||||
CalendarContract.Events.RRULE,
|
Events.RRULE,
|
||||||
CalendarContract.Events.ORIGINAL_ID,
|
Events.ORIGINAL_ID,
|
||||||
CalendarContract.Events.ORIGINAL_INSTANCE_TIME,
|
Events.ORIGINAL_INSTANCE_TIME,
|
||||||
CalendarContract.Events.EVENT_LOCATION,
|
Events.EVENT_LOCATION,
|
||||||
CalendarContract.Events.EVENT_TIMEZONE,
|
Events.EVENT_TIMEZONE,
|
||||||
CalendarContract.Events.CALENDAR_TIME_ZONE,
|
Events.CALENDAR_TIME_ZONE,
|
||||||
CalendarContract.Events.DELETED)
|
Events.DELETED)
|
||||||
|
|
||||||
val selection = "${CalendarContract.Events.CALENDAR_ID} = $calendarId"
|
val selection = "${Events.CALENDAR_ID} = $calendarId"
|
||||||
var cursor: Cursor? = null
|
context.queryCursor(uri, projection, selection, showErrors = showToasts) { cursor ->
|
||||||
try {
|
val deleted = cursor.getIntValue(Events.DELETED)
|
||||||
cursor = context.contentResolver.query(uri, projection, selection, null, null)
|
if (deleted == 1) {
|
||||||
if (cursor?.moveToFirst() == true) {
|
return@queryCursor
|
||||||
do {
|
}
|
||||||
val deleted = cursor.getIntValue(CalendarContract.Events.DELETED)
|
|
||||||
if (deleted == 1) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
val id = cursor.getLongValue(CalendarContract.Events._ID)
|
val id = cursor.getLongValue(Events._ID)
|
||||||
val title = cursor.getStringValue(CalendarContract.Events.TITLE) ?: ""
|
val title = cursor.getStringValue(Events.TITLE) ?: ""
|
||||||
val description = cursor.getStringValue(CalendarContract.Events.DESCRIPTION) ?: ""
|
val description = cursor.getStringValue(Events.DESCRIPTION) ?: ""
|
||||||
val startTS = cursor.getLongValue(CalendarContract.Events.DTSTART) / 1000L
|
val startTS = cursor.getLongValue(Events.DTSTART) / 1000L
|
||||||
var endTS = cursor.getLongValue(CalendarContract.Events.DTEND) / 1000L
|
var endTS = cursor.getLongValue(Events.DTEND) / 1000L
|
||||||
val allDay = cursor.getIntValue(CalendarContract.Events.ALL_DAY)
|
val allDay = cursor.getIntValue(Events.ALL_DAY)
|
||||||
val rrule = cursor.getStringValue(CalendarContract.Events.RRULE) ?: ""
|
val rrule = cursor.getStringValue(Events.RRULE) ?: ""
|
||||||
val location = cursor.getStringValue(CalendarContract.Events.EVENT_LOCATION) ?: ""
|
val location = cursor.getStringValue(Events.EVENT_LOCATION) ?: ""
|
||||||
val originalId = cursor.getStringValue(CalendarContract.Events.ORIGINAL_ID)
|
val originalId = cursor.getStringValue(Events.ORIGINAL_ID)
|
||||||
val originalInstanceTime = cursor.getLongValue(CalendarContract.Events.ORIGINAL_INSTANCE_TIME)
|
val originalInstanceTime = cursor.getLongValue(Events.ORIGINAL_INSTANCE_TIME)
|
||||||
val reminders = getCalDAVEventReminders(id)
|
val reminders = getCalDAVEventReminders(id)
|
||||||
val attendees = Gson().toJson(getCalDAVEventAttendees(id))
|
val attendees = Gson().toJson(getCalDAVEventAttendees(id))
|
||||||
|
|
||||||
if (endTS == 0L) {
|
if (endTS == 0L) {
|
||||||
val duration = cursor.getStringValue(CalendarContract.Events.DURATION) ?: ""
|
val duration = cursor.getStringValue(Events.DURATION) ?: ""
|
||||||
endTS = startTS + Parser().parseDurationSeconds(duration)
|
endTS = startTS + Parser().parseDurationSeconds(duration)
|
||||||
}
|
}
|
||||||
|
|
||||||
val reminder1 = reminders.getOrNull(0)
|
val reminder1 = reminders.getOrNull(0)
|
||||||
val reminder2 = reminders.getOrNull(1)
|
val reminder2 = reminders.getOrNull(1)
|
||||||
val reminder3 = reminders.getOrNull(2)
|
val reminder3 = reminders.getOrNull(2)
|
||||||
val importId = getCalDAVEventImportId(calendarId, id)
|
val importId = getCalDAVEventImportId(calendarId, id)
|
||||||
val eventTimeZone = cursor.getStringValue(CalendarContract.Events.EVENT_TIMEZONE)
|
val eventTimeZone = cursor.getStringValue(Events.EVENT_TIMEZONE)
|
||||||
?: cursor.getStringValue(CalendarContract.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)
|
||||||
event.endTS = Formatter.getShiftedImportTimestamp(event.endTS)
|
event.endTS = Formatter.getShiftedImportTimestamp(event.endTS)
|
||||||
if (event.endTS > event.startTS) {
|
if (event.endTS > event.startTS) {
|
||||||
event.endTS -= DAY
|
event.endTS -= DAY
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchedEventIds.add(importId)
|
fetchedEventIds.add(importId)
|
||||||
|
|
||||||
// if the event is an exception from another events repeat rule, find the original parent event
|
// if the event is an exception from another events repeat rule, find the original parent event
|
||||||
if (originalInstanceTime != 0L) {
|
if (originalInstanceTime != 0L) {
|
||||||
val parentImportId = "$source-$originalId"
|
val parentImportId = "$source-$originalId"
|
||||||
val parentEvent = context.eventsDB.getEventWithImportId(parentImportId)
|
val parentEvent = context.eventsDB.getEventWithImportId(parentImportId)
|
||||||
val originalDayCode = Formatter.getDayCodeFromTS(originalInstanceTime / 1000L)
|
val originalDayCode = Formatter.getDayCodeFromTS(originalInstanceTime / 1000L)
|
||||||
if (parentEvent != null && !parentEvent.repetitionExceptions.contains(originalDayCode)) {
|
if (parentEvent != null && !parentEvent.repetitionExceptions.contains(originalDayCode)) {
|
||||||
event.parentId = parentEvent.id!!
|
event.parentId = parentEvent.id!!
|
||||||
parentEvent.addRepetitionException(originalDayCode)
|
parentEvent.addRepetitionException(originalDayCode)
|
||||||
eventsHelper.insertEvent(parentEvent, false, false)
|
eventsHelper.insertEvent(parentEvent, false, false)
|
||||||
|
|
||||||
event.parentId = parentEvent.id!!
|
event.parentId = parentEvent.id!!
|
||||||
event.addRepetitionException(originalDayCode)
|
event.addRepetitionException(originalDayCode)
|
||||||
eventsHelper.insertEvent(event, false, false)
|
eventsHelper.insertEvent(event, false, false)
|
||||||
continue
|
return@queryCursor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// some calendars add repeatable event exceptions with using the "exdate" field, not by creating a child event that is an exception
|
// some calendars add repeatable event exceptions with using the "exdate" field, not by creating a child event that is an exception
|
||||||
val exdate = cursor.getStringValue(CalendarContract.Events.EXDATE) ?: ""
|
val exdate = cursor.getStringValue(Events.EXDATE) ?: ""
|
||||||
if (exdate.length > 8) {
|
if (exdate.length > 8) {
|
||||||
val lines = exdate.split("\n")
|
val lines = exdate.split("\n")
|
||||||
for (line in lines) {
|
for (line in lines) {
|
||||||
val dates = line.split(",")
|
val dates = line.split(",")
|
||||||
dates.forEach {
|
dates.forEach {
|
||||||
if (it.endsWith("Z")) {
|
if (it.endsWith("Z")) {
|
||||||
// convert for example "20190216T230000Z" to "20190217000000" in Slovakia in a weird way
|
// convert for example "20190216T230000Z" to "20190217000000" in Slovakia in a weird way
|
||||||
val formatter = DateTimeFormat.forPattern("yyyyMMdd'T'HHmmss'Z'")
|
val formatter = DateTimeFormat.forPattern("yyyyMMdd'T'HHmmss'Z'")
|
||||||
val offset = DateTimeZone.getDefault().getOffset(System.currentTimeMillis())
|
val offset = DateTimeZone.getDefault().getOffset(System.currentTimeMillis())
|
||||||
val dt = formatter.parseDateTime(it).plusMillis(offset)
|
val dt = formatter.parseDateTime(it).plusMillis(offset)
|
||||||
val daycode = Formatter.getDayCodeFromDateTime(dt)
|
val daycode = Formatter.getDayCodeFromDateTime(dt)
|
||||||
event.repetitionExceptions.add(daycode)
|
event.repetitionExceptions.add(daycode)
|
||||||
} else {
|
} else {
|
||||||
var potentialTS = it.substring(0, 8)
|
var potentialTS = it.substring(0, 8)
|
||||||
if (potentialTS.areDigitsOnly()) {
|
if (potentialTS.areDigitsOnly()) {
|
||||||
event.repetitionExceptions.add(potentialTS)
|
event.repetitionExceptions.add(potentialTS)
|
||||||
} else if (it.contains(";")) {
|
} else if (it.contains(";")) {
|
||||||
potentialTS = it.substringAfter(";").substring(0, 8)
|
potentialTS = it.substringAfter(";").substring(0, 8)
|
||||||
event.repetitionExceptions.add(potentialTS)
|
event.repetitionExceptions.add(potentialTS)
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (importIdsMap.containsKey(event.importId)) {
|
|
||||||
val existingEvent = importIdsMap[importId]
|
|
||||||
val originalEventId = existingEvent!!.id
|
|
||||||
|
|
||||||
existingEvent.apply {
|
|
||||||
this.id = null
|
|
||||||
color = 0
|
|
||||||
lastUpdated = 0L
|
|
||||||
repetitionExceptions = ArrayList()
|
|
||||||
}
|
|
||||||
|
|
||||||
if (existingEvent.hashCode() != event.hashCode() && title.isNotEmpty()) {
|
|
||||||
event.id = originalEventId
|
|
||||||
eventsHelper.updateEvent(event, false, false)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (title.isNotEmpty()) {
|
|
||||||
importIdsMap[event.importId] = event
|
|
||||||
eventsHelper.insertEvent(event, false, false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} while (cursor.moveToNext())
|
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
|
||||||
if (showToasts) {
|
if (importIdsMap.containsKey(event.importId)) {
|
||||||
context.showErrorToast(e)
|
val existingEvent = importIdsMap[importId]
|
||||||
|
val originalEventId = existingEvent!!.id
|
||||||
|
|
||||||
|
existingEvent.apply {
|
||||||
|
this.id = null
|
||||||
|
color = 0
|
||||||
|
lastUpdated = 0L
|
||||||
|
repetitionExceptions = ArrayList()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (existingEvent.hashCode() != event.hashCode() && title.isNotEmpty()) {
|
||||||
|
event.id = originalEventId
|
||||||
|
eventsHelper.updateEvent(event, false, false)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (title.isNotEmpty()) {
|
||||||
|
importIdsMap[event.importId] = event
|
||||||
|
eventsHelper.insertEvent(event, false, false)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
cursor?.close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val eventIdsToDelete = ArrayList<Long>()
|
val eventIdsToDelete = ArrayList<Long>()
|
||||||
@@ -335,7 +304,7 @@ class CalDAVHelper(val context: Context) {
|
|||||||
|
|
||||||
@SuppressLint("MissingPermission")
|
@SuppressLint("MissingPermission")
|
||||||
fun insertCalDAVEvent(event: Event) {
|
fun insertCalDAVEvent(event: Event) {
|
||||||
val uri = CalendarContract.Events.CONTENT_URI
|
val uri = Events.CONTENT_URI
|
||||||
val values = fillEventContentValues(event)
|
val values = fillEventContentValues(event)
|
||||||
val newUri = context.contentResolver.insert(uri, values)
|
val newUri = context.contentResolver.insert(uri, values)
|
||||||
|
|
||||||
@@ -350,7 +319,7 @@ class CalDAVHelper(val context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun updateCalDAVEvent(event: Event) {
|
fun updateCalDAVEvent(event: Event) {
|
||||||
val uri = CalendarContract.Events.CONTENT_URI
|
val uri = Events.CONTENT_URI
|
||||||
val values = fillEventContentValues(event)
|
val values = fillEventContentValues(event)
|
||||||
val eventRemoteID = event.getCalDAVEventId()
|
val eventRemoteID = event.getCalDAVEventId()
|
||||||
event.importId = getCalDAVEventImportId(event.getCalDAVCalendarId(), eventRemoteID)
|
event.importId = getCalDAVEventImportId(event.getCalDAVCalendarId(), eventRemoteID)
|
||||||
@@ -386,15 +355,15 @@ class CalDAVHelper(val context: Context) {
|
|||||||
val attendees = Gson().fromJson<ArrayList<Attendee>>(event.attendees, object : TypeToken<List<Attendee>>() {}.type) ?: ArrayList()
|
val attendees = Gson().fromJson<ArrayList<Attendee>>(event.attendees, object : TypeToken<List<Attendee>>() {}.type) ?: ArrayList()
|
||||||
attendees.forEach {
|
attendees.forEach {
|
||||||
val contentValues = ContentValues().apply {
|
val contentValues = ContentValues().apply {
|
||||||
put(CalendarContract.Attendees.ATTENDEE_NAME, it.name)
|
put(Attendees.ATTENDEE_NAME, it.name)
|
||||||
put(CalendarContract.Attendees.ATTENDEE_EMAIL, it.email)
|
put(Attendees.ATTENDEE_EMAIL, it.email)
|
||||||
put(CalendarContract.Attendees.ATTENDEE_STATUS, it.status)
|
put(Attendees.ATTENDEE_STATUS, it.status)
|
||||||
put(CalendarContract.Attendees.ATTENDEE_RELATIONSHIP, it.relationship)
|
put(Attendees.ATTENDEE_RELATIONSHIP, it.relationship)
|
||||||
put(CalendarContract.Attendees.EVENT_ID, event.getCalDAVEventId())
|
put(Attendees.EVENT_ID, event.getCalDAVEventId())
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
context.contentResolver.insert(CalendarContract.Attendees.CONTENT_URI, contentValues)
|
context.contentResolver.insert(Attendees.CONTENT_URI, contentValues)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
context.toast(R.string.unknown_error_occurred)
|
context.toast(R.string.unknown_error_occurred)
|
||||||
}
|
}
|
||||||
@@ -407,31 +376,31 @@ class CalDAVHelper(val context: Context) {
|
|||||||
|
|
||||||
private fun fillEventContentValues(event: Event): ContentValues {
|
private fun fillEventContentValues(event: Event): ContentValues {
|
||||||
return ContentValues().apply {
|
return ContentValues().apply {
|
||||||
put(CalendarContract.Events.CALENDAR_ID, event.getCalDAVCalendarId())
|
put(Events.CALENDAR_ID, event.getCalDAVCalendarId())
|
||||||
put(CalendarContract.Events.TITLE, event.title)
|
put(Events.TITLE, event.title)
|
||||||
put(CalendarContract.Events.DESCRIPTION, event.description)
|
put(Events.DESCRIPTION, event.description)
|
||||||
put(CalendarContract.Events.DTSTART, event.startTS * 1000L)
|
put(Events.DTSTART, event.startTS * 1000L)
|
||||||
put(CalendarContract.Events.ALL_DAY, if (event.getIsAllDay()) 1 else 0)
|
put(Events.ALL_DAY, if (event.getIsAllDay()) 1 else 0)
|
||||||
put(CalendarContract.Events.EVENT_TIMEZONE, event.getTimeZoneString())
|
put(Events.EVENT_TIMEZONE, event.getTimeZoneString())
|
||||||
put(CalendarContract.Events.EVENT_LOCATION, event.location)
|
put(Events.EVENT_LOCATION, event.location)
|
||||||
put(CalendarContract.Events.STATUS, CalendarContract.Events.STATUS_CONFIRMED)
|
put(Events.STATUS, Events.STATUS_CONFIRMED)
|
||||||
|
|
||||||
val repeatRule = Parser().getRepeatCode(event)
|
val repeatRule = Parser().getRepeatCode(event)
|
||||||
if (repeatRule.isEmpty()) {
|
if (repeatRule.isEmpty()) {
|
||||||
putNull(CalendarContract.Events.RRULE)
|
putNull(Events.RRULE)
|
||||||
} else {
|
} else {
|
||||||
put(CalendarContract.Events.RRULE, repeatRule)
|
put(Events.RRULE, repeatRule)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.getIsAllDay() && event.endTS >= event.startTS)
|
if (event.getIsAllDay() && event.endTS >= event.startTS)
|
||||||
event.endTS += DAY
|
event.endTS += DAY
|
||||||
|
|
||||||
if (event.repeatInterval > 0) {
|
if (event.repeatInterval > 0) {
|
||||||
put(CalendarContract.Events.DURATION, getDurationCode(event))
|
put(Events.DURATION, getDurationCode(event))
|
||||||
putNull(CalendarContract.Events.DTEND)
|
putNull(Events.DTEND)
|
||||||
} else {
|
} else {
|
||||||
put(CalendarContract.Events.DTEND, event.endTS * 1000L)
|
put(Events.DTEND, event.endTS * 1000L)
|
||||||
putNull(CalendarContract.Events.DURATION)
|
putNull(Events.DURATION)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -443,9 +412,9 @@ class CalDAVHelper(val context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun clearEventAttendees(event: Event) {
|
private fun clearEventAttendees(event: Event) {
|
||||||
val selection = "${CalendarContract.Attendees.EVENT_ID} = ?"
|
val selection = "${Attendees.EVENT_ID} = ?"
|
||||||
val selectionArgs = arrayOf(event.getCalDAVEventId().toString())
|
val selectionArgs = arrayOf(event.getCalDAVEventId().toString())
|
||||||
context.contentResolver.delete(CalendarContract.Attendees.CONTENT_URI, selection, selectionArgs)
|
context.contentResolver.delete(Attendees.CONTENT_URI, selection, selectionArgs)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getDurationCode(event: Event): String {
|
private fun getDurationCode(event: Event): String {
|
||||||
@@ -463,7 +432,7 @@ class CalDAVHelper(val context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun deleteCalDAVEvent(event: Event) {
|
fun deleteCalDAVEvent(event: Event) {
|
||||||
val uri = CalendarContract.Events.CONTENT_URI
|
val uri = Events.CONTENT_URI
|
||||||
val contentUri = ContentUris.withAppendedId(uri, event.getCalDAVEventId())
|
val contentUri = ContentUris.withAppendedId(uri, event.getCalDAVEventId())
|
||||||
try {
|
try {
|
||||||
context.contentResolver.delete(contentUri, null, null)
|
context.contentResolver.delete(contentUri, null, null)
|
||||||
@@ -473,7 +442,7 @@ class CalDAVHelper(val context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun insertEventRepeatException(event: Event, occurrenceTS: Long) {
|
fun insertEventRepeatException(event: Event, occurrenceTS: Long) {
|
||||||
val uri = CalendarContract.Events.CONTENT_URI
|
val uri = Events.CONTENT_URI
|
||||||
val values = fillEventRepeatExceptionValues(event, occurrenceTS)
|
val values = fillEventRepeatExceptionValues(event, occurrenceTS)
|
||||||
try {
|
try {
|
||||||
context.contentResolver.insert(uri, values)
|
context.contentResolver.insert(uri, values)
|
||||||
@@ -485,13 +454,13 @@ class CalDAVHelper(val context: Context) {
|
|||||||
|
|
||||||
private fun fillEventRepeatExceptionValues(event: Event, occurrenceTS: Long): ContentValues {
|
private fun fillEventRepeatExceptionValues(event: Event, occurrenceTS: Long): ContentValues {
|
||||||
return ContentValues().apply {
|
return ContentValues().apply {
|
||||||
put(CalendarContract.Events.CALENDAR_ID, event.getCalDAVCalendarId())
|
put(Events.CALENDAR_ID, event.getCalDAVCalendarId())
|
||||||
put(CalendarContract.Events.DTSTART, occurrenceTS)
|
put(Events.DTSTART, occurrenceTS)
|
||||||
put(CalendarContract.Events.DTEND, occurrenceTS + (event.endTS - event.startTS))
|
put(Events.DTEND, occurrenceTS + (event.endTS - event.startTS))
|
||||||
put(CalendarContract.Events.ORIGINAL_ID, event.getCalDAVEventId())
|
put(Events.ORIGINAL_ID, event.getCalDAVEventId())
|
||||||
put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().id.toString())
|
put(Events.EVENT_TIMEZONE, TimeZone.getDefault().id.toString())
|
||||||
put(CalendarContract.Events.ORIGINAL_INSTANCE_TIME, occurrenceTS * 1000L)
|
put(Events.ORIGINAL_INSTANCE_TIME, occurrenceTS * 1000L)
|
||||||
put(CalendarContract.Events.EXDATE, Formatter.getDayCodeFromTS(occurrenceTS))
|
put(Events.EXDATE, Formatter.getDayCodeFromTS(occurrenceTS))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -502,51 +471,38 @@ class CalDAVHelper(val context: Context) {
|
|||||||
Reminders.MINUTES,
|
Reminders.MINUTES,
|
||||||
Reminders.METHOD)
|
Reminders.METHOD)
|
||||||
val selection = "${Reminders.EVENT_ID} = $eventId"
|
val selection = "${Reminders.EVENT_ID} = $eventId"
|
||||||
var cursor: Cursor? = null
|
|
||||||
try {
|
context.queryCursor(uri, projection, selection) { cursor ->
|
||||||
cursor = context.contentResolver.query(uri, projection, selection, null, null)
|
val minutes = cursor.getIntValue(Reminders.MINUTES)
|
||||||
if (cursor?.moveToFirst() == true) {
|
val method = cursor.getIntValue(Reminders.METHOD)
|
||||||
do {
|
if (method == Reminders.METHOD_ALERT || method == Reminders.METHOD_EMAIL) {
|
||||||
val minutes = cursor.getIntValue(Reminders.MINUTES)
|
val type = if (method == Reminders.METHOD_EMAIL) REMINDER_EMAIL else REMINDER_NOTIFICATION
|
||||||
val method = cursor.getIntValue(Reminders.METHOD)
|
val reminder = Reminder(minutes, type)
|
||||||
if (method == Reminders.METHOD_ALERT || method == Reminders.METHOD_EMAIL) {
|
reminders.add(reminder)
|
||||||
val type = if (method == Reminders.METHOD_EMAIL) REMINDER_EMAIL else REMINDER_NOTIFICATION
|
|
||||||
val reminder = Reminder(minutes, type)
|
|
||||||
reminders.add(reminder)
|
|
||||||
}
|
|
||||||
} while (cursor.moveToNext())
|
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
cursor?.close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return reminders.sortedBy { it.minutes }
|
return reminders.sortedBy { it.minutes }
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getCalDAVEventAttendees(eventId: Long): List<Attendee> {
|
private fun getCalDAVEventAttendees(eventId: Long): List<Attendee> {
|
||||||
val attendees = ArrayList<Attendee>()
|
val attendees = ArrayList<Attendee>()
|
||||||
val uri = CalendarContract.Attendees.CONTENT_URI
|
val uri = Attendees.CONTENT_URI
|
||||||
val projection = arrayOf(
|
val projection = arrayOf(
|
||||||
CalendarContract.Attendees.ATTENDEE_NAME,
|
Attendees.ATTENDEE_NAME,
|
||||||
CalendarContract.Attendees.ATTENDEE_EMAIL,
|
Attendees.ATTENDEE_EMAIL,
|
||||||
CalendarContract.Attendees.ATTENDEE_STATUS,
|
Attendees.ATTENDEE_STATUS,
|
||||||
CalendarContract.Attendees.ATTENDEE_RELATIONSHIP)
|
Attendees.ATTENDEE_RELATIONSHIP)
|
||||||
val selection = "${CalendarContract.Attendees.EVENT_ID} = $eventId"
|
val selection = "${Attendees.EVENT_ID} = $eventId"
|
||||||
var cursor: Cursor? = null
|
context.queryCursor(uri, projection, selection) { cursor ->
|
||||||
try {
|
val name = cursor.getStringValue(Attendees.ATTENDEE_NAME) ?: ""
|
||||||
cursor = context.contentResolver.query(uri, projection, selection, null, null)
|
val email = cursor.getStringValue(Attendees.ATTENDEE_EMAIL) ?: ""
|
||||||
if (cursor?.moveToFirst() == true) {
|
val status = cursor.getIntValue(Attendees.ATTENDEE_STATUS)
|
||||||
do {
|
val relationship = cursor.getIntValue(Attendees.ATTENDEE_RELATIONSHIP)
|
||||||
val name = cursor.getStringValue(CalendarContract.Attendees.ATTENDEE_NAME) ?: ""
|
val attendee = Attendee(0, name, email, status, "", false, relationship)
|
||||||
val email = cursor.getStringValue(CalendarContract.Attendees.ATTENDEE_EMAIL) ?: ""
|
attendees.add(attendee)
|
||||||
val status = cursor.getIntValue(CalendarContract.Attendees.ATTENDEE_STATUS)
|
|
||||||
val relationship = cursor.getIntValue(CalendarContract.Attendees.ATTENDEE_RELATIONSHIP)
|
|
||||||
val attendee = Attendee(0, name, email, status, "", false, relationship)
|
|
||||||
attendees.add(attendee)
|
|
||||||
} while (cursor.moveToNext())
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
cursor?.close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return attendees
|
return attendees
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user