Notification trigger.

This commit is contained in:
Jens 2021-03-30 23:06:07 +02:00
parent 864ed2111e
commit 13bcb02ffc
4 changed files with 147 additions and 79 deletions

View File

@ -51,9 +51,20 @@ public class Rule implements Comparable<Rule>
private String name; private String name;
private boolean ruleActive = true; // rules can be deactivated, so they won't fire if you don't want them temporarily private boolean ruleActive = true; // rules can be deactivated, so they won't fire if you don't want them temporarily
private boolean ruleToggle = false; // rule will run again and do the opposite of its actions if applicable private boolean ruleToggle = false; // rule will run again and do the opposite of its actions if applicable
private Calendar lastExecution;
private static Date lastActivatedRuleActivationTime; private static Date lastActivatedRuleActivationTime;
public Calendar getLastExecution()
{
return lastExecution;
}
public void setLastExecution(Calendar lastExecution)
{
this.lastExecution = lastExecution;
}
public boolean isRuleToggle() public boolean isRuleToggle()
{ {
return ruleToggle; return ruleToggle;
@ -756,6 +767,8 @@ public class Rule implements Comparable<Rule>
boolean foundMatch = false; boolean foundMatch = false;
for (StatusBarNotification sbn : NotificationListener.getInstance().getActiveNotifications()) for (StatusBarNotification sbn : NotificationListener.getInstance().getActiveNotifications())
{
if(lastExecution == null || sbn.getPostTime() > this.lastExecution.getTimeInMillis())
{ {
String app = sbn.getPackageName(); String app = sbn.getPackageName();
String title = sbn.getNotification().extras.getString(EXTRA_TITLE); String title = sbn.getNotification().extras.getString(EXTRA_TITLE);
@ -791,6 +804,8 @@ public class Rule implements Comparable<Rule>
} }
foundMatch = true; foundMatch = true;
break;
}
} }
if(!foundMatch) if(!foundMatch)
@ -859,7 +874,7 @@ public class Rule implements Comparable<Rule>
if (Looper.myLooper() == null) if (Looper.myLooper() == null)
Looper.prepare(); Looper.prepare();
activateInternally((AutomationService)params[0], (Boolean)params[1]); wasActivated = activateInternally((AutomationService)params[0], (Boolean)params[1]);
return null; return null;
} }
@ -883,6 +898,7 @@ public class Rule implements Comparable<Rule>
*/ */
if(wasActivated) if(wasActivated)
{ {
setLastExecution(Calendar.getInstance());
AutomationService.updateNotification(); AutomationService.updateNotification();
ActivityMainScreen.updateMainScreen(); ActivityMainScreen.updateMainScreen();
super.onPostExecute(result); super.onPostExecute(result);

View File

@ -46,9 +46,20 @@ public class Rule implements Comparable<Rule>
private String name; private String name;
private boolean ruleActive = true; // rules can be deactivated, so they won't fire if you don't want them temporarily private boolean ruleActive = true; // rules can be deactivated, so they won't fire if you don't want them temporarily
private boolean ruleToggle = false; // rule will run again and do the opposite of its actions if applicable private boolean ruleToggle = false; // rule will run again and do the opposite of its actions if applicable
private Calendar lastExecution;
private static Date lastActivatedRuleActivationTime; private static Date lastActivatedRuleActivationTime;
public Calendar getLastExecution()
{
return lastExecution;
}
public void setLastExecution(Calendar lastExecution)
{
this.lastExecution = lastExecution;
}
public boolean isRuleToggle() public boolean isRuleToggle()
{ {
return ruleToggle; return ruleToggle;
@ -761,31 +772,45 @@ public class Rule implements Comparable<Rule>
boolean foundMatch = false; boolean foundMatch = false;
for (StatusBarNotification sbn : NotificationListener.getInstance().getActiveNotifications()) for (StatusBarNotification sbn : NotificationListener.getInstance().getActiveNotifications())
{
if(getLastExecution() == null || sbn.getPostTime() > this.lastExecution.getTimeInMillis())
{ {
String app = sbn.getPackageName(); String app = sbn.getPackageName();
String title = sbn.getNotification().extras.getString(EXTRA_TITLE); String title = sbn.getNotification().extras.getString(EXTRA_TITLE);
String text = sbn.getNotification().extras.getString(EXTRA_TEXT); 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 (!myApp.equals("-1"))
{ {
if (!app.equalsIgnoreCase(myApp)) if (!app.equalsIgnoreCase(myApp))
{
Miscellaneous.logEvent("i", "NotificationCheck", "Notification app name does not match rule.", 5);
continue; continue;
} }
}
if (myTitle.length() > 0) 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; continue;
} }
}
if (myText.length() > 0) 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; continue;
} }
}
foundMatch = true; foundMatch = true;
} }
}
if(!foundMatch) if(!foundMatch)
return false; return false;
@ -853,7 +878,7 @@ public class Rule implements Comparable<Rule>
if (Looper.myLooper() == null) if (Looper.myLooper() == null)
Looper.prepare(); Looper.prepare();
activateInternally((AutomationService)params[0], (Boolean)params[1]); wasActivated = activateInternally((AutomationService)params[0], (Boolean)params[1]);
return null; return null;
} }
@ -877,6 +902,7 @@ public class Rule implements Comparable<Rule>
*/ */
if(wasActivated) if(wasActivated)
{ {
setLastExecution(Calendar.getInstance());
AutomationService.updateNotification(); AutomationService.updateNotification();
ActivityMainScreen.updateMainScreen(); ActivityMainScreen.updateMainScreen();
super.onPostExecute(result); super.onPostExecute(result);

View File

@ -48,9 +48,20 @@ public class Rule implements Comparable<Rule>
private String name; private String name;
private boolean ruleActive = true; // rules can be deactivated, so they won't fire if you don't want them temporarily private boolean ruleActive = true; // rules can be deactivated, so they won't fire if you don't want them temporarily
private boolean ruleToggle = false; // rule will run again and do the opposite of its actions if applicable private boolean ruleToggle = false; // rule will run again and do the opposite of its actions if applicable
private Calendar lastExecution;
private static Date lastActivatedRuleActivationTime; private static Date lastActivatedRuleActivationTime;
public Calendar getLastExecution()
{
return lastExecution;
}
public void setLastExecution(Calendar lastExecution)
{
this.lastExecution = lastExecution;
}
public boolean isRuleToggle() public boolean isRuleToggle()
{ {
return ruleToggle; return ruleToggle;
@ -792,31 +803,45 @@ public class Rule implements Comparable<Rule>
boolean foundMatch = false; boolean foundMatch = false;
for (StatusBarNotification sbn : NotificationListener.getInstance().getActiveNotifications()) for (StatusBarNotification sbn : NotificationListener.getInstance().getActiveNotifications())
{
if(getLastExecution() == null || sbn.getPostTime() > this.lastExecution.getTimeInMillis())
{ {
String app = sbn.getPackageName(); String app = sbn.getPackageName();
String title = sbn.getNotification().extras.getString(EXTRA_TITLE); String title = sbn.getNotification().extras.getString(EXTRA_TITLE);
String text = sbn.getNotification().extras.getString(EXTRA_TEXT); 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 (!myApp.equals("-1"))
{ {
if (!app.equalsIgnoreCase(myApp)) if (!app.equalsIgnoreCase(myApp))
{
Miscellaneous.logEvent("i", "NotificationCheck", "Notification app name does not match rule.", 5);
continue; continue;
} }
}
if (myTitle.length() > 0) 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; continue;
} }
}
if (myText.length() > 0) 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; continue;
} }
}
foundMatch = true; foundMatch = true;
} }
}
if(!foundMatch) if(!foundMatch)
return false; return false;
@ -884,7 +909,7 @@ public class Rule implements Comparable<Rule>
if (Looper.myLooper() == null) if (Looper.myLooper() == null)
Looper.prepare(); Looper.prepare();
activateInternally((AutomationService)params[0], (Boolean)params[1]); wasActivated = activateInternally((AutomationService)params[0], (Boolean)params[1]);
return null; return null;
} }
@ -908,6 +933,7 @@ public class Rule implements Comparable<Rule>
*/ */
if(wasActivated) if(wasActivated)
{ {
setLastExecution(Calendar.getInstance());
AutomationService.updateNotification(); AutomationService.updateNotification();
ActivityMainScreen.updateMainScreen(); ActivityMainScreen.updateMainScreen();
super.onPostExecute(result); super.onPostExecute(result);

View File

@ -74,7 +74,7 @@ public class NotificationListener extends NotificationListenerService
checkNotification(false, sbn); checkNotification(false, sbn);
} }
boolean checkNotification(boolean created, StatusBarNotification sbn) synchronized boolean checkNotification(boolean created, StatusBarNotification sbn)
{ {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT)
{ {
@ -89,9 +89,9 @@ public class NotificationListener extends NotificationListenerService
lastNotification.title = title; lastNotification.title = title;
lastNotification.text = text; lastNotification.text = text;
if(lastResponseToNotification == null || lastResponseToNotification.getTimeInMillis() < lastNotification.publishTime.getTimeInMillis()) // if(lastResponseToNotification == null || lastResponseToNotification.getTimeInMillis() < lastNotification.publishTime.getTimeInMillis())
{ // {
lastResponseToNotification = Calendar.getInstance(); // lastResponseToNotification = Calendar.getInstance();
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.notification); ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.notification);
for (int i = 0; i < ruleCandidates.size(); i++) for (int i = 0; i < ruleCandidates.size(); i++)
@ -99,9 +99,9 @@ public class NotificationListener extends NotificationListenerService
if (ruleCandidates.get(i).applies(NotificationListener.this)) if (ruleCandidates.get(i).applies(NotificationListener.this))
ruleCandidates.get(i).activate(AutomationService.getInstance(), false); ruleCandidates.get(i).activate(AutomationService.getInstance(), false);
} }
} // }
else // else
Miscellaneous.logEvent("e", "NotificationCheck", "Ignoring notification as it is old.", 5); // Miscellaneous.logEvent("e", "NotificationCheck", "Ignoring notification as it is old.", 5);
} }
return false; return false;