Refactoring

This commit is contained in:
Jens 2021-12-07 23:10:37 +01:00
parent 128116025f
commit b88801500f
17 changed files with 34 additions and 29 deletions

View File

@ -345,6 +345,11 @@ public class Rule implements Comparable<Rule>
return false;
}
public boolean getsGreenLight(Context context)
{
return isRuleActive() && applies(context) && (hasNotAppliedSinceLastExecution() || isActuallyToggable());
}
public boolean applies(Context context)
{

View File

@ -294,7 +294,7 @@ public class ActivityDetectionReceiver extends IntentService implements Automati
ArrayList<Rule> allRulesWithActivityDetection = Rule.findRuleCandidatesByActivityDetection();
for(int i=0; i<allRulesWithActivityDetection.size(); i++)
{
if((allRulesWithActivityDetection.get(i).applies(Miscellaneous.getAnyContext()) && allRulesWithActivityDetection.get(i).hasNotAppliedSinceLastExecution()) || allRulesWithActivityDetection.get(i).isActuallyToggable())
if(allRulesWithActivityDetection.get(i).getsGreenLight(Miscellaneous.getAnyContext()))
allRulesWithActivityDetection.get(i).activate(AutomationService.getInstance(), false);
}
}

View File

@ -264,7 +264,7 @@ public class PointOfInterest implements Comparable<PointOfInterest>
for(int i=0; i<ruleCandidates.size(); i++)
{
if((ruleCandidates.get(i).applies(parentService) && ruleCandidates.get(i).haveEnoughPermissions() && ruleCandidates.get(i).hasNotAppliedSinceLastExecution()) || ruleCandidates.get(i).isActuallyToggable())
if(ruleCandidates.get(i).haveEnoughPermissions() && ruleCandidates.get(i).getsGreenLight(parentService))
{
Miscellaneous.logEvent("i", "POI", "Rule " + ruleCandidates.get(i).getName() + " applies for entering POI " + this.getName() + ".", 2);
ruleCandidates.get(i).activate(parentService, false);
@ -296,7 +296,7 @@ public class PointOfInterest implements Comparable<PointOfInterest>
Miscellaneous.logEvent("i", "POI", "POI " + this.getName() + " found in " + ruleCandidates.size() + " rule(s).", 2);
for(int i=0; i<ruleCandidates.size(); i++)
{
if((ruleCandidates.get(i).applies(parentService) && ruleCandidates.get(i).haveEnoughPermissions() && ruleCandidates.get(i).hasNotAppliedSinceLastExecution()) || ruleCandidates.get(i).isActuallyToggable())
if(ruleCandidates.get(i).haveEnoughPermissions() && ruleCandidates.get(i).getsGreenLight(parentService))
{
Miscellaneous.logEvent("i", "POI", "Rule " + ruleCandidates.get(i).getName() + " applies for leaving POI " + this.getName() + ".", 2);
ruleCandidates.get(i).activate(parentService, false);

View File

@ -42,7 +42,7 @@ public class Trigger
public boolean applies(Object triggeringObject, Context context)
{
boolean result = true;
boolean result = true;
try
{
@ -684,16 +684,20 @@ public class Trigger
public boolean hasStateNotAppliedSinceLastRuleExecution()
{
if(getParentRule().getLastExecution() == null)
{
Miscellaneous.logEvent("i", "Trigger", "Trigger " + this.toString() + " of rule " + getParentRule().getName() + " has NOT applied since the parent rule\'s last activation.", 4);
return true;
}
else if(lastTimeNotApplied != null)
{
if(lastTimeNotApplied.getTimeInMillis() > getParentRule().getLastExecution().getTimeInMillis())
{
Miscellaneous.logEvent("i", "Trigger", "Trigger " + this.toString() + " of rule " + getParentRule().getName() + " has NOT applied since the parent rule\'s last activation.", 4);
return true;
}
}
Miscellaneous.logEvent("i", "Trigger", "Trigger " + this.toString() + " of rule " + getParentRule().getName() + " may apply currently, but has not NOT applied since the rule\'s last execution.", 5);
Miscellaneous.logEvent("i", "Trigger", "Trigger " + this.toString() + " of rule " + getParentRule().getName() + " may apply currently, but has not NOT applied since the rule\'s last execution.", 4);
return false;
}

View File

@ -201,7 +201,7 @@ public class LocationProvider
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidatesBySpeed();
for (Rule oneRule : ruleCandidates)
{
if ((oneRule.applies(this.getParentService()) && oneRule.hasNotAppliedSinceLastExecution()) || oneRule.isActuallyToggable())
if(oneRule.getsGreenLight(this.getParentService()))
oneRule.activate(getParentService(), false);
}
}

View File

@ -147,7 +147,7 @@ public class WifiBroadcastReceiver extends BroadcastReceiver
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidatesByWifiConnection();
for(Rule oneRule : ruleCandidates)
{
if((oneRule.applies(automationServiceInstance) && oneRule.hasNotAppliedSinceLastExecution()) || oneRule.isActuallyToggable())
if(oneRule.getsGreenLight(automationServiceInstance))
oneRule.activate(automationServiceInstance, false);
}
}

View File

@ -206,7 +206,7 @@ public class BatteryReceiver extends BroadcastReceiver implements AutomationList
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidatesByCharging(true);
for(int i=0; i<ruleCandidates.size(); i++)
{
if((ruleCandidates.get(i).applies(context) && ruleCandidates.get(i).hasNotAppliedSinceLastExecution()) || ruleCandidates.get(i).isActuallyToggable())
if(ruleCandidates.get(i).getsGreenLight(context))
ruleCandidates.get(i).activate(automationServiceRef, false);
}
}
@ -219,7 +219,7 @@ public class BatteryReceiver extends BroadcastReceiver implements AutomationList
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidatesByBatteryLevel();
for(int i=0; i<ruleCandidates.size(); i++)
{
if((ruleCandidates.get(i).applies(context) && ruleCandidates.get(i).hasNotAppliedSinceLastExecution()) || ruleCandidates.get(i).isActuallyToggable())
if(ruleCandidates.get(i).getsGreenLight(context))
ruleCandidates.get(i).activate(automationServiceRef, false);
}
}
@ -234,7 +234,7 @@ public class BatteryReceiver extends BroadcastReceiver implements AutomationList
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidatesByCharging(false);
for(int i=0; i<ruleCandidates.size(); i++)
{
if((ruleCandidates.get(i).applies(context) && ruleCandidates.get(i).hasNotAppliedSinceLastExecution()) || ruleCandidates.get(i).isActuallyToggable())
if(ruleCandidates.get(i).getsGreenLight(context))
ruleCandidates.get(i).activate(automationServiceRef, false);
}
@ -257,7 +257,7 @@ public class BatteryReceiver extends BroadcastReceiver implements AutomationList
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidatesByUsbHost(true);
for(Rule oneRule : ruleCandidates)
{
if((oneRule.applies(context) && oneRule.hasNotAppliedSinceLastExecution()) || oneRule.isActuallyToggable())
if(oneRule.getsGreenLight(context))
oneRule.activate(automationServiceRef, false);
}
@ -278,7 +278,7 @@ public class BatteryReceiver extends BroadcastReceiver implements AutomationList
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidatesByUsbHost(false);
for(Rule oneRule : ruleCandidates)
{
if((oneRule.applies(context) && oneRule.hasNotAppliedSinceLastExecution()) || oneRule.isActuallyToggable())
if(oneRule.getsGreenLight(context))
oneRule.activate(automationServiceRef, false);
}
}

View File

@ -127,7 +127,7 @@ public class BluetoothReceiver extends BroadcastReceiver implements AutomationLi
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidatesByBluetoothConnection();
for(int i=0; i<ruleCandidates.size(); i++)
{
if((ruleCandidates.get(i).applies(AutomationService.getInstance()) && ruleCandidates.get(i).hasNotAppliedSinceLastExecution()) || ruleCandidates.get(i).isActuallyToggable())
if(ruleCandidates.get(i).getsGreenLight(AutomationService.getInstance()))
ruleCandidates.get(i).activate(AutomationService.getInstance(), false);
}
}

View File

@ -141,7 +141,7 @@ public class ConnectivityReceiver extends BroadcastReceiver implements Automatio
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidatesByAirplaneMode(isAirplaneMode);
for(int i=0; i<ruleCandidates.size(); i++)
{
if((ruleCandidates.get(i).applies(automationServiceRef) && ruleCandidates.get(i).hasNotAppliedSinceLastExecution()) || ruleCandidates.get(i).isActuallyToggable())
if(ruleCandidates.get(i).getsGreenLight(automationServiceRef))
ruleCandidates.get(i).activate(automationServiceRef, false);
}
}
@ -174,7 +174,7 @@ public class ConnectivityReceiver extends BroadcastReceiver implements Automatio
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidatesByRoaming(isRoaming);
for(int i=0; i<ruleCandidates.size(); i++)
{
if((ruleCandidates.get(i).applies(automationServiceRef) && ruleCandidates.get(i).hasNotAppliedSinceLastExecution()) || ruleCandidates.get(i).isActuallyToggable())
if(ruleCandidates.get(i).getsGreenLight(automationServiceRef))
ruleCandidates.get(i).activate(automationServiceRef, false);
}
}

View File

@ -59,7 +59,7 @@ public class DateTimeListener extends BroadcastReceiver implements AutomationLis
ArrayList<Rule> allRulesWithNowInTimeFrame = Rule.findRuleCandidatesByTime(passTime);
for(int i=0; i<allRulesWithNowInTimeFrame.size(); i++)
{
if((allRulesWithNowInTimeFrame.get(i).applies(context) && allRulesWithNowInTimeFrame.get(i).hasNotAppliedSinceLastExecution()) || allRulesWithNowInTimeFrame.get(i).isActuallyToggable())
if(allRulesWithNowInTimeFrame.get(i).getsGreenLight(context))
allRulesWithNowInTimeFrame.get(i).activate(automationServiceRef, false);
}

