From d2606b72cdc40abed6f7e67055832c620b830a16 Mon Sep 17 00:00:00 2001 From: jens Date: Wed, 27 Dec 2023 14:48:27 +0100 Subject: [PATCH] calendar trigger --- .../ActivityManageTriggerCalendar.java | 59 +++++++++++--- .../java/com/jens/automation2/Trigger.java | 76 +++++++++++++------ .../receivers/CalendarReceiver.java | 11 ++- .../activity_manage_trigger_calendar.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 5 files changed, 113 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/com/jens/automation2/ActivityManageTriggerCalendar.java b/app/src/main/java/com/jens/automation2/ActivityManageTriggerCalendar.java index 851b4ce..f270d87 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageTriggerCalendar.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageTriggerCalendar.java @@ -17,17 +17,19 @@ import androidx.annotation.Nullable; import com.jens.automation2.receivers.CalendarReceiver; +import java.sql.Array; import java.util.ArrayList; import java.util.List; public class ActivityManageTriggerCalendar extends Activity { - CheckBox chkCalendarEventDirection, chkCalendarAvailabilityBusy, chkCalendarAvailabilityFree, chkCalendarAvailabilityTentative, chkCalendarAvailabilityOutOfOffice, chkCalendarAvailabilityWorkingElsewhere; + CheckBox chkCalendarEventActive, chkCalendarAvailabilityBusy, chkCalendarAvailabilityFree, chkCalendarAvailabilityTentative, chkCalendarAvailabilityOutOfOffice, chkCalendarAvailabilityWorkingElsewhere; Spinner spinnerCalendarTitleDirection, spinnerCalendarLocationDirection, spinnerCalendarDescriptionDirection; EditText etCalendarTitle, etCalendarLocation, etCalendarDescription; LinearLayout llCalendarSelection; Button bSaveTriggerCalendar; List checkboxesCalendars = new ArrayList<>(); + final static String separator = ","; private static String[] directions; ArrayAdapter directionSpinnerAdapter; @@ -39,7 +41,7 @@ public class ActivityManageTriggerCalendar extends Activity Miscellaneous.setDisplayLanguage(this); setContentView(R.layout.activity_manage_trigger_calendar); - chkCalendarEventDirection = (CheckBox) findViewById(R.id.chkCalendarEventDirection); + chkCalendarEventActive = (CheckBox) findViewById(R.id.chkCalendarEventActive); spinnerCalendarTitleDirection = (Spinner)findViewById(R.id.spinnerCalendarTitleDirection); spinnerCalendarLocationDirection = (Spinner)findViewById(R.id.spinnerCalendarLocationDirection); spinnerCalendarDescriptionDirection = (Spinner)findViewById(R.id.spinnerCalendarDescriptionDirection); @@ -80,15 +82,15 @@ public class ActivityManageTriggerCalendar extends Activity checkboxesCalendars.add(oneCalCheckbox); } - chkCalendarEventDirection.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() + chkCalendarEventActive.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { if(checked) - chkCalendarEventDirection.setText(R.string.eventIsCurrentlyHappening); + chkCalendarEventActive.setText(R.string.eventIsCurrentlyHappening); else - chkCalendarEventDirection.setText(R.string.eventIsCurrentlyNotHappening); + chkCalendarEventActive.setText(R.string.eventIsCurrentlyNotHappening); } }); @@ -126,16 +128,19 @@ public class ActivityManageTriggerCalendar extends Activity if(calCheckbox.isChecked()) selectedCalendarsList.add((CalendarReceiver.AndroidCalendar) calCheckbox.getTag()); } + List selectedCalendarsIdArray = new ArrayList<>(); + for(CalendarReceiver.AndroidCalendar cal : selectedCalendarsList) + selectedCalendarsIdArray.add(String.valueOf(cal.calendarId)); String returnString = titleDir + Trigger.triggerParameter2Split + title + Trigger.triggerParameter2Split + descriptionDir + Trigger.triggerParameter2Split + description + Trigger.triggerParameter2Split + locationDir + Trigger.triggerParameter2Split + location + Trigger.triggerParameter2Split + - Miscellaneous.explode(",", availabilityList.toArray(new String[availabilityList.size()])) + Trigger.triggerParameter2Split + - Miscellaneous.explode(",", selectedCalendarsList.toArray(new String[availabilityList.size()])); + Miscellaneous.explode(separator, availabilityList.toArray(new String[availabilityList.size()])) + Trigger.triggerParameter2Split + + Miscellaneous.explode(separator, selectedCalendarsIdArray.toArray(new String[selectedCalendarsIdArray.size()])); Intent data = new Intent(); - data.putExtra(ActivityManageRule.intentNameTriggerParameter1, chkCalendarEventDirection.isChecked()); + data.putExtra(ActivityManageRule.intentNameTriggerParameter1, chkCalendarEventActive.isChecked()); data.putExtra(ActivityManageRule.intentNameTriggerParameter2, returnString); ActivityManageTriggerCalendar.this.setResult(RESULT_OK, data); @@ -153,7 +158,7 @@ public class ActivityManageTriggerCalendar extends Activity //TODO:try-catch if(data.hasExtra(ActivityManageRule.intentNameTriggerParameter1)) - chkCalendarEventDirection.setChecked(data.getBooleanExtra(ActivityManageRule.intentNameTriggerParameter1, true)); + chkCalendarEventActive.setChecked(data.getBooleanExtra(ActivityManageRule.intentNameTriggerParameter1, true)); if(data.hasExtra(ActivityManageRule.intentNameTriggerParameter2)) { @@ -165,8 +170,8 @@ public class ActivityManageTriggerCalendar extends Activity 3 = description 4 = locationDir 5 = location - 6 = availabilityDir - 7 = availability + 6 = availability list + 7 = calendars list */ for(int i = 0; i < directions.length; i++) @@ -184,6 +189,36 @@ public class ActivityManageTriggerCalendar extends Activity etCalendarTitle.setText(input[1]); etCalendarDescription.setText(input[3]); etCalendarLocation.setText(input[5]); + + String[] availabilities = input[6].split(separator); + String[] calendars = input[7].split(separator); + + for(String avail : availabilities) + { + if(Integer.parseInt(avail) == CalendarContract.Events.AVAILABILITY_BUSY) + chkCalendarAvailabilityBusy.setChecked(true); + else if(Integer.parseInt(avail) == CalendarContract.Events.AVAILABILITY_FREE) + chkCalendarAvailabilityFree.setChecked(true); + else if(Integer.parseInt(avail) == CalendarContract.Events.AVAILABILITY_TENTATIVE) + chkCalendarAvailabilityTentative.setChecked(true); + else if(Integer.parseInt(avail) == CalendarReceiver.AVAILABILITY_OUT_OF_OFFICE) + chkCalendarAvailabilityOutOfOffice.setChecked(true); + else if(Integer.parseInt(avail) == CalendarReceiver.AVAILABILITY_WORKING_ELSEWHERE) + chkCalendarAvailabilityWorkingElsewhere.setChecked(true); + } + + for(CheckBox checkbox : checkboxesCalendars) + { + int id = ((CalendarReceiver.AndroidCalendar)checkbox.getTag()).calendarId; + for(String calId : calendars) + { + if(calId.equals(String.valueOf(id))) + { + checkbox.setChecked(true); + break; + } + } + } } } -} +} \ No newline at end of file diff --git a/app/src/main/java/com/jens/automation2/Trigger.java b/app/src/main/java/com/jens/automation2/Trigger.java index 402bc0e..4489bd7 100644 --- a/app/src/main/java/com/jens/automation2/Trigger.java +++ b/app/src/main/java/com/jens/automation2/Trigger.java @@ -637,34 +637,66 @@ public class Trigger for(CalendarReceiver.CalendarEvent event : calendarEvents) { - boolean isActive = Boolean.parseBoolean(conditions[0]); - if(isActive == event.isCurrentActive()) + boolean isActive = getTriggerParameter(); + if(isActive != event.isCurrentlyActive()) { - if(Miscellaneous.compare(conditions[0], conditions[1], event.title)) + Miscellaneous.logEvent("i", "CalendarCheck", "Event has to be currently active: " + String.valueOf(triggerParameter) + ", but is required otherwise.", 5); + break; + } + + if(!StringUtils.isEmpty(conditions[1])) + { + 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; - } - } - } + Miscellaneous.logEvent("i", "CalendarCheck", "Title does not match.", 5); + break; } } + + if(!StringUtils.isEmpty(conditions[3])) + { + if (!Miscellaneous.compare(conditions[2], conditions[3], event.description)) + { + Miscellaneous.logEvent("i", "CalendarCheck", "Description does not match.", 5); + break; + } + } + + if(!StringUtils.isEmpty(conditions[5])) + { + if (!Miscellaneous.compare(conditions[4], conditions[5], event.location)) + { + Miscellaneous.logEvent("i", "CalendarCheck", "Location does not match.", 5); + break; + } + } + + String[] availabilities = conditions[6].split(ActivityManageTriggerCalendar.separator); + if(availabilities.length > 0) + { + if(!Miscellaneous.arraySearch(availabilities, event.availability, false, true)) + { + Miscellaneous.logEvent("i", "CalendarCheck", "Availability does not match.", 5); + break; + } + } + + String[] calendars = conditions[7].split(ActivityManageTriggerCalendar.separator); + if(availabilities.length > 0) + { + if(!Miscellaneous.arraySearch(calendars, String.valueOf(event.calendarId), false, true)) + { + Miscellaneous.logEvent("i", "CalendarCheck", "Calendar does not match.", 5); + break; + } + } + + // No contradictions found + Miscellaneous.logEvent("i", "CalendarCheck", "Event " + event + " matches.", 4); + return true; } - return false; + //At this point none of the calendar items matches this trigger //TODO: If trigger demands no calendar event and there is absolutely no future event, we'll need to check for that. } diff --git a/app/src/main/java/com/jens/automation2/receivers/CalendarReceiver.java b/app/src/main/java/com/jens/automation2/receivers/CalendarReceiver.java index a8d8f17..f0e047b 100644 --- a/app/src/main/java/com/jens/automation2/receivers/CalendarReceiver.java +++ b/app/src/main/java/com/jens/automation2/receivers/CalendarReceiver.java @@ -7,6 +7,8 @@ import android.content.IntentFilter; import android.database.Cursor; import android.net.Uri; +import androidx.annotation.NonNull; + import com.jens.automation2.AutomationService; import com.jens.automation2.Miscellaneous; import com.jens.automation2.Rule; @@ -116,11 +118,18 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis public Calendar start, end; public boolean allDay; - public boolean isCurrentActive() + public boolean isCurrentlyActive() { Calendar now = Calendar.getInstance(); return now.getTimeInMillis() >= start.getTimeInMillis() && now.getTimeInMillis() < end.getTimeInMillis(); } + + @NonNull + @Override + public String toString() + { + return title; + } } public static List readCalendars(Context context) diff --git a/app/src/main/res/layout/activity_manage_trigger_calendar.xml b/app/src/main/res/layout/activity_manage_trigger_calendar.xml index 6a682a9..bc8c344 100644 --- a/app/src/main/res/layout/activity_manage_trigger_calendar.xml +++ b/app/src/main/res/layout/activity_manage_trigger_calendar.xml @@ -36,7 +36,7 @@ android:text="@string/direction"/> working elsewhere Select no item if any will do. Calendars - It may be that only the first 4 types are actually working becaue the other types are not part of Google Calendar. + It may be that only the first 3 types are actually working becaue the other types are not part of Google Calendar. \ No newline at end of file