add a couple things related to inserting new calendar colors

This commit is contained in:
tibbi 2017-08-23 10:28:59 +02:00
parent 7fb245c78d
commit d43d06bc8c

View file

@ -74,7 +74,6 @@ class CalDAVHandler(val context: Context) {
private fun fillCalendarContentValues(eventType: EventType): ContentValues {
return ContentValues().apply {
put(CalendarContract.Calendars.CALENDAR_COLOR_KEY, getEventTypeColorKey(eventType))
put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, eventType.title)
}
}
@ -95,7 +94,53 @@ class CalDAVHandler(val context: Context) {
cursor?.close()
}
return 1
return insertNewColor(eventType)
}
private fun insertNewColor(eventType: EventType): Int {
val maxId = getMaxColorId(eventType) + 1
val values = ContentValues().apply {
put(CalendarContract.Colors.COLOR_KEY, maxId)
put(CalendarContract.Colors.COLOR, eventType.color)
put(CalendarContract.Colors.ACCOUNT_NAME, eventType.caldavEmail)
put(CalendarContract.Colors.ACCOUNT_TYPE, "com.google")
put(CalendarContract.Colors.COLOR_TYPE, CalendarContract.Colors.TYPE_CALENDAR)
}
val uri = CalendarContract.Colors.CONTENT_URI.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, eventType.caldavEmail)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google")
.build()
return if (context.contentResolver.insert(uri, values) != null) {
maxId
} else {
0
}
}
private fun getMaxColorId(eventType: EventType): Int {
val uri = CalendarContract.Colors.CONTENT_URI
val projection = arrayOf(CalendarContract.Colors.COLOR_KEY, CalendarContract.Colors.COLOR)
val selection = "${CalendarContract.Colors.COLOR_TYPE} = ? AND ${CalendarContract.Colors.ACCOUNT_NAME} = ?"
val selectionArgs = arrayOf(CalendarContract.Colors.TYPE_CALENDAR.toString(), eventType.caldavEmail)
var maxId = 1
var cursor: Cursor? = null
try {
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor != null && cursor.moveToFirst()) {
do {
maxId = Math.max(maxId, cursor.getIntValue(CalendarContract.Colors.COLOR_KEY))
} while (cursor.moveToNext())
}
} finally {
cursor?.close()
}
return maxId
}
private fun fetchCalDAVCalendarEvents(calendarId: Int, eventTypeId: Int) {