Skip to content

Commit 311307c

Browse files
author
hyb1996
committed
使代码实现和文档描述一致
1 parent d3ef346 commit 311307c

File tree

10 files changed

+185
-93
lines changed

10 files changed

+185
-93
lines changed

autojs/src/main/assets/modules/__app__.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module.exports = function (runtime, global) {
4242
}
4343
if (i.type) {
4444
if (i.data) {
45-
intent.setDataAndType(android.net.Uri.parse(i.data), i.type);
45+
intent.setDataAndType(app.parseUri(i.data), i.type);
4646
} else {
4747
intent.setType(i.type);
4848
}

autojs/src/main/assets/modules/__automator__.js

+27-9
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,15 @@ module.exports = function(runtime, global){
138138
}
139139

140140
auto.setFlags = function(flags){
141-
if(typeof(flags) !== "string"){
142-
throw new TypeError("flags should be a string");
141+
let flagStrings;
142+
if(Array.isArray(flags)){
143+
flagStrings = flags;
144+
} else if(typeof(flags) == "string"){
145+
flagStrings = [flags];
146+
} else {
147+
throw new TypeError("flags = " + flags);
143148
}
144149
let flagsInt = 0;
145-
let flagStrings = flags.split("|");
146150
for(let i = 0; i < flagStrings.length; i++){
147151
let flag = flagsMap[flagStrings[i]];
148152
if(flag == undefined){
@@ -153,22 +157,36 @@ module.exports = function(runtime, global){
153157
runtime.accessibilityBridge.setFlags(flagsInt);
154158
}
155159

156-
auto.getService = function(){
160+
auto.__defineGetter__("service", function() {
157161
return runtime.accessibilityBridge.getService();
158-
}
162+
});
159163

160-
auto.getWindows = function(){
164+
auto.__defineGetter__("windows", function() {
161165
var service = auto.getService();
162166
return service == null ? [] : util.java.toJsArray(service.getWindows(), true);
163-
}
167+
});
164168

165-
auto.getRoot = function(){
169+
auto.__defineGetter__("root", function() {
166170
var root = runtime.accessibilityBridge.getRootInCurrentWindow();
167171
if(root == null){
168172
return null;
169173
}
170174
return com.stardust.automator.UiObject.Companion.createRoot(root);
171-
}
175+
});
176+
177+
auto.__defineGetter__("rootInActiveWindow", function() {
178+
var root = runtime.accessibilityBridge.getRootInActiveWindow();
179+
if(root == null){
180+
return null;
181+
}
182+
return com.stardust.automator.UiObject.Companion.createRoot(root);
183+
});
184+
185+
auto.__defineGetter__("windowRoots", function() {
186+
return util.java.toJsArray(runtime.accessibilityBridge.windowRoots(), false)
187+
.map(root => com.stardust.automator.UiObject.Companion.createRoot(root));
188+
});
189+
172190

173191
auto.setWindowFilter = function(filter){
174192
runtime.accessibilityBridge.setWindowFilter(new com.stardust.autojs.core.accessibility.AccessibilityBridge.WindowFilter(filter));

autojs/src/main/assets/modules/__images__.js

+35-34
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,22 @@ module.exports = function (runtime, scope) {
6464
MatchingResult.prototype.best = function () {
6565
return this.findMax((l, r) => r.similarity - l.similarity);
6666
}
67-
MatchingResult.prototype.sortBy = function(cmp) {
67+
MatchingResult.prototype.sortBy = function (cmp) {
6868
var comparatorFn = null;
69-
if(typeof(cmp) == 'string'){
69+
if (typeof (cmp) == 'string') {
7070
cmp.split("-").forEach(direction => {
7171
var buildInFn = comparators[direction];
72-
if(!buildInFn){
73-
throw new Error("unknown direction '" + direction + "' in '" + cmp +"'");
72+
if (!buildInFn) {
73+
throw new Error("unknown direction '" + direction + "' in '" + cmp + "'");
7474
}
75-
(function(fn){
76-
if(comparatorFn == null){
75+
(function (fn) {
76+
if (comparatorFn == null) {
7777
comparatorFn = fn;
78-
}else{
79-
comparatorFn = (function(comparatorFn, fn){
80-
return function(l, r){
78+
} else {
79+
comparatorFn = (function (comparatorFn, fn) {
80+
return function (l, r) {
8181
var cmpValue = comparatorFn(l, r);
82-
if(cmpValue == 0){
82+
if (cmpValue == 0) {
8383
return fn(l, r);
8484
}
8585
return cmpValue;
@@ -88,7 +88,7 @@ module.exports = function (runtime, scope) {
8888
}
8989
})(buildInFn);
9090
});
91-
}else{
91+
} else {
9292
comparatorFn = cmp;
9393
}
9494
var clone = this.matches.slice();
@@ -188,29 +188,32 @@ module.exports = function (runtime, scope) {
188188

189189
images.inRange = function (img, lowerBound, upperBound) {
190190
initIfNeeded();
191-
var lb, ub;
192-
if (typeof (lowerBound) == 'string') {
193-
if (typeof (upperBound) == 'string') {
194-
lb = new Scalar(colors.red(lowerBound), colors.green(lowerBound),
195-
colors.blue(lowerBound), colors.alpha(lowerBound));
196-
ub = new Scalar(colors.red(upperBound), colors.green(upperBound),
197-
colors.blue(upperBound), colors.alpha(lowerBound));
198-
} else if (typeof (upperBound) == 'number') {
199-
var color = lowerBound;
200-
var threshold = upperBound;
201-
lb = new Scalar(colors.red(color) - threshold, colors.green(color) - threshold,
202-
colors.blue(color) - threshold, colors.alpha(color));
203-
ub = new Scalar(colors.red(color) + threshold, colors.green(color) + threshold,
204-
colors.blue(color) + threshold, colors.alpha(color));
205-
} else {
206-
throw new TypeError('lowerBound = ' + lowerBound, + 'upperBound = ' + upperBound);
207-
}
191+
var lb = new Scalar(colors.red(lowerBound), colors.green(lowerBound),
192+
colors.blue(lowerBound), colors.alpha(lowerBound));
193+
var ub = new Scalar(colors.red(upperBound), colors.green(upperBound),
194+
colors.blue(upperBound), colors.alpha(lowerBound))
195+
if (typeof (upperBound) == 'number') {
196+
var color = lowerBound;
197+
var threshold = upperBound;
198+
199+
} else {
200+
throw new TypeError('lowerBound = ' + lowerBound, + 'upperBound = ' + upperBound);
208201
}
209202
var bi = new Mat();
210203
Core.inRange(img.mat, lb, ub, bi);
211204
return images.matToImage(bi);
212205
}
213206

207+
images.interval = function (img, color, threshold) {
208+
initIfNeeded();
209+
var lb = new Scalar(colors.red(color) - threshold, colors.green(color) - threshold,
210+
colors.blue(color) - threshold, colors.alpha(color));
211+
var ub = new Scalar(colors.red(color) + threshold, colors.green(color) + threshold,
212+
colors.blue(color) + threshold, colors.alpha(color));
213+
var bi = new Mat();
214+
Core.inRange(img.mat, lb, ub, bi);
215+
return images.matToImage(bi);
216+
}
214217

215218
images.adaptiveThreshold = function (img, maxValue, adaptiveMethod, thresholdType, blockSize, C) {
216219
initIfNeeded();
@@ -225,7 +228,7 @@ module.exports = function (runtime, scope) {
225228
initIfNeeded();
226229
var mat = new Mat();
227230
size = newSize(size);
228-
type = Imgproc["BORDER_" + (type || "CONSTANT")];
231+
type = Imgproc["BORDER_" + (type || "DEFAULT")];
229232
if (point == undefined) {
230233
Imgproc.blur(img.mat, mat, size);
231234
} else {
@@ -322,12 +325,10 @@ module.exports = function (runtime, scope) {
322325
return javaImages.rotate(img, x, y, degree);
323326
}
324327

325-
images.concat = function (img1, img2, direction, rect1, rect2) {
328+
images.concat = function (img1, img2, direction) {
326329
initIfNeeded();
327330
direction = direction || "right";
328-
rect1 = buildRegion(rect1, img1);
329-
rect2 = buildRegion(rect2, img1);
330-
return javaImages.concat(img1, rect1, img2, rect2, android.view.Gravity[direction.toUpperCase()]);
331+
return javaImages.concat(img1, img2, android.view.Gravity[direction.toUpperCase()]);
331332
}
332333

333334
images.detectsColor = function (img, color, x, y, threshold, algorithm) {
@@ -525,7 +526,7 @@ module.exports = function (runtime, scope) {
525526
var width = region[2] === undefined ? img.getWidth() - x : region[2];
526527
var height = region[3] === undefined ? (img.getHeight() - y) : region[3];
527528
var r = new org.opencv.core.Rect(x, y, width, height);
528-
if(x < 0 || y < 0 || x + width > img.width || y + height > img.height) {
529+
if (x < 0 || y < 0 || x + width > img.width || y + height > img.height) {
529530
throw new Error("out of region: region = [" + [x, y, width, height] + "], image.size = [" + [img.width, img.height] + "]");
530531
}
531532
return r;

autojs/src/main/java/com/stardust/autojs/core/accessibility/AccessibilityBridge.java

+37-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
import com.stardust.view.accessibility.AccessibilityNotificationObserver;
2020
import com.stardust.view.accessibility.AccessibilityService;
2121

22+
import java.util.ArrayList;
23+
import java.util.Collections;
24+
import java.util.List;
25+
2226

2327
/**
2428
* Created by Stardust on 2017/4/2.
@@ -62,26 +66,51 @@ public void post(Runnable r) {
6266
@Nullable
6367
public abstract AccessibilityService getService();
6468

69+
public List<AccessibilityNodeInfo> windowRoots() {
70+
AccessibilityService service = getService();
71+
if (service == null)
72+
return Collections.emptyList();
73+
ArrayList<AccessibilityNodeInfo> roots = new ArrayList<>();
74+
if (mWindowFilter != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
75+
for (AccessibilityWindowInfo window : service.getWindows()) {
76+
if (mWindowFilter.filter(window)) {
77+
AccessibilityNodeInfo root = window.getRoot();
78+
if (root != null) {
79+
roots.add(root);
80+
}
81+
}
82+
}
83+
return roots;
84+
}
85+
if ((mMode & MODE_FAST) != 0) {
86+
return Collections.singletonList(service.fastRootInActiveWindow());
87+
}
88+
return Collections.singletonList(service.getRootInActiveWindow());
89+
}
90+
6591
@Nullable
6692
public AccessibilityNodeInfo getRootInCurrentWindow() {
6793
AccessibilityService service = getService();
68-
6994
if (service == null)
7095
return null;
7196
if (mWindowFilter != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
72-
AccessibilityWindowInfo activeWindow = null;
7397
for (AccessibilityWindowInfo window : service.getWindows()) {
7498
if (mWindowFilter.filter(window)) {
7599
return window.getRoot();
76100
}
77-
if (window.isActive()) {
78-
activeWindow = window;
79-
}
80-
}
81-
if (activeWindow != null) {
82-
return activeWindow.getRoot();
83101
}
102+
return null;
103+
}
104+
if ((mMode & MODE_FAST) != 0) {
105+
return service.fastRootInActiveWindow();
84106
}
107+
return service.getRootInActiveWindow();
108+
}
109+
110+
public AccessibilityNodeInfo getRootInActiveWindow() {
111+
AccessibilityService service = getService();
112+
if (service == null)
113+
return null;
85114
if ((mMode & MODE_FAST) != 0) {
86115
return service.fastRootInActiveWindow();
87116
}

autojs/src/main/java/com/stardust/autojs/core/accessibility/SimpleActionAutomator.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import android.graphics.Rect;
66
import android.os.Build;
77
import android.os.Handler;
8+
89
import androidx.annotation.RequiresApi;
10+
911
import android.util.Log;
1012
import android.view.accessibility.AccessibilityNodeInfo;
1113

@@ -20,6 +22,8 @@
2022
import com.stardust.util.DeveloperUtils;
2123
import com.stardust.util.ScreenMetrics;
2224

25+
import java.util.List;
26+
2327
/**
2428
* Created by Stardust on 2017/4/2.
2529
*/
@@ -253,11 +257,15 @@ private boolean performAction(SimpleAction simpleAction) {
253257
Log.d(TAG, "performAction: running package is self. return false");
254258
return false;
255259
}
256-
AccessibilityNodeInfo root = mAccessibilityBridge.getRootInCurrentWindow();
257-
if (root == null)
260+
List<AccessibilityNodeInfo> roots = mAccessibilityBridge.windowRoots();
261+
if (roots.isEmpty())
258262
return false;
259-
Log.v(TAG, "performAction: " + simpleAction + " root = " + root);
260-
return simpleAction.perform(UiObject.Companion.createRoot(root));
263+
Log.v(TAG, "performAction: " + simpleAction + " windowRoots = " + roots);
264+
boolean succeed = true;
265+
for (AccessibilityNodeInfo root : roots) {
266+
succeed &= simpleAction.perform(UiObject.Companion.createRoot(root));
267+
}
268+
return succeed;
261269
}
262270

263271
private boolean isRunningPackageSelf() {

autojs/src/main/java/com/stardust/autojs/core/accessibility/UiSelector.java

+17-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
import org.jetbrains.annotations.NotNull;
2323

24+
import java.util.ArrayList;
25+
import java.util.List;
26+
2427
import static androidx.core.view.accessibility.AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS;
2528
import static androidx.core.view.accessibility.AccessibilityNodeInfoCompat.ACTION_ARGUMENT_COLUMN_INT;
2629
import static androidx.core.view.accessibility.AccessibilityNodeInfoCompat.ACTION_ARGUMENT_PROGRESS_VALUE;
@@ -94,17 +97,24 @@ public UiObjectCollection find() {
9497
@NonNull
9598
@ScriptInterface
9699
protected UiObjectCollection findImpl(int max) {
97-
AccessibilityNodeInfo root = mAccessibilityBridge.getRootInCurrentWindow();
100+
List<AccessibilityNodeInfo> roots = mAccessibilityBridge.windowRoots();
98101
if (BuildConfig.DEBUG)
99-
Log.d(TAG, "find: root = " + root);
100-
if (root == null) {
102+
Log.d(TAG, "find: roots = " + roots);
103+
if (roots.isEmpty()) {
101104
return UiObjectCollection.Companion.getEMPTY();
102105
}
103-
if (root.getPackageName() != null && mAccessibilityBridge.getConfig().whiteListContains(root.getPackageName().toString())) {
104-
Log.d(TAG, "package in white list, return null");
105-
return UiObjectCollection.Companion.getEMPTY();
106+
List<UiObject> result = new ArrayList<>();
107+
for (AccessibilityNodeInfo root : roots) {
108+
if (root.getPackageName() != null && mAccessibilityBridge.getConfig().whiteListContains(root.getPackageName().toString())) {
109+
Log.d(TAG, "package in white list, return null");
110+
return UiObjectCollection.Companion.getEMPTY();
111+
}
112+
result.addAll(findAndReturnList(UiObject.Companion.createRoot(root, mAllocator), max - result.size()));
113+
if (result.size() >= max) {
114+
break;
115+
}
106116
}
107-
return findOf(UiObject.Companion.createRoot(root, mAllocator), max);
117+
return UiObjectCollection.Companion.of(result);
108118
}
109119

110120
@Override

0 commit comments

Comments
 (0)