From fccfe85287275b3bfec886f42235533f9deae37d Mon Sep 17 00:00:00 2001 From: hyb1996 <946994919@qq.com> Date: Tue, 30 Jan 2018 11:41:15 +0800 Subject: [PATCH] feat(selector): supports javascript regex --- .../autojs/codegeneration/CodeGenerator.java | 8 ++++- .../autojs/core/accessibility/UiSelector.java | 32 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/autojs/src/main/java/com/stardust/autojs/codegeneration/CodeGenerator.java b/autojs/src/main/java/com/stardust/autojs/codegeneration/CodeGenerator.java index 592c8952..c577e30b 100644 --- a/autojs/src/main/java/com/stardust/autojs/codegeneration/CodeGenerator.java +++ b/autojs/src/main/java/com/stardust/autojs/codegeneration/CodeGenerator.java @@ -77,7 +77,13 @@ public class CodeGenerator { } protected String generateCode(UiSelectorGenerator generator, UiObject root, UiObject target, int maxParentLevel, int maxChildrenLevel, boolean withFind) { - String selector = withFind ? generator.generateSelectorCode() : generator.generateSelector().toString(); + String selector; + if (withFind) { + selector = generator.generateSelectorCode(); + } else { + UiGlobalSelector s = generator.generateSelector(); + selector = s == null ? null : s.toString(); + } if (selector != null) { return selector; } diff --git a/autojs/src/main/java/com/stardust/autojs/core/accessibility/UiSelector.java b/autojs/src/main/java/com/stardust/autojs/core/accessibility/UiSelector.java index d485126e..e4b74e4c 100644 --- a/autojs/src/main/java/com/stardust/autojs/core/accessibility/UiSelector.java +++ b/autojs/src/main/java/com/stardust/autojs/core/accessibility/UiSelector.java @@ -84,6 +84,38 @@ public class UiSelector extends UiGlobalSelector { return findOf(UiObject.createRoot(root, mAllocator)); } + @Override + public UiGlobalSelector textMatches(String regex) { + return super.textMatches(convertRegex(regex)); + } + + // TODO: 2018/1/30 更好的实现方式。 + private String convertRegex(String regex) { + if (regex.startsWith("/") && regex.endsWith("/") && regex.length() > 2) { + return regex.substring(1, regex.length() - 1); + } + return regex; + } + + @Override + public UiGlobalSelector classNameMatches(String regex) { + return super.classNameMatches(convertRegex(regex)); + } + + @Override + public UiGlobalSelector idMatches(String regex) { + return super.idMatches(convertRegex(regex)); + } + + @Override + public UiGlobalSelector packageNameMatches(String regex) { + return super.packageNameMatches(convertRegex(regex)); + } + + @Override + public UiGlobalSelector descMatches(String regex) { + return super.descMatches(convertRegex(regex)); + } private void ensureAccessibilityServiceEnabled() { mAccessibilityBridge.ensureServiceEnabled();