New version prep, fix attempt in notification listener
This commit is contained in:
@ -37,6 +37,7 @@ import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.jens.automation2.location.LocationProvider;
|
||||
import com.jens.automation2.receivers.CalendarReceiver;
|
||||
import com.jens.automation2.receivers.NotificationListener;
|
||||
import com.jens.automation2.receivers.PhoneStatusListener;
|
||||
|
||||
@ -826,6 +827,42 @@ public class Miscellaneous extends Service
|
||||
}
|
||||
}
|
||||
|
||||
if(source.contains("[last_calendar_title]"))
|
||||
{
|
||||
try
|
||||
{
|
||||
source = source.replace("[last_calendar_title]", CalendarReceiver.getLastTriggeringEvent().title);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Miscellaneous.logEvent("w", "Variable replacement", "Error replacing variable last_calendar_title.", 3);
|
||||
}
|
||||
}
|
||||
|
||||
if(source.contains("[last_calendar_description]"))
|
||||
{
|
||||
try
|
||||
{
|
||||
source = source.replace("[last_calendar_description]", CalendarReceiver.getLastTriggeringEvent().description);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Miscellaneous.logEvent("w", "Variable replacement", "Error replacing variable last_calendar_description.", 3);
|
||||
}
|
||||
}
|
||||
|
||||
if(source.contains("[last_calendar_location]"))
|
||||
{
|
||||
try
|
||||
{
|
||||
source = source.replace("[last_calendar_location]", CalendarReceiver.getLastTriggeringEvent().location);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Miscellaneous.logEvent("w", "Variable replacement", "Error replacing variable last_calendar_location.", 3);
|
||||
}
|
||||
}
|
||||
|
||||
while(source.contains("[variable-"))
|
||||
{
|
||||
int pos1 = source.indexOf("[variable-");
|
||||
|
@ -366,6 +366,14 @@ public class Trigger
|
||||
else
|
||||
Miscellaneous.logEvent("i", "NotificationCheck", "A required text for a notification trigger was not specified.", 5);
|
||||
|
||||
/*
|
||||
We can only get here through the startup routine of the main service.
|
||||
Because the notification did not come in at runtime, but was there
|
||||
before we started, w need to take a record of it.
|
||||
*/
|
||||
if(NotificationListener.getLastNotification() == null)
|
||||
NotificationListener.setLastNotification(sn);
|
||||
|
||||
foundMatch = true;
|
||||
break;
|
||||
}
|
||||
|
@ -51,6 +51,16 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis
|
||||
static AlarmManager alarmManager = null;
|
||||
static boolean wakeupNeedsToBeScheduledOrRescheduled = false;
|
||||
|
||||
public static CalendarEvent getLastTriggeringEvent()
|
||||
{
|
||||
if(calendarEventsUsed.size() > 0)
|
||||
{
|
||||
return calendarEventsUsed.get(calendarEventsUsed.size() -1).event;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static class RuleEventPair
|
||||
{
|
||||
Rule rule;
|
||||
|
@ -3,13 +3,13 @@ package com.jens.automation2.receivers;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Notification;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.service.notification.NotificationListenerService;
|
||||
import android.service.notification.StatusBarNotification;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.RequiresApi;
|
||||
|
||||
import com.jens.automation2.AutomationService;
|
||||
@ -19,6 +19,7 @@ import com.jens.automation2.Trigger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
|
||||
// See here for reference: http://gmariotti.blogspot.com/2013/11/notificationlistenerservice-and-kitkat.html
|
||||
|
||||
@ -41,11 +42,42 @@ public class NotificationListener extends NotificationListenerService// implemen
|
||||
// a bitmap to be used instead of the small icon when showing the notification payload
|
||||
public static final String EXTRA_LARGE_ICON = "android.largeIcon";
|
||||
|
||||
public static void setLastNotification(SimpleNotification notification)
|
||||
{
|
||||
lastNotification = notification;
|
||||
}
|
||||
public static SimpleNotification getLastNotification()
|
||||
{
|
||||
return lastNotification;
|
||||
}
|
||||
|
||||
// To determine for which notifications which rules have been executed
|
||||
static List<RuleNotificationPair> notificationUsed = new ArrayList<>();
|
||||
|
||||
public static class RuleNotificationPair
|
||||
{
|
||||
Rule rule;
|
||||
SimpleNotification notification;
|
||||
|
||||
public RuleNotificationPair(Rule rule, SimpleNotification sn)
|
||||
{
|
||||
this.rule = rule;
|
||||
this.notification = sn;
|
||||
}
|
||||
}
|
||||
|
||||
public static void addUsedPair(RuleNotificationPair pair)
|
||||
{
|
||||
// Add pair only if it's not in the list already.
|
||||
for(RuleNotificationPair usedPair : notificationUsed)
|
||||
{
|
||||
if(usedPair.rule.equals(pair.rule) && usedPair.notification.equals(pair.notification))
|
||||
return;
|
||||
}
|
||||
|
||||
notificationUsed.add(pair);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate()
|
||||
{
|
||||
@ -211,6 +243,19 @@ public class NotificationListener extends NotificationListenerService// implemen
|
||||
", text='" + text + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj)
|
||||
{
|
||||
return
|
||||
this.publishTime.getTimeInMillis() == ((SimpleNotification)obj).publishTime.getTimeInMillis()
|
||||
&&
|
||||
this.app.equals(((SimpleNotification)obj).app)
|
||||
&&
|
||||
this.title.equals(((SimpleNotification)obj).title)
|
||||
&&
|
||||
this.text.equals(((SimpleNotification)obj).text);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -264,4 +309,46 @@ public class NotificationListener extends NotificationListenerService// implemen
|
||||
if(!buttonFound)
|
||||
Miscellaneous.logEvent("w", "clickNotificationButton()", "Button with text \n" + buttonText + "\n could not found.", 2);
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
|
||||
public static boolean mayRuleStillBeActivatedForPendingNotifications(Rule rule)
|
||||
{
|
||||
for(RuleNotificationPair pair : notificationUsed)
|
||||
Miscellaneous.logEvent("i", "mayRuleStillBeActivatedForPendingCalendarEvents()", "Existing pair of " + pair.rule.getName() + " and " + pair.notification, 5);
|
||||
|
||||
for(StatusBarNotification sbn : NotificationListener.getInstance().getActiveNotifications())
|
||||
{
|
||||
for(Trigger t : rule.getTriggerSet())
|
||||
{
|
||||
if(t.getTriggerType().equals(Trigger.Trigger_Enum.notification) && NotificationListener.getInstance().checkNotification(true, sbn))
|
||||
{
|
||||
if (!hasNotificationBeenUsedInRule(rule, convertNotificationToSimpleNotification(true, sbn)))
|
||||
{
|
||||
/*
|
||||
If there are multiple parallel calendar events and a rule has multiple
|
||||
triggers of type calendar event, we don't want the rule to fire only once.
|
||||
*/
|
||||
if(rule.getAmountOfTriggersForType(Trigger.Trigger_Enum.notification) == 1)
|
||||
{
|
||||
Miscellaneous.logEvent("i", "mayRuleStillBeActivatedForPendingNotifications()", "Rule " + rule.getName() + " has not been used in conjunction with notification " + sbn, 4);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean hasNotificationBeenUsedInRule(Rule rule, SimpleNotification notification)
|
||||
{
|
||||
for (RuleNotificationPair executedPair : notificationUsed)
|
||||
{
|
||||
if (executedPair.rule.equals(rule) && executedPair.notification.equals(notification))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user