Java Code Examples for android.view.accessibility.AccessibilityNodeInfo#findFocus()

The following examples show how to use android.view.accessibility.AccessibilityNodeInfo#findFocus() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AccessibilityUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 查找符合条件的节点
 * @param service   {@link AccessibilityService}
 * @param focus     焦点类型
 * @param className 节点所属的类 ( 类名 )
 * @return 拥有特定焦点类型的节点
 */
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public static AccessibilityNodeInfo findFocus(final AccessibilityService service, final int focus, final String className) {
    if (service == null || className == null) return null;
    // 获取根节点
    AccessibilityNodeInfo nodeInfo = service.getRootInActiveWindow();
    // 取得当前激活窗体的根节点
    if (nodeInfo == null) return null;
    // 通过指定的焦点类型找到当前的节点
    AccessibilityNodeInfo node = nodeInfo.findFocus(focus);
    // 防止为 null
    if (node != null) {
        // 判断是否符合的类型
        if (node.getClassName().equals(className) && node.isEnabled()) {
            return node;
        }
    }
    return null;
}
 
Example 2
Source File: AccessibilityUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 查找符合条件的节点
 * @param service {@link AccessibilityService}
 * @param focus   焦点类型
 * @return 拥有特定焦点类型的节点
 */
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public static AccessibilityNodeInfo findFocus(final AccessibilityService service, final int focus) {
    if (service == null) return null;
    // 获取根节点
    AccessibilityNodeInfo nodeInfo = service.getRootInActiveWindow();
    // 取得当前激活窗体的根节点
    if (nodeInfo == null) return null;
    // 通过指定的焦点类型找到当前的节点
    return nodeInfo.findFocus(focus);
}
 
Example 3
Source File: SmsCodeAutoInputService.java    From SmsCode with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 获取当前输入焦点控件
 * @return 当前输入焦点,获取失败返回null
 */
private AccessibilityNodeInfo findFocusNodeInfo() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return findFocus(AccessibilityNodeInfo.FOCUS_INPUT);
    } else {
        AccessibilityNodeInfo rootNodeInfo = getRootInActiveWindow();
        if (rootNodeInfo == null) {
            XLog.d("rootNodeInfo is null");
            return null;
        }
        return rootNodeInfo.findFocus(AccessibilityNodeInfo.FOCUS_INPUT);
    }
}
 
Example 4
Source File: FocusFinder.java    From brailleback with Apache License 2.0 5 votes vote down vote up
public static AccessibilityNodeInfoCompat getFocusedNode(
    AccessibilityService service, boolean fallbackOnRoot) {
    AccessibilityNodeInfo root =
            service.getRootInActiveWindow();
    AccessibilityNodeInfo focused = null;
    try {
        AccessibilityNodeInfo ret = null;
        if (root != null) {
            focused = root.findFocus(
                AccessibilityNodeInfo.FOCUS_ACCESSIBILITY);
            if (focused != null && focused.isVisibleToUser()) {
                ret = focused;
                focused = null;
            } else if (fallbackOnRoot) {
                ret = root;
                root = null;
            }
        } else {
            LogUtils.log(service, Log.ERROR, "No current window root");
        }
        if (ret != null) {
            return new AccessibilityNodeInfoCompat(ret);
        }
    } finally {
        if (root != null) {
            root.recycle();
        }
        if (focused != null) {
            focused.recycle();
        }
    }
    return null;
}
 
Example 5
Source File: FocusFinder.java    From talkback with Apache License 2.0 5 votes vote down vote up
public static @Nullable AccessibilityNodeInfoCompat getFocusedNode(
    AccessibilityService service, boolean fallbackOnRoot) {
  AccessibilityNodeInfo root = service.getRootInActiveWindow();
  AccessibilityNodeInfo focused = null;

  try {
    AccessibilityNodeInfo ret = null;
    if (root != null) {
      focused = root.findFocus(AccessibilityNodeInfo.FOCUS_ACCESSIBILITY);
      if (focused != null && focused.isVisibleToUser()) {
        ret = focused;
        focused = null;
      } else if (fallbackOnRoot) {
        ret = root;
        root = null;
      }
    } else {
      LogUtils.e(TAG, "No current window root");
    }

    if (ret != null) {
      return AccessibilityNodeInfoUtils.toCompat(ret);
    }
  } finally {
    if (root != null) {
      root.recycle();
    }

    if (focused != null) {
      focused.recycle();
    }
  }

  return null;
}