Calendar trigger

This commit is contained in:
2024-01-07 15:15:56 +01:00
parent eaecf63724
commit 4521bc7d4e
5 changed files with 219 additions and 38 deletions

View File

@@ -9,6 +9,7 @@ import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.CalendarContract;
import android.util.Log;
import androidx.annotation.NonNull;
@@ -39,6 +40,7 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
static List<AndroidCalendar> calendarsCache = null;
static List<CalendarEvent> calendarEventsCache = null;
static List<CalendarEvent> calendarEventsReoccuringCache = null;
static Timer timer = null;
static TimerTask timerTask = null;
@@ -166,7 +168,7 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
public String location;
public String availability;
public Calendar start, end;
public boolean allDay;
public boolean allDay, reoccurring;
public boolean isCurrentlyActive()
{
@@ -223,7 +225,8 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
cursor = context.getContentResolver().query(
Uri.parse("content://com.android.calendar/calendars"),
new String[]{ "_id", "calendar_displayName", "ownerAccount", },
new String[]{ CalendarContract.Calendars._ID, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, CalendarContract.Calendars.OWNER_ACCOUNT, },
null, null, null);
cursor.moveToFirst();
@@ -256,7 +259,7 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
return calendarsCache;
}
public static List<CalendarEvent> readCalendarEvents(Context context, boolean includePastEvents)
public static List<CalendarEvent> readCalendarEvents(Context context, boolean includeReoccurring, boolean includePastEvents)
{
if(calendarEventsCache == null)
{
@@ -266,7 +269,17 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
cursor = context.getContentResolver().query(
Uri.parse("content://com.android.calendar/events"),
new String[] { "calendar_id", "_id", "title", "description", "allDay", "dtstart", "dtend", "eventLocation", "availability" },
new String[] {
CalendarContract.Events.CALENDAR_ID,
CalendarContract.Events._ID,
CalendarContract.Events.TITLE,
CalendarContract.Events.DESCRIPTION,
CalendarContract.Events.ALL_DAY,
CalendarContract.Events.DTSTART,
CalendarContract.Events.DTEND,
CalendarContract.Events.EVENT_LOCATION,
CalendarContract.Events.AVAILABILITY
},
null, null, null);
cursor.moveToFirst();
@@ -299,6 +312,7 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
event.end = Miscellaneous.calendarFromLong(Long.parseLong(cursor.getString(6)));
event.location = cursor.getString(7);
event.availability = cursor.getString(8);
event.reoccurring = false;
if(includePastEvents || event.end.getTimeInMillis() > now.getTimeInMillis())
calendarEventsCache.add(event);
@@ -313,6 +327,87 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
}
if(includeReoccurring && calendarEventsReoccuringCache == null)
{
calendarEventsReoccuringCache = new ArrayList<>();
Cursor cursor;
Calendar queryStart, queryEnd;
if(includePastEvents)
queryStart = Miscellaneous.calendarFromLong(0);
else
queryStart = Calendar.getInstance();
queryEnd = Calendar.getInstance();
queryEnd.add(Calendar.YEAR, 1);
cursor = context.getContentResolver().query(
Uri.parse("content://com.android.calendar/instances/when/" + queryStart.getTimeInMillis() + "/" + queryEnd.getTimeInMillis()),
new String[] {
CalendarContract.Instances.CALENDAR_ID,
CalendarContract.Instances._ID,
CalendarContract.Instances.TITLE,
CalendarContract.Instances.DESCRIPTION,
CalendarContract.Instances.ALL_DAY,
CalendarContract.Instances.BEGIN,
CalendarContract.Instances.END,
// CalendarContract.Instances.DTSTART,
// CalendarContract.Instances.DTEND,
CalendarContract.Instances.EVENT_LOCATION,
CalendarContract.Instances.AVAILABILITY
},
null, null, null);
cursor.moveToFirst();
// fetching calendars name
String CNames[] = new String[cursor.getCount()];
Calendar now = Calendar.getInstance();
for (int i = 0; i < CNames.length; i++)
{
try
{
CalendarEvent event = new CalendarEvent();
event.calendarId = Integer.parseInt(cursor.getString(0));
for(AndroidCalendar cal : readCalendars(context))
{
if(cal.calendarId == event.calendarId)
{
event.calendar = cal;
break;
}
}
event.eventId = cursor.getString(1);
event.title = cursor.getString(2);
event.description = cursor.getString(3);
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);
event.reoccurring = true;
if(includePastEvents || event.end.getTimeInMillis() > now.getTimeInMillis())
{
// For the moment keeping separate records to some extent
calendarEventsReoccuringCache.add(event);
calendarEventsCache.add(event);
}
}
catch (Exception e)
{}
cursor.moveToNext();
}
if(cursor != null)
cursor.close();
}
return calendarEventsCache;
}
@@ -363,7 +458,7 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
if(nextWakeup == null)
{
readCalendarEvents(Miscellaneous.getAnyContext(), false);
readCalendarEvents(Miscellaneous.getAnyContext(), true,false);
calculateNextWakeup();
}
@@ -395,7 +490,7 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
private static void calculateNextWakeup()
{
Calendar now = Calendar.getInstance();
List<CalendarEvent> events = readCalendarEvents(Miscellaneous.getAnyContext(), false);
List<CalendarEvent> events = readCalendarEvents(Miscellaneous.getAnyContext(), true, false);
if (events.size() == 0)
{
@@ -479,7 +574,7 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
public static boolean mayRuleStillBeActivatedForPendingCalendarEvents(Rule rule)
{
for(CalendarEvent event : readCalendarEvents(Miscellaneous.getAnyContext(), false))
for(CalendarEvent event : readCalendarEvents(Miscellaneous.getAnyContext(), true,false))
{
for(Trigger t : rule.getTriggerSet())
{