Notification trigger.

This commit is contained in:
Jens 2021-03-27 22:52:42 +01:00
parent 712a374adb
commit 0df5342036
7 changed files with 196 additions and 130 deletions

View File

@ -649,45 +649,6 @@ public class Rule implements Comparable<Rule>
{ {
Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), "Checking for bluetooth...", 4); Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), "Checking for bluetooth...", 4);
// if( // connected / disconnected
// (oneTrigger.getTriggerParameter() && (BluetoothReceiver.getLastAction().equals(android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED) | BluetoothReceiver.getLastAction().equals("android.bluetooth.device.action.ACL_CONNECTED")))
// |
// (!oneTrigger.getTriggerParameter() && (BluetoothReceiver.getLastAction().equals(android.bluetooth.BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED) | BluetoothReceiver.getLastAction().equals(android.bluetooth.BluetoothDevice.ACTION_ACL_DISCONNECTED) | BluetoothReceiver.getLastAction().equals("android.bluetooth.device.action.ACTION_ACL_DISCONNECT_REQUESTED") | BluetoothReceiver.getLastAction().equals("android.bluetooth.device.action.ACTION_ACL_DISCONNECTED")))
// )
// {
// if(oneTrigger.getBluetoothDeviceAddress() != null)
// {
// if(oneTrigger.getBluetoothDeviceAddress().equals("<any>"))
// {
// Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), "No bluetooth address specified, any will do.", 4);
// }
// else if(oneTrigger.getBluetoothDeviceAddress().equals("<none>"))
// {
// // ???
// }
// else
// {
// Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), "Bluetooth address specified, checking that.", 4);
// if(!BluetoothReceiver.getLastAffectedDevice().getAddress().equals(oneTrigger.getBluetoothDeviceAddress()))
// {
// Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), context.getResources().getString(R.string.ruleDoesntApplyNotTheCorrectDeviceAddress), 3);
// return false;
// }
// else
// Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), "Bluetooth address matches. Rule will apply.", 4);
// }
// }
// }
// else if(BluetoothReceiver.getLastAction().equals(android.bluetooth.BluetoothDevice.ACTION_FOUND) | BluetoothReceiver.getLastAction().equals(android.bluetooth.BluetoothDevice.ACTION_FOUND))
// {
// if(!oneTrigger.getTriggerParameter())
// {
// Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), context.getResources().getString(R.string.ruleDoesntApplyDeviceInRangeButShouldNotBe), 3);
// return false;
// }
// }
// else // above only checks for last action, this checks for things in the past
{
if(oneTrigger.getBluetoothDeviceAddress().equals("<any>")) if(oneTrigger.getBluetoothDeviceAddress().equals("<any>"))
{ {
if(oneTrigger.getBluetoothEvent().equals(BluetoothDevice.ACTION_ACL_CONNECTED)) if(oneTrigger.getBluetoothEvent().equals(BluetoothDevice.ACTION_ACL_CONNECTED))
@ -751,7 +712,6 @@ public class Rule implements Comparable<Rule>
return false; return false;
} }
} }
}
else if(oneTrigger.getTriggerType().equals(Trigger.Trigger_Enum.headsetPlugged)) else if(oneTrigger.getTriggerType().equals(Trigger.Trigger_Enum.headsetPlugged))
{ {
if(HeadphoneJackListener.isHeadsetConnected() != oneTrigger.getTriggerParameter()) if(HeadphoneJackListener.isHeadsetConnected() != oneTrigger.getTriggerParameter())
@ -763,6 +723,18 @@ public class Rule implements Comparable<Rule>
return false; return false;
} }
} }
else if(oneTrigger.getTriggerType().equals(Trigger.Trigger_Enum.notification))
{
k
if(HeadphoneJackListener.isHeadsetConnected() != oneTrigger.getTriggerParameter())
return false;
else
if(oneTrigger.getHeadphoneType() != 2 && oneTrigger.getHeadphoneType() != HeadphoneJackListener.getHeadphoneType())
{
Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), context.getResources().getString(R.string.ruleDoesntApplyWrongHeadphoneType), 3);
return false;
}
}
} }
return true; return true;
@ -1280,6 +1252,26 @@ public class Rule implements Comparable<Rule>
return ruleCandidates; return ruleCandidates;
} }
public static ArrayList<Rule> findRuleCandidates(Trigger.Trigger_Enum triggerType)
{
ArrayList<Rule> ruleCandidates = new ArrayList<Rule>();
for(Rule oneRule : ruleCollection)
{
innerloop:
for(Trigger oneTrigger : oneRule.getTriggerSet())
{
if(oneTrigger.getTriggerType() == triggerType)
{
ruleCandidates.add(oneRule);
break innerloop; //we don't need to search the other triggers in the same rule
}
}
}
return ruleCandidates;
}
public static ArrayList<Rule> findRuleCandidatesByActivityDetection() public static ArrayList<Rule> findRuleCandidatesByActivityDetection()
{ {
ArrayList<Rule> ruleCandidates = new ArrayList<Rule>(); ArrayList<Rule> ruleCandidates = new ArrayList<Rule>();

View File

@ -1249,6 +1249,26 @@ public class Rule implements Comparable<Rule>
return ruleCandidates; return ruleCandidates;
} }
public static ArrayList<Rule> findRuleCandidates(Trigger.Trigger_Enum triggerType)
{
ArrayList<Rule> ruleCandidates = new ArrayList<Rule>();
for(Rule oneRule : ruleCollection)
{
innerloop:
for(Trigger oneTrigger : oneRule.getTriggerSet())
{
if(oneTrigger.getTriggerType() == triggerType)
{
ruleCandidates.add(oneRule);
break innerloop; //we don't need to search the other triggers in the same rule
}
}
}
return ruleCandidates;
}
public static ArrayList<Rule> findRuleCandidatesByActivityDetection() public static ArrayList<Rule> findRuleCandidatesByActivityDetection()
{ {
ArrayList<Rule> ruleCandidates = new ArrayList<Rule>(); ArrayList<Rule> ruleCandidates = new ArrayList<Rule>();

View File

@ -1280,6 +1280,26 @@ public class Rule implements Comparable<Rule>
return ruleCandidates; return ruleCandidates;
} }
public static ArrayList<Rule> findRuleCandidates(Trigger.Trigger_Enum triggerType)
{
ArrayList<Rule> ruleCandidates = new ArrayList<Rule>();
for(Rule oneRule : ruleCollection)
{
innerloop:
for(Trigger oneTrigger : oneRule.getTriggerSet())
{
if(oneTrigger.getTriggerType() == triggerType)
{
ruleCandidates.add(oneRule);
break innerloop; //we don't need to search the other triggers in the same rule
}
}
}
return ruleCandidates;
}
public static ArrayList<Rule> findRuleCandidatesByActivityDetection() public static ArrayList<Rule> findRuleCandidatesByActivityDetection()
{ {
ArrayList<Rule> ruleCandidates = new ArrayList<Rule>(); ArrayList<Rule> ruleCandidates = new ArrayList<Rule>();

View File

@ -231,7 +231,6 @@ public class Trigger
this.timeFrame = timeFrame; this.timeFrame = timeFrame;
} }
@RequiresApi(api = Build.VERSION_CODES.KITKAT) @RequiresApi(api = Build.VERSION_CODES.KITKAT)
@SuppressWarnings("unused") @SuppressWarnings("unused")
@Override @Override
@ -456,7 +455,10 @@ public class Trigger
returnString.append(String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.headsetDisconnected), type)); returnString.append(String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.headsetDisconnected), type));
break; break;
case notification: case notification:
if(this.getTriggerParameter2().contains(triggerParameter2Split))
{
String[] params = getTriggerParameter2().split(triggerParameter2Split); String[] params = getTriggerParameter2().split(triggerParameter2Split);
String app = params[0]; String app = params[0];
String titleDir = params[1]; String titleDir = params[1];
String title = params[2]; String title = params[2];
@ -474,7 +476,10 @@ public class Trigger
else else
appString = "app " + app; appString = "app " + app;
if(triggerParameter)
triggerBuilder.append(String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.postsNotification), appString)); triggerBuilder.append(String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.postsNotification), appString));
else
triggerBuilder.append(String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.removedNotification), appString));
if (title.length() > 0) if (title.length() > 0)
triggerBuilder.append(", " + Miscellaneous.getAnyContext().getString(R.string.title) + " " + Trigger.getMatchString(titleDir) + " " + title); triggerBuilder.append(", " + Miscellaneous.getAnyContext().getString(R.string.title) + " " + Trigger.getMatchString(titleDir) + " " + title);
@ -483,7 +488,11 @@ public class Trigger
triggerBuilder.append(", " + Miscellaneous.getAnyContext().getString(R.string.text) + " " + Trigger.getMatchString(textDir) + " " + text); triggerBuilder.append(", " + Miscellaneous.getAnyContext().getString(R.string.text) + " " + Trigger.getMatchString(textDir) + " " + text);
returnString.append(triggerBuilder.toString()); returnString.append(triggerBuilder.toString());
}
else
{
setTriggerParameter2("-1" + triggerParameter2Split + directionEquals + triggerParameter2Split + triggerParameter2Split + directionEquals + triggerParameter2Split + triggerParameter2Split);
}
break; break;
default: default:
returnString.append("error"); returnString.append("error");

