CalendarContract not setting calendar name properly - android

I'm creating a calendar programatically, and everything is setted well but the calendar's name. This is the method I'm using.
private Uri createCalendar(String email){
Uri target = Uri.parse(CalendarContract.Calendars.CONTENT_URI.toString());
target = target.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, email)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google").build();
ContentValues values = new ContentValues();
values.put(CalendarContract.Calendars.ACCOUNT_NAME, email);
values.put(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google");
values.put(CalendarContract.Calendars.NAME, context.getResources().getString(R.string.app_name));
values.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, context.getResources().getString(R.string.app_name));
values.put(CalendarContract.Calendars.CALENDAR_COLOR, Util.getColor(context, R.color.primary));
values.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_ROOT);
values.put(CalendarContract.Calendars.OWNER_ACCOUNT, email);
values.put(CalendarContract.Calendars.VISIBLE, 1);
values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
values.put(CalendarContract.Calendars.CALENDAR_TIME_ZONE, TimeZone.getDefault().getID());
values.put(CalendarContract.Calendars.CAN_PARTIALLY_UPDATE, 1);
values.put(CalendarContract.Calendars.CAL_SYNC1, "https://www.google.com/calendar/feeds/" + email + "/private/full");
values.put(CalendarContract.Calendars.CAL_SYNC2, "https://www.google.com/calendar/feeds/default/allcalendars/full/" + email);
values.put(CalendarContract.Calendars.CAL_SYNC3, "https://www.google.com/calendar/feeds/default/allcalendars/full/" + email);
values.put(CalendarContract.Calendars.CAL_SYNC4, 1);
values.put(CalendarContract.Calendars.CAL_SYNC5, 0);
values.put(CalendarContract.Calendars.CAL_SYNC8, System.currentTimeMillis());
Uri newCalendar = context.getContentResolver().insert(target, values);
return newCalendar;
}
As said, everything is working fine, the color, the owner account, etc. but when the calendar is created, the name of it is always "Events". I tried to put the name manually (not using getStrings()) but didn't work either.
Any ideas of what can be happening?

Related

Issue in Adding Calendar Event in Android M

I am using a code to add an event to the device's calendar. The code works fine in all the Android versions except Android M.The event when being added on Android M shows as a birthday instead of an event. Please help on how can this issue be fixed.
The code being used is as follows
// Add event to calendar
try {
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startDateInMilliSeconds);
values.put(CalendarContract.Events.DTEND, endDateInMilliSeconds);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.DESCRIPTION, description);
if (location != null && (!TextUtils.isEmpty(location))) {
values.put(CalendarContract.Events.EVENT_LOCATION, location);
}
values.put(CalendarContract.Events.CALENDAR_ID, 1);
values.put(CalendarContract.Events.EVENT_TIMEZONE, Calendar.getInstance()
.getTimeZone().getID());
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
// Save the eventId into the Task object for possible future delete.
long eventId = Long.parseLong(uri.getLastPathSegment());
Uri openCalendarEvent = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventId);
Intent calIntent = new Intent(Intent.ACTION_VIEW).setData(uri);
calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startDateInMilliSeconds);
calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endDateInMilliSeconds);
calIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(calIntent);
} catch (Exception e) {
e.printStackTrace();
}
try below method # Himmat
public void addReminder() {
Intent calIntent = new Intent(Intent.ACTION_EDIT);
calIntent.setData(CalendarContract.Events.CONTENT_URI);
calIntent.setType("vnd.android.cursor.item/event");
calIntent.putExtra(CalendarContract.Events.TITLE, e.getName());
calIntent.putExtra(CalendarContract.Events.EVENT_LOCATION, e.getLocation());
calIntent.putExtra(CalendarContract.Events.DESCRIPTION, e.getDescription());
Calendar calStart=Calendar.getInstance();
if(Validator.isNotNull(e.getStartDate())) {
calStart.setTime(e.getStartDate());
}
calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
calStart.getTimeInMillis());
Calendar calEnd=Calendar.getInstance();
if(Validator.isNotNull(e.getEndDate())) {
calEnd.setTime(e.getEndDate());
}
calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
calEnd.getTimeInMillis());
calIntent.putExtra(CalendarContract.EXTRA_CUSTOM_APP_URI,
e.getRegistrationLink());
startActivity(calIntent);
}

Remove and Update existing Calendar Event

