I am doing backup and restore of calendars, During restore if calendar is not present in phone(but related account is present), i am creating new calendar with the following code, it has successfully inserted into db (i can see the return value of URI and id). But in calendar application it is visible for an second and disappears,i have no clue what is going wrong, i have made the visible flag with 1 on insertion but still not working.Can someone Help.
final String INT_NAME_PREFIX = "priv";
Uri calUri = CalendarContract.Calendars.CONTENT_URI
.buildUpon()
.appendQueryParameter(
CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(Calendars.ACCOUNT_NAME,
mCurrentMirrorItem.ACCOUNT_NAME)
.appendQueryParameter(Calendars.ACCOUNT_TYPE,
mCurrentMirrorItem.ACCOUNT_TYPE).build();
String dispName = mItem.CALENDAR_DISPLAY_NAME;
String intName = INT_NAME_PREFIX + dispName;
ContentValues contentValues = new ContentValues();
if (columnNames.contains(Calendars.ACCOUNT_NAME)) {
contentValues.put(CalendarContract.Calendars.ACCOUNT_NAME,
mItem.ACCOUNT_NAME);
}
if (columnNames.contains(Calendars.ACCOUNT_TYPE)) {
contentValues.put(CalendarContract.Calendars.ACCOUNT_TYPE,
mItem.ACCOUNT_TYPE);
}
if (columnNames.contains(Calendars.NAME)) {
contentValues.put(CalendarContract.Calendars.NAME, intName);
}
if (columnNames.contains(Calendars.CALENDAR_DISPLAY_NAME)) {
contentValues.put(
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
mItem.CALENDAR_DISPLAY_NAME);
}
if (columnNames.contains(Calendars.CALENDAR_COLOR)) {
contentValues.put(CalendarContract.Calendars.CALENDAR_COLOR,
mItem.CALENDAR_COLOR);
}
if (columnNames.contains(Calendars.CALENDAR_ACCESS_LEVEL)) {
contentValues.put(
CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL,
Calendars.CAL_ACCESS_OWNER);
}
if (columnNames.contains(Calendars.OWNER_ACCOUNT)) {
contentValues.put(CalendarContract.Calendars.OWNER_ACCOUNT,
mItem.ACCOUNT_NAME);
}
if (columnNames.contains(Calendars.VISIBLE)) {
contentValues.put(CalendarContract.Calendars.VISIBLE, 1);
}
if (columnNames.contains(Calendars.SYNC_EVENTS)) {
contentValues.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
}
returnUri = cr.insert(calUri, contentValues);
long eventID = Long.parseLong(returnUri.getLastPathSegment());
calid = String.valueOf(eventID);
Log.d(tag,"calendar name: "+mItem.CALENDAR_DISPLAY_NAME+"\tNew calendar id is: "+calid+"\nInserted URI: "+returnUri);
Related
I'm trying to create a calendar on google account, I managed to create calendars but none syncs with google and I don't know what I'm doing wrong.
I know where the problem more or less but I can't fix it.
The code I use is this:
public static long createCalendar (Activity activity, String name, String account, boolean local){
String color = "blue";
ContentValues calendarvalues = new ContentValues();
//The account that was used to sync the entry to the device. If the account_type is not {#link #ACCOUNT_TYPE_LOCAL} then the name and
// type must match an account on the device or the calendar will be deleted.
if(local) {
calendarvalues.put(CalendarContract.Calendars.ACCOUNT_NAME, "DUMMYLOCAL");
calendarvalues.put(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
}else{
calendarvalues.put(CalendarContract.Calendars.ACCOUNT_NAME, account);
calendarvalues.put(CalendarContract.Calendars.ACCOUNT_TYPE, account);
}
//Local CalendarContract.ACCOUNT_TYPE_LOCAL
calendarvalues.put(CalendarContract.Calendars.NAME, name);
calendarvalues.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, name);
calendarvalues.put(CalendarContract.Calendars.CALENDAR_COLOR, Color.parseColor(color));
calendarvalues.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
// //None CalendarContract.Calendars.CAL_ACCESS_NONE Cannot access the calendar
// //freeBusy CalendarContract.Calendars.CAL_ACCESS_FREEBUSY Can only see free/busy information about the calendar
// //Read CalendarContract.Calendars.CAL_ACCESS_READ Can read all event details
// //Respond CalendarContract.Calendars.CAL_ACCESS_RESPOND Can reply yes/no/maybe to an event
// //Override CalendarContract.Calendars.CAL_ACCESS_OVERRIDE not used
// //Contributor CalendarContract.Calendars.CAL_ACCESS_CONTRIBUTOR Full access to modify the calendar, but not the access control settings
// //Editor CalendarContract.Calendars.CAL_ACCESS_EDITOR Full access to modify the calendar, but not the access control settings
// //Owner CalendarContract.Calendars.CAL_ACCESS_OWNER Full access to the calendar
// //Root CalendarContract.Calendars.CAL_ACCESS_ROOT Domain admin
calendarvalues.put(CalendarContract.Calendars.OWNER_ACCOUNT, account);
calendarvalues.put(CalendarContract.Calendars.VISIBLE, 1);
calendarvalues.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
// calendarvalues.put(CalendarContract.Calendars.CALENDAR_LOCATION, "Spain");
Uri calUri = null;
Uri result = null;
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
PermissionUtil.requestCalendarPermission(activity);
return -1;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
calUri = CalendarContract.Calendars.CONTENT_URI;
}else{
calUri = Uri.parse("content://com.android.calendar/calendars");
}
if(calUri != null) {
if(local) {
calUri = calUri.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, "DUMMYLOCAL")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL)
.build();
}else {
calUri = calUri.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, account)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, account)
.build();
}
result = activity.getContentResolver().insert(calUri, calendarvalues);
}
if (result != null) {
try {
return Long.parseLong(result.getLastPathSegment());
} catch (Exception e) {
return -1;
}
}
return -1;
}
I think the mistake is in this line :
calendarvalues.put(CalendarContract.Calendars.ACCOUNT_TYPE, account);
because I have seen the values returned by the following query:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
calUri = CalendarContract.Calendars.CONTENT_URI;
}else{
calUri = Uri.parse(calendarUriString);
}
String[] projection = new String[]{
CalendarContract.Calendars._ID,
CalendarContract.Calendars.NAME,
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL,
CalendarContract.Calendars.ACCOUNT_NAME,
CalendarContract.Calendars.ACCOUNT_TYPE,
// CalendarContract.Calendars.CALENDAR_COLOR,
CalendarContract.Calendars.OWNER_ACCOUNT,
CalendarContract.Calendars.VISIBLE,
CalendarContract.Calendars.SYNC_EVENTS,
};
Cursor cursor = activity.getContentResolver().query(calUri, projection, null, null, null);
and the results are as follows:
id: 1
Name: My Calendar#Local
Display name: My Calendar
access level: 700
AccountName: My Calendar#Local
AccountType: com.local
ownerAccount: Owner Account visible: 1
sync: 1
id: 2
Name: test#gmail.com
Display name: test#gmail.com
access level: 700
AccountName: test#gmail.com
AccountType: com.google
ownerAccount: test#gmail.com
visible: 1
sync: 1
id: 3
Name: Test Cal
Display name: Test Cal
access level: 700
AccountName: test#gmail.com
AccountType: test#gmail.com
ownerAccount: 1
visible: 1
sync: 1
I tried to put the following:
calendarvalues.put(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google");
but the calendar is not created.
If anyone knows how to do it or have any examples or documentation that may be useful, it would be helpful.
Thanks in advance.
I was struggling with the same Problem - I ended up creating a calendar using the Google Calendar API insert function. With the Quickstart Guide you should be able to create a Calendar.
Use the MakeRequestTask#mService like this:
Calendar newCalendar = new Calendar();
newCalendar.setSummary("Calendar Name");
newCalendar.setTimeZone(TIME_ZONE); //assuming you have it as a constant somewhere
String newCalendarId = null;
try {
com.google.api.services.calendar.model.Calendar insertedCalendar = mService.calendars().insert(newCalendar).execute();
newCalendarId = insertedCalendar.getId();
} catch(Exception ignore){}
and then force an account sync with
Bundle extras = new Bundle();
extras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
ContentResolver.requestSync(selectedAccount, CalendarContract.AUTHORITY, extras);
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.
On my LG-G3 there is a default calendar named "Phone". It's not Google's.
I build an application which syncs events with the user's Google Calendars, but when I select all the calendars with a query - I get the "Phone" calendar too. Since it's not a Google calendar, I can't use it with the Google Calendar functions (insert, delete, etc.).
I can't see any different between "Phone" calendar and Google canledars except of its name. Is there any way to know if a calendar is Google's or not?
This is my query:
String[] l_projection = new String[] { Calendars._ID, Calendars.CALENDAR_DISPLAY_NAME, Calendars.CALENDAR_ACCESS_LEVEL, Calendars.ALLOWED_REMINDERS, Calendars.SYNC_EVENTS };
Uri l_calendars;
if (Build.VERSION.SDK_INT >= 8) {
l_calendars = Uri.parse("content://com.android.calendar/calendars");
} else {
l_calendars = Uri.parse("content://calendar/calendars");
}
try {
Cursor l_managedCursor = activity.getContentResolver().query(l_calendars, l_projection, null, null, null);
if (l_managedCursor.moveToFirst()) {
String l_methodAllow;
String l_accessPermission;
String l_calName;
String l_calId;
String l_syncEvents;
int l_cnt = 0;
int l_syncEventsCol = l_managedCursor.getColumnIndex(l_projection[4]);
int l_methodAllowCol = l_managedCursor.getColumnIndex(l_projection[3]);
int l_accessPermissionCol = l_managedCursor.getColumnIndex(l_projection[2]);
int l_nameCol = l_managedCursor.getColumnIndex(l_projection[1]);
int l_idCol = l_managedCursor.getColumnIndex(l_projection[0]);
do {
String access = l_managedCursor.getString(l_accessPermissionCol);
if (access.equals("500") || access.equals("600") || access.equals("700") || access.equals("800")) {
l_syncEvents = l_managedCursor.getString(l_syncEventsCol);
l_methodAllow = l_managedCursor.getString(l_methodAllowCol);
l_accessPermission = l_managedCursor.getString(l_accessPermissionCol);
l_calName = l_managedCursor.getString(l_nameCol);
l_calId = l_managedCursor.getString(l_idCol);
calNames.add(l_calName);
// ....
++l_cnt;
}
} while (l_managedCursor.moveToNext());
}
} catch (Exception e) {
// ...
}
Google calendar can be identified by looking at the domain name of the Calendar ID. For primary calendar, calendar ID domain name is #gmail.com. If its secondary calendar, calendar ID domain name is group.calendar.google.com
I'm making a basic Dashclock extension that polls CalendarContract.Events for a list of all calendar events synced to the user's device, retrieve the one that's scheduled to happen the soonest, and post the time, title, location, and desctiption. Here's my code:
public class FullCalService extends DashClockExtension {
public static final String[] FIELDS = { Events._ID, Events.TITLE,
Events.ALL_DAY, Events.EVENT_LOCATION, Events.DTSTART,
Events.EVENT_TIMEZONE, Events.DESCRIPTION };
public FullCalService() {
}
#Override
protected void onUpdateData(int arg0) {
TimeZone tz = TimeZone.getDefault();
long currentTimeMillis = Calendar.getInstance().getTimeInMillis() - tz.getRawOffset();
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
Cursor c;
if (prefs.getBoolean("allDayAllowed", false)) {
c = getContentResolver().query(
Events.CONTENT_URI,
FIELDS,
new StringBuilder().append("(").append(Events.DTSTART)
.append(" >= ?)").toString(),
new String[] { Long.toString(currentTimeMillis) },
Events.DTSTART, null);
} else {
c = getContentResolver().query(
Events.CONTENT_URI,
FIELDS,
new StringBuilder().append("((").append(Events.ALL_DAY)
.append("= ?) AND (").append(Events.DTSTART)
.append(" >= ?))").toString(),
new String[] { Integer.toString(0),
Long.toString(currentTimeMillis) }, Events.DTSTART,
null);
}
if (c.moveToFirst()) {
long eventTimeMillis = c.getLong(c.getColumnIndex(Events.DTSTART));
// if (tz.inDaylightTime(new Date(eventTimeMillis))) {
// eventTimeMillis += tz.getDSTSavings();
// }
//Log.d("DesCal service", "Value of hoursToReveal: "+prefs.getString("hoursToReveal", "1"));
if (eventTimeMillis < currentTimeMillis + 360000
* Integer.parseInt(prefs.getString("hoursToReveal", "1"))) {
String title = c.getString(c.getColumnIndex(Events.TITLE));
String loc = c.getString(c
.getColumnIndex(Events.EVENT_LOCATION));
String time = DateUtils.formatDateTime(this, eventTimeMillis,
DateUtils.FORMAT_SHOW_TIME);
String desc = c.getString(c.getColumnIndex(Events.DESCRIPTION));
StringBuilder expandedBody = new StringBuilder(time);
if (!loc.isEmpty()){
expandedBody.append(" - ").append(loc);
}
expandedBody.append("\n").append(desc);
String uri = new StringBuilder(
"content://com.android.calendar/events/").append(
c.getLong(c.getColumnIndex(Events._ID))).toString();
publishUpdate(new ExtensionData()
.visible(true)
.status(time)
.expandedTitle(title)
.expandedBody(expandedBody.toString())
.icon(R.drawable.ic_dash_cal)
.clickIntent(
new Intent(Intent.ACTION_VIEW, Uri.parse(uri))));
c.close();
} else {
publishUpdate(new ExtensionData().visible(false));
c.close();
}
} else {
publishUpdate(new ExtensionData().visible(false));
c.close();
}
}
}
Upon first install, it appeared to work just fine. However, after the event began, it would not grab any future events. Is there a reason why the extension will not refresh itself?
How are you triggering further updates? You need to manually specify when you'd like to have onUpdateData called, e.g. when there's a change to a content provider, or when the screen turns on, etc. Extensions by default refresh only every 30 minutes or so. See the source for the built in calendar extension for example code.
I have added a calendar event programatically using the caledarcontract api and obtained a eventId. Similarly i added a reminder for this event and saved the reminderId too. Now i dont want a reminder for this event(or i would like to turn off the reminder), so i am trying to delete the reminder using the reminderId but i am not able to delete. I tried to delete the reminder using the eventId too but its not working.
public int AddEventToCalendar(String calendarId, Entity entity) {
// TODO Auto-generated method stub
ContentValues event = new ContentValues();
event.put("calendar_id", calendarId);
event.put("title", entity.description);
event.put("dtstart", System.currentTimeMillis());
event.put("dtend", System.currentTimeMillis() + 3600*1000);
event.put("allDay", 0);
//status: 0~ tentative; 1~ confirmed; 2~ canceled
event.put("eventStatus", 1);
//0~ default; 1~ confidential; 2~ private; 3~ public
event.put("visibility", 0);
//0~ opaque, no timing conflict is allowed; 1~ transparency, allow overlap of scheduling
event.put("transparency", 0);
//0~ false; 1~ true
event.put("hasAlarm", 1);
Uri add_eventUri;
if (Build.VERSION.SDK_INT >= 8) {
add_eventUri = Uri.parse("content://com.android.calendar/events");
} else {
add_eventUri = Uri.parse("content://calendar/events");
}
Uri l_uri = context.getContentResolver().insert(add_eventUri, event);
if(l_uri != null)
{
long eventID = Long.parseLong(l_uri.getLastPathSegment());
return (int) eventID;
}
else
return 0;
}
public int AddReminderOnEvent(Entity entity)
{
if(entity.eventId != 0)
{
ContentValues reminderValues = new ContentValues();
reminderValues.put("event_id", entity.eventId);
reminderValues.put("method", 1);// will alert the user with a reminder notification
reminderValues.put("minutes", 0);// number of minutes before the start time of the event to fire a reminder
Uri reminder_eventUri;
if (Build.VERSION.SDK_INT >= 8) {
reminder_eventUri = Uri.parse("content://com.android.calendar/reminders");
} else {
reminder_eventUri = Uri.parse("content://calendar/reminders");
}
Uri r_uri = context.getContentResolver().insert(reminder_eventUri, reminderValues);
if(r_uri != null)
{
long reminderID = Long.parseLong(r_uri.getLastPathSegment());
return (int) reminderID;
// Toast.makeText(getApplicationContext(), "Event Created Successfully", Toast.LENGTH_LONG).show();
}
else
return 0;
}
else
{
return 0;
}
}
public boolean DeleteReminderOnTask(int eventId, int reminderId) {
// TODO Auto-generated method stub
Uri delete_reminderUri;
if (Build.VERSION.SDK_INT >= 8) {
delete_reminderUri = Uri.parse("content://com.android.calendar/reminders");
} else {
delete_reminderUri = Uri.parse("content://calendar/reminders");
}
delete_reminderUri = ContentUris.withAppendedId(delete_reminderUri, reminderId);
int rows = context.getContentResolver().delete(delete_reminderUri,null , null);
if(rows > 0)
return true;
else
return false;
}
After executing this code everytime the rows returns 0 meaning that no rows have been altered. And the reminder comes up exactly at the appropriate time. How to delete the reminder from the calendar without deleting the event?
I'm not sure which SDK version you're running against when failing, but this code (which is essentially the same as yours, less the version check) works for me:
Uri reminderUri = ContentUris.withAppendedId(
CalendarContract.Reminders.CONTENT_URI, reminderId);
int rows = contentResolver.delete(reminderUri, null, null);
I got reminderId by querying the event's reminders:
String[] projection = new String[] {
CalendarContract.Reminders._ID,
CalendarContract.Reminders.METHOD,
CalendarContract.Reminders.MINUTES
};
Cursor cursor = CalendarContract.Reminders.query(
contentResolver, eventId, projection);
while (cursor.moveToNext()) {
long reminderId = cursor.getLong(0);
int method = cursor.getInt(1);
int minutes = cursor.getInt(2);
// etc.
}
cursor.close();
This might not be the only or best way, but all I could figure out was how to remove all reminders for an event. I don't know of a way to remove just one reminder.
//What we want to update
ContentValues values = new ContentValues();
values.put(Events.HAS_ALARM, 0);
//We're setting the event to have no alarms
int result = getContentResolver().update(
Events.CONTENT_URI,
values,
Events._ID + " = ?",
new String[]{"44"}
);
Unfortunately, this removes all reminders, but I'm not sure multiple reminders are really supported by Android 14+ or most calendar providers (e.g. Exchange). The calendar app in ICS only allows adding one reminder (despite saying "Add Reminders").
And if i use another application such as Business Calendar to add multiple reminders, when I check in Exchange, it only shows ones reminder. It does show multiple reminders in the calendar app but only on that device, not on other devices, so multiple reminders seem to be local only.