forked from jens/Automation
calendar trigger
This commit is contained in:
@@ -30,6 +30,7 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
public static final int AVAILABILITY_WORKING_ELSEWHERE = 5;
|
||||
|
||||
static List<AndroidCalendar> calendarsCache = null;
|
||||
static List<CalendarEvent> calendarEventsCache = null;
|
||||
|
||||
public static CalendarReceiver getInstance()
|
||||
{
|
||||
@@ -44,6 +45,9 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
{
|
||||
if(intent.getAction().equalsIgnoreCase(Intent.ACTION_PROVIDER_CHANGED))
|
||||
{
|
||||
calendarsCache = null;
|
||||
calendarEventsCache = null;
|
||||
|
||||
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.calendarEvent);
|
||||
for(int i = 0; i < ruleCandidates.size(); i++)
|
||||
{
|
||||
@@ -65,6 +69,7 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
{
|
||||
calendarIntentFilter = new IntentFilter();
|
||||
calendarIntentFilter.addAction(Intent.ACTION_PROVIDER_CHANGED);
|
||||
// calendarIntentFilter.addDataScheme("content");
|
||||
}
|
||||
|
||||
AutomationService.getInstance().registerReceiver(calendarReceiverInstance, calendarIntentFilter);
|
||||
@@ -104,6 +109,14 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
{
|
||||
public int calendarId;
|
||||
public String displayName;
|
||||
public String accountString;
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return displayName + " (" + accountString + ")";
|
||||
}
|
||||
}
|
||||
|
||||
public static class CalendarEvent
|
||||
@@ -142,7 +155,7 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
|
||||
cursor = context.getContentResolver().query(
|
||||
Uri.parse("content://com.android.calendar/calendars"),
|
||||
new String[]{"_id", "calendar_displayName"},
|
||||
new String[]{ "_id", "calendar_displayName", "ownerAccount", },
|
||||
null, null, null);
|
||||
|
||||
cursor.moveToFirst();
|
||||
@@ -158,6 +171,7 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
AndroidCalendar calendar = new AndroidCalendar();
|
||||
calendar.calendarId = Integer.parseInt(cursor.getString(0));
|
||||
calendar.displayName = cursor.getString(1);
|
||||
calendar.accountString = cursor.getString(2);
|
||||
|
||||
calendarsCache.add(calendar);
|
||||
}
|
||||
@@ -176,58 +190,61 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
|
||||
public static List<CalendarEvent> readCalendarEvents(Context context, boolean includePastEvents)
|
||||
{
|
||||
Cursor cursor;
|
||||
|
||||
cursor = context.getContentResolver().query(
|
||||
Uri.parse("content://com.android.calendar/events"),
|
||||
new String[] { "calendar_id", "_id", "title", "description", "allDay", "dtstart", "dtend", "eventLocation", "availability" },
|
||||
null, null, null);
|
||||
|
||||
cursor.moveToFirst();
|
||||
// fetching calendars name
|
||||
String CNames[] = new String[cursor.getCount()];
|
||||
|
||||
List<CalendarEvent> eventlist = new ArrayList<>();
|
||||
|
||||
Calendar now = Calendar.getInstance();
|
||||
|
||||
for (int i = 0; i < CNames.length; i++)
|
||||
if(calendarEventsCache == null)
|
||||
{
|
||||
try
|
||||
calendarEventsCache = new ArrayList<>();
|
||||
|
||||
Cursor cursor;
|
||||
|
||||
cursor = context.getContentResolver().query(
|
||||
Uri.parse("content://com.android.calendar/events"),
|
||||
new String[] { "calendar_id", "_id", "title", "description", "allDay", "dtstart", "dtend", "eventLocation", "availability" },
|
||||
null, null, null);
|
||||
|
||||
cursor.moveToFirst();
|
||||
// fetching calendars name
|
||||
String CNames[] = new String[cursor.getCount()];
|
||||
|
||||
Calendar now = Calendar.getInstance();
|
||||
|
||||
for (int i = 0; i < CNames.length; i++)
|
||||
{
|
||||
CalendarEvent event = new CalendarEvent();
|
||||
event.calendarId = Integer.parseInt(cursor.getString(0));
|
||||
|
||||
for(AndroidCalendar cal : readCalendars(context))
|
||||
try
|
||||
{
|
||||
if(cal.calendarId == event.calendarId)
|
||||
CalendarEvent event = new CalendarEvent();
|
||||
event.calendarId = Integer.parseInt(cursor.getString(0));
|
||||
|
||||
for(AndroidCalendar cal : readCalendars(context))
|
||||
{
|
||||
event.calendar = cal;
|
||||
break;
|
||||
if(cal.calendarId == event.calendarId)
|
||||
{
|
||||
event.calendar = cal;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
event.eventId = cursor.getString(1);
|
||||
event.title = cursor.getString(2);
|
||||
event.description = cursor.getString(3);
|
||||
event.allDay = cursor.getString(4).equals("1");
|
||||
event.start = Miscellaneous.calendarFromLong(Long.parseLong(cursor.getString(5)));
|
||||
event.end = Miscellaneous.calendarFromLong(Long.parseLong(cursor.getString(6)));
|
||||
event.location = cursor.getString(7);
|
||||
event.availability = cursor.getString(8);
|
||||
|
||||
if(includePastEvents || event.end.getTimeInMillis() > now.getTimeInMillis())
|
||||
calendarEventsCache.add(event);
|
||||
}
|
||||
|
||||
event.eventId = cursor.getString(1);
|
||||
event.title = cursor.getString(2);
|
||||
event.description = cursor.getString(3);
|
||||
event.allDay = cursor.getString(4).equals("1");
|
||||
event.start = Miscellaneous.calendarFromLong(Long.parseLong(cursor.getString(5)));
|
||||
event.end = Miscellaneous.calendarFromLong(Long.parseLong(cursor.getString(6)));
|
||||
event.location = cursor.getString(7);
|
||||
event.availability = cursor.getString(8);
|
||||
|
||||
if(includePastEvents || event.end.getTimeInMillis() > now.getTimeInMillis())
|
||||
eventlist.add(event);
|
||||
catch (Exception e)
|
||||
{}
|
||||
cursor.moveToNext();
|
||||
}
|
||||
catch (Exception e)
|
||||
{}
|
||||
cursor.moveToNext();
|
||||
|
||||
if(cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
if(cursor != null)
|
||||
cursor.close();
|
||||
|
||||
return eventlist;
|
||||
return calendarEventsCache;
|
||||
}
|
||||
|
||||
public static void startCalendarReceiver(final AutomationService automationServiceRef)
|
||||
|
Reference in New Issue
Block a user