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,822
  • calendar2 V1.12.zip
    18.7 KB · Views: 2,286
  • calendar2_v1.13.zip
    20.4 KB · Views: 1,268
Last edited:

cmweb

Active Member
Licensed User
Longtime 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.

Any news on that?

Best regards,

Carsten

Gesendet von meinem GT-N7000 mit Tapatalk 2
 

lagore

Active Member
Licensed User
Longtime User
Calendar Library updated

I have finally updated the library (please see the first post for the library files and an updated example program) to make it compatible with android 4. There is no functionality change it just includes the new calendar library API so it should work with all versions of android, although as I said before some of the older phones heavily modified by the manufacturer may not work (i don't know which ones). It has worked on everything that I have used. I may create a version just for android 4 which will only have 3 functions 1) read events, 2) create event, 3) delete event, and by specifying parameters you will be able to access all fields in the events table much like using an sql table but using the API in the background to ensure compatibility with the OS.
 

cmweb

Active Member
Licensed User
Longtime User
Hey Edward,

thank you very very much!!!

Seems to work very well!!!

Thanks again,

Carsten
 

boten

Active Member
Licensed User
Longtime User
got error on using the library on SGS (2.2.1)

pgm:
B4X:
Sub Process_Globals
End Sub

Sub Globals
Dim mcal As MyCalendar
Dim mp As Map
End Sub

Sub Activity_Create(FirstTime As Boolean)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub Activity_Click
mp.Initialize
mp=mcal.GetListOfAllCalendars(False)
Log(mp.Size)
Log(mp)
End Sub


log:
B4X:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **

main_activity_click (java line: 212)
java.lang.NullPointerException
   at edsmith.calendar.Calendar.getCalendarManagedCursor(Calendar.java:638)
   at edsmith.calendar.Calendar.GetListOfAllCalendars(Calendar.java:116)
   at boten.klum.main._activity_click(main.java:212)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:105)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:89)
   at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:49)
   at android.view.View.performClick(View.java:2461)
   at android.view.View$PerformClick.run(View.java:8888)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:4627)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
   at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException
 

lagore

Active Member
Licensed User
Longtime User
You never initialized the calendar object
B4X:
Dim mcal As MyCalendar
   mcal.Initialize
 

AndroidHGV

New Member
Issue with recurring events

@Lagore:

Thanks for the library, I think it will come in handy!

I have a few remarks though:
1. How about "Duration field" when retrieving appointments? (I have found several events that don't have dtend set but use duration field)
2. Your code does not retrieve:
Alarm,
Recurrence settings
Alarm action/kind
Minutes
HasAlarm

and whatever fields I forgot. so a nice library to add appointments but in the state it is in, certainly not useable to work with the calendar ;)

Are you going to add code for those fields?

Best regards,
AndroidHGV
 
Last edited:

cmweb

Active Member
Licensed User
Longtime User
Hi Edward,

a short question. Why is the calendar object an Activity object?

I would love to have access to calendar data from a service, but that doesn't work as the calendar is an Activity object.

But why?

Best regards,

Carsten
 

lagore

Active Member
Licensed User
Longtime User
Good question and the simple answer is that when I put the library together I knew very little about java and less about writing a library for B4A so I read the specs at 'developer.android.com' for the calendar API and used an example of another library on B4A as a starting point (it happened to be an activity object). I am going to revisit the library to add some more functionality to it (see the post 2 up) and I will have a look at it not being an activity object.
 

cmweb

Active Member
Licensed User
Longtime User
Good question and the simple answer is that when I put the library together I knew very little about java and less about writing a library for B4A so I read the specs at 'developer.android.com' for the calendar API and used an example of another library on B4A as a starting point (it happened to be an activity object). I am going to revisit the library to add some more functionality to it (see the post 2 up) and I will have a look at it not being an activity object.

That would be really great....

Best regards,

Carsten

Gesendet von meinem GT-N7000 mit Tapatalk 2
 

apepper

New Member
Licensed User
Longtime User
Thanks for the example, but I'm having some kind of stupid problem. I've loaded the example, checked the PreferenceActivity library but I get "missing library reference" for AHPreferenceManager" so I can't compile the example. I'm running v2.0 B4A, PreferenceActivity 1.01, calendar 1.20, core 2.00.

Help!

-Andrew
 

cmweb

Active Member
Licensed User
Longtime User
Good question and the simple answer is that when I put the library together I knew very little about java and less about writing a library for B4A so I read the specs at 'developer.android.com' for the calendar API and used an example of another library on B4A as a starting point (it happened to be an activity object). I am going to revisit the library to add some more functionality to it (see the post 2 up) and I will have a look at it not being an activity object.

Any news on that?

It would be really helpful, if it wouldn't be an activity object...

Best regards,

Carsten

Gesendet von meinem GT-N7000 mit Tapatalk 2
 

cmweb

Active Member
Licensed User
Longtime User
Hi Edward,

I am going to revisit the library to add some more functionality to it (see the post 2 up) and I will have a look at it not being an activity object.
Any news on that?

I figured out a problem that came with Android 4.x.

I have a weekly recurring event in my calendar, let's say every friday at 2 p.m. (event has been created last year)

- On Android 2.x, I could catch that event with GetListofEventsforCalendarBetweenDates, because the event is included in a query for friday, august 3 2012.

- On Android 4.x, GetListofEventsforCalendarBetweenDates doesn't get that event on friday, august 3 2012.

Could you please try to reproduce that problem? Is there a chance for a library update which gets all these recurring events?

Best regards,

Carsten
 

lagore

Active Member
Licensed User
Longtime User
Ya will have a look at that,I have nearly sorted out the additional features. Am having a problem getting it to be a non activity object I keep getting a java null exception

Sent from my HTC Desire using Tapatalk 2
 

cmweb

Active Member
Licensed User
Longtime User
Ya will have a look at that,I have nearly sorted out the additional features. Am having a problem getting it to be a non activity object I keep getting a java null exception
Thanks... it's great to hear that you're still working on the library.

Did you already ask Erel in terms of the problem getting it to be a non activity object?

Best regards,

Carsten
 

lagore

Active Member
Licensed User
Longtime User
I have sorted out the problem re the recurring events, the problem was slightly different code between the pre 4.0 and post 4.0 android. With the pre 4.0 the events were listed from the "instances" table but with the post 4.0 the "events" table was used. Now both versions use the same tables (I hope). The routine "GetListofAllEventsforCalendar(int)" only retrieves from the 'events' table so if you have an event that starts on the first monday and recurs for the next 5 mondays only the first events in retrieved. All of the other routines which search between dates search the "instances" table so will return all 6 events. The Event ID is the same for all recurring events and if you delete this it will delete all 6.
The new routine "ListExtendedCalendarEntryDetails(int)" lists some additional event information and returns it in a Map. The returned info is;
* hasAlarm
* recurRule
* duration
* minutes
* method
* AttendeeName
* AttendeeEmail
* AttendeeStatus
If there is more then 1 attendee then the last 3 items are repeated so if the Map has 8 items there is only 1 attendee (or none if fields are null) it will have 11 for 2 attendees etc. If there is no info the field will be "null". There are lots more fields which can be included the full list of then can be seen in the unfiltered logs, let me know if any of them are of use.
I still have not had a chance to sort out the activity/process problem.
 

cmweb

Active Member
Licensed User
Longtime User
Hi Edward,

You are great! Thanks a lot!

Best regards,

Carsten

Gesendet von meinem GT-N7000 mit Tapatalk 2
 
Top