Skip to content

Instantly share code, notes, and snippets.

@prcaen
Created January 2, 2012 08:42
Show Gist options
  • Save prcaen/1549906 to your computer and use it in GitHub Desktop.
Save prcaen/1549906 to your computer and use it in GitHub Desktop.
[Java] GoogleCalendar Android Manager
public class GoogleCalendar {
private Context _context;
private GoogleCalendarCallback _callback;
public GoogleCalendar(Context c, GoogleCalendarCallback cb) {
_context = c;
_callback = cb;
}
public void add(final String title, final Long dateStart, final Long dateEnd,
final String address, final String description, final String website) {
if (!this.isExistEvent(title, dateStart.toString())) {
String[] projection = { "_id", "displayname" };
Cursor cursor = this.getCursor("calendars", projection, null, null, null);
if (cursor.moveToFirst()) {
final String[] calNames = new String[cursor.getCount()];
final int[] calIds = new int[cursor.getCount()];
for (int i = 0; i < calNames.length; i++) {
calIds[i] = cursor.getInt(0);
calNames[i] = cursor.getString(1);
cursor.moveToNext();
}
AlertDialog.Builder builder = new AlertDialog.Builder(_context);
builder.setSingleChoiceItems(calNames, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ContentValues cv = new ContentValues();
cv.put("calendar_id", calIds[which]);
cv.put("title", title);
cv.put("dtstart", dateStart);
cv.put("dtend", dateEnd);
cv.put("hasAlarm", 1);
cv.put("visibility", 0);
cv.put("eventLocation", address);
String fullDescription = description + " Site web : " + website;
cv.put("description", fullDescription);
Uri newEvent = insertValues("events", cv);
if (newEvent != null) {
long id = Long.parseLong(newEvent.getLastPathSegment());
ContentValues values = new ContentValues();
values.put("event_id", id);
values.put("method", 1);
values.put("minutes", 15);
insertValues("events", values);
_callback.added(true);
Toast.makeText(_context, R.string.event_added, Toast.LENGTH_SHORT).show();
}
dialog.cancel();
}
});
builder.create().show();
}
cursor.close();
} else {
Toast.makeText(_context, R.string.event_already_added, Toast.LENGTH_SHORT).show();
_callback.added(false);
}
}
private Uri insertValues(String type, ContentValues cv) {
final ContentResolver cr = _context.getContentResolver();
return cr.insert(this.getUriBase(type), cv);
}
private Cursor getCursor(String type, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
final ContentResolver cr = _context.getContentResolver();
return cr.query(this.getUriBase(type), projection, selection, selectionArgs, sortOrder);
}
private Uri getUriBase(String type) {
if (Integer.parseInt(Build.VERSION.SDK) == 8)
return Uri.parse("content://com.android.calendar/" + type);
else
return Uri.parse("content://calendar/" + type);
}
public Boolean isExistEvent(String title, String dtstart) {
String[] projection = { "calendar_id", "title", "dtstart" };
String selection = "title=? AND dtstart=?";
String[] selectionArgs = { title, dtstart };
Cursor cursor = this.getCursor("events", projection, selection, selectionArgs, null);
if (cursor != null) {
if (cursor.getCount() == 0) {
Log.d("Event existing", "false");
return false;
} else {
Log.d("Event existing", "true");
return true;
}
} else {
Log.d("Event existing", "no calendars");
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment