This commit is contained in:
jens 2021-12-04 13:24:43 +01:00
parent cdf1a8baa8
commit c34ec83425
4 changed files with 136 additions and 57 deletions

View File

@ -1,17 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<runningDeviceTargetSelectedWithDropDown>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>
<Key>
<type value="VIRTUAL_DEVICE_PATH" />
<value value="C:\Users\jens\.android\avd\Android_11.avd" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<targetSelectedWithDropDown>
<Target>
<type value="QUICK_BOOT_TARGET" />

View File

@ -358,7 +358,24 @@ public class Rule implements Comparable<Rule>
{
for(Trigger oneTrigger : this.getTriggerSet())
{
if(oneTrigger.getTriggerType().equals(Trigger.Trigger_Enum.activityDetection))
if (!oneTrigger.applies(null, context))
return false;
}
return true;
}
Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), String.format(context.getResources().getString(R.string.ruleIsDeactivatedCantApply), this.getName()), 3);
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)
{
@ -371,7 +388,7 @@ public class Rule implements Comparable<Rule>
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);
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
@ -380,27 +397,16 @@ public class Rule implements Comparable<Rule>
{
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);
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;
}
}
}
}
}
else
{
if (!oneTrigger.applies(null, context))
return false;
}
}
return true;
}
Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), String.format(context.getResources().getString(R.string.ruleIsDeactivatedCantApply), this.getName()), 3);
return false;
}
private class ActivateRuleTask extends AsyncTask<Object, String, Void>
{
boolean wasActivated = false;
@ -419,6 +425,7 @@ public class Rule implements Comparable<Rule>
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<Rule>
*/
if(wasActivated)
{
setLastExecution(Calendar.getInstance());
// setLastExecution(Calendar.getInstance());
AutomationService.updateNotification();
ActivityMainScreen.updateMainScreen();
super.onPostExecute(result);
@ -462,8 +469,8 @@ public class Rule implements Comparable<Rule>
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>
{
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<Rule>
}
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;
}

View File

@ -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())

View File

@ -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<Rule> 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);
}
}