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,22 +761,33 @@ 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)
{ {
@ -864,27 +877,34 @@ public class Rule implements Comparable<Rule>
@Override @Override
protected void onPostExecute(Void result) protected void onPostExecute(Void result)
{ {
AutomationService.updateNotification(); /*
ActivityMainScreen.updateMainScreen(); Only update if the rules was actually executed. Became necessary for the notification trigger. If a user created a rule
super.onPostExecute(result); with a notification trigger and this app creates a notification itself this will otherwise end in an infinite loop.
} */
if(wasActivated)
{
AutomationService.updateNotification();
ActivityMainScreen.updateMainScreen();
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();
boolean notLastActive = getLastActivatedRule() == null || !getLastActivatedRule().equals(Rule.this); boolean notLastActive = getLastActivatedRule() == null || !getLastActivatedRule().equals(Rule.this);
boolean doToggle = ruleToggle && isActuallyToggable; boolean doToggle = ruleToggle && isActuallyToggable;
if(notLastActive | force | doToggle) if(notLastActive | force | doToggle)
{ {
String message; String message;
if(!doToggle) if(!doToggle)
message = String.format(automationService.getResources().getString(R.string.ruleActivate), Rule.this.getName()); message = String.format(automationService.getResources().getString(R.string.ruleActivate), Rule.this.getName());
else else
message = String.format(automationService.getResources().getString(R.string.ruleActivateToggle), Rule.this.getName()); message = String.format(automationService.getResources().getString(R.string.ruleActivateToggle), Rule.this.getName());
Miscellaneous.logEvent("i", "Rule", message, 2); Miscellaneous.logEvent("i", "Rule", message, 2);
@ -892,10 +912,10 @@ public class Rule implements Comparable<Rule>
// Toast.makeText(automationService, message, Toast.LENGTH_LONG).show(); // Toast.makeText(automationService, message, Toast.LENGTH_LONG).show();
if(Settings.startNewThreadForRuleActivation) if(Settings.startNewThreadForRuleActivation)
publishProgress(message); publishProgress(message);
for(int i = 0; i< Rule.this.getActionSet().size(); i++) for(int i = 0; i< Rule.this.getActionSet().size(); i++)
Rule.this.getActionSet().get(i).run(automationService, doToggle); Rule.this.getActionSet().get(i).run(automationService, doToggle);
// Keep log of last x rule activations (Settings) // Keep log of last x rule activations (Settings)
try try
{ {
@ -920,9 +940,12 @@ 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;
}
}
public void activate(AutomationService automationService, boolean force) public void activate(AutomationService automationService, boolean force)
{ {

View File

@ -939,7 +939,7 @@ public class Rule implements Comparable<Rule>
return true; return true;
} }
} }
public void activate(AutomationService automationService, boolean force) public void activate(AutomationService automationService, boolean force)
{ {

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)
{ {
@ -900,27 +902,34 @@ public class Rule implements Comparable<Rule>
@Override @Override
protected void onPostExecute(Void result) protected void onPostExecute(Void result)
{ {
AutomationService.updateNotification(); /*
ActivityMainScreen.updateMainScreen(); Only update if the rules was actually executed. Became necessary for the notification trigger. If a user created a rule
super.onPostExecute(result); with a notification trigger and this app creates a notification itself this will otherwise end in an infinite loop.
} */
if(wasActivated)
{
AutomationService.updateNotification();
ActivityMainScreen.updateMainScreen();
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();
boolean notLastActive = getLastActivatedRule() == null || !getLastActivatedRule().equals(Rule.this); boolean notLastActive = getLastActivatedRule() == null || !getLastActivatedRule().equals(Rule.this);
boolean doToggle = ruleToggle && isActuallyToggable; boolean doToggle = ruleToggle && isActuallyToggable;
if(notLastActive | force | doToggle) if(notLastActive | force | doToggle)
{ {
String message; String message;
if(!doToggle) if(!doToggle)
message = String.format(automationService.getResources().getString(R.string.ruleActivate), Rule.this.getName()); message = String.format(automationService.getResources().getString(R.string.ruleActivate), Rule.this.getName());
else else
message = String.format(automationService.getResources().getString(R.string.ruleActivateToggle), Rule.this.getName()); message = String.format(automationService.getResources().getString(R.string.ruleActivateToggle), Rule.this.getName());
Miscellaneous.logEvent("i", "Rule", message, 2); Miscellaneous.logEvent("i", "Rule", message, 2);
@ -928,10 +937,10 @@ public class Rule implements Comparable<Rule>
// Toast.makeText(automationService, message, Toast.LENGTH_LONG).show(); // Toast.makeText(automationService, message, Toast.LENGTH_LONG).show();
if(Settings.startNewThreadForRuleActivation) if(Settings.startNewThreadForRuleActivation)
publishProgress(message); publishProgress(message);
for(int i = 0; i< Rule.this.getActionSet().size(); i++) for(int i = 0; i< Rule.this.getActionSet().size(); i++)
Rule.this.getActionSet().get(i).run(automationService, doToggle); Rule.this.getActionSet().get(i).run(automationService, doToggle);
// Keep log of last x rule activations (Settings) // Keep log of last x rule activations (Settings)
try try
{ {
@ -956,9 +965,12 @@ 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;
}
}
public void activate(AutomationService automationService, boolean force) public void activate(AutomationService automationService, boolean force)
{ {

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,17 +83,25 @@ 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;
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.notification); if(lastResponseToNotification == null || lastResponseToNotification.getTimeInMillis() < lastNotification.publishTime.getTimeInMillis())
for(int i=0; i<ruleCandidates.size(); i++)
{ {
if(ruleCandidates.get(i).applies(NotificationListener.this)) lastResponseToNotification = Calendar.getInstance();
ruleCandidates.get(i).activate(AutomationService.getInstance(), false);
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.notification);
for (int i = 0; i < ruleCandidates.size(); i++)
{
if (ruleCandidates.get(i).applies(NotificationListener.this))
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;