From 80f8f9cfe20572fd3e5d568f73855a3fae946e6a Mon Sep 17 00:00:00 2001 From: jens Date: Sat, 27 Mar 2021 14:11:39 +0100 Subject: [PATCH] Notification listener started. --- app/src/apkFlavor/AndroidManifest.xml | 4 +- app/src/fdroidFlavor/AndroidManifest.xml | 2 +- app/src/googlePlayFlavor/AndroidManifest.xml | 2 +- .../jens/automation2/ActivityManageRule.java | 10 ++- .../automation2/ActivityManageTimeFrame.java | 2 +- ...=> ActivityManageTriggerNotification.java} | 49 ++++++----- .../java/com/jens/automation2/Trigger.java | 81 ++++++++++++++++++- .../jens/automation2/XmlFileInterface.java | 4 + .../receivers/NotificationListener.java | 14 +++- ...on.xml => manage_trigger_notification.xml} | 25 ++++-- ...ditor.xml => manage_trigger_timeframe.xml} | 0 app/src/main/res/values-de/strings.xml | 2 +- app/src/main/res/values/strings.xml | 4 +- 13 files changed, 158 insertions(+), 41 deletions(-) rename app/src/main/java/com/jens/automation2/{ActivityManageNotificationTrigger.java => ActivityManageTriggerNotification.java} (86%) rename app/src/main/res/layout/{trigger_notification.xml => manage_trigger_notification.xml} (86%) rename app/src/main/res/layout/{trigger_timeframe_editor.xml => manage_trigger_timeframe.xml} (100%) diff --git a/app/src/apkFlavor/AndroidManifest.xml b/app/src/apkFlavor/AndroidManifest.xml index f5404b05..67011fa5 100644 --- a/app/src/apkFlavor/AndroidManifest.xml +++ b/app/src/apkFlavor/AndroidManifest.xml @@ -202,10 +202,10 @@ - + - + - + - + (this, R.layout.text_view_for_poi_listview_mediumtextsize, ActivityManageNotificationTrigger.directions); + directionSpinnerAdapter = new ArrayAdapter(this, R.layout.text_view_for_poi_listview_mediumtextsize, ActivityManageTriggerNotification.directions); spinnerTitleDirection.setAdapter(directionSpinnerAdapter); spinnerTextDirection.setAdapter(directionSpinnerAdapter); directionSpinnerAdapter.notifyDataSetChanged(); @@ -283,7 +277,7 @@ public class ActivityManageNotificationTrigger extends Activity { GetActivityListTask getActivityListTask = new GetActivityListTask(); getActivityListTask.execute(); - progressDialog = ProgressDialog.show(ActivityManageNotificationTrigger.this, "", ActivityManageNotificationTrigger.this.getResources().getString(R.string.gettingListOfInstalledApplications)); + progressDialog = ProgressDialog.show(ActivityManageTriggerNotification.this, "", ActivityManageTriggerNotification.this.getResources().getString(R.string.gettingListOfInstalledApplications)); } }); @@ -294,12 +288,25 @@ public class ActivityManageNotificationTrigger extends Activity { if(saveAction()) { - String app = tvSelectedActivity.getText().toString(); - String titleDir = spinnerTitleDirection.getSelectedItem().toString(); + String app; + if(tvSelectedActivity.getText().toString().equalsIgnoreCase(getResources().getString(R.string.anyApp))) + app = "-1"; + else + app = tvSelectedActivity.getText().toString(); + + String titleDir = Trigger.getMatchCode(spinnerTitleDirection.getSelectedItem().toString()); String title = etNotificationTitle.getText().toString(); - String textDir = spinnerTextDirection.getSelectedItem().toString(); + String textDir = Trigger.getMatchCode(spinnerTextDirection.getSelectedItem().toString()); String text = etNotificationText.getText().toString(); - ActivityManageNotificationTrigger.this.setResult(RESULT_OK); + + Intent data = new Intent(); + data.putExtra("app", app); + data.putExtra("titleDir", titleDir); + data.putExtra("title", title); + data.putExtra("textDir", textDir); + data.putExtra("text", text); + + ActivityManageTriggerNotification.this.setResult(RESULT_OK, data); finish(); } } @@ -338,7 +345,7 @@ public class ActivityManageNotificationTrigger extends Activity { if(tvSelectedActivity.getText().toString().length() == 0) { - Toast.makeText(ActivityManageNotificationTrigger.this, getResources().getString(R.string.selectApplication), Toast.LENGTH_LONG).show(); + Toast.makeText(ActivityManageTriggerNotification.this, getResources().getString(R.string.selectApplication), Toast.LENGTH_LONG).show(); return false; } @@ -365,7 +372,7 @@ public class ActivityManageNotificationTrigger extends Activity @Override protected Void doInBackground(Void... params) { - getActivityList(ActivityManageNotificationTrigger.this); + getActivityList(ActivityManageTriggerNotification.this); return null; } diff --git a/app/src/main/java/com/jens/automation2/Trigger.java b/app/src/main/java/com/jens/automation2/Trigger.java index 344a27ad..7df0d66e 100644 --- a/app/src/main/java/com/jens/automation2/Trigger.java +++ b/app/src/main/java/com/jens/automation2/Trigger.java @@ -69,6 +69,9 @@ public class Trigger }; private boolean triggerParameter; //if true->started event, if false->stopped + private String triggerParameter2; + + public static final String triggerParameter2Split = "tp2split"; private Trigger_Enum triggerType = null; private PointOfInterest pointOfInterest = null; @@ -208,6 +211,16 @@ public class Trigger this.triggerParameter = triggerParameter; } + public String getTriggerParameter2() + { + return triggerParameter2; + } + + public void setTriggerParameter2(String triggerParameter2) + { + this.triggerParameter2 = triggerParameter2; + } + public TimeFrame getTimeFrame() { return timeFrame; @@ -442,6 +455,32 @@ public class Trigger else returnString.append(String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.headsetDisconnected), type)); break; + case notification: + String[] params = getTriggerParameter2().split(triggerParameter2Split); + String app = params[0]; + String titleDir = params[1]; + String title = params[2]; + String textDir = params[3]; + String text = params[4]; + StringBuilder triggerBuilder = new StringBuilder(); + + String appString; + if(app.equalsIgnoreCase("-1")) + appString = Miscellaneous.getAnyContext().getResources().getString(R.string.anyApp); + else + appString = "app " + app; + + triggerBuilder.append(String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.postsNotification), appString)); + + if(title.length() > 0) + triggerBuilder.append(", " + Miscellaneous.getAnyContext().getString(R.string.title) + " " + Trigger.getMatchString(titleDir) + " " + title); + + if(text.length() > 0) + triggerBuilder.append(", " + Miscellaneous.getAnyContext().getString(R.string.text) + " " + Trigger.getMatchString(textDir) + " " + text); + + returnString.append(triggerBuilder.toString()); + + break; default: returnString.append("error"); break; @@ -449,7 +488,47 @@ public class Trigger return returnString.toString(); } - + + public static final String directionEquals = "eq"; + public static final String directionContains = "ct"; + public static final String directionStartsWith = "sw"; + public static final String directionEndsWith = "ew"; + public static final String directionNotEquals = "ne"; + + public static String getMatchString(String direction) + { + switch(direction) + { + case directionEquals: + return Miscellaneous.getAnyContext().getString(R.string.directionStringEquals); + case directionContains: + return Miscellaneous.getAnyContext().getString(R.string.directionStringContains); + case directionStartsWith: + return Miscellaneous.getAnyContext().getString(R.string.directionStringStartsWith); + case directionEndsWith: + return Miscellaneous.getAnyContext().getString(R.string.directionStringEndsWith); + case directionNotEquals: + return Miscellaneous.getAnyContext().getString(R.string.directionStringNotEquals); + default: + return Miscellaneous.getAnyContext().getString(R.string.error); + } + } + + public static String getMatchCode(String direction) + { + if(direction.equalsIgnoreCase(Miscellaneous.getAnyContext().getString(R.string.directionStringEquals))) + return directionEquals; + else if(direction.equalsIgnoreCase(Miscellaneous.getAnyContext().getString(R.string.directionStringContains))) + return directionContains; + else if(direction.equalsIgnoreCase(Miscellaneous.getAnyContext().getString(R.string.directionStringStartsWith))) + return directionStartsWith; + else if(direction.equalsIgnoreCase(Miscellaneous.getAnyContext().getString(R.string.directionStringEndsWith))) + return directionEndsWith; + else if(direction.equalsIgnoreCase(Miscellaneous.getAnyContext().getString(R.string.directionStringNotEquals))) + return directionNotEquals; + else + return Miscellaneous.getAnyContext().getString(R.string.error); + } public static String[] getTriggerTypesAsArray() { diff --git a/app/src/main/java/com/jens/automation2/XmlFileInterface.java b/app/src/main/java/com/jens/automation2/XmlFileInterface.java index cb0af0da..5f8f28ea 100644 --- a/app/src/main/java/com/jens/automation2/XmlFileInterface.java +++ b/app/src/main/java/com/jens/automation2/XmlFileInterface.java @@ -889,6 +889,8 @@ public class XmlFileInterface newTrigger.setTriggerType(Trigger_Enum.phoneCall); else if(triggerEventString.equals("nfcTag")) newTrigger.setTriggerType(Trigger_Enum.nfcTag); + else if(triggerEventString.equals("notification")) + newTrigger.setTriggerType(Trigger_Enum.notification); else if(triggerEventString.equals("activityDetection")) newTrigger.setTriggerType(Trigger_Enum.activityDetection); else if(triggerEventString.equals("bluetoothConnection")) @@ -985,6 +987,8 @@ public class XmlFileInterface newTrigger.setHeadphoneType(-1); } } + + newTrigger.setTriggerParameter2(triggerParameter2); } 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 ea3af70a..cdade9cc 100644 --- a/app/src/main/java/com/jens/automation2/receivers/NotificationListener.java +++ b/app/src/main/java/com/jens/automation2/receivers/NotificationListener.java @@ -9,6 +9,8 @@ import android.service.notification.StatusBarNotification; import androidx.annotation.RequiresApi; +import com.jens.automation2.AutomationService; + // See here for reference: http://gmariotti.blogspot.com/2013/11/notificationlistenerservice-and-kitkat.html @SuppressLint("OverrideAbstract") @@ -38,9 +40,15 @@ public class NotificationListener extends NotificationListenerService public void onNotificationPosted(StatusBarNotification sbn) { super.onNotificationPosted(sbn); - String app = sbn.getPackageName(); - String title = sbn.getNotification().extras.getString(EXTRA_TITLE); - String text = sbn.getNotification().extras.getString(EXTRA_TEXT); + + if(AutomationService.isMyServiceRunning(NotificationListener.this)) + { + String app = sbn.getPackageName(); + String title = sbn.getNotification().extras.getString(EXTRA_TITLE); + String text = sbn.getNotification().extras.getString(EXTRA_TEXT); + + + } } // @Override diff --git a/app/src/main/res/layout/trigger_notification.xml b/app/src/main/res/layout/manage_trigger_notification.xml similarity index 86% rename from app/src/main/res/layout/trigger_notification.xml rename to app/src/main/res/layout/manage_trigger_notification.xml index 49595d1e..a114c813 100644 --- a/app/src/main/res/layout/trigger_notification.xml +++ b/app/src/main/res/layout/manage_trigger_notification.xml @@ -1,14 +1,21 @@ + + @@ -26,19 +33,20 @@ android:layout_width="match_parent" android:layout_height="wrap_content"> + +