From 5f278a6ba0283e9f2b75c3094cd11fc3c86433e2 Mon Sep 17 00:00:00 2001 From: jens Date: Sat, 23 Dec 2023 13:55:02 +0100 Subject: [PATCH] calendar trigger --- app/src/apkFlavor/AndroidManifest.xml | 3 + app/src/fdroidFlavor/AndroidManifest.xml | 3 + app/src/googlePlayFlavor/AndroidManifest.xml | 3 + .../ActivityManageTriggerCalendar.java | 35 +++ .../java/com/jens/automation2/Trigger.java | 69 +++++- .../receivers/CalendarReceiver.java | 128 +++++++++++ .../activity_manage_trigger_calendar.xml | 213 ++++++++++++++++++ app/src/main/res/values/strings.xml | 7 + 8 files changed, 460 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/jens/automation2/ActivityManageTriggerCalendar.java create mode 100644 app/src/main/java/com/jens/automation2/receivers/CalendarReceiver.java create mode 100644 app/src/main/res/layout/activity_manage_trigger_calendar.xml diff --git a/app/src/apkFlavor/AndroidManifest.xml b/app/src/apkFlavor/AndroidManifest.xml index 2357f464..cf6a6119 100644 --- a/app/src/apkFlavor/AndroidManifest.xml +++ b/app/src/apkFlavor/AndroidManifest.xml @@ -71,6 +71,7 @@ + @@ -143,6 +144,7 @@ + + + @@ -140,6 +141,7 @@ + + + @@ -125,6 +126,7 @@ + + calendarEvents = CalendarReceiver.readCalendarEvents(AutomationService.getInstance()); + + /* + 0 = titleDirection + 1 = title; + 2 = descriptionDirection + 3 = description + 4 = eventLocationDirection + 5 = eventLocation + 6 = availabilityDirection + 7 = availability + */ + + for(CalendarReceiver.CalendarEvent event : calendarEvents) + { + boolean isActive = Boolean.parseBoolean(conditions[0]); + if(isActive == event.isCurrentActive()) + { + if(Miscellaneous.compare(conditions[0], conditions[1], event.title)) + { + // title matches according to demands + + if(Miscellaneous.compare(conditions[2], conditions[3], event.description)) + { + // description matches according to demands + + if(Miscellaneous.compare(conditions[4], conditions[5], event.location)) + { + // location matches according to demands + + if(Miscellaneous.compare(conditions[6], conditions[7], event.availability)) + { + // availability matches according to demands + + return true; + } + } + } + } + } + } + + return false; + + //TODO: If trigger demands no calendar event and there is absolutely no future event, we'll need to check for that. + } + catch(Exception e) + { + Miscellaneous.logEvent("e", "checkVariable()", Log.getStackTraceString(e), 1); + } + + return false; + } + boolean checkBluetooth() { Miscellaneous.logEvent("i", Miscellaneous.getAnyContext().getResources().getString(R.string.ruleCheckOf), String.format("Checking for bluetooth...", this.getParentRule().getName()), 4); diff --git a/app/src/main/java/com/jens/automation2/receivers/CalendarReceiver.java b/app/src/main/java/com/jens/automation2/receivers/CalendarReceiver.java new file mode 100644 index 00000000..f9474c7e --- /dev/null +++ b/app/src/main/java/com/jens/automation2/receivers/CalendarReceiver.java @@ -0,0 +1,128 @@ +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 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 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 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; + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_manage_trigger_calendar.xml b/app/src/main/res/layout/activity_manage_trigger_calendar.xml new file mode 100644 index 00000000..64f7e99b --- /dev/null +++ b/app/src/main/res/layout/activity_manage_trigger_calendar.xml @@ -0,0 +1,213 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +