close notification action

This commit is contained in:
jens 2022-01-15 13:47:39 +01:00
parent 4b84a0c2f5
commit 22899347a1
5 changed files with 60 additions and 37 deletions

View File

@ -11,7 +11,7 @@ android {
compileSdkVersion 29 compileSdkVersion 29
buildToolsVersion '29.0.2' buildToolsVersion '29.0.2'
useLibrary 'org.apache.http.legacy' useLibrary 'org.apache.http.legacy'
versionCode 117 versionCode 118
versionName "1.7.3" versionName "1.7.3"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

View File

@ -128,15 +128,13 @@ public class Actions
for (StatusBarNotification sbn : NotificationListener.getInstance().getActiveNotifications()) for (StatusBarNotification sbn : NotificationListener.getInstance().getActiveNotifications())
{ {
String notificationApp = sbn.getPackageName(); NotificationListener.SimpleNotification sn = NotificationListener.convertNotificationToSimpleNotification(true, sbn);
String notificationTitle = null;
String notificationText = null;
Miscellaneous.logEvent("i", "NotificationCloseCheck", "Checking if this notification matches our rule " + action.getParentRule().getName() + ". App: " + notificationApp + ", title: " + notificationTitle + ", text: " + notificationText, 5); Miscellaneous.logEvent("i", "NotificationCloseCheck", "Checking if this notification should be closed in the context of rule " + action.getParentRule().getName() + ": "+ sn.toString(), 5);
if (!myApp.equals("-1")) if (!myApp.equals(Trigger.anyAppString))
{ {
if (!notificationApp.equalsIgnoreCase(myApp)) if (!myApp.equalsIgnoreCase(sn.getApp()))
{ {
Miscellaneous.logEvent("i", "NotificationCloseCheck", "Notification app name does not match rule.", 5); Miscellaneous.logEvent("i", "NotificationCloseCheck", "Notification app name does not match rule.", 5);
continue; continue;
@ -144,6 +142,9 @@ public class Actions
} }
else else
{ {
/*
Notifications from Automation are disregarded to avoid infinite loops.
*/
if(myApp.equals(BuildConfig.APPLICATION_ID)) if(myApp.equals(BuildConfig.APPLICATION_ID))
{ {
continue; continue;
@ -155,15 +156,10 @@ public class Actions
https://stackoverflow.com/questions/28047767/notificationlistenerservice-not-reading-text-of-stacked-notifications https://stackoverflow.com/questions/28047767/notificationlistenerservice-not-reading-text-of-stacked-notifications
*/ */
Bundle extras = sbn.getNotification().extras;
// T I T L E // T I T L E
if (extras.containsKey(EXTRA_TITLE))
notificationTitle = sbn.getNotification().extras.getString(EXTRA_TITLE);
if (!StringUtils.isEmpty(requiredTitle)) if (!StringUtils.isEmpty(requiredTitle))
{ {
if (!Miscellaneous.compare(myTitleDir, requiredTitle, notificationTitle)) if (!Miscellaneous.compare(myTitleDir, requiredTitle, sn.getTitle()))
{ {
Miscellaneous.logEvent("i", "NotificationCloseCheck", "Notification title does not match rule.", 5); Miscellaneous.logEvent("i", "NotificationCloseCheck", "Notification title does not match rule.", 5);
continue; continue;
@ -171,13 +167,9 @@ public class Actions
} }
// T E X T // T E X T
if (extras.containsKey(EXTRA_TEXT))
notificationText = sbn.getNotification().extras.getString(EXTRA_TEXT);
if (!StringUtils.isEmpty(requiredText)) if (!StringUtils.isEmpty(requiredText))
{ {
if (!Miscellaneous.compare(myTextDir, requiredText, notificationText)) if (!Miscellaneous.compare(myTextDir, requiredText, sn.getText()))
{ {
Miscellaneous.logEvent("i", "NotificationCloseCheck", "Notification text does not match rule.", 5); Miscellaneous.logEvent("i", "NotificationCloseCheck", "Notification text does not match rule.", 5);
continue; continue;

View File

@ -135,7 +135,7 @@ public class ActivityMainRules extends ActivityGeneric
else else
holder = (RuleHolder) v.getTag(); holder = (RuleHolder) v.getTag();
System.out.println("Position ["+position+"]"); // System.out.println("Position ["+position+"]");
Rule r = Rule.getRuleCollection().get(position); Rule r = Rule.getRuleCollection().get(position);
holder.tvRuleName.setText(r.getName()); holder.tvRuleName.setText(r.getName());
if(r.isRuleActive()) if(r.isRuleActive())

View File

@ -165,15 +165,13 @@ public class Trigger
{ {
if(getParentRule().getLastExecution() == null || sbn.getPostTime() > this.getParentRule().getLastExecution().getTimeInMillis()) if(getParentRule().getLastExecution() == null || sbn.getPostTime() > this.getParentRule().getLastExecution().getTimeInMillis())
{ {
String notificationApp = sbn.getPackageName(); NotificationListener.SimpleNotification sn = NotificationListener.convertNotificationToSimpleNotification(true, sbn);
String notificationTitle = null;
String notificationText = null;
Miscellaneous.logEvent("i", "NotificationCheck", "Checking if this notification matches our rule " + this.getParentRule().getName() + ". App: " + notificationApp + ", title: " + notificationTitle + ", text: " + notificationText, 5); Miscellaneous.logEvent("i", "NotificationCheck", "Checking if this notification matches our rule " + this.getParentRule().getName() + ": " + sn.toString(), 5);
if (!myApp.equals(anyAppString)) if (!myApp.equals(anyAppString))
{ {
if (!notificationApp.equalsIgnoreCase(myApp)) if (!myApp.equalsIgnoreCase(sn.getApp()))
{ {
Miscellaneous.logEvent("i", "NotificationCheck", "Notification app name does not match rule.", 5); Miscellaneous.logEvent("i", "NotificationCheck", "Notification app name does not match rule.", 5);
continue; continue;
@ -181,6 +179,9 @@ public class Trigger
} }
else else
{ {
/*
Notifications from Automation are disregarded to avoid infinite loops.
*/
if(myApp.equals(BuildConfig.APPLICATION_ID)) if(myApp.equals(BuildConfig.APPLICATION_ID))
{ {
return false; return false;
@ -192,15 +193,10 @@ public class Trigger
https://stackoverflow.com/questions/28047767/notificationlistenerservice-not-reading-text-of-stacked-notifications https://stackoverflow.com/questions/28047767/notificationlistenerservice-not-reading-text-of-stacked-notifications
*/ */
Bundle extras = sbn.getNotification().extras;
// T I T L E // T I T L E
if (extras.containsKey(EXTRA_TITLE))
notificationTitle = sbn.getNotification().extras.getString(EXTRA_TITLE);
if (!StringUtils.isEmpty(requiredTitle)) if (!StringUtils.isEmpty(requiredTitle))
{ {
if (!Miscellaneous.compare(myTitleDir, requiredTitle, notificationTitle)) if (!Miscellaneous.compare(myTitleDir, requiredTitle, sn.getTitle()))
{ {
Miscellaneous.logEvent("i", "NotificationCheck", "Notification title does not match rule.", 5); Miscellaneous.logEvent("i", "NotificationCheck", "Notification title does not match rule.", 5);
continue; continue;
@ -210,13 +206,9 @@ public class Trigger
Miscellaneous.logEvent("i", "NotificationCheck", "A required title for a notification trigger was not specified.", 5); Miscellaneous.logEvent("i", "NotificationCheck", "A required title for a notification trigger was not specified.", 5);
// T E X T // T E X T
if (extras.containsKey(EXTRA_TEXT))
notificationText = sbn.getNotification().extras.getString(EXTRA_TEXT);
if (!StringUtils.isEmpty(requiredText)) if (!StringUtils.isEmpty(requiredText))
{ {
if (!Miscellaneous.compare(myTextDir, requiredText, notificationText)) if (!Miscellaneous.compare(myTextDir, requiredText, sn.getText()))
{ {
Miscellaneous.logEvent("i", "NotificationCheck", "Notification text does not match rule.", 5); Miscellaneous.logEvent("i", "NotificationCheck", "Notification text does not match rule.", 5);
continue; continue;

View File

@ -1,9 +1,12 @@
package com.jens.automation2.receivers; package com.jens.automation2.receivers;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Notification;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.os.Build; import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.service.notification.NotificationListenerService; import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification; import android.service.notification.StatusBarNotification;
import android.util.Log; import android.util.Log;
@ -101,8 +104,32 @@ public class NotificationListener extends NotificationListenerService// implemen
public static SimpleNotification convertNotificationToSimpleNotification(boolean created, StatusBarNotification input) public static SimpleNotification convertNotificationToSimpleNotification(boolean created, StatusBarNotification input)
{ {
String app = input.getPackageName(); String app = input.getPackageName();
String title = input.getNotification().extras.getString(EXTRA_TITLE); String title = null;
String text = input.getNotification().extras.getString(EXTRA_TEXT); String text = null;
Bundle extras = input.getNotification().extras;
try
{
if (extras.containsKey(EXTRA_TITLE))
title = extras.getString(EXTRA_TITLE).toString();
}
catch (NullPointerException e)
{
// https://www.b4x.com/android/forum/threads/solved-reading-statusbarnotifications-extras.64416/
title = extras.get(EXTRA_TITLE).toString();
}
try
{
if (extras.containsKey(EXTRA_TEXT))
text = extras.getString(EXTRA_TEXT).toString();
}
catch (NullPointerException e)
{
// https://www.b4x.com/android/forum/threads/solved-reading-statusbarnotifications-extras.64416/
text = extras.get(EXTRA_TEXT).toString();
}
SimpleNotification returnNotification = new SimpleNotification(); SimpleNotification returnNotification = new SimpleNotification();
returnNotification.publishTime = Miscellaneous.calendarFromLong(input.getPostTime()); returnNotification.publishTime = Miscellaneous.calendarFromLong(input.getPostTime());
@ -214,6 +241,18 @@ public class NotificationListener extends NotificationListenerService// implemen
{ {
this.text = text; this.text = text;
} }
@Override
public String toString()
{
return "SimpleNotification{" +
"created=" + created +
", publishTime=" + publishTime +
", app='" + app + '\'' +
", title='" + title + '\'' +
", text='" + text + '\'' +
'}';
}
} }
@Override @Override