Small calendar library

gorelshv

Member
Licensed User
Longtime User
As we all know, there is no official API for accessing the calender events. However, there is an undocumented way to do it. Forum user eps already started working on wrapper, but I don't know how far along is he.
As I need it for my next project, I've done some searching and found this little piece of code that enables reading events from all calendars within a range of one week prior and one week after the current date. As all I want to do is to read events for next 7 days I took this code, modified it a bit and made a small library for B4A. Here's the code:
B4X:
package com.BennyG.Calendar;

import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;

import java.util.Date;
import java.util.HashSet;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.database.Cursor;
import android.net.Uri;
import android.text.format.DateUtils;

@ShortName("BGCal")
@Version(1.0f)
@Author("Benny G")
@Permissions(values={"android.permission.READ_CALENDAR"})

public class BGCal {

   public static String readCalendar(final BA ba) {
      //String strCals = "";
      String strEvents ="";
      ContentResolver contentResolver = ba.context.getContentResolver();

      // Fetch a list of all calendars synced with the device, their display names and whether the
      // user has them selected for display.
      
      final Cursor cursor = contentResolver.query(Uri.parse("content://com.android.calendar/calendars"),
            (new String[] { "_id", "displayName", "selected" }), null, null, null);
      // For a full list of available columns see http://tinyurl.com/yfbg76w

      HashSet<String> calendarIds = new HashSet<String>();
      
      while (cursor.moveToNext()) {

         final String _id = cursor.getString(0);
         //final String displayName = cursor.getString(1);
         final Boolean selected = !cursor.getString(2).equals("0"); // I have this line commented out in actual code. see notes for more information
         
         //strCals = strCals + "Id: " + _id + " Display Name: " + displayName + " Selected: " + selected;
         calendarIds.add(_id);
      }
      //return strCals;
      /// For each calendar, display all the events from now to the end of next week.      
      for (String id : calendarIds) {
         Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
         long now = new Date().getTime();
         ContentUris.appendId(builder, now);
         ContentUris.appendId(builder, now + DateUtils.WEEK_IN_MILLIS);

         Cursor eventCursor = contentResolver.query(builder.build(),
               new String[] { "title", "begin", "end", "allDay"}, "Calendars._id=" + id,
               null, "startDay ASC, startMinute ASC"); 
         /// For a full list of available columns see http://tinyurl.com/yfbg76w

         while (eventCursor.moveToNext()) {
            final String title = eventCursor.getString(0);
            final Date begin = new Date(eventCursor.getLong(1));
            final Date end = new Date(eventCursor.getLong(2));
            final Boolean allDay = !eventCursor.getString(3).equals("0");
            
            strEvents = strEvents + "Title: " + title + " Begin: " + begin + " End: " + end +
                  " All Day: " + allDay + " -- ";
         }
      }
      return strEvents;
   }

}

Usage:
B4X:
Dim Cal as BGcal
log(Cal.readCalendar)

This code will return a string with all events for the next 7 days separated by "--". That makes it easier to handle with Split.

I've attached the library.

A few things to note:

!!! This API is not officially documented by Google and may break in future updates !!!

1. This code will work on devices running Android 2.2 and up. in order to make it work on earlier version replace "content://com.android.calendar" with "content://calendar" (maybe adding an "if" statement that uses a different string depending on the version?)
2. I probably should've used string-builder instead of a regular string. Since I don't know Java, I need to do a little research...
3. It would be a good idea to change "--" to something more complex. Also note that the string also ends with "--".
4. It seems that this code reads events from all calendars, even ones which weren't selected to be displayed by the user. An "if" statement should be added to check if 'selected' is false and prevent adding that calendar to calendarIds.
5. some of the commented code was for testing...
6. Please note the added permission.
7. There is no support for writing (yet;))

I think this is a good starting point and hope someone will pick it up and make it into something much more useful.

- Benny
 

Attachments

  • BGCal.zip
    2.6 KB · Views: 503
Last edited:

eps

Expert
Licensed User
Longtime User
Hi

I've not been "around" much recently - far too much other stuff to do at the moment!!! Just in the process of completing App #2.... Your stuff sounds most interesting! I'll try and take a look, but it won't be for at least 1 week, if not a little more..

Thanks for sharing!

:)
 
Top