From 6ea30c6fcceb82df911c38f6520ab9ecd44270f3 Mon Sep 17 00:00:00 2001 From: hyb1996 <946994919@qq.com> Date: Thu, 7 Dec 2017 00:09:17 +0800 Subject: [PATCH] api: selector.findOne(timeout), selector.findOnce(index) --- .../autojs/core/accessibility/UiSelector.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) 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 22bda826..d485126e 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 @@ -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();