diff --git a/app/src/main/java/com/jens/automation2/ActivityManageRule.java b/app/src/main/java/com/jens/automation2/ActivityManageRule.java index db6af2b7..73d8ec16 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 4f00676f..851b4ce0 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 c8d522a4..402bc0e8 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 35adc998..a8d8f172 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 00000000..f529d025 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/calendar.png differ 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 8e554ca8..6a682a93 100644 --- a/app/src/main/res/layout/activity_manage_trigger_calendar.xml +++ b/app/src/main/res/layout/activity_manage_trigger_calendar.xml @@ -16,6 +16,11 @@ android:textSize="25dp" android:layout_marginBottom="@dimen/default_margin" /> + + + + @@ -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 8402eb3b..0b92ebe1 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