Notification listener finished.

This commit is contained in:
jens 2021-03-30 19:52:31 +02:00
parent 67e6a38ddc
commit 864ed2111e
5 changed files with 96 additions and 35 deletions

View File

@ -761,23 +761,34 @@ public class Rule implements Comparable<Rule>
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;
} }
@ -832,6 +843,8 @@ public class Rule implements Comparable<Rule>
private class ActivateRuleTask extends AsyncTask<Object, String, Void> private class ActivateRuleTask extends AsyncTask<Object, String, Void>
{ {
boolean wasActivated = false;
@Override @Override
protected Void doInBackground(Object... params) protected Void doInBackground(Object... params)
{ {
@ -863,17 +876,24 @@ public class Rule implements Comparable<Rule>
@Override @Override
protected void onPostExecute(Void result) protected void onPostExecute(Void result)
{
/*
Only update if the rules was actually executed. Became necessary for the notification trigger. If a user created a rule
with a notification trigger and this app creates a notification itself this will otherwise end in an infinite loop.
*/
if(wasActivated)
{ {
AutomationService.updateNotification(); AutomationService.updateNotification();
ActivityMainScreen.updateMainScreen(); ActivityMainScreen.updateMainScreen();
super.onPostExecute(result); super.onPostExecute(result);
} }
}
/** /**
* Will activate the rule. Should be called by a separate execution thread * Will activate the rule. Should be called by a separate execution thread
* @param automationService * @param automationService
*/ */
protected void activateInternally(AutomationService automationService, boolean force) protected boolean activateInternally(AutomationService automationService, boolean force)
{ {
boolean isActuallyToggable = isActuallyToggable(); boolean isActuallyToggable = isActuallyToggable();
@ -920,7 +940,10 @@ public class Rule implements Comparable<Rule>
else else
{ {
Miscellaneous.logEvent("i", "Rule", "Request to activate rule " + Rule.this.getName() + ", but it is the last one that was activated. Won't do it again.", 3); Miscellaneous.logEvent("i", "Rule", "Request to activate rule " + Rule.this.getName() + ", but it is the last one that was activated. Won't do it again.", 3);
return false;
} }
return true;
} }
} }

View File

@ -868,6 +868,8 @@ public class Rule implements Comparable<Rule>
private class ActivateRuleTask extends AsyncTask<Object, String, Void> private class ActivateRuleTask extends AsyncTask<Object, String, Void>
{ {
boolean wasActivated = false;
@Override @Override
protected Void doInBackground(Object... params) protected Void doInBackground(Object... params)
{ {
@ -899,17 +901,24 @@ public class Rule implements Comparable<Rule>
@Override @Override
protected void onPostExecute(Void result) protected void onPostExecute(Void result)
{
/*
Only update if the rules was actually executed. Became necessary for the notification trigger. If a user created a rule
with a notification trigger and this app creates a notification itself this will otherwise end in an infinite loop.
*/
if(wasActivated)
{ {
AutomationService.updateNotification(); AutomationService.updateNotification();
ActivityMainScreen.updateMainScreen(); ActivityMainScreen.updateMainScreen();
super.onPostExecute(result); super.onPostExecute(result);
} }
}
/** /**
* Will activate the rule. Should be called by a separate execution thread * Will activate the rule. Should be called by a separate execution thread
* @param automationService * @param automationService
*/ */
protected void activateInternally(AutomationService automationService, boolean force) protected boolean activateInternally(AutomationService automationService, boolean force)
{ {
boolean isActuallyToggable = isActuallyToggable(); boolean isActuallyToggable = isActuallyToggable();
@ -956,7 +965,10 @@ public class Rule implements Comparable<Rule>
else else
{ {
Miscellaneous.logEvent("i", "Rule", "Request to activate rule " + Rule.this.getName() + ", but it is the last one that was activated. Won't do it again.", 3); Miscellaneous.logEvent("i", "Rule", "Request to activate rule " + Rule.this.getName() + ", but it is the last one that was activated. Won't do it again.", 3);
return false;
} }
return true;
} }
} }

View File

@ -302,6 +302,10 @@ public class ActivityMainScreen extends ActivityGeneric
if((new File(logFilePath)).exists()) if((new File(logFilePath)).exists())
srcFilesList.add(logFilePath); srcFilesList.add(logFilePath);
String logFilePathArchive = Miscellaneous.getWriteableFolder() + "/" + Miscellaneous.logFileName + "-old";
if((new File(logFilePathArchive)).exists())
srcFilesList.add(logFilePathArchive);
String[] srcFiles = srcFilesList.toArray(new String[srcFilesList.size()]); String[] srcFiles = srcFilesList.toArray(new String[srcFilesList.size()]);
if(dstZipFile.exists()) if(dstZipFile.exists())
@ -426,9 +430,9 @@ public class ActivityMainScreen extends ActivityGeneric
if( if(
Rule.isAnyRuleUsing(Trigger_Enum.pointOfInterest) Rule.isAnyRuleUsing(Trigger_Enum.pointOfInterest)
&& &&
ActivityPermissions.havePermission(ActivityPermissions.permissionNameLocationCoarse, AutomationService.getInstance()) ActivityPermissions.havePermission(ActivityPermissions.permissionNameLocationCoarse, Miscellaneous.getAnyContext())
&& &&
ActivityPermissions.havePermission(ActivityPermissions.permissionNameLocationFine, AutomationService.getInstance()) ActivityPermissions.havePermission(ActivityPermissions.permissionNameLocationFine, Miscellaneous.getAnyContext())
) )
activityMainScreenInstance.tvActivePoi.setText(activityMainScreenInstance.getResources().getString(R.string.stillGettingPosition)); activityMainScreenInstance.tvActivePoi.setText(activityMainScreenInstance.getResources().getString(R.string.stillGettingPosition));
else else

View File

@ -8,10 +8,12 @@ import android.service.notification.StatusBarNotification;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import com.jens.automation2.AutomationService; import com.jens.automation2.AutomationService;
import com.jens.automation2.Miscellaneous;
import com.jens.automation2.Rule; import com.jens.automation2.Rule;
import com.jens.automation2.Trigger; import com.jens.automation2.Trigger;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar;
// See here for reference: http://gmariotti.blogspot.com/2013/11/notificationlistenerservice-and-kitkat.html // See here for reference: http://gmariotti.blogspot.com/2013/11/notificationlistenerservice-and-kitkat.html
@ -19,6 +21,7 @@ import java.util.ArrayList;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NotificationListener extends NotificationListenerService public class NotificationListener extends NotificationListenerService
{ {
static Calendar lastResponseToNotification = null;
static NotificationListener instance; static NotificationListener instance;
static SimpleNotification lastNotification = null; static SimpleNotification lastNotification = null;
@ -80,11 +83,16 @@ public class NotificationListener extends NotificationListenerService
String text = sbn.getNotification().extras.getString(EXTRA_TEXT); String text = sbn.getNotification().extras.getString(EXTRA_TEXT);
lastNotification = new SimpleNotification(); lastNotification = new SimpleNotification();
lastNotification.publishTime = Miscellaneous.calendarFromLong(sbn.getPostTime());
lastNotification.created = created; lastNotification.created = created;
lastNotification.app = app; lastNotification.app = app;
lastNotification.title = title; lastNotification.title = title;
lastNotification.text = text; lastNotification.text = text;
if(lastResponseToNotification == null || lastResponseToNotification.getTimeInMillis() < lastNotification.publishTime.getTimeInMillis())
{
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++)
{ {
@ -92,6 +100,9 @@ public class NotificationListener extends NotificationListenerService
ruleCandidates.get(i).activate(AutomationService.getInstance(), false); ruleCandidates.get(i).activate(AutomationService.getInstance(), false);
} }
} }
else
Miscellaneous.logEvent("e", "NotificationCheck", "Ignoring notification as it is old.", 5);
}
return false; return false;
} }
@ -99,8 +110,19 @@ public class NotificationListener extends NotificationListenerService
public static class SimpleNotification public static class SimpleNotification
{ {
boolean created; boolean created;
Calendar publishTime;
String app, title, text; String app, title, text;
public Calendar getPublishTime()
{
return publishTime;
}
public void setPublishTime(Calendar publishTime)
{
this.publishTime = publishTime;
}
public boolean isCreated() public boolean isCreated()
{ {
return created; return created;