B4A Library Calendar Library

:)Hi,
Having had a need to be able to create and delete calendar events I produced my first calendar library (most of the code I got from the net).
The functions are;
GetListOfAllCalendars(boolean)
- this will return a 'Map' with the calendar Id and name. With this ID we can then create or delete events in this calendar.

CreateEvent(int CalID, String Title, String Desc, String Location, long StartTime, long EndTime, String RRule, boolean AllDay)
- used to create a calendar event using the calendar ID found above. At the moment the fields allowed are Calendar ID, TITLE, DESCRIPTION, LOCATION, STARTTIME, ENDTIME, Recurring Rule and ALLDAY.

Added with V1.1
CreateEventWithReminder(int CalID, String Title, String Desc, String Location, long StartTime, long EndTime, int ReminderTime, String RRule, boolean AllDay)
- used to create a calendar event using the calendar ID found above. At the moment the fields allowed are Calendar ID, TITLE, DESCRIPTION, LOCATION, STARTTIME, ENDTIME, Reminder Time, Recurring Rule and ALLDAY.

GetListofAllEventsforCalendar(int)
- using the calendar ID we can get a 'List' of all events for that calendar, items returned are the same as in the CreateEvent as well as the Event ID. the data is in the form of a List you just need to iterate thru it.

GetListofEventsforCalendarBetweenDates(int, long, long)
- As above but you can specify start and finish dates/time in milliseconds from epoch.

ListofEventsWithDescKeywordBetweenDates(int, String, long, long)
- similar to above but filter on 'string' in the Description field.

ListofEventsWithTitleKeywordBetweenDates(int, String, long, long)
- this time filter on Title field

DeleteCalendarEntry(int)
- once you have the event ID you can delete it with this routine.

This library works on the "unofficial" google model which works directly on the calendars on the phone its self this then syncs up to your google account online. The advantage is no problems with authorization but it probably will not work with all phones. I do hope to create a similar one using the new API which will work on everything and will not stop working. I have tried it on a HTC Desire 2.2 and an Asus tablet 3.1? and it worked on both.

To use it in your program,
B4X:
Dim Mycal As MyCalendar
Mycal.Initialize
myMap = Mycal.GetListOfAllCalendars(False)
Mycal.CreateEvent(1,"Test title 7","This is a test of the create event","At Work",(DateTime.Now ),(DateTime.Now + 3600000),False)
The code is well commented so should not be a problem.
Hope it is of use to someone, it has lots of expansion capability, update calendar event, add more fields to the create event routine, search on different fields etc.
Edward

Edit; Version 1.1 added,
  1. New module added to create events with reminder
  2. Added a recurring Rule field to the Create Event modules, see RFC 5545 - Internet Calendaring and Scheduling Core Object Specification (iCalendar) for all the many options available, it is then passed as a string e.g. "FREQ=MONTHLY;WKST=SU;BYDAY=2WE"
  3. Calendar Example attached, this demonstrates
    • Locating Calendar ID
    • Getting list of events for the selected calendar for the previous month.
    • Creating an event with a reminder
    • Creating an all day event
    • Creating a recurring event
The list of Calendars is loaded into a listview with the Calendar Name and ID.
A Preference Screen (AHPreferenceActivity) is also created to show how the calendar selection can be achieved thru a settings page on menu press.

Edit; Version 1.2 added.
  1. Updated to include the new Calendar API for Android 4 aka ICS
  2. Example program now shows events in a listview, a long_click gives the option to delete the event.
  3. To use the example program select the radio button for the required action then select the calendar you want this action on. The event list is not dynamic so after adding or deleting an event you will have to select read events for that calendar to update the listview.

Edit; Version 1.3 added see post #39 for details
  • New function 'ListExtendedCalendarEntryDetails(Value)' this returns a Map containing
    * hasAlarm
    * recurRule
    * duration
    * minutes
    * method
    * AttendeeName
    * AttendeeEmail
    * AttendeeStatus
  • Sorted bug where ICS did not get the recurring events.

Edit; Version 1.4 added
The bug that I thought I had fixed in v1.3 was not fixed this sorts it out (I hope)

Edit; Version 1.5 added
Details for recurring event now show the start and end time for the recurring event not the original event.

Edit; Version 1.6 added
Changed library to a process object. (no longer an activity object)

Edit; Version 1.7 added
Bug fixed where event timezone was not entered on pre 4.0 android non "all day" event.

Edit; Version 1.8 added
No change in functionality but now the events are returned sorted based on the date/time of the start of the event.

Edit; Version 1.9 added
The 2 create event methods now return the created event ID as a string

