calendar trigger

This commit is contained in:
jens 2023-12-27 14:48:27 +01:00
parent 584495ef61
commit d2606b72cd
5 changed files with 113 additions and 37 deletions

View File

@ -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<CheckBox> checkboxesCalendars = new ArrayList<>();
final static String separator = ",";
private static String[] directions;
ArrayAdapter<String> 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<String> 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;
}
}
}
}
}
}

View File

@ -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.
}

View File

@ -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<AndroidCalendar> readCalendars(Context context)

View File

@ -36,7 +36,7 @@
android:text="@string/direction"/>
<CheckBox
android:id="@+id/chkCalendarEventDirection"
android:id="@+id/chkCalendarEventActive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/eventIsCurrentlyHappening"

View File

@ -919,5 +919,5 @@
<string name="calendarStringWorkingElsewhere">working elsewhere</string>
<string name="selectingNoneItemForAllToMatch">Select no item if any will do.</string>
<string name="calendars">Calendars</string>
<string name="calendarAvailabilityTypesUnsupported">It may be that only the first 4 types are actually working becaue the other types are not part of Google Calendar.</string>
<string name="calendarAvailabilityTypesUnsupported">It may be that only the first 3 types are actually working becaue the other types are not part of Google Calendar.</string>
</resources>