2023-12-23 13:55:02 +01:00
|
|
|
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;
|
2023-12-25 14:28:48 +01:00
|
|
|
import android.os.Build;
|
2023-12-23 13:55:02 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2023-12-25 11:57:55 +01:00
|
|
|
static CalendarReceiver calendarReceiverInstance = null;
|
2023-12-23 13:55:02 +01:00
|
|
|
static boolean calendarReceiverActive = false;
|
|
|
|
static IntentFilter calendarIntentFilter = null;
|
2023-12-25 11:57:55 +01:00
|
|
|
private static AutomationService automationServiceRef;
|
|
|
|
private static Intent calendarIntent = null;
|
|
|
|
|
|
|
|
public static CalendarReceiver getInstance()
|
|
|
|
{
|
|
|
|
if(calendarReceiverInstance == null)
|
|
|
|
calendarReceiverInstance = new CalendarReceiver();
|
|
|
|
|
|
|
|
return calendarReceiverInstance;
|
|
|
|
}
|
|
|
|
|
2023-12-23 13:55:02 +01:00
|
|
|
@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)
|
2023-12-25 11:57:55 +01:00
|
|
|
calendarReceiverInstance = new CalendarReceiver();
|
2023-12-23 13:55:02 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2023-12-25 14:28:48 +01:00
|
|
|
public String calendarId, eventId, title, description, location, availability;
|
2023-12-23 13:55:02 +01:00
|
|
|
public Calendar start, end;
|
2023-12-25 14:28:48 +01:00
|
|
|
public boolean allDay;
|
2023-12-23 13:55:02 +01:00
|
|
|
|
|
|
|
public boolean isCurrentActive()
|
|
|
|
{
|
|
|
|
Calendar now = Calendar.getInstance();
|
|
|
|
return now.getTimeInMillis() >= start.getTimeInMillis() && now.getTimeInMillis() < end.getTimeInMillis();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<CalendarEvent> readCalendarEvents(Context context)
|
|
|
|
{
|
2023-12-25 14:28:48 +01:00
|
|
|
Cursor cursor;
|
|
|
|
|
|
|
|
// if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO)
|
|
|
|
// cursor = context.getContentResolver().query(
|
|
|
|
// Uri.parse("content://calendar/calendars"),
|
|
|
|
// new String[] { "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation", "availability" },
|
|
|
|
// null, null, null);
|
|
|
|
// else
|
|
|
|
cursor = context.getContentResolver().query(
|
2023-12-23 13:55:02 +01:00
|
|
|
Uri.parse("content://com.android.calendar/events"),
|
2023-12-25 14:28:48 +01:00
|
|
|
new String[] { "calendar_id", "original_id", "title", "description", "allDay", "dtstart", "dtend", "eventLocation", "availability" },
|
2023-12-23 13:55:02 +01:00
|
|
|
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++)
|
|
|
|
{
|
2023-12-25 11:57:55 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
CalendarEvent event = new CalendarEvent();
|
2023-12-25 14:28:48 +01:00
|
|
|
event.calendarId = cursor.getString(0);
|
|
|
|
event.eventId = cursor.getString(1);
|
2023-12-25 11:57:55 +01:00
|
|
|
event.title = cursor.getString(2);
|
|
|
|
event.description = cursor.getString(3);
|
2023-12-25 14:28:48 +01:00
|
|
|
event.allDay = cursor.getString(4).equals("1");
|
|
|
|
event.start = Miscellaneous.calendarFromLong(Long.parseLong(cursor.getString(5)));
|
|
|
|
event.end = Miscellaneous.calendarFromLong(Long.parseLong(cursor.getString(6)));
|
|
|
|
event.location = cursor.getString(7);
|
|
|
|
event.availability = cursor.getString(8);
|
2023-12-25 11:57:55 +01:00
|
|
|
eventlist.add(event);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{}
|
2023-12-23 13:55:02 +01:00
|
|
|
cursor.moveToNext();
|
|
|
|
}
|
|
|
|
|
2023-12-25 14:28:48 +01:00
|
|
|
if(cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
|
2023-12-23 13:55:02 +01:00
|
|
|
return eventlist;
|
|
|
|
}
|
2023-12-25 11:57:55 +01:00
|
|
|
|
|
|
|
public static void startCalendarReceiver(final AutomationService automationServiceRef)
|
|
|
|
{
|
|
|
|
if (!calendarReceiverActive)
|
|
|
|
{
|
|
|
|
CalendarReceiver.automationServiceRef = automationServiceRef;
|
|
|
|
|
|
|
|
if (calendarReceiverInstance == null)
|
|
|
|
calendarReceiverInstance = new CalendarReceiver();
|
|
|
|
|
|
|
|
if (calendarIntentFilter == null)
|
|
|
|
{
|
|
|
|
calendarIntentFilter = new IntentFilter();
|
|
|
|
calendarIntentFilter.addAction(Intent.ACTION_PROVIDER_CHANGED);
|
|
|
|
}
|
|
|
|
|
|
|
|
calendarIntent = automationServiceRef.registerReceiver(calendarReceiverInstance, calendarIntentFilter);
|
|
|
|
|
|
|
|
calendarReceiverActive = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void stopScreenStateReceiver()
|
|
|
|
{
|
|
|
|
if (calendarReceiverActive)
|
|
|
|
{
|
|
|
|
if (calendarReceiverInstance != null)
|
|
|
|
{
|
|
|
|
automationServiceRef.unregisterReceiver(calendarReceiverInstance);
|
|
|
|
calendarReceiverInstance = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
calendarReceiverActive = false;
|
|
|
|
}
|
|
|
|
}
|
2023-12-23 13:55:02 +01:00
|
|
|
}
|