B4A Library Calendar Library

lagore

Active Member
Licensed User
:)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

Last edited:

peacemaker

Well-Known Member
Licensed User
Emulators do not support it, sure. But what minimum Android version is assumed for this lib ?
v.2.0 ?

On v.2.3.3 works OK.
 

lagore

Active Member
Licensed User
Hi,
I have tried it on a 2.2 and a tablet running 3.1 and both worked. It should work on a pre 2.0 but I have not had the chance to try it. The only fool proof way of doing it is to use the new google API which writes directly to your online calendar and then it syncs back to the phone/tablet so is independent of the version running, but I have not gotten around to looking at doing that yet!!.
Edward
 

eps

Well-Known Member
Licensed User
Excellent!! I was (honestly!!) going to write a lib for this, this week.

I'll start using it this week and see where I get.

One idea I had was for the library to internally work out what Android OS it was running on and the use the (currently) two different versions of accessing Calendars on Android.. Then if/when another new version comes along just update that part of the code..

:sign0098:
 

NJDude

Expert
Licensed User
@Lagore

Can you post some code samples?, personally, I'm a little confused on some of the features, especially the ones requiring the EventID.

I really like your lib, but some samples will help a lot. :D

EDIT TO ADD: If you could also add the ability to setup "Reminders" would be awesome.
 
Last edited:

lagore

Active Member
Licensed User
New version with Recurring rule and reminder added. Example program added; see first post
 

eps

Well-Known Member
Licensed User
What versions of Android will this work with?

I think there's Calendars up to 2.1 and then 2.2 --> 3.0 and now 4.0 (which finally gives us a documented Calendar interface, or I could be mistaken).

I don't think you can mix and match or rather compiling for one will mean that the others are excluded or brake?

i.e. here : New Public APIs in ICS | Android Developers Blog

I like keeping my App available to as many devices as possible... i.e. from 1.6 up...

Excellent work by the way!!
 

lagore

Active Member
Licensed User
This will work on everything below 4.0, as you said there is now a supported API for 4.0 and above (which we need to get working in B4A), remember that this may not work on some devices where the manufacturer has heavily modified the OS.
 

cmweb

Active Member
Licensed User
I'm currently dealing with the calendar2 library and like it.

I would like to catch the calendar appointments from a service. But that doesn't work, as MyCalendar appears to be an activity object.

Is there another way to use the lib from a service?

Best regards,

Carsten

Gesendet von meinem GT-N7000 mit Tapatalk
 

cmweb

Active Member
Licensed User
With Android 4.0, calendar2 doesn't work.

Is there a chance to use the calendar from Android 4.0 in any other way?

Best regards,

Carsten
 

sanjeeve

Member
Licensed User
Editing event error

Hi, the calendar example works on my mobile (Nexus S) but when i try to edit the event it throws an error (forced to close). any idea?

Thanks
 

sanjeeve

Member
Licensed User
fails when edit the event from the notification

Hi Erel
looks like i'm doing something wrong here...
I was checking the logs while debugging and everything looks okay. The issue comes only when tried to edit the event after opening the event from the notification.
It works fine if I opened the calendar app and then edit the event. don't know this makes sense and sorry if i confused you.
 

sanjeeve

Member
Licensed User
doesnt support Android 4

yes, this doesn't support Android 4. I got the below error when running on a Galaxy nexus.
would be really help if we can get a calendar function. :)

B4X:
main_activity_create (B4A line: 39)
myMap = cal.GetListOfAllCalendars(True)      'get a "Map" of the available calendars
android.database.sqlite.SQLiteException: no such column: access_level: , while compiling: SELECT _id, name FROM Calendars WHERE (access_level=700)
   at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:179)
   at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
   at android.content.ContentProviderProxy.query(ContentProviderNative.java:358)
   at android.content.ContentResolver.query(ContentResolver.java:310)
   at android.app.Activity.managedQuery(Activity.java:1706)
   at edsmith.calendar.Calendar.getCalendarManagedCursor(Calendar.java:649)
   at edsmith.calendar.Calendar.GetListOfAllCalendars(Calendar.java:116)
   at ed.smith.calendar.example.main._activity_create(main.java:248)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:136)
   at ed.smith.calendar.example.main.afterFirstLayout(main.java:84)
   at ed.smith.calendar.example.main.access$100(main.java:16)
   at ed.smith.calendar.example.main$WaitForLayout.run(main.java:72)
   at android.os.Handler.handleCallback(Handler.java:605)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4340)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
   at dalvik.system.NativeStart.main(Native Method)
android.database.sqlite.SQLiteException: no such column: access_level: , while compiling: SELECT _id, name FROM Calendars WHERE (access_level=700)
 

lagore

Active Member
Licensed User
Hi I am in the process of updating this library to work on android 4 (ICS) using the new official google calendar API, I have most of it done should be finished shortly.
 

cmweb

Active Member
Licensed User
That's great news... Please keep us posted. And thanks for your great work!

Best regards,

Carsten

Sent from my GT-N7000 using Tapatalk 2
 
Top