From c34ec83425afac6ff1745c5e8566dec2c05fa7ad Mon Sep 17 00:00:00 2001 From: jens Date: Sat, 4 Dec 2021 13:24:43 +0100 Subject: [PATCH] Rework --- .idea/deploymentTargetDropDown.xml | 11 --- .../java/com/jens/automation2/Rule.java | 96 ++++++++++--------- .../java/com/jens/automation2/Trigger.java | 61 +++++++++++- .../receivers/DevicePositionListener.java | 25 ++++- 4 files changed, 136 insertions(+), 57 deletions(-) diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml index cc7935bd..9f3e9f1b 100644 --- a/.idea/deploymentTargetDropDown.xml +++ b/.idea/deploymentTargetDropDown.xml @@ -1,17 +1,6 @@ - - - - - - - - - - - diff --git a/app/src/apkFlavor/java/com/jens/automation2/Rule.java b/app/src/apkFlavor/java/com/jens/automation2/Rule.java index 1120377d..731df84c 100644 --- a/app/src/apkFlavor/java/com/jens/automation2/Rule.java +++ b/app/src/apkFlavor/java/com/jens/automation2/Rule.java @@ -358,40 +358,8 @@ public class Rule implements Comparable { for(Trigger oneTrigger : this.getTriggerSet()) { - if(oneTrigger.getTriggerType().equals(Trigger.Trigger_Enum.activityDetection)) - { - if (ActivityDetectionReceiver.getActivityDetectionLastResult() != null) - { - boolean found = false; - for (DetectedActivity oneDetectedActivity : ActivityDetectionReceiver.getActivityDetectionLastResult().getProbableActivities()) - { - if (oneDetectedActivity.getType() == oneTrigger.getActivityDetectionType()) - found = true; - } - - if (!found) - { - Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), String.format(context.getResources().getString(R.string.ruleDoesntApplyActivityNotPresent), ActivityDetectionReceiver.getDescription(oneTrigger.getActivityDetectionType())), 3); - return false; - } - else - { - for (DetectedActivity oneDetectedActivity : ActivityDetectionReceiver.getActivityDetectionLastResult().getProbableActivities()) - { - if (oneDetectedActivity.getType() == oneTrigger.getActivityDetectionType() && oneDetectedActivity.getConfidence() < Settings.activityDetectionRequiredProbability) - { - Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), String.format(context.getResources().getString(R.string.ruleDoesntApplyActivityGivenButTooLowProbability), ActivityDetectionReceiver.getDescription(oneDetectedActivity.getType()), String.valueOf(oneDetectedActivity.getConfidence()), String.valueOf(Settings.activityDetectionRequiredProbability)), 3); - return false; - } - } - } - } - } - else - { - if (!oneTrigger.applies(null, context)) - return false; - } + if (!oneTrigger.applies(null, context)) + return false; } return true; @@ -401,6 +369,44 @@ public class Rule implements Comparable return false; } + /** + * This is actually a function of the class Trigger, but Rule is already distinguished by flavors, Trigger is not. + * Hence it is here. + * @param oneTrigger + * @return + */ + boolean checkActivityDetection(Trigger oneTrigger) + { + if (ActivityDetectionReceiver.getActivityDetectionLastResult() != null) + { + boolean found = false; + for (DetectedActivity oneDetectedActivity : ActivityDetectionReceiver.getActivityDetectionLastResult().getProbableActivities()) + { + if (oneDetectedActivity.getType() == oneTrigger.getActivityDetectionType()) + found = true; + } + + if (!found) + { + Miscellaneous.logEvent("i", String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.ruleCheckOf), this.getName()), String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.ruleDoesntApplyActivityNotPresent), ActivityDetectionReceiver.getDescription(oneTrigger.getActivityDetectionType())), 3); + return false; + } + else + { + for (DetectedActivity oneDetectedActivity : ActivityDetectionReceiver.getActivityDetectionLastResult().getProbableActivities()) + { + if (oneDetectedActivity.getType() == oneTrigger.getActivityDetectionType() && oneDetectedActivity.getConfidence() < Settings.activityDetectionRequiredProbability) + { + Miscellaneous.logEvent("i", String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.ruleCheckOf), this.getName()), String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.ruleDoesntApplyActivityGivenButTooLowProbability), ActivityDetectionReceiver.getDescription(oneDetectedActivity.getType()), String.valueOf(oneDetectedActivity.getConfidence()), String.valueOf(Settings.activityDetectionRequiredProbability)), 3); + return false; + } + } + } + } + + return true; + } + private class ActivateRuleTask extends AsyncTask { boolean wasActivated = false; @@ -418,7 +424,8 @@ public class Rule implements Comparable if (Looper.myLooper() == null) Looper.prepare(); - + + setLastExecution(Calendar.getInstance()); wasActivated = activateInternally((AutomationService)params[0], (Boolean)params[1]); return null; @@ -443,7 +450,7 @@ public class Rule implements Comparable */ if(wasActivated) { - setLastExecution(Calendar.getInstance()); +// setLastExecution(Calendar.getInstance()); AutomationService.updateNotification(); ActivityMainScreen.updateMainScreen(); super.onPostExecute(result); @@ -462,8 +469,8 @@ public class Rule implements Comparable boolean doToggle = ruleToggle && isActuallyToggable; //if(notLastActive || force || doToggle) - if(force || doToggle) - { +// if(force || doToggle) +// { String message; if(!doToggle) message = String.format(automationService.getResources().getString(R.string.ruleActivate), Rule.this.getName()); @@ -492,6 +499,7 @@ public class Rule implements Comparable { Rule.ruleRunHistory.add(0, Rule.this); // add at beginning for better visualization Rule.lastActivatedRuleActivationTime = new Date(); + while(ruleRunHistory.size() > Settings.rulesThatHaveBeenRanHistorySize) ruleRunHistory.remove(ruleRunHistory.size()-1); String history = ""; @@ -507,12 +515,12 @@ public class Rule implements Comparable } Miscellaneous.logEvent("i", "Rule", String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.ruleActivationComplete), Rule.this.getName()), 2); - } - else - { - Miscellaneous.logEvent("i", "Rule", "Request to activate rule " + Rule.this.getName() + ", but it is the last one that was activated. Won't do it again.", 3); - return false; - } +// } +// else +// { +// Miscellaneous.logEvent("i", "Rule", "Request to activate rule " + Rule.this.getName() + ", but it is the last one that was activated. Won't do it again.", 3); +// return false; +// } return true; } diff --git a/app/src/main/java/com/jens/automation2/Trigger.java b/app/src/main/java/com/jens/automation2/Trigger.java index 46259d82..d38a0ca5 100644 --- a/app/src/main/java/com/jens/automation2/Trigger.java +++ b/app/src/main/java/com/jens/automation2/Trigger.java @@ -1,7 +1,10 @@ package com.jens.automation2; +import static android.content.Context.DEVICE_POLICY_SERVICE; + import android.bluetooth.BluetoothDevice; import android.content.Context; +import android.graphics.Color; import android.os.Build; import android.os.Bundle; import android.service.notification.StatusBarNotification; @@ -15,6 +18,7 @@ import com.jens.automation2.location.WifiBroadcastReceiver; import com.jens.automation2.receivers.BatteryReceiver; import com.jens.automation2.receivers.BluetoothReceiver; import com.jens.automation2.receivers.ConnectivityReceiver; +import com.jens.automation2.receivers.DevicePositionListener; import com.jens.automation2.receivers.HeadphoneJackListener; import com.jens.automation2.receivers.NfcReceiver; import com.jens.automation2.receivers.NoiseListener; @@ -109,7 +113,13 @@ public class Trigger result = false; break; case devicePosition: - and others + if(!checkDevicePosition()) + result = false; + break; + case activityDetection: + if(!getParentRule().checkActivityDetection(this)) + result = false; + break; default: break; } @@ -266,6 +276,55 @@ public class Trigger return true; } + boolean checkDevicePosition() + { + String devicePositionPieces[] = getTriggerParameter2().split(Trigger.triggerParameter2Split); + float desiredAzimuth = Float.parseFloat(devicePositionPieces[0]); + float desiredAzimuthTolerance = Float.parseFloat(devicePositionPieces[1]); + float desiredPitch = Float.parseFloat(devicePositionPieces[2]); + float desiredPitchTolerance = Float.parseFloat(devicePositionPieces[3]); + float desiredRoll = Float.parseFloat(devicePositionPieces[4]); + float desiredRollTolerance = Float.parseFloat(devicePositionPieces[5]); + float currentAzimuth = DevicePositionListener.getInstance().getAzimuth(); + float currentPitch = DevicePositionListener.getInstance().getPitch(); + float currentRoll = DevicePositionListener.getInstance().getRoll(); + + if( + !( + currentAzimuth <= desiredAzimuth + desiredAzimuthTolerance + && + currentAzimuth >= desiredAzimuth - desiredAzimuthTolerance + ) + ) + { + return false; + } + + if( + !( + currentPitch <= desiredPitch + desiredPitchTolerance + && + currentPitch >= desiredPitch - desiredPitchTolerance + ) + ) + { + return false; + } + + if( + !( + currentRoll <= desiredRoll + desiredRollTolerance + && + currentRoll >= desiredRoll - desiredRollTolerance + ) + ) + { + return false; + } + + return true; + } + boolean checkHeadsetPlugged() { if(HeadphoneJackListener.isHeadsetConnected() != this.getTriggerParameter()) diff --git a/app/src/main/java/com/jens/automation2/receivers/DevicePositionListener.java b/app/src/main/java/com/jens/automation2/receivers/DevicePositionListener.java index b233dd15..ccef6ac8 100644 --- a/app/src/main/java/com/jens/automation2/receivers/DevicePositionListener.java +++ b/app/src/main/java/com/jens/automation2/receivers/DevicePositionListener.java @@ -49,6 +49,21 @@ public class DevicePositionListener implements SensorEventListener, AutomationLi return instance; } + public float getAzimuth() + { + return azimuth; + } + + public float getPitch() + { + return pitch; + } + + public float getRoll() + { + return roll; + } + public void startSensorFromConfigActivity(Context context, ActivityManageTriggerDevicePosition activityManageTriggerDevicePositionInstance) { this.activityManageTriggerDevicePositionInstance = activityManageTriggerDevicePositionInstance; @@ -129,7 +144,15 @@ public class DevicePositionListener implements SensorEventListener, AutomationLi ArrayList ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.devicePosition); for (int i = 0; i < ruleCandidates.size(); i++) { - if (ruleCandidates.get(i).applies(Miscellaneous.getAnyContext()) && ruleCandidates.get(i).hasNotAppliedSinceLastExecution()) + boolean applies = ruleCandidates.get(i).applies(Miscellaneous.getAnyContext()); + boolean flipped = ruleCandidates.get(i).hasNotAppliedSinceLastExecution(); + +// if ( +// ruleCandidates.get(i).applies(Miscellaneous.getAnyContext()) +// && +// ruleCandidates.get(i).hasNotAppliedSinceLastExecution() +// ) + if(applies && flipped) ruleCandidates.get(i).activate(AutomationService.getInstance(), false); } }