Skip to content

Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.

feat: add tools to edit and delete calendar events and include event IDs in listing output

Rishabh Bajpai439e3ba2e63a
diff --git a/app/src/main/java/com/gotcha/tools/CalendarTool.kt b/app/src/main/java/com/gotcha/tools/CalendarTool.kt
index a825344..78108e7 100644
--- a/app/src/main/java/com/gotcha/tools/CalendarTool.kt
+++ b/app/src/main/java/com/gotcha/tools/CalendarTool.kt
@@ -57,6 +57,7 @@ class CalendarTool(private val context: Context) {
ContentUris.appendId(builder, rangeEnd)
val projection = arrayOf(
+ CalendarContract.Instances.EVENT_ID,
CalendarContract.Instances.TITLE,
CalendarContract.Instances.BEGIN,
CalendarContract.Instances.END,
@@ -78,6 +79,7 @@ class CalendarTool(private val context: Context) {
"${CalendarContract.Instances.BEGIN} ASC"
).use { cursor ->
if (cursor == null) return ToolResult.error("Could not read the calendar.")
+ val idIdx = cursor.getColumnIndexOrThrow(CalendarContract.Instances.EVENT_ID)
val titleIdx = cursor.getColumnIndexOrThrow(CalendarContract.Instances.TITLE)
val beginIdx = cursor.getColumnIndexOrThrow(CalendarContract.Instances.BEGIN)
val endIdx = cursor.getColumnIndexOrThrow(CalendarContract.Instances.END)
@@ -88,6 +90,7 @@ class CalendarTool(private val context: Context) {
val out = StringBuilder()
var count = 0
while (cursor.moveToNext() && count < 50) {
+ val eventId = cursor.getLong(idIdx)
val title = cursor.getString(titleIdx) ?: "(untitled)"
val begin = readable.format(Date(cursor.getLong(beginIdx)))
val end = readable.format(Date(cursor.getLong(endIdx)))
@@ -101,7 +104,7 @@ class CalendarTool(private val context: Context) {
}
val calName = cursor.getString(calIdx)
- out.append("- $begin")
+ out.append("[id=$eventId] $begin")
if (status != null) out.append(" [$status]")
out.append(" $title")
if (calName != null) out.append(" 📅 $calName")
@@ -196,6 +199,89 @@ class CalendarTool(private val context: Context) {
}
}
+ fun editEvent(
+ eventId: Long,
+ title: String? = null,
+ start: String? = null,
+ end: String? = null,
+ location: String? = null,
+ description: String? = null,
+ allDay: Boolean? = null,
+ reminderMinutes: Int? = null
+ ): ToolResult {
+ if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_CALENDAR)
+ != PackageManager.PERMISSION_GRANTED
+ ) {
+ return ToolResult.permissionNeeded(
+ Manifest.permission.WRITE_CALENDAR,
+ "The Calendar permission is not granted. Go to Settings → Permissions → Calendar and enable it, then ask again."
+ )
+ }
+ return try {
+ val values = ContentValues()
+ if (title != null) values.put(CalendarContract.Events.TITLE, title)
+ if (start != null) {
+ val ms = parseWhen(start) ?: return ToolResult.error("Could not understand the start time '$start'.")
+ values.put(CalendarContract.Events.DTSTART, ms)
+ }
+ if (end != null) {
+ val ms = parseWhen(end) ?: return ToolResult.error("Could not understand the end time '$end'.")
+ values.put(CalendarContract.Events.DTEND, ms)
+ }
+ if (location != null) values.put(CalendarContract.Events.EVENT_LOCATION, location)
+ if (description != null) values.put(CalendarContract.Events.DESCRIPTION, description)
+ if (allDay != null) values.put(CalendarContract.Events.ALL_DAY, if (allDay) 1 else 0)
+
+ val uri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventId)
+ val rows = context.contentResolver.update(uri, values, null, null)
+ if (rows == 0) {
+ return ToolResult.error("Event $eventId not found or could not be updated.")
+ }
+
+ // Update reminders: clear existing, add new if requested
+ if (reminderMinutes != null) {
+ context.contentResolver.delete(
+ CalendarContract.Reminders.CONTENT_URI,
+ "${CalendarContract.Reminders.EVENT_ID} = ?",
+ arrayOf(eventId.toString())
+ )
+ if (reminderMinutes >= 0) {
+ val reminderValues = ContentValues().apply {
+ put(CalendarContract.Reminders.EVENT_ID, eventId)
+ put(CalendarContract.Reminders.MINUTES, reminderMinutes)
+ put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT)
+ }
+ context.contentResolver.insert(CalendarContract.Reminders.CONTENT_URI, reminderValues)
+ }
+ }
+
+ ToolResult.ok("Updated event $eventId.")
+ } catch (e: Exception) {
+ ToolResult.error("Could not update the event: ${e.message}")
+ }
+ }
+
+ fun deleteEvent(eventId: Long): ToolResult {
+ if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_CALENDAR)
+ != PackageManager.PERMISSION_GRANTED
+ ) {
+ return ToolResult.permissionNeeded(
+ Manifest.permission.WRITE_CALENDAR,
+ "The Calendar permission is not granted. Go to Settings → Permissions → Calendar and enable it, then ask again."
+ )
+ }
+ return try {
+ val uri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventId)
+ val rows = context.contentResolver.delete(uri, null, null)
+ if (rows == 0) {
+ return ToolResult.error("Event $eventId not found or could not be deleted.")
+ }
+ ToolResult.ok("Deleted event $eventId.")
+ } catch (e: Exception) {
+ ToolResult.error("Could not delete the event: ${e.message}")
+ }
+ }
+
private fun findCalendarByName(name: String): Long? {
val projection = arrayOf(
CalendarContract.Calendars._ID,
diff --git a/app/src/main/java/com/gotcha/tools/ToolDefinitions.kt b/app/src/main/java/com/gotcha/tools/ToolDefinitions.kt
index 719923e..b9c41e4 100644
--- a/app/src/main/java/com/gotcha/tools/ToolDefinitions.kt
+++ b/app/src/main/java/com/gotcha/tools/ToolDefinitions.kt
@@ -489,6 +489,64 @@ object ToolDefinitions {
}
)
+ val editCalendarEvent = tool(
+ "edit_calendar_event",
+ "Update an existing calendar event. Only the fields you provide will be changed. " +
+ "Get the event ID from list_calendar_events first.",
+ schema {
+ putJsonObject("properties") {
+ putJsonObject("event_id") {
+ put("type", "integer")
+ put("description", "Event ID from list_calendar_events.")
+ }
+ putJsonObject("title") {
+ put("type", "string")
+ put("description", "New title. Omit to keep current.")
+ }
+ putJsonObject("start") {
+ put("type", "string")
+ put("description", "New start time as 'yyyy-MM-dd HH:mm' or epoch millis. Omit to keep current.")
+ }
+ putJsonObject("end") {
+ put("type", "string")
+ put("description", "New end time. Omit to keep current.")
+ }
+ putJsonObject("location") {
+ put("type", "string")
+ put("description", "New location. Omit to keep current.")
+ }
+ putJsonObject("description") {
+ put("type", "string")
+ put("description", "New description. Omit to keep current.")
+ }
+ putJsonObject("all_day") {
+ put("type", "boolean")
+ put("description", "Set or unset all-day. Omit to keep current.")
+ }
+ putJsonObject("reminder_minutes") {
+ put("type", "integer")
+ put("description", "New reminder before event in minutes. -1 to remove reminder. Omit to keep current.")
+ }
+ }
+ putJsonArray("required") { add("event_id") }
+ }
+ )
+
+ val deleteCalendarEvent = tool(
+ "delete_calendar_event",
+ "Permanently delete a calendar event. Requires explicit user confirmation (destructive action). " +
+ "Get the event ID from list_calendar_events first.",
+ schema {
+ putJsonObject("properties") {
+ putJsonObject("event_id") {
+ put("type", "integer")
+ put("description", "Event ID from list_calendar_events.")
+ }
+ }
+ putJsonArray("required") { add("event_id") }
+ }
+ )
+
val setAlarm = tool(
"set_alarm",
"Set a clock alarm at a given hour and minute via the device clock app.",
@@ -1217,7 +1275,7 @@ object ToolDefinitions {
setWallpaper, runCommand,
// Tier 0–2 additions
callNumber, readCallLog, findContact, addContact, sendSms, readRecentSms,
- listCalendarEvents, createCalendarEvent, setAlarm, setTimer,
+ listCalendarEvents, createCalendarEvent, editCalendarEvent, deleteCalendarEvent, setAlarm, setTimer,
toggleTorch, setVolume, getVolume, setRingerMode, vibrate, setDnd,
getLocation, listInstalledApps, uninstallApp, getAppUsage, getDataUsage,
getClipboard, setClipboard, takePhoto, startAudioRecording, stopAudioRecording,
diff --git a/app/src/main/java/com/gotcha/tools/ToolExecutor.kt b/app/src/main/java/com/gotcha/tools/ToolExecutor.kt
index 8d42fc4..43f5eaf 100644
--- a/app/src/main/java/com/gotcha/tools/ToolExecutor.kt
+++ b/app/src/main/java/com/gotcha/tools/ToolExecutor.kt
@@ -186,6 +186,19 @@ class ToolExecutor(context: Context) {
reminderMinutes = args.requireInt("reminder_minutes"),
calendarName = args.requireString("calendar_name")
)
+ "edit_calendar_event" -> calendarTool.editEvent(
+ eventId = args.requireInt("event_id")?.toLong() ?: return missing("event_id"),
+ title = args.requireString("title"),
+ start = args.requireString("start"),
+ end = args.requireString("end"),
+ location = args.requireString("location"),
+ description = args.requireString("description"),
+ allDay = args["all_day"]?.jsonPrimitive?.booleanOrNull,
+ reminderMinutes = args.requireInt("reminder_minutes")
+ )
+ "delete_calendar_event" -> calendarTool.deleteEvent(
+ eventId = args.requireInt("event_id")?.toLong() ?: return missing("event_id")
+ )
"set_alarm" -> alarmTool.setAlarm(
hour = args.requireInt("hour") ?: return missing("hour"),
minute = args.requireInt("minute") ?: return missing("minute"),
diff --git a/app/src/main/java/com/gotcha/tools/ToolRegistry.kt b/app/src/main/java/com/gotcha/tools/ToolRegistry.kt
index fb5bee2..4613ea8 100644
--- a/app/src/main/java/com/gotcha/tools/ToolRegistry.kt
+++ b/app/src/main/java/com/gotcha/tools/ToolRegistry.kt
@@ -30,7 +30,8 @@ object ToolRegistry {
* to the user before the tool is actually executed.
*/
val destructiveTools: Set<String> = setOf(
- "uninstall_app"
+ "uninstall_app",
+ "delete_calendar_event"
)
/**