From 8c0cee958974b52720ed124297457294714b00bb Mon Sep 17 00:00:00 2001 From: Jens Date: Sun, 15 Oct 2023 23:54:27 +0200 Subject: [PATCH] Wifi trigger fix attempt --- .../jens/automation2/ActivityPermissions.java | 1 - .../java/com/jens/automation2/Trigger.java | 21 +++++++++++++++---- .../location/LocationProvider.java | 4 ++-- .../location/WifiBroadcastReceiver.java | 12 +++++++++++ 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/jens/automation2/ActivityPermissions.java b/app/src/main/java/com/jens/automation2/ActivityPermissions.java index 96b7d2b..4d2a5eb 100644 --- a/app/src/main/java/com/jens/automation2/ActivityPermissions.java +++ b/app/src/main/java/com/jens/automation2/ActivityPermissions.java @@ -705,7 +705,6 @@ public class ActivityPermissions extends Activity break; case startPhoneCall: addToArrayListUnique(Manifest.permission.CALL_PHONE, requiredPermissions); -// addToArrayListUnique(Manifest.permission.SYSTEM_ALERT_WINDOW, requiredPermissions); addToArrayListUnique(Manifest.permission.SYSTEM_ALERT_WINDOW, requiredPermissions); break; case stopPhoneCall: diff --git a/app/src/main/java/com/jens/automation2/Trigger.java b/app/src/main/java/com/jens/automation2/Trigger.java index 9f38428..829de8a 100644 --- a/app/src/main/java/com/jens/automation2/Trigger.java +++ b/app/src/main/java/com/jens/automation2/Trigger.java @@ -803,13 +803,26 @@ public class Trigger { Miscellaneous.logEvent("i", Miscellaneous.getAnyContext().getResources().getString(R.string.ruleCheckOf), String.format("Wifi name specified, checking that.", this.getParentRule().getName()), 4); - if(!WifiBroadcastReceiver.getLastWifiSsid().equals(this.getTriggerParameter2()) && !(Miscellaneous.isRegularExpression(this.getTriggerParameter2()) && WifiBroadcastReceiver.getLastWifiSsid().matches(this.getTriggerParameter2()))) + if(WifiBroadcastReceiver.lastConnectedState) //when connected { - Miscellaneous.logEvent("i", Miscellaneous.getAnyContext().getResources().getString(R.string.ruleCheckOf), String.format(String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.ruleDoesntApplyNotTheCorrectSsid), getParentRule().getName(), this.getTriggerParameter2(), WifiBroadcastReceiver.getLastWifiSsid()),this.getParentRule().getName()), 3); - return false; + if (!WifiBroadcastReceiver.getLastWifiSsid().equals(this.getTriggerParameter2()) && !(Miscellaneous.isRegularExpression(this.getTriggerParameter2()) && WifiBroadcastReceiver.getLastWifiSsid().matches(this.getTriggerParameter2()))) + { + Miscellaneous.logEvent("i", Miscellaneous.getAnyContext().getResources().getString(R.string.ruleCheckOf), String.format(String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.ruleDoesntApplyNotTheCorrectSsid), getParentRule().getName(), this.getTriggerParameter2(), WifiBroadcastReceiver.getLastWifiSsid()), this.getParentRule().getName()), 3); + return false; + } + else + Miscellaneous.logEvent("i", Miscellaneous.getAnyContext().getResources().getString(R.string.ruleCheckOf), String.format("Wifi name matches. Rule will apply.", this.getParentRule().getName()), 4); } else - Miscellaneous.logEvent("i", Miscellaneous.getAnyContext().getResources().getString(R.string.ruleCheckOf), String.format("Wifi name matches. Rule will apply.", this.getParentRule().getName()), 4); + { + if (!WifiBroadcastReceiver.getLastWifiSsidReal().equals(this.getTriggerParameter2()) && !(Miscellaneous.isRegularExpression(this.getTriggerParameter2()) && WifiBroadcastReceiver.getLastWifiSsidReal().matches(this.getTriggerParameter2()))) + { + Miscellaneous.logEvent("i", Miscellaneous.getAnyContext().getResources().getString(R.string.ruleCheckOf), String.format(String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.ruleDoesntApplyNotTheCorrectSsid), getParentRule().getName(), this.getTriggerParameter2(), WifiBroadcastReceiver.getLastWifiSsidReal()), this.getParentRule().getName()), 3); + return false; + } + else + Miscellaneous.logEvent("i", Miscellaneous.getAnyContext().getResources().getString(R.string.ruleCheckOf), String.format("Wifi name matches. Rule will apply.", this.getParentRule().getName()), 4); + } } else Miscellaneous.logEvent("i", Miscellaneous.getAnyContext().getResources().getString(R.string.ruleCheckOf), String.format("No wifi name specified, any will do.", this.getParentRule().getName()), 4); diff --git a/app/src/main/java/com/jens/automation2/location/LocationProvider.java b/app/src/main/java/com/jens/automation2/location/LocationProvider.java index 59ceab5..8e98c6d 100644 --- a/app/src/main/java/com/jens/automation2/location/LocationProvider.java +++ b/app/src/main/java/com/jens/automation2/location/LocationProvider.java @@ -400,12 +400,12 @@ public class LocationProvider Miscellaneous.logEvent("i", "LocationProvider", this.getParentService().getResources().getString(R.string.applyingSettingsAndRules), 3); // *********** SETTING CHANGES *********** - if(Settings.useWifiForPositioning && !WifiBroadcastReceiver.isWifiListenerActive()) + if(Settings.useWifiForPositioning && !WifiBroadcastReceiver.isWifiListenerActive() || Rule.isAnyRuleUsing(Trigger_Enum.wifiConnection)) { Miscellaneous.logEvent("i", "LocationProvider", "Starting WifiReceiver because settings now allow to.", 4); WifiBroadcastReceiver.startWifiReceiver(this); } - else if(!Settings.useWifiForPositioning && WifiBroadcastReceiver.isWifiListenerActive()) + else if(!Settings.useWifiForPositioning && WifiBroadcastReceiver.isWifiListenerActive() && !Rule.isAnyRuleUsing(Trigger_Enum.wifiConnection)) { Miscellaneous.logEvent("i", "LocationProvider", "Shutting down WifiReceiver because settings forbid to.", 4); WifiBroadcastReceiver.stopWifiReceiver(); diff --git a/app/src/main/java/com/jens/automation2/location/WifiBroadcastReceiver.java b/app/src/main/java/com/jens/automation2/location/WifiBroadcastReceiver.java index 2ecfa13..55f5862 100644 --- a/app/src/main/java/com/jens/automation2/location/WifiBroadcastReceiver.java +++ b/app/src/main/java/com/jens/automation2/location/WifiBroadcastReceiver.java @@ -25,12 +25,20 @@ public class WifiBroadcastReceiver extends BroadcastReceiver public static LocationProvider parentLocationProvider; public static Boolean wasConnected = false; protected static String lastWifiSsid = ""; + protected static String lastWifiSsidReal = ""; public static boolean lastConnectedState = false; protected static boolean mayCellLocationChangedReceiverBeActivatedFromWifiPointOfView = true; protected static WifiBroadcastReceiver wifiBrInstance; protected static IntentFilter wifiListenerIntentFilter; protected static boolean wifiListenerActive=false; + final static String unknownSsidName = ""; + + public static String getLastWifiSsidReal() + { + return lastWifiSsidReal; + } + public static String getLastWifiSsid() { return lastWifiSsid; @@ -42,7 +50,11 @@ public class WifiBroadcastReceiver extends BroadcastReceiver newWifiSsid = newWifiSsid.substring(1, newWifiSsid.length()-1); if(newWifiSsid.length() > 0) + { + if(newWifiSsid.equals(unknownSsidName)) + WifiBroadcastReceiver.lastWifiSsidReal = lastWifiSsid; WifiBroadcastReceiver.lastWifiSsid = newWifiSsid; + } } public static boolean isWifiListenerActive()