Calendar trigger

This commit is contained in:
Jens 2023-12-25 11:57:55 +01:00
parent 5f278a6ba0
commit 67238bd2f0
8 changed files with 281 additions and 30 deletions

View File

@ -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)

View File

@ -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<String> directionSpinnerAdapter;
private static String[] availabilities;
ArrayAdapter<String> 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<String>(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<String>(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]);
}
}
}

View File

@ -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;

View File

@ -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)
{

View File

@ -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;

View File

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

View File

@ -77,12 +77,12 @@
android:layout_height="wrap_content">
<Spinner
android:id="@+id/spinnerTitleDirection"
android:id="@+id/spinnerCalendarTitleDirection"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/etNotificationTitle"
android:id="@+id/etCalendarTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
@ -113,12 +113,12 @@
android:layout_height="wrap_content">
<Spinner
android:id="@+id/spinnerTextDirection"
android:id="@+id/spinnerCalendarLocationDirection"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/etNotificationText"
android:id="@+id/etCalendarLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
@ -149,12 +149,12 @@
android:layout_height="wrap_content">
<Spinner
android:id="@+id/spinnerMarkedAsDirection"
android:id="@+id/spinnerCalendarDescriptionDirection"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spinnerMarkedAsOptions"
<EditText
android:id="@+id/etCalendarDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
@ -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" />
<LinearLayout
android:orientation="vertical"
@ -185,12 +185,12 @@
android:layout_height="wrap_content">
<Spinner
android:id="@+id/spinnerTextDirection"
android:id="@+id/spinnerCalendarAvailabilityDirection"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/etNotificationText"
<Spinner
android:id="@+id/spinnerCalendarAvailability"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
@ -202,7 +202,7 @@
</TableLayout>
<Button
android:id="@+id/bSaveActionCloseNotification"
android:id="@+id/bSaveTriggerCalendar"
android:layout_marginTop="@dimen/default_margin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

View File

@ -910,6 +910,11 @@
<string name="calendarEventCapital">Calendar event</string>
<string name="location">Location</string>
<string name="calendarDescription">Description/notes</string>
<string name="markedAs">Marked as</string>
<string name="availability">Availability</string>
<string name="eventIsCurrentlyNotHappening">Event is currently not happening/has ended</string>
<string name="calendarStringBusy">busy</string>
<string name="calendarStringFree">free</string>
<string name="calendarStringTentative">tentative</string>
<string name="calendarStringOutOfOffice">out of office</string>
<string name="calendarStringWorkingElsewhere">working elsewhere</string>
</resources>