Calendar trigger

This commit is contained in:
Jens 2023-12-27 13:33:09 +01:00
parent 9b28aeef8b
commit 584495ef61
7 changed files with 214 additions and 54 deletions

View File

@ -645,7 +645,7 @@ public class ActivityManageRule extends Activity
else if(types[i].toString().equals(Trigger_Enum.subSystemState.toString())) else if(types[i].toString().equals(Trigger_Enum.subSystemState.toString()))
items.add(new Item(typesLong[i].toString(), R.drawable.subsystemstate)); items.add(new Item(typesLong[i].toString(), R.drawable.subsystemstate));
else if(types[i].toString().equals(Trigger_Enum.calendarEvent.toString())) else if(types[i].toString().equals(Trigger_Enum.calendarEvent.toString()))
items.add(new Item(typesLong[i].toString(), R.drawable.placeholder)); items.add(new Item(typesLong[i].toString(), R.drawable.calendar));
else else
items.add(new Item(typesLong[i].toString(), R.drawable.placeholder)); items.add(new Item(typesLong[i].toString(), R.drawable.placeholder));
} }

View File

@ -3,29 +3,35 @@ package com.jens.automation2;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.provider.CalendarContract;
import android.view.View; import android.view.View;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button; import android.widget.Button;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.CompoundButton; import android.widget.CompoundButton;
import android.widget.EditText; import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner; import android.widget.Spinner;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.jens.automation2.receivers.CalendarReceiver;
import java.util.ArrayList;
import java.util.List;
public class ActivityManageTriggerCalendar extends Activity public class ActivityManageTriggerCalendar extends Activity
{ {
CheckBox chkCalendarEventDirection; CheckBox chkCalendarEventDirection, chkCalendarAvailabilityBusy, chkCalendarAvailabilityFree, chkCalendarAvailabilityTentative, chkCalendarAvailabilityOutOfOffice, chkCalendarAvailabilityWorkingElsewhere;
Spinner spinnerCalendarTitleDirection, spinnerCalendarLocationDirection, spinnerCalendarDescriptionDirection, spinnerCalendarAvailabilityDirection, spinnerCalendarAvailability; Spinner spinnerCalendarTitleDirection, spinnerCalendarLocationDirection, spinnerCalendarDescriptionDirection;
EditText etCalendarTitle, etCalendarLocation, etCalendarDescription; EditText etCalendarTitle, etCalendarLocation, etCalendarDescription;
LinearLayout llCalendarSelection;
Button bSaveTriggerCalendar; Button bSaveTriggerCalendar;
List<CheckBox> checkboxesCalendars = new ArrayList<>();
private static String[] directions; private static String[] directions;
ArrayAdapter<String> directionSpinnerAdapter; ArrayAdapter<String> directionSpinnerAdapter;
private static String[] availabilities;
ArrayAdapter<String> availabilitySpinnerAdapter;
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) protected void onCreate(@Nullable Bundle savedInstanceState)
{ {
@ -37,8 +43,13 @@ public class ActivityManageTriggerCalendar extends Activity
spinnerCalendarTitleDirection = (Spinner)findViewById(R.id.spinnerCalendarTitleDirection); spinnerCalendarTitleDirection = (Spinner)findViewById(R.id.spinnerCalendarTitleDirection);
spinnerCalendarLocationDirection = (Spinner)findViewById(R.id.spinnerCalendarLocationDirection); spinnerCalendarLocationDirection = (Spinner)findViewById(R.id.spinnerCalendarLocationDirection);
spinnerCalendarDescriptionDirection = (Spinner)findViewById(R.id.spinnerCalendarDescriptionDirection); spinnerCalendarDescriptionDirection = (Spinner)findViewById(R.id.spinnerCalendarDescriptionDirection);
spinnerCalendarAvailabilityDirection = (Spinner)findViewById(R.id.spinnerCalendarAvailabilityDirection); chkCalendarAvailabilityBusy = (CheckBox)findViewById(R.id.chkCalendarAvailabilityBusy);
spinnerCalendarAvailability = (Spinner)findViewById(R.id.spinnerCalendarAvailability); chkCalendarAvailabilityFree = (CheckBox)findViewById(R.id.chkCalendarAvailabilityFree);
chkCalendarAvailabilityTentative = (CheckBox)findViewById(R.id.chkCalendarAvailabilityTentative);
chkCalendarAvailabilityOutOfOffice = (CheckBox)findViewById(R.id.chkCalendarAvailabilityOutOfOffice);
chkCalendarAvailabilityWorkingElsewhere = (CheckBox)findViewById(R.id.chkCalendarAvailabilityWorkingElsewhere);
llCalendarSelection = (LinearLayout)findViewById(R.id.llCalendarSelection);
etCalendarTitle = (EditText)findViewById(R.id.etCalendarTitle); etCalendarTitle = (EditText)findViewById(R.id.etCalendarTitle);
etCalendarLocation = (EditText)findViewById(R.id.etCalendarLocation); etCalendarLocation = (EditText)findViewById(R.id.etCalendarLocation);
@ -58,27 +69,16 @@ public class ActivityManageTriggerCalendar extends Activity
spinnerCalendarTitleDirection.setAdapter(directionSpinnerAdapter); spinnerCalendarTitleDirection.setAdapter(directionSpinnerAdapter);
spinnerCalendarLocationDirection.setAdapter(directionSpinnerAdapter); spinnerCalendarLocationDirection.setAdapter(directionSpinnerAdapter);
spinnerCalendarDescriptionDirection.setAdapter(directionSpinnerAdapter); spinnerCalendarDescriptionDirection.setAdapter(directionSpinnerAdapter);
spinnerCalendarAvailabilityDirection.setAdapter(directionSpinnerAdapter);
directionSpinnerAdapter.notifyDataSetChanged(); directionSpinnerAdapter.notifyDataSetChanged();
/* for(CalendarReceiver.AndroidCalendar cal : CalendarReceiver.readCalendars(ActivityManageTriggerCalendar.this))
AVAILABILITY_BUSY = 0 {
AVAILABILITY_FREE = 1; CheckBox oneCalCheckbox = new CheckBox(ActivityManageTriggerCalendar.this);
AVAILABILITY_TENTATIVE = 2; oneCalCheckbox.setText(cal.displayName);
============================ Own Types: oneCalCheckbox.setTag(cal);
EXCHANGE OOF = 3 llCalendarSelection.addView(oneCalCheckbox);
EXCHANGE WORKING ELSEWHERE = 4 checkboxesCalendars.add(oneCalCheckbox);
*/ }
availabilities = new String[] {
getResources().getString(R.string.calendarStringBusy),
getResources().getString(R.string.calendarStringFree),
getResources().getString(R.string.calendarStringTentative),
getResources().getString(R.string.calendarStringOutOfOffice),
getResources().getString(R.string.calendarStringWorkingElsewhere),
};
availabilitySpinnerAdapter = new ArrayAdapter<String>(this, R.layout.text_view_for_poi_listview_mediumtextsize, ActivityManageTriggerCalendar.availabilities);
spinnerCalendarAvailability.setAdapter(availabilitySpinnerAdapter);
availabilitySpinnerAdapter.notifyDataSetChanged();
chkCalendarEventDirection.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() chkCalendarEventDirection.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{ {
@ -103,14 +103,36 @@ public class ActivityManageTriggerCalendar extends Activity
String description = etCalendarDescription.getText().toString(); String description = etCalendarDescription.getText().toString();
String locationDir = Trigger.getMatchCode(spinnerCalendarLocationDirection.getSelectedItem().toString()); String locationDir = Trigger.getMatchCode(spinnerCalendarLocationDirection.getSelectedItem().toString());
String location = etCalendarLocation.getText().toString(); String location = etCalendarLocation.getText().toString();
String availabilityDir = Trigger.getMatchCode(spinnerCalendarAvailabilityDirection.getSelectedItem().toString());
int availability = spinnerCalendarAvailability.getSelectedItemPosition(); List<String> availabilityList = new ArrayList<>();
if(chkCalendarAvailabilityBusy.isChecked())
availabilityList.add(String.valueOf(CalendarContract.Events.AVAILABILITY_BUSY));
if(chkCalendarAvailabilityFree.isChecked())
availabilityList.add(String.valueOf(CalendarContract.Events.AVAILABILITY_FREE));
if(chkCalendarAvailabilityTentative.isChecked())
availabilityList.add(String.valueOf(CalendarContract.Events.AVAILABILITY_TENTATIVE));
if(chkCalendarAvailabilityOutOfOffice.isChecked())
availabilityList.add(String.valueOf(CalendarReceiver.AVAILABILITY_OUT_OF_OFFICE));
if(chkCalendarAvailabilityWorkingElsewhere.isChecked())
availabilityList.add(String.valueOf(CalendarReceiver.AVAILABILITY_WORKING_ELSEWHERE));
List<CalendarReceiver.AndroidCalendar> selectedCalendarsList = new ArrayList<>();
for(CheckBox calCheckbox : checkboxesCalendars)
{
if(calCheckbox.isChecked())
selectedCalendarsList.add((CalendarReceiver.AndroidCalendar) calCheckbox.getTag());
}
String returnString = String returnString =
titleDir + Trigger.triggerParameter2Split + title + Trigger.triggerParameter2Split + titleDir + Trigger.triggerParameter2Split + title + Trigger.triggerParameter2Split +
descriptionDir + Trigger.triggerParameter2Split + description + Trigger.triggerParameter2Split + descriptionDir + Trigger.triggerParameter2Split + description + Trigger.triggerParameter2Split +
locationDir + Trigger.triggerParameter2Split + location + Trigger.triggerParameter2Split + locationDir + Trigger.triggerParameter2Split + location + Trigger.triggerParameter2Split +
availabilityDir + Trigger.triggerParameter2Split + String.valueOf(availability); Miscellaneous.explode(",", availabilityList.toArray(new String[availabilityList.size()])) + Trigger.triggerParameter2Split +
Miscellaneous.explode(",", selectedCalendarsList.toArray(new String[availabilityList.size()]));
Intent data = new Intent(); Intent data = new Intent();
data.putExtra(ActivityManageRule.intentNameTriggerParameter1, chkCalendarEventDirection.isChecked()); data.putExtra(ActivityManageRule.intentNameTriggerParameter1, chkCalendarEventDirection.isChecked());
@ -157,13 +179,8 @@ public class ActivityManageTriggerCalendar extends Activity
if(Trigger.getMatchCode(directions[i]).equalsIgnoreCase(input[4])) if(Trigger.getMatchCode(directions[i]).equalsIgnoreCase(input[4]))
spinnerCalendarLocationDirection.setSelection(i); spinnerCalendarLocationDirection.setSelection(i);
if(Trigger.getMatchCode(directions[i]).equalsIgnoreCase(input[6]))
spinnerCalendarAvailabilityDirection.setSelection(i);
} }
spinnerCalendarAvailability.setSelection(Integer.parseInt(input[7]));
etCalendarTitle.setText(input[1]); etCalendarTitle.setText(input[1]);
etCalendarDescription.setText(input[3]); etCalendarDescription.setText(input[3]);
etCalendarLocation.setText(input[5]); etCalendarLocation.setText(input[5]);

View File

@ -622,7 +622,7 @@ public class Trigger
try try
{ {
String[] conditions = this.getTriggerParameter2().split(Trigger.triggerParameter2Split); String[] conditions = this.getTriggerParameter2().split(Trigger.triggerParameter2Split);
List<CalendarReceiver.CalendarEvent> calendarEvents = CalendarReceiver.readCalendarEvents(AutomationService.getInstance(), false); List<CalendarReceiver.CalendarEvent> calendarEvents = CalendarReceiver.readCalendarEvents(AutomationService.getInstance(), true);
/* /*
0 = titleDirection 0 = titleDirection

View File

@ -6,7 +6,6 @@ import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import com.jens.automation2.AutomationService; import com.jens.automation2.AutomationService;
import com.jens.automation2.Miscellaneous; import com.jens.automation2.Miscellaneous;
@ -25,6 +24,11 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
private static AutomationService automationServiceRef; private static AutomationService automationServiceRef;
private static Intent calendarIntent = null; private static Intent calendarIntent = null;
public static final int AVAILABILITY_OUT_OF_OFFICE = 4;
public static final int AVAILABILITY_WORKING_ELSEWHERE = 5;
static List<AndroidCalendar> calendarsCache = null;
public static CalendarReceiver getInstance() public static CalendarReceiver getInstance()
{ {
if(calendarReceiverInstance == null) if(calendarReceiverInstance == null)
@ -94,9 +98,21 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
return new Trigger.Trigger_Enum[]{Trigger.Trigger_Enum.calendarEvent}; return new Trigger.Trigger_Enum[]{Trigger.Trigger_Enum.calendarEvent};
} }
public static class AndroidCalendar
{
public int calendarId;
public String displayName;
}
public static class CalendarEvent public static class CalendarEvent
{ {
public String calendarId, eventId, title, description, location, availability; public AndroidCalendar calendar;
public int calendarId;
public String eventId;
public String title;
public String description;
public String location;
public String availability;
public Calendar start, end; public Calendar start, end;
public boolean allDay; public boolean allDay;
@ -107,19 +123,55 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
} }
} }
public static List<AndroidCalendar> readCalendars(Context context)
{
if(calendarsCache == null)
{
calendarsCache = new ArrayList<>();
Cursor cursor;
cursor = context.getContentResolver().query(
Uri.parse("content://com.android.calendar/calendars"),
new String[]{"_id", "calendar_displayName"},
null, null, null);
cursor.moveToFirst();
// fetching calendars name
String CNames[] = new String[cursor.getCount()];
List<AndroidCalendar> calendarlist = new ArrayList<>();
for (int i = 0; i < CNames.length; i++)
{
try
{
AndroidCalendar calendar = new AndroidCalendar();
calendar.calendarId = Integer.parseInt(cursor.getString(0));
calendar.displayName = cursor.getString(1);
calendarsCache.add(calendar);
}
catch (Exception e)
{
}
cursor.moveToNext();
}
if (cursor != null)
cursor.close();
}
return calendarsCache;
}
public static List<CalendarEvent> readCalendarEvents(Context context, boolean includePastEvents) public static List<CalendarEvent> readCalendarEvents(Context context, boolean includePastEvents)
{ {
Cursor cursor; 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( cursor = context.getContentResolver().query(
Uri.parse("content://com.android.calendar/events"), Uri.parse("content://com.android.calendar/events"),
new String[] { "calendar_id", "original_id", "title", "description", "allDay", "dtstart", "dtend", "eventLocation", "availability" }, new String[] { "calendar_id", "_id", "title", "description", "allDay", "dtstart", "dtend", "eventLocation", "availability" },
null, null, null); null, null, null);
cursor.moveToFirst(); cursor.moveToFirst();
@ -135,7 +187,17 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
try try
{ {
CalendarEvent event = new CalendarEvent(); CalendarEvent event = new CalendarEvent();
event.calendarId = cursor.getString(0); event.calendarId = Integer.parseInt(cursor.getString(0));
for(AndroidCalendar cal : readCalendars(context))
{
if(cal.calendarId == event.calendarId)
{
event.calendar = cal;
break;
}
}
event.eventId = cursor.getString(1); event.eventId = cursor.getString(1);
event.title = cursor.getString(2); event.title = cursor.getString(2);
event.description = cursor.getString(3); event.description = cursor.getString(3);

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

@ -16,6 +16,11 @@
android:textSize="25dp" android:textSize="25dp"
android:layout_marginBottom="@dimen/default_margin" /> android:layout_marginBottom="@dimen/default_margin" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/selectingNoneItemForAllToMatch"/>
<TableLayout <TableLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -170,6 +175,15 @@
android:layout_marginVertical="@dimen/default_margin" android:layout_marginVertical="@dimen/default_margin"
android:background="#aa000000" /> android:background="#aa000000" />
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginBottom="@dimen/default_margin"
android:layout_gravity="center_vertical"
android:text="@string/selectingNoneItemForAllToMatch" />
<TableRow <TableRow
android:layout_marginBottom="@dimen/activity_vertical_margin"> android:layout_marginBottom="@dimen/activity_vertical_margin">
@ -184,16 +198,80 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content"> android:layout_height="wrap_content">
<Spinner <CheckBox
android:id="@+id/spinnerCalendarAvailabilityDirection" android:id="@+id/chkCalendarAvailabilityBusy"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spinnerCalendarAvailability"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:ems="10" /> android:text="@string/calendarStringBusy" />
<CheckBox
android:id="@+id/chkCalendarAvailabilityFree"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/calendarStringFree" />
<CheckBox
android:id="@+id/chkCalendarAvailabilityTentative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/calendarStringTentative" />
<CheckBox
android:id="@+id/chkCalendarAvailabilityOutOfOffice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/calendarStringOutOfOffice" />
<CheckBox
android:id="@+id/chkCalendarAvailabilityWorkingElsewhere"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/calendarStringWorkingElsewhere" />
</LinearLayout>
</TableRow>
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginBottom="@dimen/default_margin"
android:layout_gravity="center_vertical"
android:text="@string/calendarAvailabilityTypesUnsupported" />
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="10dp"
android:layout_marginVertical="@dimen/default_margin"
android:background="#aa000000" />
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginBottom="@dimen/default_margin"
android:layout_gravity="center_vertical"
android:text="@string/selectingNoneItemForAllToMatch" />
<TableRow
android:layout_marginBottom="@dimen/activity_vertical_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/calendars" />
<LinearLayout
android:id="@+id/llCalendarSelection"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout> </LinearLayout>

View File

@ -917,4 +917,7 @@
<string name="calendarStringTentative">tentative</string> <string name="calendarStringTentative">tentative</string>
<string name="calendarStringOutOfOffice">out of office</string> <string name="calendarStringOutOfOffice">out of office</string>
<string name="calendarStringWorkingElsewhere">working elsewhere</string> <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>
</resources> </resources>