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

@ -346,6 +346,11 @@ public class Rule implements Comparable<Rule>
return false; return false;
} }
public boolean getsGreenLight(Context context)
{
return isRuleActive() && applies(context) && (hasNotAppliedSinceLastExecution() || isActuallyToggable());
}
public boolean applies(Context context) public boolean applies(Context context)
{ {
if(AutomationService.getInstance() == null) if(AutomationService.getInstance() == null)

View File

@ -294,7 +294,7 @@ public class ActivityDetectionReceiver extends IntentService implements Automati
ArrayList<Rule> allRulesWithActivityDetection = Rule.findRuleCandidatesByActivityDetection(); ArrayList<Rule> allRulesWithActivityDetection = Rule.findRuleCandidatesByActivityDetection();
for(int i=0; i<allRulesWithActivityDetection.size(); i++) 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); 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++) 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); Miscellaneous.logEvent("i", "POI", "Rule " + ruleCandidates.get(i).getName() + " applies for entering POI " + this.getName() + ".", 2);
ruleCandidates.get(i).activate(parentService, false); 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); Miscellaneous.logEvent("i", "POI", "POI " + this.getName() + " found in " + ruleCandidates.size() + " rule(s).", 2);
for(int i=0; i<ruleCandidates.size(); i++) 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); Miscellaneous.logEvent("i", "POI", "Rule " + ruleCandidates.get(i).getName() + " applies for leaving POI " + this.getName() + ".", 2);
ruleCandidates.get(i).activate(parentService, false); ruleCandidates.get(i).activate(parentService, false);

View File

@ -684,16 +684,20 @@ public class Trigger
public boolean hasStateNotAppliedSinceLastRuleExecution() public boolean hasStateNotAppliedSinceLastRuleExecution()
{ {
if(getParentRule().getLastExecution() == null) 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; return true;
}
else if(lastTimeNotApplied != null) else if(lastTimeNotApplied != null)
{ {
if(lastTimeNotApplied.getTimeInMillis() > getParentRule().getLastExecution().getTimeInMillis()) 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; 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; return false;
} }

View File

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

View File

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

View File

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

View File

@ -127,7 +127,7 @@ public class BluetoothReceiver extends BroadcastReceiver implements AutomationLi
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidatesByBluetoothConnection(); ArrayList<Rule> ruleCandidates = Rule.findRuleCandidatesByBluetoothConnection();
for(int i=0; i<ruleCandidates.size(); i++) 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); 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); ArrayList<Rule> ruleCandidates = Rule.findRuleCandidatesByAirplaneMode(isAirplaneMode);
for(int i=0; i<ruleCandidates.size(); i++) 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); ruleCandidates.get(i).activate(automationServiceRef, false);
} }
} }
@ -174,7 +174,7 @@ public class ConnectivityReceiver extends BroadcastReceiver implements Automatio
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidatesByRoaming(isRoaming); ArrayList<Rule> ruleCandidates = Rule.findRuleCandidatesByRoaming(isRoaming);
for(int i=0; i<ruleCandidates.size(); i++) 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); 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); ArrayList<Rule> allRulesWithNowInTimeFrame = Rule.findRuleCandidatesByTime(passTime);
for(int i=0; i<allRulesWithNowInTimeFrame.size(); i++) 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); 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); ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.devicePosition);
for (int i = 0; i < ruleCandidates.size(); i++) for (int i = 0; i < ruleCandidates.size(); i++)
{ {
applies = ruleCandidates.get(i).applies(Miscellaneous.getAnyContext()); if(ruleCandidates.get(i).getsGreenLight(Miscellaneous.getAnyContext()))
flipped = ruleCandidates.get(i).hasNotAppliedSinceLastExecution();
toggable = ruleCandidates.get(i).isActuallyToggable();
if((applies && flipped) || toggable)
ruleCandidates.get(i).activate(AutomationService.getInstance(), false); 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()); ArrayList<Rule> ruleCandidates = Rule.findRuleCandidatesByHeadphoneJack(isHeadsetConnected());
for(int i=0; i<ruleCandidates.size(); i++) 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); ruleCandidates.get(i).activate(AutomationService.getInstance(), false);
} }
} }

View File

@ -174,7 +174,7 @@ public class NfcReceiver
ArrayList<Rule> allRulesWithNfcTags = Rule.findRuleCandidatesByNfc(); ArrayList<Rule> allRulesWithNfcTags = Rule.findRuleCandidatesByNfc();
for(int i=0; i<allRulesWithNfcTags.size(); i++) 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); allRulesWithNfcTags.get(i).activate(asInstance, false);
} }
} }

View File

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

View File

@ -92,7 +92,7 @@ public class NotificationListener extends NotificationListenerService
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.notification); ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.notification);
for (int i = 0; i < ruleCandidates.size(); i++) 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); ruleCandidates.get(i).activate(AutomationService.getInstance(), false);
} }
} }

View File

@ -114,7 +114,7 @@ public class PhoneStatusListener implements AutomationListenerInterface
{ {
AutomationService asInstance = AutomationService.getInstance(); AutomationService asInstance = AutomationService.getInstance();
if(asInstance != null) 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); ruleCandidates.get(i).activate(asInstance, false);
} }
} }
@ -146,7 +146,7 @@ public class PhoneStatusListener implements AutomationListenerInterface
{ {
AutomationService asInstance = AutomationService.getInstance(); AutomationService asInstance = AutomationService.getInstance();
if (asInstance != null) 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); ruleCandidates.get(i).activate(asInstance, false);
} }
} }
@ -183,7 +183,7 @@ public class PhoneStatusListener implements AutomationListenerInterface
{ {
AutomationService asInstance = AutomationService.getInstance(); AutomationService asInstance = AutomationService.getInstance();
if(asInstance != null) 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); ruleCandidates.get(i).activate(asInstance, false);
} }
} }

View File

@ -60,7 +60,7 @@ public class ProcessListener implements AutomationListenerInterface
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidatesByProcess(); ArrayList<Rule> ruleCandidates = Rule.findRuleCandidatesByProcess();
for(int i=0; i<ruleCandidates.size(); i++) 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); ruleCandidates.get(i).activate(automationService, false);
} }
} }