@@ -336,13 +336,19 @@ abstract class AndroidEvent(
336
336
event.location = row.getAsString(Events .EVENT_LOCATION )
337
337
event.description = row.getAsString(Events .DESCRIPTION )
338
338
339
- row.getAsString(Events .EVENT_COLOR_KEY )?.let { name ->
340
- try {
341
- event.color = Css3Color .valueOf(name)
342
- } catch (e: IllegalArgumentException ) {
343
- logger.warning(" Ignoring unknown color $name from Calendar Provider" )
339
+ // color can be specified as RGB value and/or as index key (CSS3 color of AndroidCalendar)
340
+ event.color =
341
+ row.getAsString(Events .EVENT_COLOR_KEY )?.let { name -> // try color key first
342
+ try {
343
+ Css3Color .valueOf(name)
344
+ } catch (_: IllegalArgumentException ) {
345
+ logger.warning(" Ignoring unknown color name \" $name \" " )
346
+ null
347
+ }
348
+ } ? :
349
+ row.getAsInteger(Events .EVENT_COLOR )?.let { color -> // otherwise, try to find the color name from the value
350
+ Css3Color .entries.firstOrNull { it.argb == color }
344
351
}
345
- }
346
352
347
353
// status
348
354
when (row.getAsInteger(Events .STATUS )) {
@@ -928,18 +934,22 @@ abstract class AndroidEvent(
928
934
builder.withValue(Events .TITLE , event.summary)
929
935
builder.withValue(Events .EVENT_LOCATION , event.location)
930
936
builder.withValue(Events .DESCRIPTION , event.description)
931
- builder.withValue(Events .EVENT_COLOR_KEY , event.color?.let { color ->
932
- val colorName = color.name
937
+
938
+ val color = event.color
939
+ if (color != null ) {
933
940
// set event color (if it's available for this account)
934
941
calendar.provider.query(Colors .CONTENT_URI .asSyncAdapter(calendar.account), arrayOf(Colors .COLOR_KEY ),
935
- " ${Colors .COLOR_KEY } =? AND ${Colors .COLOR_TYPE } =${Colors .TYPE_EVENT } " , arrayOf(colorName ), null )?.use { cursor ->
942
+ " ${Colors .COLOR_KEY } =? AND ${Colors .COLOR_TYPE } =${Colors .TYPE_EVENT } " , arrayOf(color.name ), null )?.use { cursor ->
936
943
if (cursor.moveToNext())
937
- return @let colorName
944
+ builder.withValue( Events . EVENT_COLOR_KEY , color.name)
938
945
else
939
- logger.fine(" Ignoring event color: $colorName (not available for this account)" )
946
+ logger.fine(" Ignoring event color \" ${color.name} \" (not available for this account)" )
940
947
}
941
- null
942
- })
948
+ } else {
949
+ // reset color index and value
950
+ builder .withValue(Events .EVENT_COLOR_KEY , null )
951
+ .withValue(Events .EVENT_COLOR , null )
952
+ }
943
953
944
954
// scheduling
945
955
val groupScheduled = event.attendees.isNotEmpty()
0 commit comments