From fe924f6fe98fcb3e5933710e22264ba53122479e Mon Sep 17 00:00:00 2001 From: Jens Date: Sun, 14 Jan 2024 23:18:12 +0100 Subject: [PATCH] Calendar trigger --- .../java/com/jens/automation2/Rule.java | 25 ++++++++++++++- .../java/com/jens/automation2/Trigger.java | 3 -- .../receivers/CalendarReceiver.java | 31 ++++++++++++++++++- 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/app/src/apkFlavor/java/com/jens/automation2/Rule.java b/app/src/apkFlavor/java/com/jens/automation2/Rule.java index 4af1dd0..9de1f29 100644 --- a/app/src/apkFlavor/java/com/jens/automation2/Rule.java +++ b/app/src/apkFlavor/java/com/jens/automation2/Rule.java @@ -515,7 +515,7 @@ public class Rule implements Comparable { boolean isActuallyToggleable = isActuallyToggable(); - boolean notLastActive = getLastActivatedRule() == null || !getLastActivatedRule().equals(Rule.this); +// boolean notLastActive = getLastActivatedRule() == null || !getLastActivatedRule().equals(Rule.this); boolean doToggle = ruleToggle && isActuallyToggleable; String message; @@ -529,6 +529,29 @@ public class Rule implements Comparable if(Settings.startNewThreadForRuleActivation) publishProgress(message); + /* + Make a note of Rule/CalendarEvent executed combinations + */ + if(Rule.this.hasTriggerOfType(Trigger.Trigger_Enum.calendarEvent)) + { + for(CalendarReceiver.CalendarEvent event : CalendarReceiver.getApplyingCalendarEvents(Rule.this)) + { + if(!CalendarReceiver.hasEventBeenUsedInRule(Rule.this, event)) + { + /* + Record only the first calendar event that matched because the rule may + be executed once for every matching event. + */ + Miscellaneous.logEvent("i", "Rule", "Executing this rule run for calender event: " + event, 5); + CalendarReceiver.addUsedPair(new CalendarReceiver.RuleEventPair(Rule.this, event)); + break; + } + } + } + + /* + Run actions one after another + */ for(int i = 0; i< Rule.this.getActionSet().size(); i++) { try diff --git a/app/src/main/java/com/jens/automation2/Trigger.java b/app/src/main/java/com/jens/automation2/Trigger.java index 584fde8..6fcabea 100644 --- a/app/src/main/java/com/jens/automation2/Trigger.java +++ b/app/src/main/java/com/jens/automation2/Trigger.java @@ -628,9 +628,6 @@ public class Trigger if(!checkCalendarEvent(event, ignoreActive)) continue; - //TODO: This line alone does not suffice and might also not be correct in some cases - CalendarReceiver.addUsedPair(new CalendarReceiver.RuleEventPair(getParentRule(), event)); - return true; } 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 c0ef73a..e0b00d7 100644 --- a/app/src/main/java/com/jens/automation2/receivers/CalendarReceiver.java +++ b/app/src/main/java/com/jens/automation2/receivers/CalendarReceiver.java @@ -137,6 +137,7 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis } clearCaches(); + calendarEventsUsed.clear(); calendarReceiverActive = false; } @@ -599,7 +600,7 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis return false; } - static boolean hasEventBeenUsedInRule(Rule rule, CalendarEvent event) + public static boolean hasEventBeenUsedInRule(Rule rule, CalendarEvent event) { for (RuleEventPair executedPair : calendarEventsUsed) { @@ -609,4 +610,32 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis return false; } + + public static List getApplyingCalendarEvents(Rule rule) + { + List returnList = new ArrayList<>(); + + try + { + List calendarEvents = CalendarReceiver.readCalendarEvents(AutomationService.getInstance(), true,false); + + for(Trigger t : rule.getTriggerSet()) + { + if(t.getTriggerType().equals(calendarEvents)) + { + for (CalendarReceiver.CalendarEvent event : calendarEvents) + { + if (t.checkCalendarEvent(event, false)) + returnList.add(event); + } + } + } + } + catch(Exception e) + { + Miscellaneous.logEvent("e", "getApplyingCalendarEvents()", Log.getStackTraceString(e), 1); + } + + return returnList; + } } \ No newline at end of file