diff --git a/app/src/main/java/com/jens/automation2/Trigger.java b/app/src/main/java/com/jens/automation2/Trigger.java index 62d19c7d..7bd2ec94 100644 --- a/app/src/main/java/com/jens/automation2/Trigger.java +++ b/app/src/main/java/com/jens/automation2/Trigger.java @@ -1348,7 +1348,7 @@ public class Trigger * we're inside the specified timeframe. */ - Calendar timeSupposedToRunNext = DateTimeListener.getNextRepeatedExecutionAfter(this, lastExec); + Calendar timeSupposedToRunNext = DateTimeListener.getNextRepeatedExecution(this); Miscellaneous.logEvent("i", "isSupposedToRepeatSinceLastExecution()", "Last execution: " + Miscellaneous.formatDate(lastExec.getTime()), 5); Miscellaneous.logEvent("i", "isSupposedToRepeatSinceLastExecution()", "Next execution would be: " + Miscellaneous.formatDate(timeSupposedToRunNext.getTime()), 5); diff --git a/app/src/main/java/com/jens/automation2/receivers/DateTimeListener.java b/app/src/main/java/com/jens/automation2/receivers/DateTimeListener.java index 4610e124..d60a7d07 100644 --- a/app/src/main/java/com/jens/automation2/receivers/DateTimeListener.java +++ b/app/src/main/java/com/jens/automation2/receivers/DateTimeListener.java @@ -197,7 +197,7 @@ public class DateTimeListener extends BroadcastReceiver implements AutomationLis { // if(oneTrigger.applies(calNow, Miscellaneous.getAnyContext())) // { - Calendar calSchedule = getNextRepeatedExecutionAfter(oneTrigger, calNow); + Calendar calSchedule = getNextRepeatedExecution(oneTrigger); alarmCandidates.add(new ScheduleElement(calSchedule, "Rule " + oneRule.getName() + ", trigger " + oneTrigger.toString())); // } @@ -369,15 +369,31 @@ public class DateTimeListener extends BroadcastReceiver implements AutomationLis @Nullable @RequiresApi(api = Build.VERSION_CODES.N) - public static Calendar getNextRepeatedExecutionAfter(Trigger trigger, Calendar repetitionSearchStartPoint) + public static Calendar getNextRepeatedExecution(Trigger trigger) { -// Miscellaneous.logEvent("i", "DateTimeListener", "Checking for next repetition execution after " + Miscellaneous.formatDate(now.getTime()), 5); + Miscellaneous.logEvent("i", "DateTimeListener", "Checking for next repetition execution after " + Miscellaneous.formatDate(repetitionSearchStartPoint.getTime()), 5); + Calendar now = Calendar.getInstance(); Calendar calculationStart; TimeObject setTime; TimeFrame tf = new TimeFrame(trigger.getTriggerParameter2()); if(tf.getRepetition() > 0) { + /* + Are we inside of the timeframe or outside? + + Inside -> is this demanded? + If last execution known, calculate from it + If not known, calculate from start of timeframe + No: + Use end-time and add repetition + + Outisde? -> is this demanded? + If last execution known, calculate from it + If not known, calculate from end of timeframe + No: + Use start-time and add repetition + */ if(trigger.getParentRule().getLastExecution() != null) { calculationStart = trigger.getParentRule().getLastExecution(); @@ -386,26 +402,26 @@ public class DateTimeListener extends BroadcastReceiver implements AutomationLis { if(trigger.getTriggerParameter()) { - calculationStart = (Calendar) repetitionSearchStartPoint.clone(); + calculationStart = (Calendar) now.clone(); calculationStart.set(Calendar.HOUR_OF_DAY, tf.getTriggerTimeStart().getHours()); calculationStart.set(Calendar.MINUTE, tf.getTriggerTimeStart().getMinutes()); calculationStart.set(Calendar.SECOND, tf.getTriggerTimeStart().getSeconds()); calculationStart.set(Calendar.MILLISECOND, 0); calculationStart.add(Calendar.SECOND, (int) tf.getRepetition()); - if(calculationStart.getTimeInMillis() < repetitionSearchStartPoint.getTimeInMillis() && !areWeInTimeFrame(trigger, new Date())) + if(calculationStart.getTimeInMillis() < now.getTimeInMillis() && !areWeInTimeFrame(trigger, new Date())) calculationStart.add(Calendar.DAY_OF_MONTH, 1); } else { - calculationStart = (Calendar) repetitionSearchStartPoint.clone(); + calculationStart = (Calendar) now.clone(); calculationStart.set(Calendar.HOUR_OF_DAY, tf.getTriggerTimeStop().getHours()); calculationStart.set(Calendar.MINUTE, tf.getTriggerTimeStop().getMinutes()); calculationStart.set(Calendar.SECOND, tf.getTriggerTimeStop().getSeconds()); calculationStart.set(Calendar.MILLISECOND, 0); calculationStart.add(Calendar.SECOND, (int) tf.getRepetition()); - if(calculationStart.getTimeInMillis() < repetitionSearchStartPoint.getTimeInMillis() && !areWeInTimeFrame(trigger, new Date())) + if(calculationStart.getTimeInMillis() < now.getTimeInMillis() && areWeInTimeFrame(trigger, new Date())) calculationStart.add(Calendar.DAY_OF_MONTH, 1); } } @@ -423,28 +439,34 @@ public class DateTimeListener extends BroadcastReceiver implements AutomationLis setTime = tf.getTriggerTimeStop(); } - calculationStart = (Calendar) repetitionSearchStartPoint.clone(); + calculationStart = (Calendar) now.clone(); calculationStart.set(Calendar.HOUR_OF_DAY, setTime.getHours()); calculationStart.set(Calendar.MINUTE, setTime.getMinutes()); calculationStart.set(Calendar.SECOND, setTime.getSeconds()); + calculationStart.add(Calendar.SECOND, (int) tf.getRepetition()); - if(calculationStart.getTimeInMillis() < repetitionSearchStartPoint.getTimeInMillis() && areWeInTimeFrame(trigger, new Date())) + if(calculationStart.getTimeInMillis() < now.getTimeInMillis() && areWeInTimeFrame(trigger, new Date())) calculationStart.add(Calendar.DAY_OF_MONTH, 1); } calculationStart.set(Calendar.MILLISECOND, 0); Miscellaneous.logEvent("i", "getNextRepeatedExecutionAfter()", "calcStart: " + Miscellaneous.formatDate(calculationStart.getTime()) + ", now: " + Miscellaneous.formatDate(repetitionSearchStartPoint.getTime()), 5); -// Miscellaneous.logEvent("i", "getNextRepeatedExecutionAfter()", "Chose " + Miscellaneous.formatDate(calculationStart.getTime()) + " as calculation start.", 5); + Calendar calSchedule; - - Miscellaneous.logEvent("i", "getNextRepeatedExecutionAfter()", "calcStartAfterDayAdd " + Miscellaneous.formatDate(calculationStart.getTime()) + " as calculation start.", 5); - - long differenceInSeconds = Math.abs(repetitionSearchStartPoint.getTimeInMillis() - calculationStart.getTimeInMillis()) / 1000; - long nextExecutionMultiplier = Math.floorDiv(differenceInSeconds, tf.getRepetition()) + 1; - long nextScheduleTimestamp = (calculationStart.getTimeInMillis() / 1000) + (nextExecutionMultiplier * tf.getRepetition()); - Calendar calSchedule = Calendar.getInstance(); - calSchedule.setTimeInMillis(nextScheduleTimestamp * 1000); + if(calculationStart.getTimeInMillis() < now.getTimeInMillis()) + { + long differenceInSeconds = Math.abs(now.getTimeInMillis() - calculationStart.getTimeInMillis()) / 1000; + long nextExecutionMultiplier = Math.floorDiv(differenceInSeconds, tf.getRepetition()) + 1; + long nextScheduleTimestamp = (calculationStart.getTimeInMillis() / 1000) + (nextExecutionMultiplier * tf.getRepetition()); + calSchedule = Calendar.getInstance(); + calSchedule.setTimeInMillis(nextScheduleTimestamp * 1000); + } + else + { + calSchedule = (Calendar) calculationStart.clone(); +// calSchedule.add(Calendar.SECOND, (int)tf.getRepetition()); + } Miscellaneous.logEvent("i", "getNextRepeatedExecutionAfter()", "Chose " + Miscellaneous.formatDate(calSchedule.getTime()) + " as next repeated execution time.", 5);