Edit; Version 1.11 added
Attached is an updated version of the library with 2 new functions;
GetEventDetails & UpdateEvent.
At the moment UpdateEvent allows you to change the
1) Title
2) Description
3) Location
4) StartTime
5) EndTime
If you do not want to change any of the first 3 pass "" for the relevant one.
To change the time set the ChangeTime Boolean to 'true' and pass both the start & end times. Both must be passed as the system can update both or neither not just one. If ChangeTime is set to 'false' a value must be still be entered in the StartTime & EndTime fields as the function still expects values even if they are not used. Normally I would not use the Boolean and just pass -1 for the time to indicate that it is not to be used but -1 is a valid time (1 millisecond before the epoch Jan 01 1970).

Fixed a bug where 'GetListOfAllCalendars(False)' only returned your "owned" calendars instead of all calendars.

Edit; Version 1.12 added See this post for some more detail
Added function 'GetExtendedListOfAllCalendars(int)'
This returns a LIST with the following calendar values
  • calID (long)
  • name (string)
  • displayName (string)
  • colourName (string)
  • location (string)
  • timezone (string)
  • accesslevel (string)
  • owner account (string)
The value passed in the function is the Access Level, details of the different values are contained in the info popup in B4A.
The Returned LIST can contain no calendars to many calendars depending on the Access Level used. If there is more then 1 calendar the group of 8 values for the additional calendars is repeated.
 

Attachments

  • CalendarExample v1.2.zip
    9.5 KB · Views: 3,986
  • calendar2 V1.12.zip
    18.7 KB · Views: 2,425
  • calendar2_v1.13.zip
    20.4 KB · Views: 1,436
Last edited:

lagore

Active Member
Licensed User
Longtime User
Will read through the api and see what's involved.


Sent from my Nexus 7 using Tapatalk HD
 

lagore

Active Member
Licensed User
Longtime User
To implement Tasks is going to be a bit more complicated. As far as I can see android does not have support for Tasks directly. Tasks are part of your online google account that are synced back to your device. There is an API for creating/viewing/modifying your online Tasks which will require OAuth2, all of this is possible just not sure what sort of time scale I am looking at.
 

Harris

Expert
Licensed User
Longtime User
Thanks for having a look. I am just trying to keep the complexity result storage to what (may) be provided on-line (in the shared calendar). Perhaps the next version(s) of Android may have native task support.
 

mcmanu

Active Member
Licensed User
Longtime User
Android 4.2.1

Hi,
Does the calendar Library work under 4.2.1?
I have a rooted Sgs2 and when i create an event, the event is saved but some seconds after the calender entry is automatic deleted


Sorry because of my english ;)
 

lagore

Active Member
Licensed User
Longtime User
It does work in 4.2. my Nexux 7 is running 4.2 and it works fine in that. it may be something to do with being rooted, if the stock calendar database was changed it the root version then that may be your problem.
 

mcmanu

Active Member
Licensed User
Longtime User
Another Question

I have another Question :) Sorry for that ;)

What Values can the calendar id have? only 1 to 9?
Or even two letters (10 for exampe)

I hope you know what i mean ;)
 

lagore

Active Member
Licensed User
Longtime User
The value of calendar ID depends on how many calendars you have created and how many you subscribe to, in my case mine goes up to 12 although in the last android update it re-numbered my calendars and the ID'S now go into the twenties. the value is set by android when the calendar is created, it is a field in the calendar sql database.

Sent from my HTC One X using Tapatalk 2
 
Last edited:

mcmanu

Active Member
Licensed User
Longtime User
Thanks

Thank you very much ;)
Yes i Noticed this, because i have two Phones, one with ICS one with the latest Jelly bean version, and they use different id´s for the same Calendar.
 
Last edited:

quotidianvoid

Member
Licensed User
Longtime User
Thank you for a great library! I have one request though, would it be possible to change the CreateEvent and CreateEventWithReminder functions to return the newly created event ID?
 

lagore

Active Member
Licensed User
Longtime User
The CreateEvent and CreateEventWithReminder functions now return the created event ID as a 'string' see post #1.
 

MichaelAust

Member
Licensed User
Longtime User
GetListofAllEventsforCalendar returns deleted events

Nice library, really useful.
When I used GetListofAllEventsforCalendar, it returned 3 events which had previously been deleted (and did not show up in the normal calendar app). Is there some flag in the event record which says that it is a deleted event ?

