api: selector.findOne(timeout), selector.findOnce(index)

This commit is contained in:
hyb1996 2017-12-07 00:09:17 +08:00
parent cd5def8700
commit 6ea30c6fcc

View File

@ -1,5 +1,6 @@
package com.stardust.autojs.core.accessibility;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.accessibility.AccessibilityNodeInfo;
@ -111,6 +112,50 @@ public class UiSelector extends UiGlobalSelector {
return uiObjectCollection;
}
@ScriptInterface
public UiObject findOne(long timeout) {
if (timeout == -1) {
return untilFindOne();
}
UiObjectCollection uiObjectCollection = find();
long start = SystemClock.uptimeMillis();
while (uiObjectCollection.empty()) {
if (Thread.currentThread().isInterrupted()) {
throw new ScriptInterruptedException();
}
if (SystemClock.uptimeMillis() - start > timeout) {
return null;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
throw new ScriptInterruptedException();
}
uiObjectCollection = find();
}
return uiObjectCollection.get(0);
}
public UiObject findOnce(int index) {
UiObjectCollection uiObjectCollection = find();
while (uiObjectCollection.empty()) {
if (Thread.currentThread().isInterrupted()) {
throw new ScriptInterruptedException();
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
throw new ScriptInterruptedException();
}
uiObjectCollection = find();
}
if (index >= uiObjectCollection.size()) {
return null;
}
return uiObjectCollection.get(index);
}
@ScriptInterface
public UiObject findOne() {
return untilFindOne();