Android CalendarProvider Events Color

后端 未结 2 973
遥遥无期
遥遥无期 2021-02-10 02:15

I\'m most confused on how to use Colors in the Android CalendarProvider. I can add events alright, but when I try to insert one with a particular color, this preference seems to

2条回答
  •  广开言路
    2021-02-10 03:02

    Well, I guess I was burned out the other night. Yet, in case someone else ends up here, it's only fair to give a brief explanation on the Calendar->Events interaction as far as I got it.

    Long story short: event colors depend upon the calendar you put them in. Why is there an events_color column if it gets overridden by the Calendar colour? I whish I knew.

    So, once you set up your fancy syncadapter, user etc. you need to create a new calendar:

    public long insertCalendar(EventType eventType) {
    
        Uri calUri = CalendarContract.Calendars.CONTENT_URI;
        ContentValues cv = new ContentValues();
        cv.put(CalendarContract.Calendars.ACCOUNT_NAME, "DUMMY");
        cv.put(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
        cv.put(CalendarContract.Calendars.NAME, eventType.getDescription());
        cv.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, eventType.getDescription());
        cv.put(CalendarContract.Calendars.CALENDAR_COLOR, Color.parseColor(eventType.getColor()));
        cv.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
        cv.put(CalendarContract.Calendars.OWNER_ACCOUNT, true);
        cv.put(CalendarContract.Calendars.VISIBLE, 1);
        cv.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
        //cv.put(CalendarContract.Calendars.CAL_SYNC1, eventType.getId());
    
        calUri = calUri.buildUpon()
            .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
            .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, "DUMMY")
            .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL)
            .build();
    
        Uri result = mContentResolver.insert(calUri, cv);
        return Long.parseLong(result.getLastPathSegment());
    }
    

    Please notice I'm putting a color in the CALENDAR_COLOR column, in this case I'm parsing something like #000000 into Color.BLACK. Also notice the value inside ACCOUNT_TYPE. From what I gathered there doesn't seem to be another way around this.

    public long insert(Event event, long calendarID) throws ParseException {
    
        EventTypeDal adapter = new EventTypeDal();
        EventType et = adapter.GetEventTypeById(event.getFkEventType());
    
        ContentValues eventValues = new ContentValues();
    
        eventValues.put("calendar_id", calendarID); 
        //eventValues.put(Events.SYNC_DATA1, event.getId());
        eventValues.put(Events.TITLE, event.getTitle());
        eventValues.put(Events.DESCRIPTION, et.getDescription());
    
        eventValues.put(Events.EVENT_TIMEZONE, "Europe/Rome");
    
        eventValues.put(Events.DTSTART, event.getStart().getTime());
        eventValues.put(Events.DTEND, event.getEnd().getTime());
    
        Uri eventUri = mContentResolver.insert(Uri.parse(eventUriString).buildUpon().appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER,"true").appendQueryParameter(Calendars.ACCOUNT_NAME, "DUMMY").appendQueryParameter(Calendars.ACCOUNT_TYPE, "accountType").build(), eventValues);
        return Long.parseLong(eventUri.getLastPathSegment());
    }
    

    Now I'm putting an event inside a calendar (calendarID param). This event will be displayed with the calendar's color.

提交回复
热议问题