View File

@ -149,11 +149,7 @@ public class DevicePositionListener implements SensorEventListener, AutomationLi
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.devicePosition);
for (int i = 0; i < ruleCandidates.size(); i++)
{
applies = ruleCandidates.get(i).applies(Miscellaneous.getAnyContext());
flipped = ruleCandidates.get(i).hasNotAppliedSinceLastExecution();
toggable = ruleCandidates.get(i).isActuallyToggable();
if((applies && flipped) || toggable)
if(ruleCandidates.get(i).getsGreenLight(Miscellaneous.getAnyContext()))
ruleCandidates.get(i).activate(AutomationService.getInstance(), false);
}
}

View File

@ -77,7 +77,7 @@ public class HeadphoneJackListener extends BroadcastReceiver implements Automati
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidatesByHeadphoneJack(isHeadsetConnected());
for(int i=0; i<ruleCandidates.size(); i++)
{
if((ruleCandidates.get(i).applies(context) && ruleCandidates.get(i).hasNotAppliedSinceLastExecution()) || ruleCandidates.get(i).isActuallyToggable())
if(ruleCandidates.get(i).getsGreenLight(context))
ruleCandidates.get(i).activate(AutomationService.getInstance(), false);
}
}

View File

@ -174,7 +174,7 @@ public class NfcReceiver
ArrayList<Rule> allRulesWithNfcTags = Rule.findRuleCandidatesByNfc();
for(int i=0; i<allRulesWithNfcTags.size(); i++)
{
if((allRulesWithNfcTags.get(i).applies(asInstance) && allRulesWithNfcTags.get(i).hasNotAppliedSinceLastExecution()) || allRulesWithNfcTags.get(i).isActuallyToggable())
if(allRulesWithNfcTags.get(i).getsGreenLight(asInstance))
allRulesWithNfcTags.get(i).activate(asInstance, false);
}
}

View File

@ -33,7 +33,7 @@ public class NoiseListener implements AutomationListenerInterface
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidatesByNoiseLevel();
for(Rule oneRule : ruleCandidates)
{
if((oneRule.applies(automationService) && oneRule.hasNotAppliedSinceLastExecution()) || oneRule.isActuallyToggable())
if(oneRule.getsGreenLight(automationService))
oneRule.activate(automationService, false);
}
}

View File

@ -92,7 +92,7 @@ public class NotificationListener extends NotificationListenerService
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.notification);
for (int i = 0; i < ruleCandidates.size(); i++)
{
if ((ruleCandidates.get(i).applies(NotificationListener.this) && ruleCandidates.get(i).hasNotAppliedSinceLastExecution()) || ruleCandidates.get(i).isActuallyToggable())
if(ruleCandidates.get(i).getsGreenLight(NotificationListener.this))
ruleCandidates.get(i).activate(AutomationService.getInstance(), false);
}
}

View File

@ -114,7 +114,7 @@ public class PhoneStatusListener implements AutomationListenerInterface
{
AutomationService asInstance = AutomationService.getInstance();
if(asInstance != null)
if((ruleCandidates.get(i).applies(asInstance) && ruleCandidates.get(i).hasNotAppliedSinceLastExecution()) || ruleCandidates.get(i).isActuallyToggable())
if(ruleCandidates.get(i).getsGreenLight(asInstance))
ruleCandidates.get(i).activate(asInstance, false);
}
}
@ -146,7 +146,7 @@ public class PhoneStatusListener implements AutomationListenerInterface
{
AutomationService asInstance = AutomationService.getInstance();
if (asInstance != null)
if ((ruleCandidates.get(i).applies(asInstance) && ruleCandidates.get(i).hasNotAppliedSinceLastExecution()) || ruleCandidates.get(i).isActuallyToggable())
if (ruleCandidates.get(i).getsGreenLight(asInstance))
ruleCandidates.get(i).activate(asInstance, false);
}
}
@ -183,7 +183,7 @@ public class PhoneStatusListener implements AutomationListenerInterface
{
AutomationService asInstance = AutomationService.getInstance();
if(asInstance != null)
if((ruleCandidates.get(i).applies(asInstance) && ruleCandidates.get(i).hasNotAppliedSinceLastExecution()) || ruleCandidates.get(i).isActuallyToggable())
if(ruleCandidates.get(i).getsGreenLight(asInstance))
ruleCandidates.get(i).activate(asInstance, false);
}
}

View File

@ -60,7 +60,7 @@ public class ProcessListener implements AutomationListenerInterface
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidatesByProcess();
for(int i=0; i<ruleCandidates.size(); i++)
{
if((ruleCandidates.get(i).applies(automationService) && ruleCandidates.get(i).hasNotAppliedSinceLastExecution()) || ruleCandidates.get(i).isActuallyToggable())
if(ruleCandidates.get(i).getsGreenLight(automationService))
ruleCandidates.get(i).activate(automationService, false);
}
}