forked from jens/Automation
calendar trigger
This commit is contained in:
@ -0,0 +1,35 @@
|
||||
package com.jens.automation2;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.CompoundButton;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
public class ActivityManageTriggerCalendar extends Activity
|
||||
{
|
||||
CheckBox chkCalendarEventDirection;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState)
|
||||
{
|
||||
super.onCreate(savedInstanceState);
|
||||
Miscellaneous.setDisplayLanguage(this);
|
||||
setContentView(R.layout.activity_manage_trigger_calendar);
|
||||
|
||||
chkCalendarEventDirection = (CheckBox) findViewById(R.id.chkCalendarEventDirection);
|
||||
|
||||
chkCalendarEventDirection.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
|
||||
{
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton compoundButton, boolean checked)
|
||||
{
|
||||
if(checked)
|
||||
chkCalendarEventDirection.setText(R.string.eventIsCurrentlyHappening);
|
||||
else
|
||||
chkCalendarEventDirection.setText(R.string.eventIsCurrentlyNotHappening);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -14,6 +14,7 @@ import com.jens.automation2.location.WifiBroadcastReceiver;
|
||||
import com.jens.automation2.receivers.BatteryReceiver;
|
||||
import com.jens.automation2.receivers.BluetoothReceiver;
|
||||
import com.jens.automation2.receivers.BroadcastListener;
|
||||
import com.jens.automation2.receivers.CalendarReceiver;
|
||||
import com.jens.automation2.receivers.ConnectivityReceiver;
|
||||
import com.jens.automation2.receivers.DeviceOrientationListener;
|
||||
import com.jens.automation2.receivers.HeadphoneJackListener;
|
||||
@ -31,6 +32,7 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Trigger
|
||||
@ -63,6 +65,7 @@ public class Trigger
|
||||
tethering,
|
||||
subSystemState,
|
||||
checkVariable,
|
||||
calendarEvent,
|
||||
phoneCall; //phoneCall always needs to be at the very end because of Google's shitty so called privacy
|
||||
|
||||
public String getFullName(Context context)
|
||||
@ -123,6 +126,8 @@ public class Trigger
|
||||
return context.getResources().getString(R.string.subSystemState);
|
||||
case checkVariable:
|
||||
return context.getResources().getString(R.string.checkVariable);
|
||||
case calendarEvent:
|
||||
return context.getResources().getString(R.string.calendarEvent);
|
||||
default:
|
||||
return "Unknown";
|
||||
}
|
||||
@ -247,11 +252,14 @@ public class Trigger
|
||||
case subSystemState:
|
||||
if(!checkSubSystemState())
|
||||
result = false;
|
||||
break;
|
||||
case checkVariable:
|
||||
if(!checkVariable())
|
||||
result = false;
|
||||
break;
|
||||
case calendarEvent:
|
||||
if(!checkCalendarEvent())
|
||||
result = false;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@ -609,6 +617,65 @@ public class Trigger
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean checkCalendarEvent()
|
||||
{
|
||||
try
|
||||
{
|
||||
String[] conditions = this.getTriggerParameter2().split(Trigger.triggerParameter2Split);
|
||||
List<CalendarReceiver.CalendarEvent> calendarEvents = CalendarReceiver.readCalendarEvents(AutomationService.getInstance());
|
||||
|
||||
/*
|
||||
0 = titleDirection
|
||||
1 = title;
|
||||
2 = descriptionDirection
|
||||
3 = description
|
||||
4 = eventLocationDirection
|
||||
5 = eventLocation
|
||||
6 = availabilityDirection
|
||||
7 = availability
|
||||
*/
|
||||
|
||||
for(CalendarReceiver.CalendarEvent event : calendarEvents)
|
||||
{
|
||||
boolean isActive = Boolean.parseBoolean(conditions[0]);
|
||||
if(isActive == event.isCurrentActive())
|
||||
{
|
||||
if(Miscellaneous.compare(conditions[0], conditions[1], event.title))
|
||||
{
|
||||
// title matches according to demands
|
||||
|
||||
if(Miscellaneous.compare(conditions[2], conditions[3], event.description))
|
||||
{
|
||||
// description matches according to demands
|
||||
|
||||
if(Miscellaneous.compare(conditions[4], conditions[5], event.location))
|
||||
{
|
||||
// location matches according to demands
|
||||
|
||||
if(Miscellaneous.compare(conditions[6], conditions[7], event.availability))
|
||||
{
|
||||
// availability matches according to demands
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
//TODO: If trigger demands no calendar event and there is absolutely no future event, we'll need to check for that.
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
Miscellaneous.logEvent("e", "checkVariable()", Log.getStackTraceString(e), 1);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean checkBluetooth()
|
||||
{
|
||||
Miscellaneous.logEvent("i", Miscellaneous.getAnyContext().getResources().getString(R.string.ruleCheckOf), String.format("Checking for bluetooth...", this.getParentRule().getName()), 4);
|
||||
|
@ -0,0 +1,128 @@
|
||||
package com.jens.automation2.receivers;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
|
||||
import com.jens.automation2.AutomationService;
|
||||
import com.jens.automation2.Miscellaneous;
|
||||
import com.jens.automation2.Rule;
|
||||
import com.jens.automation2.Trigger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
|
||||
public class CalendarReceiver extends BroadcastReceiver implements AutomationListenerInterface
|
||||
{
|
||||
static BroadcastReceiver calendarReceiverInstance = null;
|
||||
static boolean calendarReceiverActive = false;
|
||||
static IntentFilter calendarIntentFilter = null;
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent)
|
||||
{
|
||||
if(intent.getAction().equalsIgnoreCase(Intent.ACTION_PROVIDER_CHANGED))
|
||||
{
|
||||
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.calendarEvent);
|
||||
for(int i = 0; i < ruleCandidates.size(); i++)
|
||||
{
|
||||
if(ruleCandidates.get(i).getsGreenLight(context))
|
||||
ruleCandidates.get(i).activate(AutomationService.getInstance(), false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startListener(AutomationService automationService)
|
||||
{
|
||||
if(!calendarReceiverActive)
|
||||
{
|
||||
if(calendarReceiverInstance == null)
|
||||
calendarReceiverInstance = new BatteryReceiver();
|
||||
|
||||
if(calendarIntentFilter == null)
|
||||
{
|
||||
calendarIntentFilter = new IntentFilter();
|
||||
calendarIntentFilter.addAction(Intent.ACTION_PROVIDER_CHANGED);
|
||||
}
|
||||
|
||||
AutomationService.getInstance().registerReceiver(calendarReceiverInstance, calendarIntentFilter);
|
||||
|
||||
calendarReceiverActive = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopListener(AutomationService automationService)
|
||||
{
|
||||
if(calendarReceiverActive)
|
||||
{
|
||||
if(calendarReceiverInstance != null)
|
||||
{
|
||||
AutomationService.getInstance().unregisterReceiver(calendarReceiverInstance);
|
||||
calendarReceiverInstance = null;
|
||||
}
|
||||
|
||||
calendarReceiverActive = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isListenerRunning()
|
||||
{
|
||||
return calendarReceiverActive;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Trigger.Trigger_Enum[] getMonitoredTrigger()
|
||||
{
|
||||
return new Trigger.Trigger_Enum[]{Trigger.Trigger_Enum.calendarEvent};
|
||||
}
|
||||
|
||||
public static class CalendarEvent
|
||||
{
|
||||
public String id, title, description, location, availability;
|
||||
public Calendar start, end;
|
||||
|
||||
public boolean isCurrentActive()
|
||||
{
|
||||
Calendar now = Calendar.getInstance();
|
||||
return now.getTimeInMillis() >= start.getTimeInMillis() && now.getTimeInMillis() < end.getTimeInMillis();
|
||||
}
|
||||
}
|
||||
|
||||
public static List<CalendarEvent> readCalendarEvents(Context context)
|
||||
{
|
||||
Cursor cursor = context.getContentResolver()
|
||||
.query(
|
||||
Uri.parse("content://com.android.calendar/events"),
|
||||
new String[] { "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation", "AVAILABILITY" },
|
||||
null, null, null);
|
||||
|
||||
cursor.moveToFirst();
|
||||
// fetching calendars name
|
||||
String CNames[] = new String[cursor.getCount()];
|
||||
|
||||
List<CalendarEvent> eventlist = new ArrayList<>();
|
||||
|
||||
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);
|
||||
|
||||
cursor.moveToNext();
|
||||
}
|
||||
|
||||
return eventlist;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user