From d17e8b70fed58101c23be806eb1f1777f75258f9 Mon Sep 17 00:00:00 2001 From: jens Date: Sun, 24 Jul 2022 14:22:17 +0200 Subject: [PATCH] notification dismiss with button --- .../java/com/jens/automation2/Action.java | 8 ++++ .../java/com/jens/automation2/Actions.java | 18 ++++++-- ...ActivityManageActionCloseNotification.java | 31 ++++++------- .../receivers/NotificationListener.java | 27 ++++++----- ...ivity_manage_action_close_notification.xml | 46 ++++++++++++++++++- app/src/main/res/values-de/strings.xml | 2 +- app/src/main/res/values-it/strings.xml | 2 +- app/src/main/res/values-nl/strings.xml | 2 +- app/src/main/res/values/strings.xml | 3 +- .../metadata/android/en-US/changelogs/122.txt | 1 + 10 files changed, 100 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/com/jens/automation2/Action.java b/app/src/main/java/com/jens/automation2/Action.java index 6c9aae2d..e758ee31 100644 --- a/app/src/main/java/com/jens/automation2/Action.java +++ b/app/src/main/java/com/jens/automation2/Action.java @@ -336,6 +336,14 @@ public class Action if (parts.length > 4 && !StringUtils.isBlank(parts[4])) returnString.append(", " + Miscellaneous.getAnyContext().getResources().getString(R.string.ifString) + " " + Miscellaneous.getAnyContext().getResources().getString(R.string.text) + " " + Trigger.getMatchString(parts[3]) + " " + parts[4]); + + if(parts.length >= 6) + { + if(!parts[5].equals(ActivityManageActionCloseNotification.dismissRegularString)) + { + returnString.append(" " + String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.withButton), parts[5])); + } + } } else if(this.getAction().equals(Action_Enum.setWifi)) { diff --git a/app/src/main/java/com/jens/automation2/Actions.java b/app/src/main/java/com/jens/automation2/Actions.java index d907475e..de1db7f4 100644 --- a/app/src/main/java/com/jens/automation2/Actions.java +++ b/app/src/main/java/com/jens/automation2/Actions.java @@ -125,11 +125,14 @@ public class Actions String myTitleDir = params[1]; String requiredTitle = params[2]; String myTextDir = params[3]; - String requiredText; - if (params.length >= 5) + String requiredText = ""; + String method = ActivityManageActionCloseNotification.dismissRegularString; + + if(params.length >= 5) requiredText = params[4]; - else - requiredText = ""; + + if(params.length >= 6 && !params[5].equals(ActivityManageActionCloseNotification.dismissRegularString)) + method = params[5]; for (StatusBarNotification sbn : NotificationListener.getInstance().getActiveNotifications()) { @@ -183,7 +186,12 @@ public class Actions Miscellaneous.logEvent("i", "NotificationCloseCheck", "All criteria matches. Closing notification: " + sbn.getNotification().toString(), 3); if(NotificationListener.getInstance() != null) - NotificationListener.getInstance().dismissNotification(sbn); + { + if(method == ActivityManageActionCloseNotification.dismissRegularString) + NotificationListener.getInstance().dismissNotification(sbn); + else + NotificationListener.getInstance().clickNotificationButton(sbn, method); + } else Miscellaneous.logEvent("i", "NotificationCloseCheck", "NotificationListener instance is null. Can\'t close notification.", 3); } diff --git a/app/src/main/java/com/jens/automation2/ActivityManageActionCloseNotification.java b/app/src/main/java/com/jens/automation2/ActivityManageActionCloseNotification.java index 3e2eaf6a..8ab2cfe7 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageActionCloseNotification.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageActionCloseNotification.java @@ -314,13 +314,6 @@ public class ActivityManageActionCloseNotification extends Activity String text = etNotificationText.getText().toString(); Intent responseData = new Intent(); -// if(edit) -// { -// responseData.putExtra(ActivityManageRule.intentNameActionParameter2, app + Action.actionParameter2Split + titleDir + Action.actionParameter2Split + title + Action.actionParameter2Split + textDir + Action.actionParameter2Split + text); -// ActivityManageActionCloseNotification.this.setResult(RESULT_OK, responseData); -// } -// else -// { String dismissMethod; if (rbNotificationDismissSimple.isChecked()) @@ -336,17 +329,16 @@ public class ActivityManageActionCloseNotification extends Activity dismissMethod = etNotificationDismissalButtonText.getText().toString(); } - responseData.putExtra(ActivityManageRule.intentNameActionParameter2, - app + Action.actionParameter2Split + - titleDir + Action.actionParameter2Split + - title + Action.actionParameter2Split + - textDir + Action.actionParameter2Split + - text + Action.actionParameter2Split + - dismissMethod - ); - ActivityManageActionCloseNotification.this.setResult(RESULT_OK, responseData); -// } + responseData.putExtra(ActivityManageRule.intentNameActionParameter2, + app + Action.actionParameter2Split + + titleDir + Action.actionParameter2Split + + title + Action.actionParameter2Split + + textDir + Action.actionParameter2Split + + text + Action.actionParameter2Split + + dismissMethod + ); + ActivityManageActionCloseNotification.this.setResult(RESULT_OK, responseData); finish(); } }); @@ -387,14 +379,17 @@ public class ActivityManageActionCloseNotification extends Activity be incorrectly interpreted as a text notification text. */ - if (params.length >= 6) + if (params.length >= 6 && !params[5].equals(dismissRegularString)) { rbNotificationDismissButton.setChecked(true); etNotificationDismissalButtonText.setText(params[5]); + etNotificationDismissalButtonText.setEnabled(true); } else { rbNotificationDismissSimple.setChecked(true); + etNotificationDismissalButtonText.setText(""); + etNotificationDismissalButtonText.setEnabled(false); } if(!app.equals(Trigger.anyAppString)) 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 8603a354..c3d623da 100644 --- a/app/src/main/java/com/jens/automation2/receivers/NotificationListener.java +++ b/app/src/main/java/com/jens/automation2/receivers/NotificationListener.java @@ -245,21 +245,24 @@ public class NotificationListener extends NotificationListenerService// implemen { boolean buttonFound = false; - for (Notification.Action a : sbn.getNotification().actions) + if(sbn.getNotification().actions != null) { - if(a.toString().equalsIgnoreCase(buttonText)) + for (Notification.Action a : sbn.getNotification().actions) { - if(!buttonFound) - buttonFound = true; + if((Miscellaneous.isRegularExpression(buttonText) && a.title.toString().matches(buttonText)) || a.title.toString().equalsIgnoreCase(buttonText)) + { + if (!buttonFound) + buttonFound = true; - try - { - Miscellaneous.logEvent("w", "clickNotificationButton()", "Pressing button with text \"" + a.title.toString() + "\".", 2); - a.actionIntent.send(); - } - catch (PendingIntent.CanceledException e) - { - Miscellaneous.logEvent("w", "clickNotificationButton()", Log.getStackTraceString(e), 2); + try + { + Miscellaneous.logEvent("w", "clickNotificationButton()", "Pressing button with text \"" + a.title.toString() + "\".", 2); + a.actionIntent.send(); + } + catch (PendingIntent.CanceledException e) + { + Miscellaneous.logEvent("w", "clickNotificationButton()", Log.getStackTraceString(e), 2); + } } } } diff --git a/app/src/main/res/layout/activity_manage_action_close_notification.xml b/app/src/main/res/layout/activity_manage_action_close_notification.xml index 0b87f3c0..4cf4ebca 100644 --- a/app/src/main/res/layout/activity_manage_action_close_notification.xml +++ b/app/src/main/res/layout/activity_manage_action_close_notification.xml @@ -119,6 +119,22 @@ android:layout_margin="10dp" android:layout_marginVertical="@dimen/default_margin" android:background="#aa000000" /> + + + + @@ -177,13 +193,41 @@ + + + + + + + + + + + + + + + +