From b6bf31589ab4b1f45e93a335527dcab0f92cfa39 Mon Sep 17 00:00:00 2001 From: jens Date: Mon, 25 Dec 2023 14:28:48 +0100 Subject: [PATCH] calendar trigger --- .../jens/automation2/ActivityManageRule.java | 2 +- .../receivers/CalendarReceiver.java | 32 +++++++++++++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/jens/automation2/ActivityManageRule.java b/app/src/main/java/com/jens/automation2/ActivityManageRule.java index 73d8ec1..db6af2b 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageRule.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageRule.java @@ -645,7 +645,7 @@ public class ActivityManageRule extends Activity else if(types[i].toString().equals(Trigger_Enum.subSystemState.toString())) items.add(new Item(typesLong[i].toString(), R.drawable.subsystemstate)); else if(types[i].toString().equals(Trigger_Enum.calendarEvent.toString())) - items.add(new Item(typesLong[i].toString(), R.drawable.calendar)); + items.add(new Item(typesLong[i].toString(), R.drawable.placeholder)); else items.add(new Item(typesLong[i].toString(), R.drawable.placeholder)); } diff --git a/app/src/main/java/com/jens/automation2/receivers/CalendarReceiver.java b/app/src/main/java/com/jens/automation2/receivers/CalendarReceiver.java index e8fdba3..053b76b 100644 --- a/app/src/main/java/com/jens/automation2/receivers/CalendarReceiver.java +++ b/app/src/main/java/com/jens/automation2/receivers/CalendarReceiver.java @@ -6,6 +6,7 @@ import android.content.Intent; import android.content.IntentFilter; import android.database.Cursor; import android.net.Uri; +import android.os.Build; import com.jens.automation2.AutomationService; import com.jens.automation2.Miscellaneous; @@ -95,8 +96,9 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis public static class CalendarEvent { - public String id, title, description, location, availability; + public String calendarId, eventId, title, description, location, availability; public Calendar start, end; + public boolean allDay; public boolean isCurrentActive() { @@ -107,10 +109,17 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis public static List readCalendarEvents(Context context) { - Cursor cursor = context.getContentResolver() - .query( + Cursor cursor; + +// if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO) +// cursor = context.getContentResolver().query( +// Uri.parse("content://calendar/calendars"), +// new String[] { "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation", "availability" }, +// null, null, null); +// else + cursor = context.getContentResolver().query( Uri.parse("content://com.android.calendar/events"), - new String[] { "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation", "availability" }, + new String[] { "calendar_id", "original_id", "title", "description", "allDay", "dtstart", "dtend", "eventLocation", "availability" }, null, null, null); cursor.moveToFirst(); @@ -124,13 +133,15 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis try { CalendarEvent event = new CalendarEvent(); - event.id = cursor.getString(1); + event.calendarId = cursor.getString(0); + event.eventId = cursor.getString(1); event.title = cursor.getString(2); event.description = cursor.getString(3); - event.start = Miscellaneous.calendarFromLong(Long.parseLong(cursor.getString(4))); - event.end = Miscellaneous.calendarFromLong(Long.parseLong(cursor.getString(5))); - event.location = cursor.getString(6); - event.availability = cursor.getString(7); + event.allDay = cursor.getString(4).equals("1"); + event.start = Miscellaneous.calendarFromLong(Long.parseLong(cursor.getString(5))); + event.end = Miscellaneous.calendarFromLong(Long.parseLong(cursor.getString(6))); + event.location = cursor.getString(7); + event.availability = cursor.getString(8); eventlist.add(event); } catch (Exception e) @@ -138,6 +149,9 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis cursor.moveToNext(); } + if(cursor != null) + cursor.close(); + return eventlist; }