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"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="deploymentTargetDropDown"> <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> <targetSelectedWithDropDown>
<Target> <Target>
<type value="QUICK_BOOT_TARGET" /> <type value="QUICK_BOOT_TARGET" />

View File

@ -358,40 +358,8 @@ public class Rule implements Comparable<Rule>
{ {
for(Trigger oneTrigger : this.getTriggerSet()) for(Trigger oneTrigger : this.getTriggerSet())
{ {
if(oneTrigger.getTriggerType().equals(Trigger.Trigger_Enum.activityDetection)) if (!oneTrigger.applies(null, context))
{ return false;
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;
}
} }
return true; return true;
@ -401,6 +369,44 @@ public class Rule implements Comparable<Rule>
return false; 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<Object, String, Void> private class ActivateRuleTask extends AsyncTask<Object, String, Void>
{ {
boolean wasActivated = false; boolean wasActivated = false;
@ -418,7 +424,8 @@ public class Rule implements Comparable<Rule>
if (Looper.myLooper() == null) if (Looper.myLooper() == null)
Looper.prepare(); Looper.prepare();
setLastExecution(Calendar.getInstance());
wasActivated = activateInternally((AutomationService)params[0], (Boolean)params[1]); wasActivated = activateInternally((AutomationService)params[0], (Boolean)params[1]);
return null; return null;
@ -443,7 +450,7 @@ public class Rule implements Comparable<Rule>
*/ */
if(wasActivated) if(wasActivated)
{ {
setLastExecution(Calendar.getInstance()); // setLastExecution(Calendar.getInstance());
AutomationService.updateNotification(); AutomationService.updateNotification();
ActivityMainScreen.updateMainScreen(); ActivityMainScreen.updateMainScreen();
super.onPostExecute(result); super.onPostExecute(result);
@ -462,8 +469,8 @@ public class Rule implements Comparable<Rule>
boolean doToggle = ruleToggle && isActuallyToggable; boolean doToggle = ruleToggle && isActuallyToggable;
//if(notLastActive || force || doToggle) //if(notLastActive || force || doToggle)
if(force || doToggle) // if(force || doToggle)
{ // {
String message; String message;
if(!doToggle) if(!doToggle)
message = String.format(automationService.getResources().getString(R.string.ruleActivate), Rule.this.getName()); 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.ruleRunHistory.add(0, Rule.this); // add at beginning for better visualization
Rule.lastActivatedRuleActivationTime = new Date(); Rule.lastActivatedRuleActivationTime = new Date();
while(ruleRunHistory.size() > Settings.rulesThatHaveBeenRanHistorySize) while(ruleRunHistory.size() > Settings.rulesThatHaveBeenRanHistorySize)
ruleRunHistory.remove(ruleRunHistory.size()-1); ruleRunHistory.remove(ruleRunHistory.size()-1);
String history = ""; 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); Miscellaneous.logEvent("i", "Rule", String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.ruleActivationComplete), Rule.this.getName()), 2);
} // }
else // 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); // 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 false;
} // }
return true; return true;
} }

View File

@ -1,7 +1,10 @@
package com.jens.automation2; package com.jens.automation2;
import static android.content.Context.DEVICE_POLICY_SERVICE;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.content.Context; import android.content.Context;
import android.graphics.Color;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.service.notification.StatusBarNotification; 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.BatteryReceiver;
import com.jens.automation2.receivers.BluetoothReceiver; import com.jens.automation2.receivers.BluetoothReceiver;
import com.jens.automation2.receivers.ConnectivityReceiver; import com.jens.automation2.receivers.ConnectivityReceiver;
import com.jens.automation2.receivers.DevicePositionListener;
import com.jens.automation2.receivers.HeadphoneJackListener; import com.jens.automation2.receivers.HeadphoneJackListener;
import com.jens.automation2.receivers.NfcReceiver; import com.jens.automation2.receivers.NfcReceiver;
import com.jens.automation2.receivers.NoiseListener; import com.jens.automation2.receivers.NoiseListener;
@ -109,7 +113,13 @@ public class Trigger
result = false; result = false;
break; break;
case devicePosition: case devicePosition:
and others if(!checkDevicePosition())
result = false;
break;
case activityDetection:
if(!getParentRule().checkActivityDetection(this))
result = false;
break;
default: default:
break; break;
} }
@ -266,6 +276,55 @@ public class Trigger
return true; 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() boolean checkHeadsetPlugged()
{ {
if(HeadphoneJackListener.isHeadsetConnected() != this.getTriggerParameter()) if(HeadphoneJackListener.isHeadsetConnected() != this.getTriggerParameter())

View File

@ -49,6 +49,21 @@ public class DevicePositionListener implements SensorEventListener, AutomationLi
return instance; return instance;
} }
public float getAzimuth()
{
return azimuth;
}
public float getPitch()
{
return pitch;
}
public float getRoll()
{
return roll;
}
public void startSensorFromConfigActivity(Context context, ActivityManageTriggerDevicePosition activityManageTriggerDevicePositionInstance) public void startSensorFromConfigActivity(Context context, ActivityManageTriggerDevicePosition activityManageTriggerDevicePositionInstance)
{ {
this.activityManageTriggerDevicePositionInstance = activityManageTriggerDevicePositionInstance; this.activityManageTriggerDevicePositionInstance = activityManageTriggerDevicePositionInstance;
@ -129,7 +144,15 @@ 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++)
{ {
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); ruleCandidates.get(i).activate(AutomationService.getInstance(), false);
} }
} }