From 67238bd2f095403d0d138bc632f937ed7f4d07d4 Mon Sep 17 00:00:00 2001 From: Jens Date: Mon, 25 Dec 2023 11:57:55 +0100 Subject: [PATCH] Calendar trigger --- .../jens/automation2/ActivityManageRule.java | 47 +++++- .../ActivityManageTriggerCalendar.java | 137 ++++++++++++++++++ .../jens/automation2/ActivityPermissions.java | 7 + .../jens/automation2/ReceiverCoordinator.java | 5 + .../java/com/jens/automation2/Trigger.java | 8 +- .../receivers/CalendarReceiver.java | 76 ++++++++-- .../activity_manage_trigger_calendar.xml | 24 +-- app/src/main/res/values/strings.xml | 7 +- 8 files changed, 281 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/com/jens/automation2/ActivityManageRule.java b/app/src/main/java/com/jens/automation2/ActivityManageRule.java index f07971a..73d8ec1 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageRule.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageRule.java @@ -143,6 +143,8 @@ public class ActivityManageRule extends Activity final static int requestCodeActionCopyTextToClipboardEdit = 830; final static int requestCodeActionSetLocationServiceAdd = 831; final static int requestCodeActionSetLocationServiceEdit = 832; + final static int requestCodeTriggerCalendarEventAdd = 833; + final static int requestCodeTriggerCalendarEventEdit = 834; public static ActivityManageRule getInstance() { @@ -347,6 +349,12 @@ public class ActivityManageRule extends Activity variableStateEditor.putExtra(intentNameTriggerParameter2, selectedTrigger.getTriggerParameter2()); startActivityForResult(variableStateEditor, requestCodeTriggerCheckVariableEdit); break; + case calendarEvent: + Intent calendarStateEditor = new Intent(ActivityManageRule.this, ActivityManageTriggerCalendar.class); + calendarStateEditor.putExtra(intentNameTriggerParameter1, selectedTrigger.getTriggerParameter()); + calendarStateEditor.putExtra(intentNameTriggerParameter2, selectedTrigger.getTriggerParameter2()); + startActivityForResult(calendarStateEditor, requestCodeTriggerCalendarEventEdit); + break; default: break; } @@ -636,6 +644,8 @@ public class ActivityManageRule extends Activity items.add(new Item(typesLong[i].toString(), R.drawable.router)); 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.calendar)); else items.add(new Item(typesLong[i].toString(), R.drawable.placeholder)); } @@ -644,15 +654,15 @@ public class ActivityManageRule extends Activity { public View getView(int position, View convertView, ViewGroup parent) { - //User super class to create the View + // User super class to create the View View v = super.getView(position, convertView, parent); TextView tv = (TextView)v.findViewById(android.R.id.text1); - //Put the image on the TextView + // Put the image on the TextView tv.setCompoundDrawablesWithIntrinsicBounds(items.get(position).icon, 0, 0, 0); - //Add margin between image and text (support various screen densities) + // Add margin between image and text (support various screen densities) int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f); tv.setCompoundDrawablePadding(dp5); @@ -861,6 +871,13 @@ public class ActivityManageRule extends Activity startActivityForResult(variableTriggerEditor, requestCodeTriggerCheckVariableAdd); return; } + else if(triggerType == Trigger_Enum.calendarEvent) + { + newTrigger.setTriggerType(Trigger_Enum.calendarEvent); + Intent calendarTriggerEditor = new Intent(myContext, ActivityManageTriggerCalendar.class); + startActivityForResult(calendarTriggerEditor, requestCodeTriggerCalendarEventAdd); + return; + } else getTriggerParameterDialog(context, booleanChoices).show(); @@ -1994,6 +2011,17 @@ public class ActivityManageRule extends Activity this.refreshTriggerList(); } } + else if(requestCode == requestCodeTriggerCalendarEventAdd) + { + if(resultCode == RESULT_OK) + { + newTrigger.setTriggerParameter(data.getBooleanExtra(intentNameTriggerParameter1, true)); + newTrigger.setTriggerParameter2(data.getStringExtra(intentNameTriggerParameter2)); + newTrigger.setParentRule(ruleToEdit); + ruleToEdit.getTriggerSet().add(newTrigger); + this.refreshTriggerList(); + } + } else if(requestCode == requestCodeTriggerTetheringEdit) { if(resultCode == RESULT_OK) @@ -2033,6 +2061,19 @@ public class ActivityManageRule extends Activity this.refreshTriggerList(); } } + else if(requestCode == requestCodeTriggerCalendarEventEdit) + { + if(resultCode == RESULT_OK) + { + Trigger editedTrigger = new Trigger(); + editedTrigger.setTriggerType(Trigger_Enum.calendarEvent); + editedTrigger.setTriggerParameter(data.getBooleanExtra(intentNameTriggerParameter1, true)); + editedTrigger.setTriggerParameter2(data.getStringExtra(intentNameTriggerParameter2)); + editedTrigger.setParentRule(ruleToEdit); + ruleToEdit.getTriggerSet().set(editIndex, editedTrigger); + this.refreshTriggerList(); + } + } else if(requestCode == requestCodeActionCopyTextToClipboardAdd) { if(resultCode == RESULT_OK) diff --git a/app/src/main/java/com/jens/automation2/ActivityManageTriggerCalendar.java b/app/src/main/java/com/jens/automation2/ActivityManageTriggerCalendar.java index 83542a9..4f00676 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageTriggerCalendar.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageTriggerCalendar.java @@ -1,15 +1,30 @@ package com.jens.automation2; import android.app.Activity; +import android.content.Intent; import android.os.Bundle; +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.Spinner; import androidx.annotation.Nullable; public class ActivityManageTriggerCalendar extends Activity { CheckBox chkCalendarEventDirection; + Spinner spinnerCalendarTitleDirection, spinnerCalendarLocationDirection, spinnerCalendarDescriptionDirection, spinnerCalendarAvailabilityDirection, spinnerCalendarAvailability; + EditText etCalendarTitle, etCalendarLocation, etCalendarDescription; + Button bSaveTriggerCalendar; + + private static String[] directions; + ArrayAdapter directionSpinnerAdapter; + + private static String[] availabilities; + ArrayAdapter availabilitySpinnerAdapter; @Override protected void onCreate(@Nullable Bundle savedInstanceState) @@ -19,6 +34,51 @@ public class ActivityManageTriggerCalendar extends Activity setContentView(R.layout.activity_manage_trigger_calendar); chkCalendarEventDirection = (CheckBox) findViewById(R.id.chkCalendarEventDirection); + 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); + + etCalendarTitle = (EditText)findViewById(R.id.etCalendarTitle); + etCalendarLocation = (EditText)findViewById(R.id.etCalendarLocation); + etCalendarDescription = (EditText)findViewById(R.id.etCalendarDescription); + + bSaveTriggerCalendar = (Button)findViewById(R.id.bSaveTriggerCalendar); + + directions = new String[] { + getResources().getString(R.string.directionStringEquals), + getResources().getString(R.string.directionStringContains), + getResources().getString(R.string.directionStringDoesNotContain), + getResources().getString(R.string.directionStringStartsWith), + getResources().getString(R.string.directionStringEndsWith), + getResources().getString(R.string.directionStringNotEquals) + }; + directionSpinnerAdapter = new ArrayAdapter(this, R.layout.text_view_for_poi_listview_mediumtextsize, ActivityManageTriggerCalendar.directions); + 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(); chkCalendarEventDirection.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @@ -31,5 +91,82 @@ public class ActivityManageTriggerCalendar extends Activity chkCalendarEventDirection.setText(R.string.eventIsCurrentlyNotHappening); } }); + + bSaveTriggerCalendar.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View view) + { + String titleDir = Trigger.getMatchCode(spinnerCalendarTitleDirection.getSelectedItem().toString()); + String title = etCalendarTitle.getText().toString(); + String descriptionDir = Trigger.getMatchCode(spinnerCalendarDescriptionDirection.getSelectedItem().toString()); + 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(); + + 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); + + Intent data = new Intent(); + data.putExtra(ActivityManageRule.intentNameTriggerParameter1, chkCalendarEventDirection.isChecked()); + data.putExtra(ActivityManageRule.intentNameTriggerParameter2, returnString); + ActivityManageTriggerCalendar.this.setResult(RESULT_OK, data); + + finish(); + } + }); + + Intent inputIntent = getIntent(); + if(inputIntent.hasExtra(ActivityManageRule.intentNameTriggerParameter1)) + loadValuesIntoGui(inputIntent); + } + + void loadValuesIntoGui(Intent data) + { + //TODO:try-catch + + if(data.hasExtra(ActivityManageRule.intentNameTriggerParameter1)) + chkCalendarEventDirection.setChecked(data.getBooleanExtra(ActivityManageRule.intentNameTriggerParameter1, true)); + + if(data.hasExtra(ActivityManageRule.intentNameTriggerParameter2)) + { + String input[] = data.getStringExtra(ActivityManageRule.intentNameTriggerParameter2).split(Trigger.triggerParameter2Split); + /* + 0 = titleDir + 1 = title + 2 = descriptionDir + 3 = description + 4 = locationDir + 5 = location + 6 = availabilityDir + 7 = availability + */ + + for(int i = 0; i < directions.length; i++) + { + if(Trigger.getMatchCode(directions[i]).equalsIgnoreCase(input[0])) + spinnerCalendarTitleDirection.setSelection(i); + + if(Trigger.getMatchCode(directions[i]).equalsIgnoreCase(input[2])) + spinnerCalendarDescriptionDirection.setSelection(i); + + 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/ActivityPermissions.java b/app/src/main/java/com/jens/automation2/ActivityPermissions.java index de627e3..76e1795 100644 --- a/app/src/main/java/com/jens/automation2/ActivityPermissions.java +++ b/app/src/main/java/com/jens/automation2/ActivityPermissions.java @@ -580,6 +580,9 @@ public class ActivityPermissions extends Activity case notification: addToArrayListUnique(Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE, requiredPermissions); break; + case calendarEvent: + addToArrayListUnique(Manifest.permission.READ_CALENDAR, requiredPermissions); + break; default: break; } @@ -1023,6 +1026,10 @@ public class ActivityPermissions extends Activity for(String ruleName : getRulesUsing(Action.Action_Enum.setLocationService)) usingElements.add(String.format(getResources().getString(R.string.ruleXrequiresThis), ruleName)); break; + case Manifest.permission.READ_CALENDAR: + for(String ruleName : getRulesUsing(Trigger.Trigger_Enum.calendarEvent)) + usingElements.add(String.format(getResources().getString(R.string.ruleXrequiresThis), ruleName)); + break; } return usingElements; diff --git a/app/src/main/java/com/jens/automation2/ReceiverCoordinator.java b/app/src/main/java/com/jens/automation2/ReceiverCoordinator.java index 93b0339..65b144d 100644 --- a/app/src/main/java/com/jens/automation2/ReceiverCoordinator.java +++ b/app/src/main/java/com/jens/automation2/ReceiverCoordinator.java @@ -6,6 +6,7 @@ import android.util.Log; import com.jens.automation2.location.CellLocationChangedReceiver; import com.jens.automation2.location.WifiBroadcastReceiver; import com.jens.automation2.receivers.BroadcastListener; +import com.jens.automation2.receivers.CalendarReceiver; import com.jens.automation2.receivers.DateTimeListener; import com.jens.automation2.receivers.AutomationListenerInterface; import com.jens.automation2.receivers.BatteryReceiver; @@ -210,6 +211,9 @@ public class ReceiverCoordinator if(Rule.isAnyRuleUsing(Trigger.Trigger_Enum.screenState)) ScreenStateReceiver.startScreenStateReceiver(AutomationService.getInstance()); + + if(Rule.isAnyRuleUsing(Trigger.Trigger_Enum.calendarEvent)) + CalendarReceiver.startCalendarReceiver(AutomationService.getInstance()); } public static void stopAllReceivers() @@ -243,6 +247,7 @@ public class ReceiverCoordinator BluetoothReceiver.stopBluetoothReceiver(); HeadphoneJackListener.getInstance().stopListener(AutomationService.getInstance()); DeviceOrientationListener.getInstance().stopListener(AutomationService.getInstance()); + CalendarReceiver.getInstance().stopListener(AutomationService.getInstance()); } catch(Exception e) { diff --git a/app/src/main/java/com/jens/automation2/Trigger.java b/app/src/main/java/com/jens/automation2/Trigger.java index 4bb023a..cd1b88c 100644 --- a/app/src/main/java/com/jens/automation2/Trigger.java +++ b/app/src/main/java/com/jens/automation2/Trigger.java @@ -127,7 +127,7 @@ public class Trigger case checkVariable: return context.getResources().getString(R.string.checkVariable); case calendarEvent: - return context.getResources().getString(R.string.calendarEvent); + return context.getResources().getString(R.string.calendarEventCapital); default: return "Unknown"; } @@ -1880,6 +1880,12 @@ public class Trigger else returnString.append(String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.variableCheckStringDeleted), triggerParameter2)); break; + case calendarEvent: + if(triggerParameter) + returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.eventIsCurrentlyHappening)); + else + returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.eventIsCurrentlyNotHappening)); + break; default: returnString.append("error"); break; 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 f9474c7..e8fdba3 100644 --- a/app/src/main/java/com/jens/automation2/receivers/CalendarReceiver.java +++ b/app/src/main/java/com/jens/automation2/receivers/CalendarReceiver.java @@ -18,9 +18,20 @@ import java.util.List; public class CalendarReceiver extends BroadcastReceiver implements AutomationListenerInterface { - static BroadcastReceiver calendarReceiverInstance = null; + static CalendarReceiver calendarReceiverInstance = null; static boolean calendarReceiverActive = false; static IntentFilter calendarIntentFilter = null; + private static AutomationService automationServiceRef; + private static Intent calendarIntent = null; + + public static CalendarReceiver getInstance() + { + if(calendarReceiverInstance == null) + calendarReceiverInstance = new CalendarReceiver(); + + return calendarReceiverInstance; + } + @Override public void onReceive(Context context, Intent intent) { @@ -41,7 +52,7 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis if(!calendarReceiverActive) { if(calendarReceiverInstance == null) - calendarReceiverInstance = new BatteryReceiver(); + calendarReceiverInstance = new CalendarReceiver(); if(calendarIntentFilter == null) { @@ -99,7 +110,7 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis Cursor cursor = context.getContentResolver() .query( Uri.parse("content://com.android.calendar/events"), - new String[] { "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation", "AVAILABILITY" }, + new String[] { "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation", "availability" }, null, null, null); cursor.moveToFirst(); @@ -110,19 +121,58 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis 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); - + try + { + 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); + } + catch (Exception e) + {} cursor.moveToNext(); } return eventlist; } + + public static void startCalendarReceiver(final AutomationService automationServiceRef) + { + if (!calendarReceiverActive) + { + CalendarReceiver.automationServiceRef = automationServiceRef; + + if (calendarReceiverInstance == null) + calendarReceiverInstance = new CalendarReceiver(); + + if (calendarIntentFilter == null) + { + calendarIntentFilter = new IntentFilter(); + calendarIntentFilter.addAction(Intent.ACTION_PROVIDER_CHANGED); + } + + calendarIntent = automationServiceRef.registerReceiver(calendarReceiverInstance, calendarIntentFilter); + + calendarReceiverActive = true; + } + } + + public static void stopScreenStateReceiver() + { + if (calendarReceiverActive) + { + if (calendarReceiverInstance != null) + { + automationServiceRef.unregisterReceiver(calendarReceiverInstance); + calendarReceiverInstance = null; + } + + calendarReceiverActive = false; + } + } } \ 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 index 64f7e99..8e554ca 100644 --- a/app/src/main/res/layout/activity_manage_trigger_calendar.xml +++ b/app/src/main/res/layout/activity_manage_trigger_calendar.xml @@ -77,12 +77,12 @@ android:layout_height="wrap_content"> @@ -113,12 +113,12 @@ android:layout_height="wrap_content"> @@ -149,12 +149,12 @@ android:layout_height="wrap_content"> - @@ -177,7 +177,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" - android:text="@string/markedAs" /> + android:text="@string/availability" /> - @@ -202,7 +202,7 @@