forked from jens/Automation
calendar trigger
This commit is contained in:
@@ -17,6 +17,8 @@ import com.jens.automation2.Trigger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
public class CalendarReceiver extends BroadcastReceiver implements AutomationListenerInterface
|
||||
{
|
||||
@@ -32,6 +34,10 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
static List<AndroidCalendar> calendarsCache = null;
|
||||
static List<CalendarEvent> calendarEventsCache = null;
|
||||
|
||||
static Timer timer = null;
|
||||
static TimerTask timerTask = null;
|
||||
static Calendar nextWakeup = null;
|
||||
|
||||
public static CalendarReceiver getInstance()
|
||||
{
|
||||
if(calendarReceiverInstance == null)
|
||||
@@ -45,15 +51,23 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
{
|
||||
if(intent.getAction().equalsIgnoreCase(Intent.ACTION_PROVIDER_CHANGED))
|
||||
{
|
||||
Miscellaneous.logEvent("i", "CalendarReceiver", "Received " + intent.getAction(), 5);
|
||||
|
||||
calendarsCache = null;
|
||||
calendarEventsCache = null;
|
||||
|
||||
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.calendarEvent);
|
||||
for(int i = 0; i < ruleCandidates.size(); i++)
|
||||
{
|
||||
if(ruleCandidates.get(i).getsGreenLight(context))
|
||||
ruleCandidates.get(i).activate(AutomationService.getInstance(), false);
|
||||
}
|
||||
checkForRules(context);
|
||||
armOrRearmTimer();
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkForRules(Context context)
|
||||
{
|
||||
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.calendarEvent);
|
||||
for(int i = 0; i < ruleCandidates.size(); i++)
|
||||
{
|
||||
if(ruleCandidates.get(i).getsGreenLight(context))
|
||||
ruleCandidates.get(i).activate(AutomationService.getInstance(), false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -242,11 +256,79 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
|
||||
if(cursor != null)
|
||||
cursor.close();
|
||||
|
||||
}
|
||||
|
||||
return calendarEventsCache;
|
||||
}
|
||||
|
||||
private static void armOrRearmTimer()
|
||||
{
|
||||
timerTask = new TimerTask()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
checkForRules(Miscellaneous.getAnyContext());
|
||||
|
||||
// Set next timer
|
||||
|
||||
calculateNextWakeup();
|
||||
armOrRearmTimer();
|
||||
}
|
||||
};
|
||||
|
||||
if(timer != null)
|
||||
{
|
||||
timer.cancel();
|
||||
timer.purge();
|
||||
}
|
||||
timer = new Timer();
|
||||
|
||||
if(nextWakeup == null)
|
||||
{
|
||||
readCalendarEvents(Miscellaneous.getAnyContext(), false);
|
||||
calculateNextWakeup();
|
||||
}
|
||||
|
||||
// If it's now filled, go on
|
||||
if(nextWakeup != null)
|
||||
timer.schedule(timerTask, nextWakeup.getTimeInMillis());
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
for (CalendarEvent event : events)
|
||||
{
|
||||
if (event.isCurrentlyActive())
|
||||
{
|
||||
if (nextWakeup == null || event.end.getTimeInMillis() < nextWakeup.getTimeInMillis())
|
||||
{
|
||||
nextWakeup = event.end;
|
||||
Miscellaneous.logEvent("i", "calculateNextWakeupForCalendar()", "Choosing end of event " + event.title + " as next wakeup.", 5);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (nextWakeup == null || event.start.getTimeInMillis() < nextWakeup.getTimeInMillis())
|
||||
{
|
||||
nextWakeup = event.start;
|
||||
Miscellaneous.logEvent("i", "calculateNextWakeupForCalendar()", "Choosing start of event " + event.title + " as next wakeup.", 5);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//else
|
||||
// we expect to be called byOnReceive() when new items exist
|
||||
}
|
||||
|
||||
public static void startCalendarReceiver(final AutomationService automationServiceRef)
|
||||
{
|
||||
if (!calendarReceiverActive)
|
||||
@@ -260,25 +342,15 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
{
|
||||
calendarIntentFilter = new IntentFilter();
|
||||
calendarIntentFilter.addAction(Intent.ACTION_PROVIDER_CHANGED);
|
||||
calendarIntentFilter.addDataScheme("content");
|
||||
calendarIntentFilter.addDataAuthority("com.android.calendar", null);
|
||||
}
|
||||
|
||||
calendarIntent = automationServiceRef.registerReceiver(calendarReceiverInstance, calendarIntentFilter);
|
||||
|
||||
calendarReceiverActive = true;
|
||||
}
|
||||
}
|
||||
|
||||
public static void stopScreenStateReceiver()
|
||||
{
|
||||
if (calendarReceiverActive)
|
||||
{
|
||||
if (calendarReceiverInstance != null)
|
||||
{
|
||||
automationServiceRef.unregisterReceiver(calendarReceiverInstance);
|
||||
calendarReceiverInstance = null;
|
||||
}
|
||||
|
||||
calendarReceiverActive = false;
|
||||
armOrRearmTimer();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user