calendar trigger
This commit is contained in:
@@ -9,7 +9,6 @@ import android.content.IntentFilter;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.SystemClock;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
@@ -20,6 +19,7 @@ import com.jens.automation2.Trigger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
@@ -43,7 +43,7 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
static TimerTask timerTask = null;
|
||||
static Calendar nextWakeup = null;
|
||||
static AlarmManager alarmManager = null;
|
||||
static boolean alarmHasChanged = false;
|
||||
static boolean wakeupNeedsToBeScheduled = false;
|
||||
|
||||
public static CalendarReceiver getInstance()
|
||||
{
|
||||
@@ -68,7 +68,7 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
}
|
||||
else if(intent.getAction().equalsIgnoreCase(calendarAlarmAction))
|
||||
{
|
||||
Miscellaneous.logEvent("i", "AlarmReceiver", "Received alarm for calendar receiver.", 5);
|
||||
Miscellaneous.logEvent("i", "CalendarReceiver", "Received alarm for calendar receiver.", 5);
|
||||
routineAtAlarm();
|
||||
}
|
||||
}
|
||||
@@ -326,7 +326,7 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
}
|
||||
|
||||
// If it's now filled, go on
|
||||
if(nextWakeup != null)
|
||||
if(nextWakeup != null && wakeupNeedsToBeScheduled)
|
||||
{
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
|
||||
{
|
||||
@@ -340,10 +340,9 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
{
|
||||
|
||||
}
|
||||
Miscellaneous.logEvent("i", "armOrRearmTimer()", "Setting calendar alarm for " + nextWakeup.toString(), 5);
|
||||
Miscellaneous.logEvent("i", "armOrRearmTimer()", "Scheduling wakeup for calendar at " + Miscellaneous.formatDate(nextWakeup.getTime()), 5);
|
||||
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nextWakeup.getTimeInMillis(), pi);
|
||||
|
||||
//alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextWakeup.getTimeInMillis(), pi);
|
||||
wakeupNeedsToBeScheduled = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -354,38 +353,58 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
private static void calculateNextWakeup()
|
||||
{
|
||||
Calendar now = Calendar.getInstance();
|
||||
if (nextWakeup != null && nextWakeup.getTimeInMillis() < now.getTimeInMillis())
|
||||
nextWakeup = null;
|
||||
|
||||
List<CalendarEvent> events = readCalendarEvents(Miscellaneous.getAnyContext(), false);
|
||||
if (events.size() > 0)
|
||||
|
||||
if (events.size() == 0)
|
||||
{
|
||||
Miscellaneous.logEvent("i", "calculateNextWakeup()", "No future events, nothing to schedule.", 5);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.calendarEvent);
|
||||
List<Long> wakeUpCandidatesList = new ArrayList<>();
|
||||
|
||||
for (CalendarEvent event : events)
|
||||
{
|
||||
if (event.isCurrentlyActive())
|
||||
for (Rule r : ruleCandidates)
|
||||
{
|
||||
if (nextWakeup == null || event.end.getTimeInMillis() < nextWakeup.getTimeInMillis())
|
||||
for (Trigger t : r.getTriggerSet())
|
||||
{
|
||||
nextWakeup = event.end;
|
||||
Miscellaneous.logEvent("i", "calculateNextWakeupForCalendar()", "Chose end of event " + event.title + " as next wakeup.", 5);
|
||||
if(!alarmHasChanged)
|
||||
alarmHasChanged = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (nextWakeup == null || event.start.getTimeInMillis() < nextWakeup.getTimeInMillis())
|
||||
{
|
||||
nextWakeup = event.start;
|
||||
Miscellaneous.logEvent("i", "calculateNextWakeupForCalendar()", "Chose start of event " + event.title + " as next wakeup.", 5);
|
||||
if(!alarmHasChanged)
|
||||
alarmHasChanged = true;
|
||||
if (t.getTriggerType().equals(Trigger.Trigger_Enum.calendarEvent) && t.checkCalendarEvent(event, true))
|
||||
{
|
||||
/*
|
||||
Device needs to wakeup at start AND end of events, no matter what is specified in triggers.
|
||||
This is because we also need to know when a trigger doesn't apply anymore to make it
|
||||
count for hasStateNotAppliedSinceLastRuleExecution().
|
||||
Otherwise the same rule would not get executed again even after calendar events have come and gone.
|
||||
*/
|
||||
|
||||
if(event.start.getTimeInMillis() > now.getTimeInMillis())
|
||||
wakeUpCandidatesList.add(event.start.getTimeInMillis());
|
||||
|
||||
if(event.end.getTimeInMillis() > now.getTimeInMillis())
|
||||
wakeUpCandidatesList.add(event.end.getTimeInMillis());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Collections.sort(wakeUpCandidatesList);
|
||||
|
||||
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);
|
||||
else
|
||||
Miscellaneous.logEvent("i", "calculateNextWakeupForCalendar()", "Chose " + Miscellaneous.formatDate(newAlarm.getTime()) + " as next wakeup for calendar triggers. Old was " + Miscellaneous.formatDate(nextWakeup.getTime()), 5);
|
||||
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);
|
||||
}
|
||||
//else
|
||||
// we expect to be called byOnReceive() when new items exist
|
||||
}
|
||||
|
||||
public static void startCalendarReceiver(final AutomationService automationServiceRef)
|
||||
|
Reference in New Issue
Block a user