forked from jens/Automation
128 lines
4.1 KiB
Java
128 lines
4.1 KiB
Java
|
package com.jens.automation2.receivers;
|
||
|
|
||
|
import android.content.BroadcastReceiver;
|
||
|
import android.content.Context;
|
||
|
import android.content.Intent;
|
||
|
import android.content.IntentFilter;
|
||
|
import android.database.Cursor;
|
||
|
import android.net.Uri;
|
||
|
|
||
|
import com.jens.automation2.AutomationService;
|
||
|
import com.jens.automation2.Miscellaneous;
|
||
|
import com.jens.automation2.Rule;
|
||
|
import com.jens.automation2.Trigger;
|
||
|
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Calendar;
|
||
|
import java.util.List;
|
||
|
|
||
|
public class CalendarReceiver extends BroadcastReceiver implements AutomationListenerInterface
|
||
|
{
|
||
|
static BroadcastReceiver calendarReceiverInstance = null;
|
||
|
static boolean calendarReceiverActive = false;
|
||
|
static IntentFilter calendarIntentFilter = null;
|
||
|
@Override
|
||
|
public void onReceive(Context context, Intent intent)
|
||
|
{
|
||
|
if(intent.getAction().equalsIgnoreCase(Intent.ACTION_PROVIDER_CHANGED))
|
||
|
{
|
||
|
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);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void startListener(AutomationService automationService)
|
||
|
{
|
||
|
if(!calendarReceiverActive)
|
||
|
{
|
||
|
if(calendarReceiverInstance == null)
|
||
|
calendarReceiverInstance = new BatteryReceiver();
|
||
|
|
||
|
if(calendarIntentFilter == null)
|
||
|
{
|
||
|
calendarIntentFilter = new IntentFilter();
|
||
|
calendarIntentFilter.addAction(Intent.ACTION_PROVIDER_CHANGED);
|
||
|
}
|
||
|
|
||
|
AutomationService.getInstance().registerReceiver(calendarReceiverInstance, calendarIntentFilter);
|
||
|
|
||
|
calendarReceiverActive = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void stopListener(AutomationService automationService)
|
||
|
{
|
||
|
if(calendarReceiverActive)
|
||
|
{
|
||
|
if(calendarReceiverInstance != null)
|
||
|
{
|
||
|
AutomationService.getInstance().unregisterReceiver(calendarReceiverInstance);
|
||
|
calendarReceiverInstance = null;
|
||
|
}
|
||
|
|
||
|
calendarReceiverActive = false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean isListenerRunning()
|
||
|
{
|
||
|
return calendarReceiverActive;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public Trigger.Trigger_Enum[] getMonitoredTrigger()
|
||
|
{
|
||
|
return new Trigger.Trigger_Enum[]{Trigger.Trigger_Enum.calendarEvent};
|
||
|
}
|
||
|
|
||
|
public static class CalendarEvent
|
||
|
{
|
||
|
public String id, title, description, location, availability;
|
||
|
public Calendar start, end;
|
||
|
|
||
|
public boolean isCurrentActive()
|
||
|
{
|
||
|
Calendar now = Calendar.getInstance();
|
||
|
return now.getTimeInMillis() >= start.getTimeInMillis() && now.getTimeInMillis() < end.getTimeInMillis();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static List<CalendarEvent> readCalendarEvents(Context context)
|
||
|
{
|
||
|
Cursor cursor = context.getContentResolver()
|
||
|
.query(
|
||
|
Uri.parse("content://com.android.calendar/events"),
|
||
|
new String[] { "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation", "AVAILABILITY" },
|
||
|
null, null, null);
|
||
|
|
||
|
cursor.moveToFirst();
|
||
|
// fetching calendars name
|
||
|
String CNames[] = new String[cursor.getCount()];
|
||
|
|
||
|
List<CalendarEvent> eventlist = new ArrayList<>();
|
||
|
|
||
|
for (int i = 0; i < CNames.length; i++)
|
||
|
{
|
||
|
CalendarEvent event = new CalendarEvent();
|
||
|
event.id = cursor.getString(1);
|
||
|
event.title = cursor.getString(2);
|
||
|
event.description = cursor.getString(3);
|
||
|
event.start = Miscellaneous.calendarFromLong(Long.parseLong(cursor.getString(4)));
|
||
|
event.end = Miscellaneous.calendarFromLong(Long.parseLong(cursor.getString(5)));
|
||
|
event.location = cursor.getString(6);
|
||
|
event.availability = cursor.getString(7)
|
||
|
eventlist.add(event);
|
||
|
|
||
|
cursor.moveToNext();
|
||
|
}
|
||
|
|
||
|
return eventlist;
|
||
|
}
|
||
|
}
|