diff --git a/app/build.gradle b/app/build.gradle index d3a6879..1110d21 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -11,7 +11,7 @@ android { compileSdkVersion 29 buildToolsVersion '29.0.2' useLibrary 'org.apache.http.legacy' - versionCode 117 + versionCode 118 versionName "1.7.3" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" diff --git a/app/src/main/java/com/jens/automation2/Actions.java b/app/src/main/java/com/jens/automation2/Actions.java index caf6985..a8e5dd1 100644 --- a/app/src/main/java/com/jens/automation2/Actions.java +++ b/app/src/main/java/com/jens/automation2/Actions.java @@ -128,15 +128,13 @@ public class Actions for (StatusBarNotification sbn : NotificationListener.getInstance().getActiveNotifications()) { - String notificationApp = sbn.getPackageName(); - String notificationTitle = null; - String notificationText = null; + NotificationListener.SimpleNotification sn = NotificationListener.convertNotificationToSimpleNotification(true, sbn); - 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); continue; @@ -144,6 +142,9 @@ public class Actions } else { + /* + Notifications from Automation are disregarded to avoid infinite loops. + */ if(myApp.equals(BuildConfig.APPLICATION_ID)) { continue; @@ -155,15 +156,10 @@ public class Actions https://stackoverflow.com/questions/28047767/notificationlistenerservice-not-reading-text-of-stacked-notifications */ - Bundle extras = sbn.getNotification().extras; - // T I T L E - if (extras.containsKey(EXTRA_TITLE)) - notificationTitle = sbn.getNotification().extras.getString(EXTRA_TITLE); - 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); continue; @@ -171,13 +167,9 @@ public class Actions } // T E X T - - if (extras.containsKey(EXTRA_TEXT)) - notificationText = sbn.getNotification().extras.getString(EXTRA_TEXT); - 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); continue; diff --git a/app/src/main/java/com/jens/automation2/ActivityMainRules.java b/app/src/main/java/com/jens/automation2/ActivityMainRules.java index afcacbc..7b830d3 100644 --- a/app/src/main/java/com/jens/automation2/ActivityMainRules.java +++ b/app/src/main/java/com/jens/automation2/ActivityMainRules.java @@ -135,7 +135,7 @@ public class ActivityMainRules extends ActivityGeneric else holder = (RuleHolder) v.getTag(); - System.out.println("Position ["+position+"]"); +// System.out.println("Position ["+position+"]"); Rule r = Rule.getRuleCollection().get(position); holder.tvRuleName.setText(r.getName()); if(r.isRuleActive()) diff --git a/app/src/main/java/com/jens/automation2/Trigger.java b/app/src/main/java/com/jens/automation2/Trigger.java index bac8453..0b7a6d7 100644 --- a/app/src/main/java/com/jens/automation2/Trigger.java +++ b/app/src/main/java/com/jens/automation2/Trigger.java @@ -165,15 +165,13 @@ public class Trigger { if(getParentRule().getLastExecution() == null || sbn.getPostTime() > this.getParentRule().getLastExecution().getTimeInMillis()) { - String notificationApp = sbn.getPackageName(); - String notificationTitle = null; - String notificationText = null; + NotificationListener.SimpleNotification sn = NotificationListener.convertNotificationToSimpleNotification(true, sbn); - 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 (!notificationApp.equalsIgnoreCase(myApp)) + if (!myApp.equalsIgnoreCase(sn.getApp())) { Miscellaneous.logEvent("i", "NotificationCheck", "Notification app name does not match rule.", 5); continue; @@ -181,6 +179,9 @@ public class Trigger } else { + /* + Notifications from Automation are disregarded to avoid infinite loops. + */ if(myApp.equals(BuildConfig.APPLICATION_ID)) { return false; @@ -192,15 +193,10 @@ public class Trigger https://stackoverflow.com/questions/28047767/notificationlistenerservice-not-reading-text-of-stacked-notifications */ - Bundle extras = sbn.getNotification().extras; - // T I T L E - if (extras.containsKey(EXTRA_TITLE)) - notificationTitle = sbn.getNotification().extras.getString(EXTRA_TITLE); - 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); continue; @@ -210,13 +206,9 @@ public class Trigger Miscellaneous.logEvent("i", "NotificationCheck", "A required title for a notification trigger was not specified.", 5); // T E X T - - if (extras.containsKey(EXTRA_TEXT)) - notificationText = sbn.getNotification().extras.getString(EXTRA_TEXT); - 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); continue; 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 85c180f..acdd2e7 100644 --- a/app/src/main/java/com/jens/automation2/receivers/NotificationListener.java +++ b/app/src/main/java/com/jens/automation2/receivers/NotificationListener.java @@ -1,9 +1,12 @@ package com.jens.automation2.receivers; import android.annotation.SuppressLint; +import android.app.Notification; import android.bluetooth.BluetoothDevice; import android.content.IntentFilter; import android.os.Build; +import android.os.Bundle; +import android.os.Parcelable; import android.service.notification.NotificationListenerService; import android.service.notification.StatusBarNotification; import android.util.Log; @@ -101,8 +104,32 @@ public class NotificationListener extends NotificationListenerService// implemen public static SimpleNotification convertNotificationToSimpleNotification(boolean created, StatusBarNotification input) { String app = input.getPackageName(); - String title = input.getNotification().extras.getString(EXTRA_TITLE); - String text = input.getNotification().extras.getString(EXTRA_TEXT); + String title = null; + 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(); returnNotification.publishTime = Miscellaneous.calendarFromLong(input.getPostTime()); @@ -214,6 +241,18 @@ public class NotificationListener extends NotificationListenerService// implemen { this.text = text; } + + @Override + public String toString() + { + return "SimpleNotification{" + + "created=" + created + + ", publishTime=" + publishTime + + ", app='" + app + '\'' + + ", title='" + title + '\'' + + ", text='" + text + '\'' + + '}'; + } } @Override