From 584495ef61ab9ad709e398b13bce96276f9afb7d Mon Sep 17 00:00:00 2001 From: Jens Date: Wed, 27 Dec 2023 13:33:09 +0100 Subject: [PATCH] Calendar trigger --- .../jens/automation2/ActivityManageRule.java | 2 +- .../ActivityManageTriggerCalendar.java | 85 +++++++++------- .../java/com/jens/automation2/Trigger.java | 2 +- .../receivers/CalendarReceiver.java | 82 +++++++++++++-- app/src/main/res/drawable-hdpi/calendar.png | Bin 0 -> 2155 bytes .../activity_manage_trigger_calendar.xml | 94 ++++++++++++++++-- app/src/main/res/values/strings.xml | 3 + 7 files changed, 214 insertions(+), 54 deletions(-) create mode 100644 app/src/main/res/drawable-hdpi/calendar.png diff --git a/app/src/main/java/com/jens/automation2/ActivityManageRule.java b/app/src/main/java/com/jens/automation2/ActivityManageRule.java index db6af2b..73d8ec1 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageRule.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageRule.java @@ -645,7 +645,7 @@ public class ActivityManageRule extends Activity else if(types[i].toString().equals(Trigger_Enum.subSystemState.toString())) items.add(new Item(typesLong[i].toString(), R.drawable.subsystemstate)); 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 items.add(new Item(typesLong[i].toString(), R.drawable.placeholder)); } diff --git a/app/src/main/java/com/jens/automation2/ActivityManageTriggerCalendar.java b/app/src/main/java/com/jens/automation2/ActivityManageTriggerCalendar.java index 4f00676..851b4ce 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageTriggerCalendar.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageTriggerCalendar.java @@ -3,29 +3,35 @@ package com.jens.automation2; import android.app.Activity; import android.content.Intent; import android.os.Bundle; +import android.provider.CalendarContract; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; +import android.widget.LinearLayout; import android.widget.Spinner; import androidx.annotation.Nullable; +import com.jens.automation2.receivers.CalendarReceiver; + +import java.util.ArrayList; +import java.util.List; + public class ActivityManageTriggerCalendar extends Activity { - CheckBox chkCalendarEventDirection; - Spinner spinnerCalendarTitleDirection, spinnerCalendarLocationDirection, spinnerCalendarDescriptionDirection, spinnerCalendarAvailabilityDirection, spinnerCalendarAvailability; + CheckBox chkCalendarEventDirection, chkCalendarAvailabilityBusy, chkCalendarAvailabilityFree, chkCalendarAvailabilityTentative, chkCalendarAvailabilityOutOfOffice, chkCalendarAvailabilityWorkingElsewhere; + Spinner spinnerCalendarTitleDirection, spinnerCalendarLocationDirection, spinnerCalendarDescriptionDirection; EditText etCalendarTitle, etCalendarLocation, etCalendarDescription; + LinearLayout llCalendarSelection; Button bSaveTriggerCalendar; + List checkboxesCalendars = new ArrayList<>(); private static String[] directions; ArrayAdapter directionSpinnerAdapter; - private static String[] availabilities; - ArrayAdapter availabilitySpinnerAdapter; - @Override protected void onCreate(@Nullable Bundle savedInstanceState) { @@ -37,8 +43,13 @@ public class ActivityManageTriggerCalendar extends Activity spinnerCalendarTitleDirection = (Spinner)findViewById(R.id.spinnerCalendarTitleDirection); spinnerCalendarLocationDirection = (Spinner)findViewById(R.id.spinnerCalendarLocationDirection); spinnerCalendarDescriptionDirection = (Spinner)findViewById(R.id.spinnerCalendarDescriptionDirection); - spinnerCalendarAvailabilityDirection = (Spinner)findViewById(R.id.spinnerCalendarAvailabilityDirection); - spinnerCalendarAvailability = (Spinner)findViewById(R.id.spinnerCalendarAvailability); + chkCalendarAvailabilityBusy = (CheckBox)findViewById(R.id.chkCalendarAvailabilityBusy); + 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); etCalendarLocation = (EditText)findViewById(R.id.etCalendarLocation); @@ -58,27 +69,16 @@ public class ActivityManageTriggerCalendar extends Activity spinnerCalendarTitleDirection.setAdapter(directionSpinnerAdapter); spinnerCalendarLocationDirection.setAdapter(directionSpinnerAdapter); spinnerCalendarDescriptionDirection.setAdapter(directionSpinnerAdapter); - spinnerCalendarAvailabilityDirection.setAdapter(directionSpinnerAdapter); directionSpinnerAdapter.notifyDataSetChanged(); - /* - AVAILABILITY_BUSY = 0 - AVAILABILITY_FREE = 1; - AVAILABILITY_TENTATIVE = 2; - ============================ Own Types: - EXCHANGE OOF = 3 - EXCHANGE WORKING ELSEWHERE = 4 - */ - 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(this, R.layout.text_view_for_poi_listview_mediumtextsize, ActivityManageTriggerCalendar.availabilities); - spinnerCalendarAvailability.setAdapter(availabilitySpinnerAdapter); - availabilitySpinnerAdapter.notifyDataSetChanged(); + for(CalendarReceiver.AndroidCalendar cal : CalendarReceiver.readCalendars(ActivityManageTriggerCalendar.this)) + { + CheckBox oneCalCheckbox = new CheckBox(ActivityManageTriggerCalendar.this); + oneCalCheckbox.setText(cal.displayName); + oneCalCheckbox.setTag(cal); + llCalendarSelection.addView(oneCalCheckbox); + checkboxesCalendars.add(oneCalCheckbox); + } chkCalendarEventDirection.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @@ -103,14 +103,36 @@ public class ActivityManageTriggerCalendar extends Activity String description = etCalendarDescription.getText().toString(); String locationDir = Trigger.getMatchCode(spinnerCalendarLocationDirection.getSelectedItem().toString()); String location = etCalendarLocation.getText().toString(); - String availabilityDir = Trigger.getMatchCode(spinnerCalendarAvailabilityDirection.getSelectedItem().toString()); - int availability = spinnerCalendarAvailability.getSelectedItemPosition(); + + List 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 selectedCalendarsList = new ArrayList<>(); + for(CheckBox calCheckbox : checkboxesCalendars) + { + if(calCheckbox.isChecked()) + selectedCalendarsList.add((CalendarReceiver.AndroidCalendar) calCheckbox.getTag()); + } String returnString = titleDir + Trigger.triggerParameter2Split + title + Trigger.triggerParameter2Split + descriptionDir + Trigger.triggerParameter2Split + description + 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(); data.putExtra(ActivityManageRule.intentNameTriggerParameter1, chkCalendarEventDirection.isChecked()); @@ -157,13 +179,8 @@ public class ActivityManageTriggerCalendar extends Activity if(Trigger.getMatchCode(directions[i]).equalsIgnoreCase(input[4])) spinnerCalendarLocationDirection.setSelection(i); - - if(Trigger.getMatchCode(directions[i]).equalsIgnoreCase(input[6])) - spinnerCalendarAvailabilityDirection.setSelection(i); } - spinnerCalendarAvailability.setSelection(Integer.parseInt(input[7])); - etCalendarTitle.setText(input[1]); etCalendarDescription.setText(input[3]); etCalendarLocation.setText(input[5]); diff --git a/app/src/main/java/com/jens/automation2/Trigger.java b/app/src/main/java/com/jens/automation2/Trigger.java index c8d522a..402bc0e 100644 --- a/app/src/main/java/com/jens/automation2/Trigger.java +++ b/app/src/main/java/com/jens/automation2/Trigger.java @@ -622,7 +622,7 @@ public class Trigger try { String[] conditions = this.getTriggerParameter2().split(Trigger.triggerParameter2Split); - List calendarEvents = CalendarReceiver.readCalendarEvents(AutomationService.getInstance(), false); + List calendarEvents = CalendarReceiver.readCalendarEvents(AutomationService.getInstance(), true); /* 0 = titleDirection 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 35adc99..a8d8f17 100644 --- a/app/src/main/java/com/jens/automation2/receivers/CalendarReceiver.java +++ b/app/src/main/java/com/jens/automation2/receivers/CalendarReceiver.java @@ -6,7 +6,6 @@ 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; @@ -25,6 +24,11 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis private static AutomationService automationServiceRef; private static Intent calendarIntent = null; + public static final int AVAILABILITY_OUT_OF_OFFICE = 4; + public static final int AVAILABILITY_WORKING_ELSEWHERE = 5; + + static List calendarsCache = null; + public static CalendarReceiver getInstance() { if(calendarReceiverInstance == null) @@ -94,9 +98,21 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis return new Trigger.Trigger_Enum[]{Trigger.Trigger_Enum.calendarEvent}; } + public static class AndroidCalendar + { + public int calendarId; + public String displayName; + } + 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 boolean allDay; @@ -107,19 +123,55 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis } } + public static List 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 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 readCalendarEvents(Context context, boolean includePastEvents) { 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" }, + new String[] { "calendar_id", "_id", "title", "description", "allDay", "dtstart", "dtend", "eventLocation", "availability" }, null, null, null); cursor.moveToFirst(); @@ -135,7 +187,17 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis try { 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.title = cursor.getString(2); event.description = cursor.getString(3); diff --git a/app/src/main/res/drawable-hdpi/calendar.png b/app/src/main/res/drawable-hdpi/calendar.png new file mode 100644 index 0000000000000000000000000000000000000000..f529d02587c67cbbda778d1fd4da728d8f7f06b4 GIT binary patch literal 2155 zcmV-x2$c7UP)Px#1ZP1_K>z@;j|==^1poj532;bRa{vG-N&o;UN&zE@nE?O*2mnb$K~!i%%~@TH zRn-;#?mg%JUj~7hVPHTeLPY|x0gVzN#Y%{28WOQDCZxXje`bfaapE5)nmo+jja9cS8D2;}*3=_rn{HQW-B>czv|| zCLUjR*d5k}8$JJ)@!A)Y_Nr_O|0!9BbJwzQ5rZVyjVfR@kjiA$SiMr>gKebK8ARE7 zcIrJ`OSOE7kT^rNWLxa`Dx69y<4Lah*0(eGq--5UvGxWI_BqUJ03V|Vul#Mot{6$2 zD_F>K=O#C9io-?(Uw`r^EMK!8tGm`BZkouA4ddG7e`5R2pL50XX5U25rmdLx?k2C;eD z4xBmpE`Iao`{-N0K}DSqBdI8)uQ45BM886E`U{QMNzPGsON)Y#uMHMMzhc>_GV&d< zQMc(tG9#0S(PqUxKjk>ns^bpC866hMx$kGcKo9#yG>VuJLwZ>UVqB|O$Yb{}A4lh! zJ5jDTTGkceUlSXWQS46}N8IkBp^X^XW3>U~DEWqsIZEEu^HBqh>_MkUjMG9#x>Qibdi*D0wMD7(U;X2?r)E|<{J z1Z^w5%Ho@(qk~K(fANiS!*;C*!(_bFD(thM#*?tCWmR7yG>S=GY1CO;sxadTdR$la z#9+;lPc9`zTGry26kj>zBvpHuN`GN0#W*I!OfsDQuy-|ki-(p)1i$|4i2Xn(>w_F1 z0W^lY~!J?WIS|nZIAO@7a7e zeGn(Oth;ja0UkL-i#Ff!t1ewGRL2?dREe~?nQ8oSU>x72dyr&*YAHs{#S7={t=qP# zVk*nd2T4g=<_;rDwoFK%~n<@+)5^}(rPh1f_YvF6c7ap%EVPJz_)}tTR@<4kMFMb1h)n%zX8gN~8Gw_an7_J?u-DoH-Z%wLJLsFc5ByA)30h z{@UR}(rD(>PnDkDz8z*q$HHRk>H^jGtsfRbkC>*ev;de!rO{E8PMuQSEbl9F3N3)U zRyzp`p+$^eca?daP_ZLv^vySD@S@`4;i6FjKEJ$JGU&>o&qm@ zLQ-V)Y7Fy^bM3C`bjsdJQ0wJrw+i>yw@13Z*jR>{)wh-diT}Us%?@H;XoF4u#jPkDI zk+vjHrT+f@*OJNPkxaYaT>cOIm~;^`L_FIXi}jK_mGQ4y;YhJqRDJIg4O@gH?|HhQmu44CtKT!F4#i@z@Q)7{LQ1Jr h!oGd`G + + + + @@ -184,16 +198,80 @@ android:layout_width="wrap_content" android:layout_height="wrap_content"> - - - + android:text="@string/calendarStringBusy" /> + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8402eb3..0b92ebe 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -917,4 +917,7 @@ tentative out of office 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. \ No newline at end of file