Calendar trigger
This commit is contained in:
@@ -9,8 +9,10 @@ import android.content.IntentFilter;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.jens.automation2.AutomationService;
|
||||
import com.jens.automation2.Miscellaneous;
|
||||
@@ -29,7 +31,6 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
static CalendarReceiver calendarReceiverInstance = null;
|
||||
static boolean calendarReceiverActive = false;
|
||||
static IntentFilter calendarIntentFilter = null;
|
||||
private static AutomationService automationServiceRef;
|
||||
private static Intent calendarIntent = null;
|
||||
|
||||
public static final int AVAILABILITY_OUT_OF_OFFICE = 4;
|
||||
@@ -45,6 +46,30 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
static AlarmManager alarmManager = null;
|
||||
static boolean wakeupNeedsToBeScheduled = false;
|
||||
|
||||
public static class RuleEventPair
|
||||
{
|
||||
Rule rule;
|
||||
CalendarEvent event;
|
||||
|
||||
public RuleEventPair(Rule rule, CalendarEvent event)
|
||||
{
|
||||
this.rule = rule;
|
||||
this.event = event;
|
||||
}
|
||||
}
|
||||
static List<RuleEventPair> calendarEventsUsed = new ArrayList<>(); // To determine for which events which rules have been executed
|
||||
public static void addUsedPair(RuleEventPair pair)
|
||||
{
|
||||
// Add pair only if it's not in the list already.
|
||||
for(RuleEventPair usedPair : calendarEventsUsed)
|
||||
{
|
||||
if(usedPair.rule.equals(pair.rule) && usedPair.event.equals(pair.event))
|
||||
return;
|
||||
}
|
||||
|
||||
calendarEventsUsed.add(pair);
|
||||
}
|
||||
|
||||
public static CalendarReceiver getInstance()
|
||||
{
|
||||
if(calendarReceiverInstance == null)
|
||||
@@ -73,9 +98,15 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkForRules(Context context)
|
||||
static void checkForRules(Context context)
|
||||
{
|
||||
//TODO: Overwrite notification
|
||||
/*
|
||||
Kann die selbe Regel mehrfach pro Termin ausgeführt werden? Nein, eh nicht, ne?
|
||||
Am nächsten Tag ist es wieder ein anderer Termin.
|
||||
|
||||
Wenn zwei zeitgleiche Termine mit gleichen Inhalten in verschiedenen Kalendern sind,
|
||||
würde die Regel so 2x ausgeführt werden.
|
||||
*/
|
||||
//TODO: Second appointment directly one after another or overlapping won't get executed
|
||||
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.calendarEvent);
|
||||
for (int i = 0; i < ruleCandidates.size(); i++)
|
||||
@@ -86,24 +117,9 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startListener(AutomationService automationService)
|
||||
public void startListener(AutomationService automationServiceRef)
|
||||
{
|
||||
if(!calendarReceiverActive)
|
||||
{
|
||||
if(calendarReceiverInstance == null)
|
||||
calendarReceiverInstance = new CalendarReceiver();
|
||||
|
||||
if(calendarIntentFilter == null)
|
||||
{
|
||||
calendarIntentFilter = new IntentFilter();
|
||||
calendarIntentFilter.addAction(Intent.ACTION_PROVIDER_CHANGED);
|
||||
// calendarIntentFilter.addDataScheme("content");
|
||||
}
|
||||
|
||||
AutomationService.getInstance().registerReceiver(calendarReceiverInstance, calendarIntentFilter);
|
||||
|
||||
calendarReceiverActive = true;
|
||||
}
|
||||
startCalendarReceiver(automationServiceRef);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -171,6 +187,37 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
{
|
||||
return title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
CalendarEvent compareEvent = (CalendarEvent) obj;
|
||||
return calendarId == compareEvent.calendarId
|
||||
&&
|
||||
eventId.equals(compareEvent.eventId)
|
||||
&&
|
||||
title.equals(compareEvent.title)
|
||||
&&
|
||||
description.equals(compareEvent.description)
|
||||
&&
|
||||
location.equals(compareEvent.location)
|
||||
&&
|
||||
availability.equals(compareEvent.availability)
|
||||
&&
|
||||
start.getTimeInMillis() == compareEvent.start.getTimeInMillis()
|
||||
&&
|
||||
end.getTimeInMillis() == compareEvent.end.getTimeInMillis()
|
||||
&&
|
||||
allDay == compareEvent.allDay;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Miscellaneous.logEvent("e", "CalendarReceiver compare()", Log.getStackTraceString(e), 5);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static List<AndroidCalendar> readCalendars(Context context)
|
||||
@@ -394,22 +441,22 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
Collections.sort(wakeUpCandidatesList);
|
||||
|
||||
if(wakeUpCandidatesList.size() == 0)
|
||||
Miscellaneous.logEvent("i", "calculateNextWakeupForCalendar()", "Not scheduling any calendar related wakeup as there are no future events that might match a configured trigger.", 5);
|
||||
Miscellaneous.logEvent("i", "calculateNextWakeupForCalendar()", "Not scheduling any calendar related wakeup as there are no future events that might match a configured trigger.", 4);
|
||||
else
|
||||
{
|
||||
if (nextWakeup == null || nextWakeup.getTimeInMillis() != wakeUpCandidatesList.get(0))
|
||||
{
|
||||
Calendar newAlarm = Miscellaneous.calendarFromLong(wakeUpCandidatesList.get(0));
|
||||
if (nextWakeup == null)
|
||||
Miscellaneous.logEvent("i", "calculateNextWakeupForCalendar()", "Chose " + Miscellaneous.formatDate(newAlarm.getTime()) + " as next wakeup for calendar triggers. Old was null.", 5);
|
||||
Miscellaneous.logEvent("i", "calculateNextWakeupForCalendar()", "Chose " + Miscellaneous.formatDate(newAlarm.getTime()) + " as next wakeup for calendar triggers. Old was null.", 4);
|
||||
else
|
||||
Miscellaneous.logEvent("i", "calculateNextWakeupForCalendar()", "Chose " + Miscellaneous.formatDate(newAlarm.getTime()) + " as next wakeup for calendar triggers. Old was " + Miscellaneous.formatDate(nextWakeup.getTime()), 5);
|
||||
Miscellaneous.logEvent("i", "calculateNextWakeupForCalendar()", "Chose " + Miscellaneous.formatDate(newAlarm.getTime()) + " as next wakeup for calendar triggers. Old was " + Miscellaneous.formatDate(nextWakeup.getTime()), 4);
|
||||
nextWakeup = newAlarm;
|
||||
if (!wakeupNeedsToBeScheduled)
|
||||
wakeupNeedsToBeScheduled = true;
|
||||
}
|
||||
else
|
||||
Miscellaneous.logEvent("i", "calculateNextWakeupForCalendar()", "Alarm " + Miscellaneous.formatDate(nextWakeup.getTime()) + " has been selected as next wakeup, but not rescheduling since this was not a change.", 5);
|
||||
Miscellaneous.logEvent("i", "calculateNextWakeupForCalendar()", "Alarm " + Miscellaneous.formatDate(nextWakeup.getTime()) + " has been selected as next wakeup, but not rescheduling since this was not a change.", 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -418,8 +465,6 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
{
|
||||
if (!calendarReceiverActive)
|
||||
{
|
||||
CalendarReceiver.automationServiceRef = automationServiceRef;
|
||||
|
||||
if (calendarReceiverInstance == null)
|
||||
calendarReceiverInstance = new CalendarReceiver();
|
||||
|
||||
@@ -438,4 +483,31 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
armOrRearmTimer();
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean mayRuleStillBeActivatedForPendingCalendarEvents(Rule rule)
|
||||
{
|
||||
for(CalendarEvent event : readCalendarEvents(Miscellaneous.getAnyContext(), false))
|
||||
{
|
||||
for(Trigger t : rule.getTriggerSet())
|
||||
{
|
||||
if(t.getTriggerType().equals(Trigger.Trigger_Enum.calendarEvent) && t.checkCalendarEvent(event, false))
|
||||
{
|
||||
if (!hasEventBeenUsedInRule(rule, event))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static boolean hasEventBeenUsedInRule(Rule rule, CalendarEvent event)
|
||||
{
|
||||
for (RuleEventPair executedPair : calendarEventsUsed)
|
||||
{
|
||||
if (executedPair.rule.equals(rule) && executedPair.event.equals(event))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user