I am following this gist, to insert event into Calendar
How do I update existing Calendar Event, which i have inserted earlier using below code:
public void addToCalender() throws ParseException {
......
ContentValues event = new ContentValues();
event.put(CalendarContract.Events.CALENDAR_ID, calendarId[0]);
event.put(CalendarContract.Events.TITLE, "Event Title");
event.put(CalendarContract.Events.DESCRIPTION, "Event Description");
event.put(CalendarContract.Events.EVENT_LOCATION, "Eevnt Location");
event.put(CalendarContract.Events.DTSTART, startCalTime);
event.put(CalendarContract.Events.DTEND, endCalTime);
event.put(CalendarContract.Events.STATUS, 1);
event.put(CalendarContract.Events.HAS_ALARM, 1);
event.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
Uri insertEventUri = AddEventActivity.this.getContentResolver().insert(
eventsUri, event);
ContentValues reminders = new ContentValues();
reminders.put(Reminders.EVENT_ID,
Long.parseLong(insertEventUri.getLastPathSegment()));
reminders.put(Reminders.METHOD, Reminders.METHOD_ALERT);
reminders.put(Reminders.MINUTES, 10);
AddEventActivity.this.getContentResolver().insert(remainderUri, reminders);
}
I would like to know, How do I :
1. Remove existing event
2. Update existing event
Here is how you can modify an event. Let's say its ID is eventID:
public void updateEvent(int eventID)
{
ContentResolver cr = context.getContentResolver();
Uri eventUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventID);
ContentValues event = new ContentValues();
event.put(CalendarContract.Events.TITLE, "new title");
event.put(CalendarContract.Events.DESCRIPTION, "My cool event!");
cr.update(eventUri, event, null, null);
}
To remove an event, you have two possibilities:
If you are a standard application:
Use the previous code and replace all the event.put by this one:
event.put(CalendarContract.Events.DELETED, 1);
If you are the SyncAdapter of the calendar and want a real deletion (The previous method just says that the event should be deleted and the one that follows must be used once the calendar is being synced :
public deleteEvent(Uri eventUri)
{
cr.delete(event, null, null);
}
Where eventUri is the Uri of the event obtained as shown previously from the event ID.
For all of the previous methods, if you get an exception about not being in a sync adapter, you can use this:
public static Uri asSyncAdapter(Uri uri, String account, String accountType)
{
return uri.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, account)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, accountType)
.build();
}
I hope this will help you.
Source: personnal code + http://developer.android.com/guide/topics/providers/calendar-provider.html
...
Outlook in fact seems to respond to the rules defined in RFC 2446
In summary you have to specify
METHOD:REQUEST and ORGANIZER:xxxxxxxx
...
Please read the original answer by Tom Carter.

Calendar I created on "com.google" on android erased after sync

I created a new calendar on android and added events.
After first sync or after a few seconds it's erasing. Can somebody help with this problem?
Code:
private void createCalendar(Context context, String accName) {
Uri calUri = CalendarContract.Calendars.CONTENT_URI;
final ContentValues v = new ContentValues();
v.put(CalendarContract.Calendars.ACCOUNT_NAME, accName);
v.put(CalendarContract.Calendars.NAME, CALENDAR_NAME);
v.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, CALENDAR_NAME);
v.put(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google");
v.put(CalendarContract.Calendars.CALENDAR_COLOR, 0xEA8561);
v.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
v.put(CalendarContract.Calendars.OWNER_ACCOUNT, accName);
v.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
v.put(CalendarContract.Calendars.VISIBLE, 1);
calUri = calUri.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, accName)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google")
.build();
calUri = calUri.buildUpon().build();
final Uri result = context.getContentResolver().insert(calUri, v);
}
Unfortinately this is a known issue with the calendar API. you have only one option if you want to use the Android calendar- create the calendar as a local one and synchronize it yourself.

How to programmatically populate Reminders section on Android device's Calendar app?

