The js file is as follows
function CalendarPlugin() {}
CalendarPlugin.prototype.createEvent = function(title, location, notes, startDate, endDate, successCallback, errorCallback) {
if (typeof errorCallback != "function") {
console.log("CalendarPlugin.createEvent failure: errorCallback parameter must be a function");
return;
}
Code: Select all
if (typeof successCallback != "function") {
console.log("CalendarPlugin.createEvent failure: successCallback parameter must be a function");
return;
}
cordova.exec(successCallback, errorCallback, "CalendarPlugin", "createEvent", [title, location, notes, startDate, endDate, {
"title": title,
"description": notes,
"eventLocation": location,
"startTimeMillis": startDate.getTime(),
"endTimeMillis": endDate.getTime()
}]);
};
CalendarPlugin.install = function() {
if (!window.plugins) {
window.plugins = {};
}
Code: Select all
window.plugins.CalendarPlugin = new CalendarPlugin();
return window.plugins.CalendarPlugin;
};
cordova.addConstructor(CalendarPlugin.install);
The Java file is as follows
package de.drid.calendarPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Intent;
public class CalendarPlugin extends CordovaPlugin {
public static final String ACTION_ADD_CALENDAR_ENTRY = "createEvent";
Code: Select all
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
try {
if (ACTION_ADD_CALENDAR_ENTRY.equals(action)) {
JSONObject arg_object = args.getJSONObject(5);
Intent calIntent = new Intent(Intent.ACTION_EDIT)
.setType("vnd.android.cursor.item/event")
.putExtra("beginTime", arg_object.getLong("startTimeMillis"))
.putExtra("endTime", arg_object.getLong("endTimeMillis"))
.putExtra("title", arg_object.getString("title"))
.putExtra("description", arg_object.getString("description"))
.putExtra("eventLocation", arg_object.getString("eventLocation"));
this.cordova.getActivity().startActivity(calIntent);
callbackContext.success();
return true;
}
callbackContext.error("Invalid action");
return false;
} catch(Exception e) {
System.err.println("Exception: " + e.getMessage());
callbackContext.error(e.getMessage());
return false;
}