diff --git a/app/src/apkFlavor/java/com/jens/automation2/Rule.java b/app/src/apkFlavor/java/com/jens/automation2/Rule.java index 4252e079..79e251a4 100644 --- a/app/src/apkFlavor/java/com/jens/automation2/Rule.java +++ b/app/src/apkFlavor/java/com/jens/automation2/Rule.java @@ -761,22 +761,33 @@ public class Rule implements Comparable String title = sbn.getNotification().extras.getString(EXTRA_TITLE); String text = sbn.getNotification().extras.getString(EXTRA_TEXT); + Miscellaneous.logEvent("i", "NotificationCheck", "Checking if this notification matches our rule " + this.getName() + ". App: " + app + ", title: " + title + ", text: " + text, 5); + if (!myApp.equals("-1")) { if (!app.equalsIgnoreCase(myApp)) + { + Miscellaneous.logEvent("i", "NotificationCheck", "Notification app name does not match rule.", 5); continue; + } } if (myTitle.length() > 0) { - if (!Miscellaneous.compare(myTitleDir, title, myTitle)) + if (!Miscellaneous.compare(myTitleDir, myTitle, title)) + { + Miscellaneous.logEvent("i", "NotificationCheck", "Notification title does not match rule.", 5); continue; + } } if (myText.length() > 0) { - if (!Miscellaneous.compare(myTextDir, text, myText)) + if (!Miscellaneous.compare(myTextDir, myText, text)) + { + Miscellaneous.logEvent("i", "NotificationCheck", "Notification text does not match rule.", 5); continue; + } } foundMatch = true; @@ -832,6 +843,8 @@ public class Rule implements Comparable private class ActivateRuleTask extends AsyncTask { + boolean wasActivated = false; + @Override protected Void doInBackground(Object... params) { @@ -864,27 +877,34 @@ public class Rule implements Comparable @Override protected void onPostExecute(Void result) { - AutomationService.updateNotification(); - ActivityMainScreen.updateMainScreen(); - super.onPostExecute(result); - } - + /* + Only update if the rules was actually executed. Became necessary for the notification trigger. If a user created a rule + with a notification trigger and this app creates a notification itself this will otherwise end in an infinite loop. + */ + if(wasActivated) + { + AutomationService.updateNotification(); + ActivityMainScreen.updateMainScreen(); + super.onPostExecute(result); + } + } + /** * Will activate the rule. Should be called by a separate execution thread * @param automationService */ - protected void activateInternally(AutomationService automationService, boolean force) + protected boolean activateInternally(AutomationService automationService, boolean force) { boolean isActuallyToggable = isActuallyToggable(); - + boolean notLastActive = getLastActivatedRule() == null || !getLastActivatedRule().equals(Rule.this); boolean doToggle = ruleToggle && isActuallyToggable; - + if(notLastActive | force | doToggle) { String message; 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()); else message = String.format(automationService.getResources().getString(R.string.ruleActivateToggle), Rule.this.getName()); Miscellaneous.logEvent("i", "Rule", message, 2); @@ -892,10 +912,10 @@ public class Rule implements Comparable // Toast.makeText(automationService, message, Toast.LENGTH_LONG).show(); if(Settings.startNewThreadForRuleActivation) publishProgress(message); - + for(int i = 0; i< Rule.this.getActionSet().size(); i++) Rule.this.getActionSet().get(i).run(automationService, doToggle); - + // Keep log of last x rule activations (Settings) try { @@ -920,9 +940,12 @@ public class Rule implements Comparable 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; + } + } public void activate(AutomationService automationService, boolean force) { diff --git a/app/src/fdroidFlavor/java/com/jens/automation2/Rule.java b/app/src/fdroidFlavor/java/com/jens/automation2/Rule.java index af80b78a..ab3865a1 100644 --- a/app/src/fdroidFlavor/java/com/jens/automation2/Rule.java +++ b/app/src/fdroidFlavor/java/com/jens/automation2/Rule.java @@ -939,7 +939,7 @@ public class Rule implements Comparable return true; } - } + } public void activate(AutomationService automationService, boolean force) { diff --git a/app/src/googlePlayFlavor/java/com/jens/automation2/Rule.java b/app/src/googlePlayFlavor/java/com/jens/automation2/Rule.java index 37168334..c2f569f8 100644 --- a/app/src/googlePlayFlavor/java/com/jens/automation2/Rule.java +++ b/app/src/googlePlayFlavor/java/com/jens/automation2/Rule.java @@ -868,6 +868,8 @@ public class Rule implements Comparable private class ActivateRuleTask extends AsyncTask { + boolean wasActivated = false; + @Override protected Void doInBackground(Object... params) { @@ -900,27 +902,34 @@ public class Rule implements Comparable @Override protected void onPostExecute(Void result) { - AutomationService.updateNotification(); - ActivityMainScreen.updateMainScreen(); - super.onPostExecute(result); - } - + /* + Only update if the rules was actually executed. Became necessary for the notification trigger. If a user created a rule + with a notification trigger and this app creates a notification itself this will otherwise end in an infinite loop. + */ + if(wasActivated) + { + AutomationService.updateNotification(); + ActivityMainScreen.updateMainScreen(); + super.onPostExecute(result); + } + } + /** * Will activate the rule. Should be called by a separate execution thread * @param automationService */ - protected void activateInternally(AutomationService automationService, boolean force) + protected boolean activateInternally(AutomationService automationService, boolean force) { boolean isActuallyToggable = isActuallyToggable(); - + boolean notLastActive = getLastActivatedRule() == null || !getLastActivatedRule().equals(Rule.this); boolean doToggle = ruleToggle && isActuallyToggable; - + if(notLastActive | force | doToggle) { String message; 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()); else message = String.format(automationService.getResources().getString(R.string.ruleActivateToggle), Rule.this.getName()); Miscellaneous.logEvent("i", "Rule", message, 2); @@ -928,10 +937,10 @@ public class Rule implements Comparable // Toast.makeText(automationService, message, Toast.LENGTH_LONG).show(); if(Settings.startNewThreadForRuleActivation) publishProgress(message); - + for(int i = 0; i< Rule.this.getActionSet().size(); i++) Rule.this.getActionSet().get(i).run(automationService, doToggle); - + // Keep log of last x rule activations (Settings) try { @@ -956,9 +965,12 @@ public class Rule implements Comparable 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; + } + } public void activate(AutomationService automationService, boolean force) { diff --git a/app/src/main/java/com/jens/automation2/ActivityMainScreen.java b/app/src/main/java/com/jens/automation2/ActivityMainScreen.java index d4a509c2..0eba5b11 100644 --- a/app/src/main/java/com/jens/automation2/ActivityMainScreen.java +++ b/app/src/main/java/com/jens/automation2/ActivityMainScreen.java @@ -302,6 +302,10 @@ public class ActivityMainScreen extends ActivityGeneric if((new File(logFilePath)).exists()) srcFilesList.add(logFilePath); + String logFilePathArchive = Miscellaneous.getWriteableFolder() + "/" + Miscellaneous.logFileName + "-old"; + if((new File(logFilePathArchive)).exists()) + srcFilesList.add(logFilePathArchive); + String[] srcFiles = srcFilesList.toArray(new String[srcFilesList.size()]); if(dstZipFile.exists()) @@ -426,9 +430,9 @@ public class ActivityMainScreen extends ActivityGeneric if( Rule.isAnyRuleUsing(Trigger_Enum.pointOfInterest) && - ActivityPermissions.havePermission(ActivityPermissions.permissionNameLocationCoarse, AutomationService.getInstance()) + ActivityPermissions.havePermission(ActivityPermissions.permissionNameLocationCoarse, Miscellaneous.getAnyContext()) && - ActivityPermissions.havePermission(ActivityPermissions.permissionNameLocationFine, AutomationService.getInstance()) + ActivityPermissions.havePermission(ActivityPermissions.permissionNameLocationFine, Miscellaneous.getAnyContext()) ) activityMainScreenInstance.tvActivePoi.setText(activityMainScreenInstance.getResources().getString(R.string.stillGettingPosition)); else diff --git a/app/src/main/java/com/jens/automation2/receivers/NotificationListener.java b/app/src/main/java/com/jens/automation2/receivers/NotificationListener.java index 9eb3ac3d..2e458a60 100644 --- a/app/src/main/java/com/jens/automation2/receivers/NotificationListener.java +++ b/app/src/main/java/com/jens/automation2/receivers/NotificationListener.java @@ -8,10 +8,12 @@ import android.service.notification.StatusBarNotification; import androidx.annotation.RequiresApi; import com.jens.automation2.AutomationService; +import com.jens.automation2.Miscellaneous; import com.jens.automation2.Rule; import com.jens.automation2.Trigger; import java.util.ArrayList; +import java.util.Calendar; // See here for reference: http://gmariotti.blogspot.com/2013/11/notificationlistenerservice-and-kitkat.html @@ -19,6 +21,7 @@ import java.util.ArrayList; @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) public class NotificationListener extends NotificationListenerService { + static Calendar lastResponseToNotification = null; static NotificationListener instance; static SimpleNotification lastNotification = null; @@ -80,17 +83,25 @@ public class NotificationListener extends NotificationListenerService String text = sbn.getNotification().extras.getString(EXTRA_TEXT); lastNotification = new SimpleNotification(); + lastNotification.publishTime = Miscellaneous.calendarFromLong(sbn.getPostTime()); lastNotification.created = created; lastNotification.app = app; lastNotification.title = title; lastNotification.text = text; - ArrayList ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.notification); - for(int i=0; i ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.notification); + for (int i = 0; i < ruleCandidates.size(); i++) + { + if (ruleCandidates.get(i).applies(NotificationListener.this)) + ruleCandidates.get(i).activate(AutomationService.getInstance(), false); + } } + else + Miscellaneous.logEvent("e", "NotificationCheck", "Ignoring notification as it is old.", 5); } return false; @@ -99,8 +110,19 @@ public class NotificationListener extends NotificationListenerService public static class SimpleNotification { boolean created; + Calendar publishTime; String app, title, text; + public Calendar getPublishTime() + { + return publishTime; + } + + public void setPublishTime(Calendar publishTime) + { + this.publishTime = publishTime; + } + public boolean isCreated() { return created;