From 8e25079bbedb59e44fb22a5d2ac9aaa230fa035c Mon Sep 17 00:00:00 2001 From: Pablo Ortigosa <55493443+PabloOQ@users.noreply.github.com> Date: Mon, 18 Nov 2024 14:37:45 +0000 Subject: [PATCH 1/2] Add regex arrays to automation --- .../urlchecker/modules/AutomationRules.java | 11 +++++- .../utilities/methods/JavaUtils.java | 39 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/trianguloy/urlchecker/modules/AutomationRules.java b/app/src/main/java/com/trianguloy/urlchecker/modules/AutomationRules.java index 3051230e..9db896bb 100644 --- a/app/src/main/java/com/trianguloy/urlchecker/modules/AutomationRules.java +++ b/app/src/main/java/com/trianguloy/urlchecker/modules/AutomationRules.java @@ -10,6 +10,7 @@ import com.trianguloy.urlchecker.utilities.methods.AndroidUtils; import com.trianguloy.urlchecker.utilities.methods.JavaUtils; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -73,11 +74,17 @@ public List check(UrlData urlData) { var automation = catalog.getJSONObject(key); if (!automation.optBoolean("enabled", true)) continue; - if (urlData.url.matches(automation.getString("regex"))) { - matches.add(automation.getString("action")); + for (String pattern : JavaUtils.getArrayOrElement(automation.get("regex"), String.class)){ + if (urlData.url.matches(pattern)) { + matches.add(automation.getString("action")); + break; + } } + } catch (JSONException e) { AndroidUtils.assertError("Invalid automation rule", e); + } catch (ClassCastException e) { + AndroidUtils.assertError("Invalid automation regex", e); } } return matches; diff --git a/app/src/main/java/com/trianguloy/urlchecker/utilities/methods/JavaUtils.java b/app/src/main/java/com/trianguloy/urlchecker/utilities/methods/JavaUtils.java index 0accdaa0..4e25b74c 100644 --- a/app/src/main/java/com/trianguloy/urlchecker/utilities/methods/JavaUtils.java +++ b/app/src/main/java/com/trianguloy/urlchecker/utilities/methods/JavaUtils.java @@ -1,5 +1,6 @@ package com.trianguloy.urlchecker.utilities.methods; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -37,6 +38,44 @@ static JSONObject toJson(String content) { } } + /** + * Given an {@link Object} gotten from {@link JSONObject#get(String)} and a {@link Class}, + * it will check if the object is either a value or an array, of said class and return it + * as a list. + * + * @throws ClassCastException if the values are not from type {@code T} + */ + static List getArrayOrElement(Object object, Class clazz) throws ClassCastException, JSONException { + List result = new ArrayList(); + + if (object instanceof JSONArray array) { + // List + for (int i = 0; i < array.length(); i++) { + result.add(getAsOrCrash(array.get(i), clazz)); + } + } else { + // Value + result.add(getAsOrCrash(object, clazz)); + } + + return result; + } + + // I don't like it but it wasn't throwing an exception when wrongly casting, + // probably due to type casting + /** + * Returns the value as type {@code T} if possible, if not, it throws an exception + * + * @throws ClassCastException if the values are not from type {@code T} + */ + static T getAsOrCrash(Object object, Class clazz) throws ClassCastException{ + if (clazz.isInstance(object)) { + return (T) object; + } else { + throw new ClassCastException("Not of class " + clazz.getName()); + } + } + /** * Clamps a value between two other values. */ From aa3f3a85520a3fc0e3c7e7a5ae6d5f11f2832146 Mon Sep 17 00:00:00 2001 From: TrianguloY Date: Sun, 24 Nov 2024 17:19:27 +0100 Subject: [PATCH 2/2] missing explanation --- app/src/main/res/values/strings.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8891427c..fbf4a29e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -160,7 +160,7 @@ Hope you find the app useful! And don't hesitate to suggest features, report bug Show toast if automation fails "No automation with key '%s' is available. Make sure it is valid and the required module is active." "Automations configuration. Format: list of objects where the key is a unique automation name and the content includes the following values: -- 'regex': string, required: a valid java regex that the url must match for the automation to run. +- 'regex': string|list, required: a valid java regex that the url must match for the automation to run. If list, at least one regex must match. - 'action': string, required: the key of the automation to run. Check below for a list of all the available keys. - 'enabled': boolean, optional: if false this automation will be skipped. True by default." Available automation actions (keys):