View File

@ -271,6 +271,8 @@ public class XmlFileInterface
} }
else if(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getTriggerType() == Trigger_Enum.headsetPlugged) else if(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getTriggerType() == Trigger_Enum.headsetPlugged)
serializer.text(String.valueOf(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getHeadphoneType())); serializer.text(String.valueOf(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getHeadphoneType()));
else if(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getTriggerType() == Trigger_Enum.notification)
serializer.text(String.valueOf(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getTriggerParameter2()));
serializer.endTag(null, "TriggerParameter2"); serializer.endTag(null, "TriggerParameter2");
serializer.endTag(null, "Trigger"); serializer.endTag(null, "Trigger");
} }

View File

@ -10,6 +10,10 @@ 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.Rule;
import com.jens.automation2.Trigger;
import java.util.ArrayList;
// 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
@ -47,17 +51,35 @@ public class NotificationListener extends NotificationListenerService
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);
checkNotification(true, app, title, text);
} }
} }
// @Override @RequiresApi(api = Build.VERSION_CODES.KITKAT)
// public void onNotificationPosted(StatusBarNotification sbn, RankingMap rankingMap) @Override
// { public void onNotificationRemoved(StatusBarNotification sbn)
// super.onNotificationPosted(sbn, rankingMap); {
// sbn.getNotification().extras.getString(EXTRA_TITLE); super.onNotificationRemoved(sbn);
// 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);
checkNotification(true, app, title, text);
}
}
void checkNotification(boolean created, String appName, String title, String text)
{
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);
}
}
@Override @Override
public void onListenerConnected() public void onListenerConnected()

View File

@ -622,6 +622,7 @@
<string name="anyApp">Any app</string> <string name="anyApp">Any app</string>
<string name="notificationTriggerExplanation">This trigger will respond to other applications opening notifications in the notification area. You can specify another application from which the notification has to come from. If you don\'t the notifications from any other application will count.\nYou can also specify strings that must be or must not be in the notification title or notification body.</string> <string name="notificationTriggerExplanation">This trigger will respond to other applications opening notifications in the notification area. You can specify another application from which the notification has to come from. If you don\'t the notifications from any other application will count.\nYou can also specify strings that must be or must not be in the notification title or notification body.</string>
<string name="postsNotification">%1$s posts notification</string> <string name="postsNotification">%1$s posts notification</string>
<string name="removedNotification">notification from %1$s removed</string>
<string name="notificationAppears">Notification appears</string> <string name="notificationAppears">Notification appears</string>
<string name="notificationDisappears">Notification disappears</string> <string name="notificationDisappears">Notification disappears</string>
<string name="direction">Direction</string> <string name="direction">Direction</string>