I need to open Android device's Calendar app with some pre-populated data. The logic that Iam using seems to populate fields like:
Event description
Event location
From Date, To Date
All day event/not
Repeat /Recurrence Info
I am not able to populate "Reminders" section, I would like to populate Reminders section. It will great to get some help on this
Here is the code that I am using to open Calendar app and populate date.
// Intent to open Calendar Event
Intent intent = new Intent(Intent.ACTION_INSERT)
.setData(Events.CONTENT_URI);
intent.putExtra(Events.DESCRIPTION, desc);
intent.putExtra(Events.EVENT_LOCATION, location);
intent.putExtra(Events.TITLE, summary);
intent.putExtra(Events.EVENT_TIMEZONE, beginTime.getTimeZone().getID());
intent.putExtra(Events.STATUS, statusStr);
intent.putExtra(Events.VISIBLE, transparency);
intent.putExtra(Events.RRULE, "FREQ=YEARLY;INTERVAL=1;BYYEARDAY=1,2;UNTIL=20161210;");
intent.putExtra(Events.EXDATE, androidExDateStr.toString());
// Not sure on how to use CalendarContract.Reminders, Tried the following but does not seem to be working
intent.putExtra(CalendarContract.Reminders.DESCRIPTION, desc);
intent.putExtra(CalendarContract.Reminders.EVENT_LOCATION, location);
intent.putExtra(CalendarContract.Reminders.TITLE, summary);
intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis());
intent.putExtra(CalendarContract.Reminders.DTSTART, beginTime.getTimeInMillis());
intent.putExtra(CalendarContract.Reminders.EVENT_TIMEZONE, beginTime.getTimeZone().getID());
intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis());
intent.putExtra(CalendarContract.Reminders.DTEND, endTime.getTimeInMillis());
intent.putExtra(CalendarContract.Reminders.STATUS, statusStr);
intent.putExtra(CalendarContract.Reminders.RRULE,"FREQ=YEARLY;INTERVAL=1;BYYEARDAY=1,2;UNTIL=20161210;");
intent.putExtra(CalendarContract.Reminders.EXDATE, androidExDateStr.toString());
//intent.putExtra(CalendarContract.Reminders.METHOD, Reminders.METHOD_EMAIL);
//intent.putExtra(CalendarContract.Reminders.MINUTES, reminderVal) ;
//intent.putExtra(CalendarContract.Events.HAS_ALARM, 1);
//}
try {
context.startActivity(intent);
} catch(Exception e) {
e.printStackTrace();
Log.v(LOG_TAG, "Cannot schedule Calendar event as specified ");
return false;
}
Did you check the example from http://developer.android.com/guide/topics/providers/calendar-provider.html#reminders?
long eventID = 221;
...
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Reminders.MINUTES, 15);
values.put(Reminders.EVENT_ID, eventID);
values.put(Reminders.METHOD, Reminders.METHOD_ALERT);
Uri uri = cr.insert(Reminders.CONTENT_URI, values);

Make exception event from original recurring event?

I found that Events.CONTENT_EXCEPTION_URI (here) used for make recurring event.
It's hardly to find document or code example from internet. So I try many ways
1 Insert as SyncAdapter
ContentValues values = new ContentValues();
values.put(Events.ORIGINAL_INSTANCE_TIME, CaldavGlobalVar.getCurrentTime_());
values.put(Events.SELF_ATTENDEE_STATUS, status);
if(!username.equals("")){
values.put(Events.ORGANIZER, username);
}
if(event.getSummarry()!=null){
values.put(Events.TITLE, event.getSummarry());
}
if(event.getDescription()!=null){
values.put(Events.DESCRIPTION, event.getDescription());
}
if(event.getDateStart()!=null){
values.put(Events.DTSTART, CaldavGlobalVar.convertTIMEtomilisecond(event.getDateStart(), event.getAllDay()));
}
Uri exceptionUri = Uri. withAppendedPath(Events.CONTENT_EXCEPTION_URI, String.valueOf(event.getEventId()));
Uri syncUri = CalendarProvider.asSyncAdapter(exceptionUri, username,context.getResources().getString(R.string.ACCOUNT_TYPE));
Uri resultUri = context.getContentResolver().insert(syncUri, values);
resultUri return null, I didnot see any exception or any relation things, So I dig Android source code (from here) and find out the way they use Events.CONTENT_EXCEPTION_URI So I change
2 Insert by "ContentProviderOperation" like this, in line 1003
ContentValues values = new ContentValues();
values.put(Events.ORIGINAL_INSTANCE_TIME, CaldavGlobalVar.getCurrentTime_());
values.put(Events.SELF_ATTENDEE_STATUS, 1);
values.put(Events.STATUS, Events.STATUS_CONFIRMED);
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Uri exceptionUri = Uri.withAppendedPath(Events.CONTENT_EXCEPTION_URI,
String.valueOf(eventId));
ops.add(ContentProviderOperation.newInsert(exceptionUri).withValues(values).build());
mHandler.startBatch(mHandler.getNextToken(), null, CalendarContract.AUTHORITY, ops, 1000);
But it show log that It installed unsuccessfully, I am so worry about that, may be Google not support it fully, I also list all Content Provider in Android, I dont has any exception uri (Events.CONTENT_EXCEPTION_URI) --content://com.android.calendar/exception
Exception throwed
java.lang.IllegalArgumentException: Unknown URL content://com.android.calendar/exception
Does anyone have experience ? Any help are appreciate :)
Kind regards
A small part of my code:
ContentValues args = new ContentValues();
args.put(CalendarContract.Events.ORIGINAL_INSTANCE_TIME, originalinstancetime);
args.put(CalendarContract.Events.STATUS, status);
Uri.Builder eventUriBuilder = CalendarContract.Events.CONTENT_EXCEPTION_URI.buildUpon();
ContentUris.appendId(eventUriBuilder, originalEventID);
try {
final Uri resultUri = context.getContentResolver().insert(eventUriBuilder.build(), args);
int eventID = Integer.parseInt(resultUri.getLastPathSegment());
} catch (Exception e) {
}

Categories

Resources