Using
B4X:
ListOfEvents=cal.GetListofEventsforCalendarBetweenDates(calendar_ID_global,DateTime.Add(DateTime.Now,-20,0,0),DateTime.Add(DateTime.Now,20,0,0))
(aiming for 20 years ago to 20 years ahead)
I get 2 events which were last year but not one which was yesterday (all deleted).
Maybe I am doing something wwong.
I have attached my test project.
I am using a Samsung Galaxy Player 5.0, Android Gingerbread 2.3.5
 

Attachments

  • CalendarTest.zip
    7.8 KB · Views: 396
Last edited:

lagore

Active Member
Licensed User
Longtime User
Hi Michael,
Your code usage is correct, I suspect the deleted events that are showing up are in some way orphaned/in limbo. Calendar events have many hidden fields and flags, two of these are 'Dirty & Deleted'. When an event is modified the dirty flag is = '1' when that modification is synced across all devices & online then the dirty flag is '0'. The same thing happens when you delete an event both the deleted & dirty flags are = '1' but the event exists in the background until synced then it is removed completely. You can watch this happening if you are quick. Create an event with your app. While connected to the log viewer in B4A uncheck 'filter' it will list a large amount of items wait a moment for it to more or less stop then click on clear, now using your app list the events for the calendar that you created the event in (easiest if you use a calendar which only has the one event) all of the fields from the event database are shown in the log. To make it easier to find the calendar data in all of the other data I have put
"************ START ************" and
*************FINISH *************
at both ends of the data. Have a look at the different fields, deleted is in the middle of the bunch and dirty is about 2/3 down. If you now go into a regular calendar app and delete that event then quickly back to your app and re-list the events you should see the event with the deleted & dirty flags = 1. Then once that event is synced it will no longer list in your app, this normally happens fairly quickly. Have a look at your 3 rogue events and see it their deleted & dirty flags are = 1, that would explain why you are seeing them. My library does not check the deleted flag when listing events I had not thought about this scenario it is probably something I could change in the background. I hope this makes sense and is of use.
Regards
 

MichaelAust

Member
Licensed User
Longtime User
Thanks for the reply Edward. I checked the log as you suggested, and the deleted flag is not set for any of them but _sync_dirty is. The calendar that they are in does not sync anywhere as far as I can tell (not listed in the Google Calendar settings). I have looked through the flags and cannot see any one which would obviously prevent these events being listed in Google Calendar. Here are the flags for one of the events.
B4X:
**START Calendar Event Description**
Test--1345945680000-1345949280000--0-30
originalEvent=null
contact_data_id=null
contact_id=null
visibility=0
rrule=null
hasAlarm=1
rdate=null
transparency=0
timezone=null
dtstart=1345945680000
_sync_time=null
hasAttendeeData=1
commentsUri=null
description=
htmlUri=null
longitude=0
_sync_account=local
_sync_version=null
hasExtendedProperties=1
facebook_service_provider=null
eventLocation=
dtend=1345949280000
allDay=0
facebook_post_time=null
organizer=
deleted=0
url=null
originalInstanceTime=null
selfAttendeeStatus=0
facebook_mem_count=null
facebook_owner=null
contact_account_type=null
latitude=0
eventTimezone=Australia/Sydney
availabilityStatus=0
ownerAccount=
_sync_account_type=local
lastDate=1345949280000
guestsCanModify=0
guestsCanSeeGuests=1
exrule=null
selected=1
title=Test
_id=30
facebook_schedule_id=null
_sync_id=MyCalendar2
facebook_photo_url=null
facebook_event_type=null
facebook_hostname=null
calendar_id=1
contactEventType=0
access_level=700
_sync_local_id=null
originalAllDay=null
_sync_dirty=1
duration=null
color=-16468481
guestsCanInviteOthers=1
exdate=null
eventStatus=null
**END Calendar Event Description**
I am aiming to write a calendar app which duplicates some of the functions I had in my old palm device (the alarms in particular), and I think I may write it completely separately from the built in calendar, as there seem to be too many gotchas and too much complexity in the built in one.
 

motley

Member
Licensed User
Longtime User
if I'm using the example program, when I create new event you do not do anything, annex the screenshot

thanks

I have the same problem with emulator - it just show nothing, clicking on radio buttons does nothing... What I can do to test it on emulator???

see image
 

Attachments

  • cal1.png
    cal1.png
    27.8 KB · Views: 382

peacemaker

Expert
Licensed User
Longtime User
Emulator does not support. Use a real device.
 

motley

Member
Licensed User
Longtime User
btw it seems doesn't work on some devices...

so have anybody some example of calendar application that works with the sql db or something like this??
 

lagore

Active Member
Licensed User
Longtime User
Which version of android does the LG use. I have used it on many different types of phone and tablet from 2.1 to 4.2 with no problem.

Sent from my HTC One X using Tapatalk 2
 
Top