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 android.os.Build; 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 CalendarReceiver calendarReceiverInstance = null; static boolean calendarReceiverActive = false; static IntentFilter calendarIntentFilter = null; private static AutomationService automationServiceRef; private static Intent calendarIntent = null; public static CalendarReceiver getInstance() { if(calendarReceiverInstance == null) calendarReceiverInstance = new CalendarReceiver(); return calendarReceiverInstance; } @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equalsIgnoreCase(Intent.ACTION_PROVIDER_CHANGED)) { ArrayList 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 CalendarReceiver(); 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 calendarId, eventId, title, description, location, availability; public Calendar start, end; public boolean allDay; public boolean isCurrentActive() { Calendar now = Calendar.getInstance(); return now.getTimeInMillis() >= start.getTimeInMillis() && now.getTimeInMillis() < end.getTimeInMillis(); } } public static List readCalendarEvents(Context context) { 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( Uri.parse("content://com.android.calendar/events"), new String[] { "calendar_id", "original_id", "title", "description", "allDay", "dtstart", "dtend", "eventLocation", "availability" }, null, null, null); cursor.moveToFirst(); // fetching calendars name String CNames[] = new String[cursor.getCount()]; List eventlist = new ArrayList<>(); for (int i = 0; i < CNames.length; i++) { try { CalendarEvent event = new CalendarEvent(); event.calendarId = cursor.getString(0); event.eventId = cursor.getString(1); event.title = cursor.getString(2); event.description = cursor.getString(3); 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); eventlist.add(event); } catch (Exception e) {} cursor.moveToNext(); } if(cursor != null) cursor.close(); return eventlist; } 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; } } }